Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

withMetadata() doesn't add sRGB profile to untagged images #734

Closed
aramando opened this issue Mar 15, 2017 · 4 comments
Closed

withMetadata() doesn't add sRGB profile to untagged images #734

aramando opened this issue Mar 15, 2017 · 4 comments

Comments

@aramando
Copy link

aramando commented Mar 15, 2017

My understanding of the intended behaviour of the withMetadata() output method is that it should add an sRGB ICC v2 profile to images that aren't already tagged with their own profile, or otherwise preserve any existing profile. However, using the below code, while images whose source is tagged with an sRGB profile are served with an sRGB profile, images not already tagged with a colour profile don't get one in the output.

sharp(imageFileDataBuffer) 
    .resize(w, h).max();
    .jpeg({ quality: 90 })
    .withMetadata()
    .toBuffer((err, buffer, info) => {
        // send response
    });

On a related note, it would be good to have the ability to strip all metadata except colour profile, which I don't think is possible with the current options. One might well wish to get rid of all EXIF/IPTC data for privacy reasons but still tag with an sRGB profile to ensure correct rendering in Chrome (which is the reason for my interest in this - Chrome's annoying failure to treat untagged images as sRGB is causing most images in my web app to display poorly on non-sRGB monitors).

@aramando aramando changed the title withMetadata() doesn't add sRGB profile for untagged images withMetadata() doesn't add sRGB profile to untagged images Mar 15, 2017
@lovell lovell added the triage label Mar 15, 2017
@lovell
Copy link
Owner

lovell commented Mar 15, 2017

As you're probably aware, sharp currently always converts to the sRGB colourspace before generating the output image (be that with or without a profile).

#218 is tracking the work to implement more fine-grained control over withMetadata.

"images not already tagged with a colour profile don't get one in the output"

This sounds like a possible bug, or at least I don't see an existing unit test for this case, so let me investigate.

@aramando
Copy link
Author

aramando commented Mar 21, 2017

@lovell I don't know if this is quite how you would approach solving the issue (especially as this is my first ever c++ code!), but these changes seem to do the trick; if withMetadata was invoked, images that do not already have an sRGB profile associated with them following a conversion from a different colourspace or input ICC profile will have the default sRGB profile attached (no conversion) at the point of outputting the JPEG or PNG buffer or file (or TIFF file). I've also added a couple of tests to check for an RGB profile in the JPG and PNG output from an untagged input image which fail until my changes to the pipeline are compiled.

An alternative solution I tested was adding the following code here:

} else if (image.interpretation() == VIPS_INTERPRETATION_sRGB) {
  image = image.icc_export(VImage::option()
    ->set("output_profile", profileMap[VIPS_INTERPRETATION_sRGB].data())
    ->set("intent", VIPS_INTENT_PERCEPTUAL));
}

This also seems to work, and is neater in that it achieves the end result in a single location in the code, but I thought that this was perhaps performing an unccessary conversion computation in addition to attaching the profile.

@lovell
Copy link
Owner

lovell commented Mar 21, 2017

Thanks for the update and test case, I agree your first proposed change is a suitable way to fix this for now.

I expect the work for #218 will result in a similar change, encapsulating a slightly more complex profile-determining logic before the image output stage is reached and hopefully without too much repetition.

@lovell
Copy link
Owner

lovell commented Oct 1, 2018

This is now being tracked at #1323

@lovell lovell closed this as completed Oct 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants