Twinside / Juicy.Pixels

Haskell library to load & save pictures

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Jpeg has false colors

alexkazik opened this issue · comments

If an RGB image is written to a JPEG it is not automatically converted to YCbCr and is larger, seems to be too short (GIMP, jpegtopnm and djpeg complain about "premature end of data segment") and the built in viewers in Mac OS and Photoshop Elements show it in false colors.

Please see here for the initial discovery: lehins/hip#11

This is likely related to #131, which was fixed in 3.2.7.2, but it seems that "shitty fix" turned out to be... well... shitty :)
Details are in lehins/hip#11 issuecomment-284154546, but to sum up the issue:

  • seems to happen on Mac OS (cannot reproduce it on Ubuntu 16.04)
  • affects JPEG images encoded in RGB8 color space.

Sadly I don't think this is related to #131, for which I've made a non-shity© fix (aherm).

I'll have some problem to reproduce the issue: I don't have Mac OS nor Photoshop Elements. But given the name of one of the software, I've added the writing of the Adobe's infamous APP14 segment which can help specify the color space used in the JPEG file. If you could test HEAD and report the result that would be great.

That does solve the problem (correct colors in Mac OS viewers and Photoshop Express and also djpeg is no longer complaining).

I'm using a conversion to YCbCr now and it's smaller (and works even without the patch).

I'm curious why RGB is not converted to YCbCr automatically?

Well using encodeDirectJpegAtQualityWithMetadata you can encode any known color-space you want, it's a function that has been asked. The proper way to encode a jpeg is through encodeJpegAtQualityWithMetadata (without the direct).

And for why there isn't any automatic conversion for Jpeg encoding, there is one actually: Codec.Picture.Saving.imageToJpg. It's not the default because any color conversion is potentially lossy and I prefer to make them explicit as much as possible.

I'll upload the fixed package on Hackage tomorrow :)