sorccu / node-jpeg-turbo

Limited libjpeg-turbo bindings for Node.js.
Other
39 stars 36 forks source link

Any plans on implementing encoding? #1

Closed willbamford closed 8 years ago

sorccu commented 8 years ago

Perhaps, it would be quite easy. I don't need it so I haven't implemented it so far. Which features do you need? As long as you don't need anything terribly fancy, like rotation or misc colorspaces, it should be doable without adding new deps.

willbamford commented 8 years ago

Hi, I'm just looking for very basic encoding. Currently I'm using https://github.com/tcr/node-jpeg/tree/tcr-nan2. An example:

var JPEG = require('jpeg').Jpeg;

var width = 400;
var height = 300;
var pixels = new Uint8Array[width * height * 4];

/*
 * Populate pixels here, then...
 */

var jpeg = new JPEG(pixels, width, height, 'rgba');

jpeg.setQuality(90);

var image = jpeg.encodeSync();
willbamford commented 8 years ago

I might have a go at forking and implementing this (although my C/C++ is a bit rusty) . Looks like this might be a good reference, although it's not implementing NAN.

sorccu commented 8 years ago

Actually I'm almost done with the compressor. I'm quite busy but it's not a huge thing. I'm hoping to release over the weekend. Can you volunteer to try it out and provide feedback once it's done?

willbamford commented 8 years ago

That's great thanks, I'll hold off trying to do this then. Will be happy to try this out :+1:

sorccu commented 8 years ago

Please try the master branch now, if it looks OK I'll publish to NPM. Also, I added some simple docs to the README.

willbamford commented 8 years ago

Hi, the API itself looks good, but when I try to install from master (npm install https://github.com/sorccu/node-jpeg-turbo.git) and try out the example with bufferSize method I get:

Unhandled rejection TypeError: jpg.bufferSize is not a function

Likewise, without using compressSync method I get:

Unhandled rejection TypeError: binding.compressSync is not a function

When I do a console.log on jpg I see:

{
  decompressSync: [Function],
  FORMAT_RGB: 0,
  FORMAT_BGR: 1,
  FORMAT_RGBX: 2,
  FORMAT_BGRX: 3,
  FORMAT_XRGB: 5,
  FORMAT_XBGR: 4,
  FORMAT_GRAY: 6,
  FORMAT_RGBA: 7,
  FORMAT_BGRA: 8,
  FORMAT_ABGR: 9,
  FORMAT_ARGB: 10,
  compressSync: [Function]
}

I thought this might be because I'd need to rebuild on my machine (as node-pre-gyp is installing out-of-date pre-built binary from remote?), but I'm struggling to get this to work on my machine (Mac OS X). I've tried:

brew install yasm

Then inside the jpeg-turbo module folder:

npm install --build-from-source

But I'm getting:

> jpeg-turbo@0.3.2 install ./node_modules/jpeg-turbo
> node-pre-gyp install --fallback-to-build

make: *** No rule to make target `../deps/libjpeg-turbo/simd/jfdctflt-sse-64.asm', needed by `Release/obj/gen/jfdctflt-sse-64.o'.  Stop.
...
sorccu commented 8 years ago

When you build from the repo, you have to fetch submodules (i.e. libjpeg-turbo). That's what causes the error. This wasn't mentioned in the README, so I added some instructions in d4a7be3ad1356e7c3adeb01cf89050dfaca692a2.

willbamford commented 8 years ago

Ah yes, thanks. Tried that and it's now working very well (<2ms encode 512x512 at 90% quality on my 2013 MacBook Pro). I'm not using the preallocated buffer at the moment:

var jpg = require('jpeg-turbo');

module.exports = function(imageData) {

  console.time('Encoding JPEG');

  var options = {
    format: jpg.FORMAT_RGBA,
    width: imageData.width,
    height: imageData.height,
    quality: 90
    // subsampling: jpg.SAMP_444,
  }

  // var preallocated = new Buffer(jpg.bufferSize(options));
  // var raw = new Buffer(imageData.data);

  var encoded = jpg.compressSync(imageData.data, options);
  // var encoded = jpg.compressSync(raw, preallocated, options);

  console.timeEnd('Encoding JPEG');

  return encoded;
};
sorccu commented 8 years ago

Great! I've released 0.4.0 with prebuilt binaries.