photopea / UTIF.js

Fast and advanced TIFF decoder
MIT License
424 stars 87 forks source link

Decode grayscale tiff wrong #129

Closed GHolk closed 2 months ago

GHolk commented 2 months ago

This TIFF file scaned with grayscale option in my office's scaner. It displays correctly on windows, but photopea and UTIF.js decode it wrong.

If the tiff file decoded correctly, it should look like this:

tiff in jpeg

But photopea (and UTIF) decode it like this:

screen-shot_2024-07-08_20-35-00

The grayscale tiff in zip: my-newspaper.zip

This bug does not bother me very much, but it seem that there are a lot of scaners which can produce TIFF in this format. Maybe it is worthy to fix.

PS: Imagick show this message while convert this tiff to jpeg:

convert-im6.q16: Deprecated and troublesome old-style JPEG compression mode,
please convert to new-style JPEG compression and notify vendor of writing software.
 `OJPEGSetupDecode' @ warning/tiff.c/TIFFWarnings/981.
johnthad commented 2 months ago

FWIW, I've run across many TIFFs of this type. Its format is known as "Old-style JPEG", that is its Compression tag equals 6. There has been much disagreement over this format's description in the TIFF spec, and I've seen these TIFF formatted two different ways. It does not open in many TIFF readers that I've come across, including macOS Preview.

The only way that I have found to reliably open this type of TIFF is to ignore the TIFF tags whenever the Compression Tag (259 or 0x103) equals 6, and extract the JPEG that's inside. I do this by searching for the string 'JFIF' in the byte array, backing up 6 bytes to the start of the JPEG, and reading from there to the end of the array. That technique works for this image. Unfortunately I don't have code for this in JavaScript. The images I deal with are fetched from a server, and that code is written in Java.

photopea commented 2 months ago

It should be fixed now, both in Photopea and in UTIF.js