jpg-stream
A streaming JPEG encoder and decoder for Node and the browser. It is a direct compilation of libjpeg to JavaScript using Emscripten.
Installation
npm install jpg-stream
For the browser, you can build using Browserify.
Decoding
This example uses the concat-frames module to collect the output of the JPEG decoder into a single buffer. It also shows how to get EXIF metadata contained in the JPEG file.
var JPEGDecoder = ;var concat = ; // decode a JPEG file to RGB pixelsfs ;
Scaling
Large JPEGs from DSLRs can be somewhat slow to decode. If you don't need the image at its full size for preview, or will be resizing the image anyway, there is an option to perform scaling at decode time. This improves performance dramatically since only the DCT coefficients necessary for the desired size are decoded.
To specify decode scaling, provide width
and height
options to the decoder. This
represents the minimum size you want, and the decoder will output an image of at least
this size, but likely not exactly that size. For exact resizing, provide your minimum
allowed size to the decoder and use the resize-pixels
module to resize the JPEG decoder's output to the exact size.
fs ;
Encoding
You can encode a JPEG by writing or piping pixel data to a JPEGEncoder
stream.
You can set the quality
option to a number between 1 and 100 to control the
size vs quality tradeoff made by the encoder.
The JPEG encoder supports writing data in the RGB, grayscale, or CMYK color spaces. If you need to convert from another unsupported color space, first pipe your data through the color-transform module.
var JPEGEncoder = ;var ColorTransform = ; // convert a PNG to a JPEGfs ; // colorspace conversion to convert from RGBA to RGBfs ;
License
MIT