New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@pioug/get-pixels

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@pioug/get-pixels - npm Package Compare versions

Comparing version 3.3.3 to 4.0.0

CHANGELOG.md

193

dom-pixels.js

@@ -1,26 +0,34 @@

'use strict'
"use strict";
var path = require('path')
var ndarray = require('ndarray')
var GifReader = require('omggif').GifReader
var pack = require('ndarray-pack')
var through = require('through')
var parseDataURI = require('data-uri-to-buffer')
var path = require("path");
var ndarray = require("ndarray");
var GifReader = require("omggif").GifReader;
var pack = require("ndarray-pack");
var through = require("through");
var parseDataURI = require("data-uri-to-buffer");
function defaultImage(url, cb) {
var img = new Image()
img.crossOrigin = "Anonymous"
img.onload = function() {
var canvas = document.createElement('canvas')
canvas.width = img.width
canvas.height = img.height
var context = canvas.getContext('2d')
context.drawImage(img, 0, 0)
var pixels = context.getImageData(0, 0, img.width, img.height)
cb(null, ndarray(new Uint8Array(pixels.data), [img.width, img.height, 4], [4, 4*img.width, 1], 0))
}
img.onerror = function(err) {
cb(err)
}
img.src = url
var img = new Image();
img.crossOrigin = "Anonymous";
img.onload = function () {
var canvas = document.createElement("canvas");
canvas.width = img.width;
canvas.height = img.height;
var context = canvas.getContext("2d");
context.drawImage(img, 0, 0);
var pixels = context.getImageData(0, 0, img.width, img.height);
cb(
null,
ndarray(
new Uint8Array(pixels.data),
[img.width, img.height, 4],
[4, 4 * img.width, 1],
0
)
);
};
img.onerror = function (err) {
cb(err);
};
img.src = url;
}

@@ -30,35 +38,36 @@

