lovell / sharp

High performance Node.js image processing, the fastest module to resize JPEG, PNG, WebP, AVIF and TIFF images. Uses the libvips library.
https://sharp.pixelplumbing.com
Apache License 2.0
29.17k stars 1.3k forks source link

ICC profiles in PNG images generated by Figma have truncated iCCP chunks #4128

Open richhost opened 4 months ago

richhost commented 4 months ago

Possible bug

Is this a possible bug in a feature of sharp, unrelated to installation?

If you cannot confirm both of these, please open an installation issue instead.

Are you using the latest version of sharp?

If you cannot confirm this, please upgrade to the latest version and try again before opening an issue.

If you are using another package which depends on a version of sharp that is not the latest, please open an issue against that package instead.

What is the output of running npx envinfo --binaries --system --npmPackages=sharp --npmGlobalPackages=sharp?

Does this problem relate to file caching?

The default behaviour of libvips is to cache input files, which can lead to EBUSY or EPERM errors on Windows. Use sharp.cache(false) to switch this feature off.

Does this problem relate to images appearing to have been rotated by 90 degrees?

Images that contain EXIF Orientation metadata are not auto-oriented. By default, EXIF metadata is removed.

What are the steps to reproduce?

My operating system is macOS, and I use a display with P3 color.

sharp('img.png').resize({ width: 800 }).toFile("out.png");

What is the expected behaviour?

Keep the original colors.

Please provide sample image(s) that help explain this problem

original image: img

output image: out

lovell commented 4 months ago

My operating system is macOS, and I use a display with P3 color.

The input image contains a P3 ICC profile.

https://sharp.pixelplumbing.com/api-output#keepiccprofile

sharp('img.png')
  .resize({ width: 800 })
  .keepIccProfile()
  .toFile("out.png");
richhost commented 4 months ago

I tried the above code, but it didn't work.

lovell commented 4 months ago

I took a closer look and the compressed iCCP chunk in the sample PNG image you've provided, which is where the PNG format stores ICC profiles, cannot be decompressed via libspng. This means that libspng reports that there is no profile. Here's the output from metadata() for this image - note hasProfile: false.

{
  format: 'png',
  width: 738,
  height: 1758,
  space: 'srgb',
  channels: 4,
  depth: 'uchar',
  density: 216,
  isProgressive: false,
  hasProfile: false,
  hasAlpha: true
}

When using libvips compiled with support for libpng there is an ICC profile available, so my best guess would be that this relates to https://github.com/randy408/libspng/issues/14. Do you know the source of this image? Perhaps the compressed zlib stream within the iCCP chunk is truncated?

Please can you report this issue upstream at https://github.com/randy408/libspng

kleisauke commented 4 months ago

The DEFLATE stream within the iCCP chunk appears to be truncated for this particular image, according to both exiftool and https://www.nayuki.io/page/png-file-chunk-inspector.

$ exiftool -icc_profile -b x.png
Warning: Error inflating iCCP - x.png
lovell commented 4 months ago

@kleisauke Thanks for confirming my suspicions, I'm not sure how libpng manages to inflate the truncated profile though :shrug:

richhost commented 4 months ago

@lovell Thank you for the information. I exported a PNG image with the P3 Color profile using Figma. In Sharp, it shows hasProfile: false.

image
lovell commented 4 months ago

@richhost Thank you, in that case please can you report this to Figma.

lovell commented 4 months ago

@richhost Were you able to report this to Figma? If so, what did they say?

richhost commented 4 months ago

@richhost Were you able to report this to Figma? If so, what did they say?

@lovell Yes, I sent an email to Figma two weeks ago, but I haven't heard back yet.

lovell commented 2 months ago

@richhost Have you had any response from Figma?

@jake-figma Wearing your Figma Developer Advocate hat, are you able to provide any insight into this? The summary is that it appears Figma is currently exporting PNG images with corrupt P3 ICC profiles. Thank you.

richhost commented 2 months ago

@lovell Unfortunately, I have not received any response from Figma, and the problem still persists. If @jake-figma sees this message, I would greatly appreciate if he could help follow up on this issue.