diff --git a/docs/changelog.md b/docs/changelog.md
index 55ac8292..a1c4701f 100644
--- a/docs/changelog.md
+++ b/docs/changelog.md
@@ -18,6 +18,10 @@ Requires libvips v8.3.1
[#511](https://github.com/lovell/sharp/pull/511)
[@mhirsch](https://github.com/mhirsch)
+* Ensure ICC profiles are removed from PNG output unless withMetadata used.
+ [#521](https://github.com/lovell/sharp/issues/521)
+ [@ChrisPinewood](https://github.com/ChrisPinewood)
+
#### v0.15.1 - 12th July 2016
* Concat Stream-based input in single operation for ~+3% perf and less GC.
diff --git a/src/pipeline.cc b/src/pipeline.cc
index 3e440525..ce89bce8 100644
--- a/src/pipeline.cc
+++ b/src/pipeline.cc
@@ -884,9 +884,12 @@ class PipelineWorker : public AsyncWorker {
baton->formatOut = "jpeg";
baton->channels = std::min(baton->channels, 3);
} else if (baton->formatOut == "png" || (baton->formatOut == "input" && inputImageType == ImageType::PNG)) {
+ // Strip profile
+ if (!baton->withMetadata) {
+ vips_image_remove(image.get_image(), VIPS_META_ICC_NAME);
+ }
// Write PNG to buffer
VipsArea *area = VIPS_AREA(image.pngsave_buffer(VImage::option()
- ->set("strip", !baton->withMetadata)
->set("compression", baton->compressionLevel)
->set("interlace", baton->progressive)
->set("filter", baton->withoutAdaptiveFiltering ?
@@ -961,9 +964,12 @@ class PipelineWorker : public AsyncWorker {
baton->formatOut = "jpeg";
baton->channels = std::min(baton->channels, 3);
} else if (baton->formatOut == "png" || isPng || (matchInput && inputImageType == ImageType::PNG)) {
+ // Strip profile
+ if (!baton->withMetadata) {
+ vips_image_remove(image.get_image(), VIPS_META_ICC_NAME);
+ }
// Write PNG to file
image.pngsave(const_cast(baton->fileOut.data()), VImage::option()
- ->set("strip", !baton->withMetadata)
->set("compression", baton->compressionLevel)
->set("interlace", baton->progressive)
->set("filter", baton->withoutAdaptiveFiltering ?
diff --git a/test/unit/metadata.js b/test/unit/metadata.js
index 84dd6be8..f4a30aeb 100644
--- a/test/unit/metadata.js
+++ b/test/unit/metadata.js
@@ -328,6 +328,22 @@ describe('Image metadata', function() {
});
});
+ it('Remove metadata from PNG output', function(done) {
+ sharp(fixtures.inputJpgWithExif)
+ .png()
+ .toBuffer(function(err, buffer) {
+ if (err) throw err;
+ sharp(buffer).metadata(function(err, metadata) {
+ if (err) throw err;
+ assert.strictEqual(false, metadata.hasProfile);
+ assert.strictEqual('undefined', typeof metadata.orientation);
+ assert.strictEqual('undefined', typeof metadata.exif);
+ assert.strictEqual('undefined', typeof metadata.icc);
+ done();
+ });
+ });
+ });
+
it('File input with corrupt header fails gracefully', function(done) {
sharp(fixtures.inputJpgWithCorruptHeader)
.metadata(function(err) {