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

Add support for synchronous decode #29

Open
wants to merge 1 commit 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
26 changes: 16 additions & 10 deletions png-node.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,9 @@ module.exports = class PNG
b1 = @data[@pos++] << 8
b2 = @data[@pos++]
b1 | b2

decodePixels: (fn) ->
zlib.inflate @imgData, (err, data) =>
throw err if err


decodePixels: (fn, sync) ->
worker = (data) =>
pixelBytes = @pixelBitlength / 8
scanlineLength = pixelBytes * @width

Expand Down Expand Up @@ -205,7 +203,14 @@ module.exports = class PNG
row++

fn pixels


if sync
worker (zlib.inflateSync @imgData)
else
zlib.inflate @imgData, (err, data) =>
throw err if err
worker data

decodePalette: ->
palette = @palette
transparency = @transparency.indexed or []
Expand Down Expand Up @@ -256,9 +261,10 @@ module.exports = class PNG
j = k

return
decode: (fn) ->

decode: (fn, sync) ->
ret = new Buffer(@width * @height * 4)
@decodePixels (pixels) =>
worker = (pixels) =>
@copyToImageData ret, pixels
fn ret
fn ret
@decodePixels worker, sync
Loading