Skip to content

Commit

Permalink
Merge pull request aheckmann#144 from jonathanong/buffer-stream-auto-…
Browse files Browse the repository at this point in the history
…orient

add bufferStream option again, add streaming autoOrient test
  • Loading branch information
jonathanong committed Apr 10, 2013
2 parents bf2dcc1 + e281183 commit 134e935
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 14 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
language: node_js
node_js:
- "0.4"
- "0.6"
- "0.8"
- "0.10"
25 changes: 25 additions & 0 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,31 @@ module.exports = function (proto) {
}

self.sourceStream.pipe(proc.stdin);

// We cache the buffers for get operations
// as we may use it again.
// This wouldn't make sense for a convert operation.
// We rely on garbage collection for optimization,
// so if want to kill trash the garbage quickly,
// do `this._buffers = null`.
// We do this in lieu of `bufferStream`,
// with this being more magical.
if (self.bufferStream && !this._buffering) {
// Incase there are multiple processes in parallel,
// we only need one
this._buffering = true
var buffers = []

self.sourceStream.on('data', function (chunk) {
buffers.push(chunk)
})

// Kill reference as we assume the stream is dead.
self.sourceStream.on('end', function () {
self.sourceBuffer = Buffer.concat(buffers)
self.sourceStream = null
})
}
}

// for _exec operations (identify() mostly), we also
Expand Down
2 changes: 1 addition & 1 deletion lib/convenience/autoOrient.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module.exports = function (proto) {

proto.autoOrient = function autoOrient () {
this.preprocessor(function (callback) {
this.orientation(function (err, orientation) {
this.orientation({bufferStream: true}, function (err, orientation) {
if (err) return callback(err);

var transforms = exifTransforms[orientation.toLowerCase()];
Expand Down
4 changes: 4 additions & 0 deletions lib/getters.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ module.exports = function (gm) {

self.on(getter, createListener(callback));

self.bufferStream = !!opts.bufferStream;

if (val.verbose) {
self.identify(opts, function (err, stdout, stderr, cmd) {
if (err) {
Expand Down Expand Up @@ -111,6 +113,8 @@ module.exports = function (gm) {

self._identifyState = IDENTIFYING;

self.bufferStream = !!opts.bufferStream;

var args = makeArgs(self, { verbose: true });

self._exec(args, function (err, stdout, stderr, cmd) {
Expand Down
42 changes: 42 additions & 0 deletions test/autoOrientStream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

// gm - Copyright Aaron Heckmann <[email protected]> (MIT Licensed)
// This is a copy of `autoOrient.js` using streams

var assert = require('assert')
var fs = require('fs')

module.exports = function (_, dir, finish, gm) {
if (!gm.integration)
return finish();

var filename = dir + '/autoOrient.jpg';

gm(fs.createReadStream(dir + '/originalSideways.jpg')).orientation(function (err, o) {
if (err) return finish(err);

assert.equal('RightTop', o);
assert.ok(!! this.data['Profile-EXIF'], 'No Profile-EXIF data found');
assert.equal('155x460', this.data.Geometry);

// this image is sideways, but may be auto-oriented by modern OS's
// try opening it in a browser to see its true orientation
gm(fs.createReadStream(dir + '/originalSideways.jpg'))
.autoOrient()
.write(filename, function autoOrient (err) {
if (err) return finish(err);

// fs race condition
setTimeout(function () {
gm(filename).identify(function (err) {
if (err) return finish(err);

assert.equal('Unknown', this.data.Orientation);
assert.ok(! this.data['Profile-EXIF'], 'Profile-EXIF still exists');
assert.equal('460x155', this.data.Geometry);

finish(err);
});
}, 200);
});
});
}
15 changes: 4 additions & 11 deletions test/streamInGetter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,10 @@ module.exports = function (_, dir, finish, gm) {
if (!gm.integration)
return finish();

var imageStream = fs.createReadStream(dir + '/original.jpg')

var buffers = []
imageStream.on('data', function (chunk) {
buffers.push(chunk)
})

gm(imageStream)
.size(function (err, size) {
gm(Buffer.concat(buffers)).write(dir + '/streamInGetter.png', function streamInGetter (err){
gm(fs.createReadStream(dir + '/original.jpg'))
.size({bufferStream: true}, function (err, size) {
this.write(dir + '/streamInGetter.png', function streamInGetter (err){
finish(err);
});
});
}
}

0 comments on commit 134e935

Please sign in to comment.