Skip to content

Commit

Permalink
Merge b76e7e0 into 65b7f7d
Browse files Browse the repository at this point in the history
  • Loading branch information
mhirsch committed Jul 7, 2016
2 parents 65b7f7d + b76e7e0 commit f18238f
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ In the above example if `input.png` is a 3 channel RGB image, `output.png` will

`path` is a String containing the path to write the image data to.

If an explicit output format is not selected, it will be inferred from the extension, with JPEG, PNG, WebP, TIFF and DZI supported. Note that RAW format is only supported for buffer output.
If an explicit output format is not selected, it will be inferred from the extension, with JPEG, PNG, WebP, TIFF, DZI, and VIPS V format supported. Note that RAW format is only supported for buffer output.

`callback`, if present, is called with two arguments `(err, info)` where:

Expand Down
11 changes: 10 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,15 @@ Sharp.prototype.webp = function() {
return this;
};

/*
Force VIPS file output
*/
Sharp.prototype.v = function() {
this.options.formatOut = 'v';
return this;
};
Sharp.prototype.vips = Sharp.prototype.v;

/*
Force raw, uint8 output
*/
Expand All @@ -957,7 +966,7 @@ Sharp.prototype.toFormat = function(formatOut) {
}
if (
isDefined(formatOut) &&
['jpeg', 'png', 'webp', 'raw', 'tiff', 'dz', 'input'].indexOf(formatOut) !== -1
['jpeg', 'png', 'webp', 'raw', 'tiff', 'dz', 'v', 'input'].indexOf(formatOut) !== -1
) {
this.options.formatOut = formatOut;
} else {
Expand Down
6 changes: 6 additions & 0 deletions src/common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ namespace sharp {
bool IsDzZip(std::string const &str) {
return EndsWith(str, ".zip") || EndsWith(str, ".ZIP") || EndsWith(str, ".szi") || EndsWith(str, ".SZI");
}
bool IsV(std::string const &str) {
return EndsWith(str, ".v") || EndsWith(str, ".V") || EndsWith(str, ".vips") || EndsWith(str, ".VIPS");
}

/*
Provide a string identifier for the given image type.
Expand All @@ -73,6 +76,7 @@ namespace sharp {
case ImageType::OPENSLIDE: id = "openslide"; break;
case ImageType::PPM: id = "ppm"; break;
case ImageType::FITS: id = "fits"; break;
case ImageType::VIPS: id = "v"; break;
case ImageType::RAW: id = "raw"; break;
case ImageType::UNKNOWN: id = "unknown"; break;
}
Expand Down Expand Up @@ -136,6 +140,8 @@ namespace sharp {
imageType = ImageType::PPM;
} else if (EndsWith(loader, "Fits")) {
imageType = ImageType::FITS;
} else if (EndsWith(loader, "Vips")) {
imageType = ImageType::VIPS;
} else if (EndsWith(loader, "Magick") || EndsWith(loader, "MagickFile")) {
imageType = ImageType::MAGICK;
}
Expand Down
2 changes: 2 additions & 0 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace sharp {
OPENSLIDE,
PPM,
FITS,
VIPS,
RAW,
UNKNOWN
};
Expand All @@ -39,6 +40,7 @@ namespace sharp {
bool IsTiff(std::string const &str);
bool IsDz(std::string const &str);
bool IsDzZip(std::string const &str);
bool IsV(std::string const &str);

/*
Provide a string identifier for the given image type.
Expand Down
11 changes: 10 additions & 1 deletion src/pipeline.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ using sharp::IsWebp;
using sharp::IsTiff;
using sharp::IsDz;
using sharp::IsDzZip;
using sharp::IsV;
using sharp::FreeCallback;
using sharp::CalculateCrop;
using sharp::counterProcess;
Expand Down Expand Up @@ -872,7 +873,9 @@ class PipelineWorker : public AsyncWorker {
bool isTiff = IsTiff(baton->fileOut);
bool isDz = IsDz(baton->fileOut);
bool isDzZip = IsDzZip(baton->fileOut);
bool matchInput = baton->formatOut == "input" && !(isJpeg || isPng || isWebp || isTiff || isDz || isDzZip);
bool isV = IsV(baton->fileOut);
bool matchInput = baton->formatOut == "input" &&
!(isJpeg || isPng || isWebp || isTiff || isDz || isDzZip || isV);
if (baton->formatOut == "jpeg" || isJpeg || (matchInput && inputImageType == ImageType::JPEG)) {
// Write JPEG to file
image.jpegsave(const_cast<char*>(baton->fileOut.data()), VImage::option()
Expand Down Expand Up @@ -926,6 +929,12 @@ class PipelineWorker : public AsyncWorker {
->set("layout", baton->tileLayout)
);
baton->formatOut = "dz";
} else if (baton->formatOut == "v" || isV || (matchInput && inputImageType == ImageType::VIPS)) {
// Write V to file
image.vipssave(const_cast<char*>(baton->fileOut.data()), VImage::option()
->set("strip", !baton->withMetadata)
);
baton->formatOut = "v";
} else {
// Unsupported output format
(baton->err).append("Unsupported output format " + baton->fileOut);
Expand Down
2 changes: 1 addition & 1 deletion src/utilities.cc
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ NAN_METHOD(format) {
// Which load/save operations are available for each compressed format?
Local<Object> format = New<Object>();
for (std::string f : {
"jpeg", "png", "webp", "tiff", "magick", "openslide", "dz", "ppm", "fits", "gif", "svg", "pdf"
"jpeg", "png", "webp", "tiff", "magick", "openslide", "dz", "ppm", "fits", "gif", "svg", "pdf", "vips"
}) {
// Input
Local<Boolean> hasInputFile =
Expand Down
Binary file added test/fixtures/expected/vfile.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions test/fixtures/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,12 @@ module.exports = {
inputPngStripesV: getPath('stripesV.png'),
inputPngStripesH: getPath('stripesH.png'),

inputV: getPath('vfile.v'),

outputJpg: getPath('output.jpg'),
outputPng: getPath('output.png'),
outputWebP: getPath('output.webp'),
outputV: getPath('output.v'),
outputZoinks: getPath('output.zoinks'), // an 'unknown' file extension

// Path for tests requiring human inspection
Expand Down
Binary file added test/fixtures/vfile.v
Binary file not shown.
32 changes: 32 additions & 0 deletions test/unit/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,38 @@ describe('Input/output', function() {
});
}

if (sharp.format.vips.input.file) {
it("Load Vips V file", function(done) {
sharp(fixtures.inputV)
.jpeg()
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual(true, data.length > 0);
assert.strictEqual('jpeg', info.format);
assert.strictEqual(70, info.width);
assert.strictEqual(60, info.height);
fixtures.assertSimilar(fixtures.expected('vfile.jpg'), data, done);
});
});
}

if (sharp.format.vips.output.file) {
it("Save Vips V file", function(done) {
sharp(fixtures.inputJpg)
.extract({left: 910, top: 1105, width: 70, height: 60})
.v()
.toFile(fixtures.outputV, function(err, info) {
if(err) throw err;
assert.strictEqual(true, info.size > 0);
assert.strictEqual('v', info.format);
assert.strictEqual(70, info.width);
assert.strictEqual(60, info.height);
fs.unlinkSync(fixtures.outputV);
done();
});
});
}

if (sharp.format.raw.output.buffer) {
describe('Ouput raw, uncompressed image data', function() {
it('1 channel greyscale image', function(done) {
Expand Down

0 comments on commit f18238f

Please sign in to comment.