sharp icon indicating copy to clipboard operation
sharp copied to clipboard

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

Open richhost opened this issue 1 year ago • 11 comments

Possible bug

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

  • [x] Running npm install sharp completes without error.
  • [x] Running node -e "require('sharp')" completes without error.

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

Are you using the latest version of sharp?

  • [x] I am using the latest version of sharp as reported by npm view sharp dist-tags.latest.

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.

  • [ ] Adding sharp.cache(false) does not fix this problem.

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.

  • To auto-orient pixel values use the parameter-less rotate() operation.

  • To retain EXIF Orientation use keepExif().

  • [ ] Using rotate() or keepExif() does not fix this problem.

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

richhost avatar Jun 10 '24 02:06 richhost

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");

lovell avatar Jun 10 '24 06:06 lovell

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

richhost avatar Jun 10 '24 07:06 richhost

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

lovell avatar Jun 10 '24 11:06 lovell

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

kleisauke avatar Jun 10 '24 17:06 kleisauke

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

lovell avatar Jun 10 '24 17:06 lovell

@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

richhost avatar Jun 11 '24 02:06 richhost

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

lovell avatar Jun 11 '24 06:06 lovell

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

lovell avatar Jul 02 '24 08:07 lovell

@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.

richhost avatar Jul 02 '24 14:07 richhost

@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.

lovell avatar Aug 11 '24 09:08 lovell

@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.

richhost avatar Aug 11 '24 12:08 richhost