Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass framesInfo to the CallBack #42

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Example
```javascript
var getPixels = require("get-pixels")

getPixels("lena.png", function(err, pixels) {
getPixels("lena.png", function(err, pixels, frameInfo) {
if(err) {
console.log("Bad image path")
return
Expand All @@ -28,17 +28,38 @@ Install

npm install get-pixels

### `require("get-pixels")(url[, type], cb(err, pixels))`
### `require("get-pixels")(url[, type], cb(err, pixels, frameInfo))`
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.
* `cb(err, pixels, framesInfo)` is a callback which gets triggered once the image is loaded.

**Returns** An ndarray of pixels in raster order having shape equal to `[width, height, channels]`.
**Returns** An ndarray of pixels in raster order having shape equal to `[width, height, channels]` and **frameInfo** param if available (for animated GIFs).

**Note** For animated GIFs, a 4D array is returned with shape `[numFrames, width, height, 4]`, where each frame is a slice of the final array.

**frameInfo** is an Array of Objects with these fields:

Name|Type|Description
----|-----|-----------
x | Integer | Image Left Position
y | Integer | Image Top Position
width | Integer | Image Width
height | Integer | Image Height
has_local_palette | Boolean | Image local palette presentation flag
palette_offset | Integer | Image palette offset
palette_size | Integer | Image palette size
data_offset | Integer | Image data offset
data_length | Integer | Image data length
transparent_index | Integer | Transparent Color Index
interlaced | Boolean | Interlace Flag
delay | Integer | Delay Time (1/100ths of a second)
disposal | Integer | Disposal method

See GIF spec for details. Summary http://www.onicos.com/staff/iz/formats/gif.html


Credits
=======
(c) 2013-2014 Mikola Lysenko. MIT License
8 changes: 5 additions & 3 deletions dom-pixels.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,22 @@ function handleGif(data, cb) {
return
}
if(reader.numFrames() > 0) {
var framesInfo = []
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)))
result.index(i+1, 0, 0, 0)));
framesInfo.push(reader.frameInfo(i));
}
} catch(err) {
cb(err)
return
}
cb(null, result.transpose(0,2,1))
cb(null, result.transpose(0,2,1), framesInfo)
} else {
var nshape = [reader.height, reader.width, 4]
var ndata = new Uint8Array(nshape[0] * nshape[1] * nshape[2])
Expand Down Expand Up @@ -132,4 +134,4 @@ module.exports = function getPixels(url, type, cb) {
defaultImage(url, cb)
}
}
}
}
6 changes: 4 additions & 2 deletions node-pixels.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ function handleGIF(data, cb) {
return
}
if(reader.numFrames() > 0) {
var framesInfo = []
var nshape = [reader.numFrames(), reader.height, reader.width, 4]
try {
var ndata = new Uint8Array(nshape[0] * nshape[1] * nshape[2] * nshape[3])
Expand All @@ -65,13 +66,14 @@ function handleGIF(data, cb) {
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)))
result.index(i+1, 0, 0, 0)));
framesInfo.push(reader.frameInfo(i));
}
} catch(err) {
cb(err)
return
}
cb(null, result.transpose(0,2,1))
cb(null, result.transpose(0,2,1), framesInfo)
} else {
var nshape = [reader.height, reader.width, 4]
var ndata = new Uint8Array(nshape[0] * nshape[1] * nshape[2])
Expand Down