Skip to content

Commit

Permalink
Add support for profile-less CMYK images #99
Browse files Browse the repository at this point in the history
  • Loading branch information
lovell committed Nov 6, 2014
1 parent 62fcfb3 commit f7c2a83
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 12 deletions.
Binary file added icc/USWebCoatedSWOP.icc
Binary file not shown.
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

var path = require('path');
var util = require('util');
var stream = require('stream');

Expand All @@ -17,6 +18,8 @@ var Sharp = function(input) {
// input options
streamIn: false,
sequentialRead: false,
// ICC profile to use when input CMYK image has no embedded profile
iccProfileCmyk: path.join(__dirname, 'icc', 'USWebCoatedSWOP.icc'),
// resize options
topOffsetPre: -1,
leftOffsetPre: -1,
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sharp",
"version": "0.7.1",
"version": "0.7.2",
"author": "Lovell Fuller <npm@lovell.info>",
"contributors": [
"Pierre Inglebert <pierre.inglebert@gmail.com>",
Expand Down Expand Up @@ -41,9 +41,9 @@
"stream"
],
"dependencies": {
"bluebird": "^2.3.9",
"bluebird": "^2.3.10",
"color": "^0.7.1",
"nan": "^1.3.0"
"nan": "^1.4.0"
},
"devDependencies": {
"mocha": "^2.0.1",
Expand Down
33 changes: 24 additions & 9 deletions src/resize.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ struct ResizeBaton {
std::string fileIn;
void* bufferIn;
size_t bufferInLength;
std::string iccProfileCmyk;
std::string output;
std::string outputFormat;
void* bufferOut;
Expand Down Expand Up @@ -236,16 +237,28 @@ class ResizeWorker : public NanAsyncWorker {
}

// Handle colour profile, if any, for non sRGB images
if (image->Type != VIPS_INTERPRETATION_sRGB && vips_image_get_typeof(image, VIPS_META_ICC_NAME)) {
// Import embedded profile
VipsImage *profile = vips_image_new();
vips_object_local(hook, profile);
if (vips_icc_import(image, &profile, NULL, "embedded", TRUE, "pcs", VIPS_PCS_XYZ, NULL)) {
return Error(baton, hook);
if (image->Type != VIPS_INTERPRETATION_sRGB) {
// Get the input colour profile
if (vips_image_get_typeof(image, VIPS_META_ICC_NAME)) {
// Use embedded profile
VipsImage *profile = vips_image_new();
vips_object_local(hook, profile);
if (vips_icc_import(image, &profile, "pcs", VIPS_PCS_XYZ, "embedded", TRUE, NULL)) {
return Error(baton, hook);
}
g_object_unref(image);
image = profile;
} else if (image->Type == VIPS_INTERPRETATION_CMYK) {
// CMYK with no embedded profile
VipsImage *profile = vips_image_new();
vips_object_local(hook, profile);
if (vips_icc_import(image, &profile, "pcs", VIPS_PCS_XYZ, "input_profile", (baton->iccProfileCmyk).c_str(), NULL)) {
return Error(baton, hook);
}
g_object_unref(image);
image = profile;
}
g_object_unref(image);
image = profile;
// Convert to sRGB colour space
// Attempt to convert to sRGB colour space
VipsImage *colourspaced = vips_image_new();
vips_object_local(hook, colourspaced);
if (vips_colourspace(image, &colourspaced, VIPS_INTERPRETATION_sRGB, NULL)) {
Expand Down Expand Up @@ -738,6 +751,8 @@ NAN_METHOD(resize) {
baton->bufferInLength = node::Buffer::Length(buffer);
baton->bufferIn = node::Buffer::Data(buffer);
}
// ICC profile to use when input CMYK image has no embedded profile
baton->iccProfileCmyk = *String::Utf8Value(options->Get(NanNew<String>("iccProfileCmyk"))->ToString());
// Extract image options
baton->topOffsetPre = options->Get(NanNew<String>("topOffsetPre"))->Int32Value();
baton->leftOffsetPre = options->Get(NanNew<String>("leftOffsetPre"))->Int32Value();
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions test/fixtures/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = {
inputJpgWithExifMirroring: getPath('Landscape_5.jpg'), // https://github.com/recurser/exif-orientation-examples/blob/master/Landscape_5.jpg
inputJpgWithGammaHoliness: getPath('gamma_dalai_lama_gray.jpg'), // http://www.4p8.com/eric.brasseur/gamma.html
inputJpgWithCmykProfile: getPath('Channel_digital_image_CMYK_color.jpg'), // http://en.wikipedia.org/wiki/File:Channel_digital_image_CMYK_color.jpg
inputJpgWithCmykNoProfile: getPath('Channel_digital_image_CMYK_color_no_profile.jpg'),

inputPng: getPath('50020484-00001.png'), // http://c.searspartsdirect.com/lis_png/PLDM/50020484-00001.png
inputPngWithTransparency: getPath('blackbug.png'), // public domain
Expand Down
12 changes: 12 additions & 0 deletions test/unit/colourspace.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,16 @@ describe('Colour space conversion', function() {
});
});

it('From profile-less CMYK to sRGB', function(done) {
sharp(fixtures.inputJpgWithCmykNoProfile)
.resize(320)
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual(true, data.length > 0);
assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width);
done();
});
});

});

0 comments on commit f7c2a83

Please sign in to comment.