function handleGif(data, cb) {
var reader
var reader;
try {
reader = new GifReader(data)
} catch(err) {
cb(err)
return
reader = new GifReader(data);
} catch (err) {
cb(err);
return;
}
if(reader.numFrames() > 0) {
var nshape = [reader.numFrames(), reader.height, reader.width, 4]
var ndata = new Uint8Array(nshape[0] * nshape[1] * nshape[2] * nshape[3])
var result = ndarray(ndata, nshape)
if (reader.numFrames() > 0) {
var nshape = [reader.numFrames(), reader.height, reader.width, 4];
var ndata = new Uint8Array(nshape[0] * nshape[1] * nshape[2] * nshape[3]);
var result = ndarray(ndata, nshape);
try {
for(var i=0; i<reader.numFrames(); ++i) {
reader.decodeAndBlitFrameRGBA(i, ndata.subarray(
result.index(i, 0, 0, 0),
result.index(i+1, 0, 0, 0)))
for (var i = 0; i < reader.numFrames(); ++i) {
reader.decodeAndBlitFrameRGBA(
i,
ndata.subarray(result.index(i, 0, 0, 0), result.index(i + 1, 0, 0, 0))
);
}
} catch(err) {
cb(err)
return
} catch (err) {
cb(err);
return;
}
cb(null, result.transpose(0,2,1))
cb(null, result.transpose(0, 2, 1));
} else {
var nshape = [reader.height, reader.width, 4]
var ndata = new Uint8Array(nshape[0] * nshape[1] * nshape[2])
var result = ndarray(ndata, nshape)
var nshape = [reader.height, reader.width, 4];
var ndata = new Uint8Array(nshape[0] * nshape[1] * nshape[2]);
var result = ndarray(ndata, nshape);
try {
reader.decodeAndBlitFrameRGBA(0, ndata)
} catch(err) {
cb(err)
return
reader.decodeAndBlitFrameRGBA(0, ndata);
} catch (err) {
cb(err);
return;
}
cb(null, result.transpose(1,0))
cb(null, result.transpose(1, 0));
}

@@ -68,32 +77,32 @@ }

function httpGif(url, cb) {
var xhr = new XMLHttpRequest()
xhr.open('GET', url, true)
xhr.responseType = 'arraybuffer'
if(xhr.overrideMimeType){
xhr.overrideMimeType('application/binary')
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.responseType = "arraybuffer";
if (xhr.overrideMimeType) {
xhr.overrideMimeType("application/binary");
}
xhr.onerror = function(err) {
cb(err)
}
xhr.onload = function() {
if(xhr.readyState !== 4) {
return
xhr.onerror = function (err) {
cb(err);
};
xhr.onload = function () {
if (xhr.readyState !== 4) {
return;
}
var data = new Uint8Array(xhr.response)
handleGif(data, cb)
return
}
xhr.send()
var data = new Uint8Array(xhr.response);
handleGif(data, cb);
return;
};
xhr.send();
}
function copyBuffer(buffer) {
if(buffer[0] === undefined) {
var n = buffer.length
var result = new Uint8Array(n)
for(var i=0; i<n; ++i) {
result[i] = buffer.get(i)
if (buffer[0] === undefined) {
var n = buffer.length;
var result = new Uint8Array(n);
for (var i = 0; i < n; ++i) {
result[i] = buffer.get(i);
}
return result
return result;
} else {
return new Uint8Array(buffer)
return new Uint8Array(buffer);
}

@@ -103,36 +112,36 @@ }

function dataGif(url, cb) {
process.nextTick(function() {
process.nextTick(function () {
try {
var buffer = parseDataURI(url)
if(buffer) {
handleGif(copyBuffer(buffer), cb)
var buffer = parseDataURI(url);
if (buffer) {
handleGif(copyBuffer(buffer), cb);
} else {
cb(new Error('Error parsing data URI'))
cb(new Error("Error parsing data URI"));
}
} catch(err) {
cb(err)
} catch (err) {
cb(err);
}
})
});
}
module.exports = function getPixels(url, type, cb) {
if(!cb) {
cb = type
type = ''
if (!cb) {
cb = type;
type = "";
}
var ext = path.extname(url)
switch(type || ext.toUpperCase()) {
case '.GIF':
httpGif(url, cb)
break
var ext = path.extname(url);
switch (type || ext.toUpperCase()) {
case ".GIF":
httpGif(url, cb);
break;
default:
if(Buffer.isBuffer(url)) {
url = 'data:' + type + ';base64,' + url.toString('base64')
if (Buffer.isBuffer(url)) {
url = "data:" + type + ";base64," + url.toString("base64");
}
if(url.indexOf('data:image/gif;') === 0) {
dataGif(url, cb)
if (url.indexOf("data:image/gif;") === 0) {
dataGif(url, cb);
} else {
defaultImage(url, cb)
defaultImage(url, cb);
}
}
}
};

@@ -1,86 +0,88 @@

'use strict'
"use strict";
var ndarray = require('ndarray')
var path = require('path')
var PNG = require('pngjs').PNG
var jpeg = require('jpeg-js')
var pack = require('ndarray-pack')
var GifReader = require('omggif').GifReader
var Bitmap = require('node-bitmap')
var fs = require('fs')
var request = require('request')
var mime = require('mime-types')
var parseDataURI = require('parse-data-uri')
var ndarray = require("ndarray");
var PNG = require("pngjs").PNG;
var jpeg = require("jpeg-js");
var pack = require("ndarray-pack");
var GifReader = require("omggif").GifReader;
var Bitmap = require("node-bitmap");
var fs = require("fs");
var mime = require("mime-types");
function handlePNG(data, cb) {
var png = new PNG();
png.parse(data, function(err, img_data) {
if(err) {
cb(err)
return
png.parse(data, function (err, img_data) {
if (err) {
cb(err);
return;
}
cb(null, ndarray(new Uint8Array(img_data.data),
[img_data.width|0, img_data.height|0, 4],
[4, 4*img_data.width|0, 1],
0))
})
cb(
null,
ndarray(
new Uint8Array(img_data.data),
[img_data.width | 0, img_data.height | 0, 4],
[4, (4 * img_data.width) | 0, 1],
0
)
);
});
}
function handleJPEG(data, cb) {
var jpegData
var jpegData;
try {
jpegData = jpeg.decode(data)
jpegData = jpeg.decode(data);
} catch (e) {
cb(e);
return;
}
catch(e) {
cb(e)
return
if (!jpegData) {
cb(new Error("Error decoding jpeg"));
return;
}
if(!jpegData) {
cb(new Error("Error decoding jpeg"))
return
}
var nshape = [ jpegData.height, jpegData.width, 4 ]
var result = ndarray(jpegData.data, nshape)
cb(null, result.transpose(1,0))
var nshape = [jpegData.height, jpegData.width, 4];
var result = ndarray(jpegData.data, nshape);
cb(null, result.transpose(1, 0));
}
function handleGIF(data, cb) {
var reader
var reader;
try {
reader = new GifReader(data)
} catch(err) {
cb(err)
return
reader = new GifReader(data);
} catch (err) {
cb(err);
return;
}
if(reader.numFrames() > 0) {
var nshape = [reader.numFrames(), reader.height, reader.width, 4]
try {
var ndata = new Uint8Array(nshape[0] * nshape[1] * nshape[2] * nshape[3])
} catch(err) {
cb(err)
return
if (reader.numFrames() > 0) {
var nshape = [reader.numFrames(), reader.height, reader.width, 4];
try {
var ndata = new Uint8Array(nshape[0] * nshape[1] * nshape[2] * nshape[3]);
} catch (err) {
cb(err);
return;
}
var result = ndarray(ndata, nshape)
var result = ndarray(ndata, nshape);
try {
for(var i=0; i<reader.numFrames(); ++i) {
reader.decodeAndBlitFrameRGBA(i, ndata.subarray(
result.index(i, 0, 0, 0),
result.index(i+1, 0, 0, 0)))
for (var i = 0; i < reader.numFrames(); ++i) {
reader.decodeAndBlitFrameRGBA(
i,
ndata.subarray(result.index(i, 0, 0, 0), result.index(i + 1, 0, 0, 0))
);
}
} catch(err) {
cb(err)
return
} catch (err) {
cb(err);
return;
}
cb(null, result.transpose(0,2,1))
cb(null, result.transpose(0, 2, 1));
} else {
var nshape = [reader.height, reader.width, 4]
var ndata = new Uint8Array(nshape[0] * nshape[1] * nshape[2])
var result = ndarray(ndata, nshape)
var nshape = [reader.height, reader.width, 4];
var ndata = new Uint8Array(nshape[0] * nshape[1] * nshape[2]);
var result = ndarray(ndata, nshape);
try {
reader.decodeAndBlitFrameRGBA(0, ndata)
} catch(err) {
cb(err)
return
reader.decodeAndBlitFrameRGBA(0, ndata);
} catch (err) {
cb(err);
return;
}
cb(null, result.transpose(1,0))
cb(null, result.transpose(1, 0));
}

@@ -90,39 +92,38 @@ }

function handleBMP(data, cb) {
var bmp = new Bitmap(data)
var bmp = new Bitmap(data);
try {
bmp.init()
} catch(e) {
cb(e)
return
bmp.init();
} catch (e) {
cb(e);
return;
}
var bmpData = bmp.getData()
var nshape = [ bmpData.getHeight(), bmpData.getWidth(), 4 ]
var ndata = new Uint8Array(nshape[0] * nshape[1] * nshape[2])
var result = ndarray(ndata, nshape)
pack(bmpData, result)
cb(null, result.transpose(1,0))
var bmpData = bmp.getData();
var nshape = [bmpData.getHeight(), bmpData.getWidth(), 4];
var ndata = new Uint8Array(nshape[0] * nshape[1] * nshape[2]);
var result = ndarray(ndata, nshape);
pack(bmpData, result);
cb(null, result.transpose(1, 0));
}
function doParse(mimeType, data, cb) {
switch(mimeType) {
case 'image/png':
handlePNG(data, cb)
break
switch (mimeType) {
case "image/png":
handlePNG(data, cb);
break;
case 'image/jpg':
case 'image/jpeg':
handleJPEG(data, cb)
break
case "image/jpg":
case "image/jpeg":
handleJPEG(data, cb);
break;
case 'image/gif':
handleGIF(data, cb)
break
case "image/gif":
handleGIF(data, cb);
break;
case 'image/bmp':
handleBMP(data, cb)
break
case "image/bmp":
handleBMP(data, cb);
break;
default:
cb(new Error("Unsupported file type: " + mimeType))
cb(new Error("Unsupported file type: " + mimeType));
}

@@ -132,64 +133,47 @@ }

module.exports = function getPixels(url, type, cb) {
if(!cb) {
cb = type
type = ''
if (!cb) {
cb = type;
type = "";
}
if(Buffer.isBuffer(url)) {
if(!type) {
cb(new Error('Invalid file type'))
return
if (Buffer.isBuffer(url)) {
if (!type) {
cb(new Error("Invalid file type"));
return;
}
doParse(type, url, cb)
} else if(url.indexOf('data:') === 0) {
try {
var buffer = parseDataURI(url)
if(buffer) {
process.nextTick(function() {
doParse(type || buffer.mimeType, buffer.data, cb)
})
} else {
process.nextTick(function() {
cb(new Error('Error parsing data URI'))
})
}
} catch(err) {
process.nextTick(function() {
cb(err)
doParse(type, url, cb);
} else if (
url.indexOf("data:") === 0 ||
url.indexOf("http://") === 0 ||
url.indexOf("https://") === 0
) {
fetch(url)
.then(function (response) {
if (!type) {
type = response.headers.get("content-type");
}
if (!type) {
return Promise.reject(new Error("Invalid content-type"));
}
return response.arrayBuffer();
})
}
} else if(url.indexOf('http://') === 0 || url.indexOf('https://') === 0) {
request({url:url, encoding:null}, function(err, response, body) {
if(err) {
cb(err)
return
}
type = type;
if(!type){
if(response.getHeader !== undefined){
type = response.getHeader('content-type');
}else if(response.headers !== undefined){
type = response.headers['content-type'];
}
}
if(!type) {
cb(new Error('Invalid content-type'))
return
}
doParse(type, body, cb)
})
.then(function (data) {
doParse(type, Buffer.from(data), cb);
})
.catch(function (err) {
cb(err);
});
} else {
fs.readFile(url, function(err, data) {
if(err) {
cb(err)
return
fs.readFile(url, function (err, data) {
if (err) {
cb(err);
return;
}
type = type || mime.lookup(url)
if(!type) {
cb(new Error('Invalid file type'))
return
type = type || mime.lookup(url);
if (!type) {
cb(new Error("Invalid file type"));
return;
}
doParse(type, data, cb)
})
doParse(type, data, cb);
});
}
}
};
{
"name": "@pioug/get-pixels",
"version": "3.3.3",
"version": "4.0.0",
"description": "Reads the pixels of an image as an ndarray",

@@ -17,5 +17,3 @@ "main": "node-pixels.js",

"omggif": "^1.0.5",
"parse-data-uri": "^0.2.0",
"pngjs": "^3.3.3",
"request": "^2.44.0",
"through": "^2.3.4"

@@ -30,2 +28,6 @@ },

},
"scripts": {
"test": "tap test/*.js",
"test-browser": "beefy test/test.js --open -- -t brfs"
},
"repository": {

@@ -59,7 +61,3 @@ "type": "git",

"readmeFilename": "README.md",
"gitHead": "380bbda330666e4a4066c48ef5a42770d13bcd5c",
"scripts": {
"test": "tap test/*.js",
"test-browser": "beefy test/test.js --open -- -t brfs"
}
}
"gitHead": "380bbda330666e4a4066c48ef5a42770d13bcd5c"
}

@@ -1,28 +0,26 @@

get-pixels
==========
Given a URL/path, grab all the pixels in an image and return the result as an [ndarray](https://github.com/mikolalysenko/ndarray). Written in 100% JavaScript, works both in browserify and in node.js and has no external native dependencies.
# get-pixels
Given a URL/path, grab all the pixels in an image and return the result as an [ndarray](https://github.com/mikolalysenko/ndarray). Written in 100% JavaScript, works both in browserify and in node.js and has no external native dependencies.
Currently the following file formats are supported:
* `PNG`
* `JPEG`
* `GIF`
- `PNG`
- `JPEG`
- `GIF`
Example
=======
# Example
```javascript
var getPixels = require("get-pixels")
var getPixels = require("get-pixels");
getPixels("lena.png", function(err, pixels) {
if(err) {
console.log("Bad image path")
return
getPixels("lena.png", function (err, pixels) {
if (err) {
console.log("Bad image path");
return;
}
console.log("got pixels", pixels.shape.slice())
})
console.log("got pixels", pixels.shape.slice());
});
```
Install
=======
# Install

@@ -32,7 +30,8 @@ npm install get-pixels

### `require("get-pixels")(url[, type], cb(err, pixels))`
Reads all the pixels from url into an ndarray.
* `url` is the path to the file. It can be a relative path, an http url, a data url, or an [in-memory Buffer](http://nodejs.org/api/buffer.html).
* `type` is an optional mime type for the image (required when using a Buffer)
* `cb(err, pixels)` is a callback which gets triggered once the image is loaded.
- `url` is the path to the file. It can be a relative path, an http url, a data url, or an [in-memory Buffer](http://nodejs.org/api/buffer.html).
- `type` is an optional mime type for the image (required when using a Buffer)
- `cb(err, pixels)` is a callback which gets triggered once the image is loaded.

@@ -43,4 +42,4 @@ **Returns** An ndarray of pixels in raster order having shape equal to `[width, height, channels]`.

Credits
=======
# Credits
(c) 2013-2014 Mikola Lysenko. MIT License
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc