Giter Site home page Giter Site logo

jpg-stream's Introduction

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 = require('jpg-stream/decoder');
var concat = require('concat-frames');

// decode a JPEG file to RGB pixels
fs.createReadStream('in.jpg')
  .pipe(new JPEGDecoder)
  .on('meta', function(meta) {
    // meta contains an exif object as decoded by
    // https://github.com/devongovett/exif-reader
  })
  .pipe(concat(function(frames) {
    // frames is an array of frame objects (one for JPEGs)
    // each element has a `pixels` property containing
    // the raw RGB pixel data for that frame, as
    // well as the width, height, etc.
  }));

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.createReadStream('large.jpg')
  .pipe(new JPEGDecoder({ width: 600, height: 400 }))
  .pipe(concat(function(frames) {
    // frames[0].width >= 600 and frames[0].height >= 400
  }));

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 = require('jpg-stream/encoder');
var ColorTransform = require('color-transform');

// convert a PNG to a JPEG
fs.createReadStream('in.png')
  .pipe(new PNGDecoder)
  .pipe(new JPEGEncoder({ quality: 80 }))
  .pipe(fs.createWriteStream('out.jpg'));
  
// colorspace conversion to convert from RGBA to RGB
fs.createReadStream('rgba.png')
  .pipe(new PNGDecoder)
  .pipe(new ColorTransform('rgb'))
  .pipe(new JPEGEncoder)
  .pipe(fs.createWriteStream('rgb.jpg'));

License

MIT

jpg-stream's People

Contributors

devongovett avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

jpg-stream's Issues

Breaking with a lot of images

I get the following message when i want to extract Exif data from around 400 jpeg files. I never seen this message, and have no clue, how to solve this.

Cannot enlarge memory arrays. Either (1) compile with -s TOTAL_MEMORY=X with X higher than the current value 33554432, (2) compile with ALLOW_MEMORY_GROWTH which adjusts the size at runtime but prevents some optimizations, or (3) set Module.TOTAL_MEMORY before the program runs.
Cannot enlarge memory arrays. Either (1) compile with -s TOTAL_MEMORY=X with X higher than the current value 33554432, (2) compile with ALLOW_MEMORY_GROWTH which adjusts the size at runtime but prevents some optimizations, or (3) set Module.TOTAL_MEMORY before the program runs.

/var/development/node_modules/jpg-stream/build/jpeg.js:1
(function (exports, require, module, __filename, __dirname) { var Module;if(!Module)Module=(typeof Module!=="undefined"?Module:null)||{};var moduleOverrides={};for(var key in Module){if(Module.hasOwnProperty(key)){moduleOverrides[key]=Module[key]}}var ENVIRONMENT_IS_NODE=typeof process==="object"&&typeof require==="function";var ENVIRONMENT_IS_WEB=typeof window==="object";var ENVIRONMENT_IS_WORKER=typeof importScripts==="function";var ENVIRONMENT_IS_SHELL=!ENVIRONMENT_IS_WEB&&!ENVIRONMENT_IS_NODE&&!ENVIRONMENT_IS_WORKER;if(ENVIRONMENT_IS_NODE){if(!Module["print"])Module["print"]=function print(x){process["stdout"].write(x+"\n")};if(!Module["printErr"])Module["printErr"]=function printErr(x){process["stderr"].write(x+"\n")};var nodeFS=require("fs");var nodePath=require("path");Module["read"]=function read(filename,binary){filename=nodePath"normalize";var ret=nodeFS"readFileSync";if(!ret&&filename!=nodePath"resolve"){
abort() at Error
at jsStackTrace (/var/development/node_modules/jpg-stream/build/jpeg.js:1:18435)
at stackTrace (/var/development/node_modules/jpg-stream/build/jpeg.js:1:18618)
at abort (/var/development/node_modules/jpg-stream/build/jpeg.js:42:35740)
at enlargeMemory (/var/development/node_modules/jpg-stream/build/jpeg.js:1:18951)
at Function.Runtime.dynamicAlloc as alloc
at _sbrk (/var/development/node_modules/jpg-stream/build/jpeg.js:1:122885)
at Ts (/var/development/node_modules/jpg-stream/build/jpeg.js:9:12346)
at Mi (/var/development/node_modules/jpg-stream/build/jpeg.js:29:62714)
at Array.Ni (/var/development/node_modules/jpg-stream/build/jpeg.js:29:63216)
at ng (/var/development/node_modules/jpg-stream/build/jpeg.js:17:61717)
If this abort() is unexpected, build with -s ASSERTIONS=1 which can give more information.

Scaling ratio incorrect?

The code to compute the scaling ratio is:

  // Calculate scale so we only decode what we need
  if (desiredWidth && desiredHeight) {
    int wdeg = imageWidth / desiredWidth;
    int hdeg = imageHeight / desiredHeight;
    dec.scale_num = 1;
    dec.scale_denom = std::max(1, std::min(std::min(wdeg, hdeg), 8));
  }

However, the following article says:

Just set the desired scaling factor of N/8 (N=1...16) in the djpeg -scale option or via the scale_num/scale_denom variables in library application.

So I guess you should set the scale_denom to 8, and then compute the scale_num so that imageWidth * scale_num / 8 <= desiredWidth?

Breaks in Browser due to process

The compiled output currently breaks in browserify. Offending line:

Module["thisProgram"]=process["argv"][1].replace(/\\/g,"/")

   Uncaught TypeError: Cannot read property 'replace' of undefined

It also relies on other features like process.stdout etc.

More documentation, or links to...

It would be nice if the README.md could either include or link to more documentation on how to use this library, or the structure a frame uses... are there a series of related libraries that are interchangable (there's mention of a png one)... what about simple transforms (resize, etc).

Very cool project, just interested in a little bit more.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.