Skip to content

Commit

Permalink
Ensure dimensions of final output image are provided (#399)
Browse files Browse the repository at this point in the history
Add a failing test for vips::VError exception
* fix buffer size mismatch
* Loosen error message assertion
* Update image
  • Loading branch information
salzhrani authored and lovell committed Apr 8, 2016
1 parent f214269 commit ef61da3
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/pipeline.cc
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,19 @@ class PipelineWorker : public AsyncWorker {
// From buffer
if (baton->rawWidth > 0 && baton->rawHeight > 0 && baton->rawChannels > 0) {
// Raw, uncompressed pixel data
image = VImage::new_from_memory(baton->bufferIn, baton->bufferInLength,
baton->rawWidth, baton->rawHeight, baton->rawChannels, VIPS_FORMAT_UCHAR);
if (baton->rawChannels < 3) {
image.get_image()->Type = VIPS_INTERPRETATION_B_W;
} else {
image.get_image()->Type = VIPS_INTERPRETATION_sRGB;
try {
image = VImage::new_from_memory(baton->bufferIn, baton->bufferInLength,
baton->rawWidth, baton->rawHeight, baton->rawChannels, VIPS_FORMAT_UCHAR);
if (baton->rawChannels < 3) {
image.get_image()->Type = VIPS_INTERPRETATION_B_W;
} else {
image.get_image()->Type = VIPS_INTERPRETATION_sRGB;
}
inputImageType = ImageType::RAW;
} catch(VError const &err) {
(baton->err).append(err.what());
inputImageType = ImageType::UNKNOWN;
}
inputImageType = ImageType::RAW;
} else {
// Compressed data
inputImageType = DetermineImageType(baton->bufferIn, baton->bufferInLength);
Expand Down Expand Up @@ -656,7 +661,8 @@ class PipelineWorker : public AsyncWorker {

// Number of channels used in output image
baton->channels = image.bands();

baton->width = image.width();
baton->height = image.height();
// Output
if (baton->fileOut == "") {
// Buffer output
Expand Down
Binary file added test/fixtures/flowers.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions test/fixtures/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ module.exports = {

inputSvs: getPath('CMU-1-Small-Region.svs'), // http://openslide.cs.cmu.edu/download/openslide-testdata/Aperio/CMU-1-Small-Region.svs

inputJPGBig: getPath('flowers.jpeg'),

outputJpg: getPath('output.jpg'),
outputPng: getPath('output.png'),
outputWebP: getPath('output.webp'),
Expand Down
28 changes: 28 additions & 0 deletions test/unit/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -1031,4 +1031,32 @@ describe('Input/output', function() {
});
});

it('Info event data', function(done) {
var readable = fs.createReadStream(fixtures.inputJPGBig);
var inPipeline = sharp()
.resize(840)
.raw()
.on('info', function(info) {
assert.strictEqual(840, info.width);
assert.strictEqual(472, info.height);
assert.strictEqual(3, info.channels);
});
var badPipeline = sharp(null, {raw: {width: 840, height: 473, channels: 3}})
.toFormat('jpeg')
.toBuffer(function(err, data, info) {
assert.strictEqual(err.message.indexOf('memory area too small') > 0, true);
readable = fs.createReadStream(fixtures.inputJPGBig);
var goodPipeline = sharp(null, {raw: {width: 840, height: 472, channels: 3}})
.toFormat('jpeg')
.toBuffer(function(err, data, info) {
if (err) throw err;
done();
});
inPipeline = sharp()
.resize(840)
.raw();
readable.pipe(inPipeline).pipe(goodPipeline);
});
readable.pipe(inPipeline).pipe(badPipeline);
});
});

0 comments on commit ef61da3

Please sign in to comment.