Skip to content

Commit

Permalink
Emit warning for invalid ICC profile #3895
Browse files Browse the repository at this point in the history
  • Loading branch information
lovell committed Dec 15, 2023
1 parent c9e3996 commit fe2b298
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
3 changes: 3 additions & 0 deletions docs/changelog.md
Expand Up @@ -9,6 +9,9 @@ Requires libvips v8.15.0
* Add support for Yarn Plug'n'Play filesystem layout.
[#3888](https://github.com/lovell/sharp/issues/3888)

* Emit warning when attempting to use invalid ICC profiles.
[#3895](https://github.com/lovell/sharp/issues/3895)

### v0.33.0 - 29th November 2023

* Drop support for Node.js 14 and 16, now requires Node.js >= 18.17.0
Expand Down
14 changes: 9 additions & 5 deletions src/pipeline.cc
Expand Up @@ -794,11 +794,15 @@ class PipelineWorker : public Napi::AsyncWorker {

// Apply output ICC profile
if (!baton->withIccProfile.empty()) {
image = image.icc_transform(const_cast<char*>(baton->withIccProfile.data()), VImage::option()
->set("input_profile", processingProfile)
->set("embedded", TRUE)
->set("depth", sharp::Is16Bit(image.interpretation()) ? 16 : 8)
->set("intent", VIPS_INTENT_PERCEPTUAL));
try {
image = image.icc_transform(const_cast<char*>(baton->withIccProfile.data()), VImage::option()
->set("input_profile", processingProfile)
->set("embedded", TRUE)
->set("depth", sharp::Is16Bit(image.interpretation()) ? 16 : 8)
->set("intent", VIPS_INTENT_PERCEPTUAL));
} catch(...) {
sharp::VipsWarningCallback(nullptr, G_LOG_LEVEL_WARNING, "Invalid profile", nullptr);
}
} else if (baton->keepMetadata & VIPS_FOREIGN_KEEP_ICC) {
image = sharp::SetProfile(image, inputProfile);
}
Expand Down
Binary file added test/fixtures/invalid-illuminant.icc
Binary file not shown.
18 changes: 18 additions & 0 deletions test/unit/metadata.js
Expand Up @@ -598,6 +598,24 @@ describe('Image metadata', function () {
assert.strictEqual(undefined, metadata.icc);
});

it('transform to invalid ICC profile emits warning', async () => {
const img = sharp({ create })
.png()
.withIccProfile(fixtures.path('invalid-illuminant.icc'));

let warningEmitted = '';
img.on('warning', (warning) => {
warningEmitted = warning;
});

const data = await img.toBuffer();
assert.strictEqual('Invalid profile', warningEmitted);

const metadata = await sharp(data).metadata();
assert.strictEqual(3, metadata.channels);
assert.strictEqual(undefined, metadata.icc);
});

it('Apply CMYK output ICC profile', function (done) {
const output = fixtures.path('output.icc-cmyk.jpg');
sharp(fixtures.inputJpg)
Expand Down

0 comments on commit fe2b298

Please sign in to comment.