Bug 344334 - Tiff filter uses integers instead of float when reading/writing 16f and 32f files
Summary: Tiff filter uses integers instead of float when reading/writing 16f and 32f f...
Status: RESOLVED FIXED
Alias: None
Product: krita
Classification: Applications
Component: File formats (show other bugs)
Version: git master (please specify the git hash!)
Platform: Other Linux
: NOR normal
Target Milestone: ---
Assignee: Halla Rempt
URL: http://ninedegreesbelow.com/bug-repor...
Keywords:
Depends on:
Blocks:
 
Reported: 2015-02-18 22:41 UTC by Elle Stone
Modified: 2015-05-02 18:51 UTC (History)
2 users (show)

See Also:
Latest Commit:
Version Fixed In:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Elle Stone 2015-02-18 22:41:50 UTC
Upon opening a 32-bit floating point tiff that had been exported from GIMP 2.9, Krita showed the checkerboard pattern. Upon exporting the tiff from Krita, GIMP opened the exported tiff and displayed it properly. 

Upon exporting the tiff from GIMP again, this time after adding an alpha channel, and opening it with Krita, the tiff was blue instead of showing the right colors. Upon exporting from Krita yet again, GIMP opened the image and displayed it properly.

darktable opens and displays the GIMP-exported tiffs with no problem.

Reproducible: Always

Steps to Reproduce:
1. Export a 32-bit floating point tiff from GIMP 2.9
2. Open it with Krita.
3. It doesn't display properly, though upon export from Krita under a new name, GIMP can open the Krita-exported image and display it properly.

Actual Results:  
The Krita-opened tiff shouldn't look like a checkerboard and it shouldn't look blue.

Expected Results:  
The Krita-opened tiff should look like the same tiff that GIMP exported.

Krita 2.9 beta compiled from git yesterday, on 64-bit Gentoo Linux. GIMP 2.9 compiled from git.
Comment 1 Elle Stone 2015-02-18 22:43:20 UTC
A sample tiff can be downloaded from http://ninedegreesbelow.com/bug-reports/krita-32-bit-floating-point-tiff.html
Comment 2 Halla Rempt 2015-02-19 08:37:34 UTC
Yes, I can confirm the issue. The same happens with 32f tiff files exported from photoshop.
Comment 3 Halla Rempt 2015-02-19 08:48:21 UTC
I'm not sure, but it looks like our tiff code assumes 32 bits integer, not float...

uint KisTIFFReaderTarget32bit::copyDataToChannels(quint32 x, quint32 y, quint32 dataWidth, KisBufferStreamBase* tiffstream)
{
    KisHLineIteratorSP it = paintDevice()->createHLineIteratorNG(x, y, dataWidth);
    double coeff = quint32_MAX / (double)(pow(2.0, sourceDepth()) - 1);
//         dbgFile <<" depth expension coefficient :" << coeff;
    do {
        quint32 *d = reinterpret_cast<quint32 *>(it->rawData());
        quint8 i;
        for (i = 0; i < nbColorsSamples(); i++) {
            d[poses()[i]] = (quint32)(tiffstream->nextValue() * coeff);
        }
        postProcessor()->postProcess32bit(d);
        if (transform()) transform()->transform((quint8*)d, (quint8*)d, 1);
        d[poses()[i]] = quint32_MAX;
        for (int k = 0; k < nbExtraSamples(); k++) {
            if (k == alphaPos())
                d[poses()[i]] = (quint32)(tiffstream->nextValue() * coeff);
            else
                tiffstream->nextValue();
        }

    } while (it->nextPixel());
    return 1;
}
Comment 4 Halla Rempt 2015-02-19 09:08:20 UTC
Git commit fed7d2ab64f87c9c1c0d0039eca6a7fa47152222 by Boudewijn Rempt.
Committed on 19/02/2015 at 09:07.
Pushed by rempt into branch 'calligra/2.9'.

Fix overflow: alphapos is -1 by default, so don't store it in a quint8.

M  +1    -1    krita/plugins/formats/tiff/kis_tiff_reader.h

http://commits.kde.org/calligra/fed7d2ab64f87c9c1c0d0039eca6a7fa47152222
Comment 5 Halla Rempt 2015-02-19 09:14:23 UTC
More data:

09:48:57 < boud> CyrilleB: do you remember whether our tiff import/export code was written for 32 bits integer/channel or 32 bits float/channel?
09:53:15 < boud> because if it is, I don't understand the use of quint32_MAX in KisTIFFReaderTarget32bit::copyDataToChannels
10:03:29 < CyrilleB> boud: it should be able to handle both, I think
10:06:16 < boud> hm... something is definitely wrong there, we set a  quint8 to -1, so the alpha channel position becomes 255, and in the case of 32f, we set alpha by default to quint32_MAX.
10:06:37 < boud> if I make that OPACITY_OPAQUE_F, the images shows up, but all colors are skewed
10:07:03 < boud> oh, well, that's for later, now I need to focus on the job first
10:08:08 < CyrilleB> and that would break for integer32
10:08:50 < CyrilleB> there should probably be a different reader for floating point
10:09:16 < boud> yes... but getColorSpaceForColorType actually seems to return nothing for a 32 integer file
10:09:45 < boud> so, the KisTIFFReaderTarget32bit only kicks in for 32 float files, where it does the wrong thing. At least... That's my guess.
10:10:06 < CyrilleB> right there is no 32bits integer colorspace
10:10:34 < boud> I was thinking it might get downsampled to 16 bits integer if we encounter such a file
10:10:43 < boud> but that doesn't seem to happen either
10:10:48 < CyrilleB> no, it just fails
10:11:11 < CyrilleB> but KisTIFFReaderTarget16bit::copyDataToChannels has the same problem for the "half" color space
10:11:52 < boud> so that does need to be copied and adapted for float
10:12:20 < CyrilleB> or templatified :)
10:12:27 < boud> yes, true
10:13:44 < boud> and KisTIFFWriterVisitor::copyDataToStrips seems to have the same problem, it assumes integers
Comment 6 Halla Rempt 2015-02-20 08:25:14 UTC
Git commit 128a1fa4e022a42ff1ff36240992fd21ab7739e3 by Boudewijn Rempt.
Committed on 19/02/2015 at 09:31.
Pushed by rempt into branch 'calligra/2.9'.

Do different things depending on whether the image is float or not

32f was saved as 32i
16f and 16i were both saved as 16i

However, we're still saving red in a way that photoshop loads it as
blue...

M  +47   -22   krita/plugins/formats/tiff/kis_tiff_writer_visitor.cpp
M  +1    -1    krita/plugins/formats/tiff/kis_tiff_writer_visitor.h

http://commits.kde.org/calligra/128a1fa4e022a42ff1ff36240992fd21ab7739e3
Comment 7 Halla Rempt 2015-02-20 08:25:15 UTC
Git commit 65e0480ee959c5863fcdeaa6563ae175994759de by Boudewijn Rempt.
Committed on 19/02/2015 at 09:51.
Pushed by rempt into branch 'calligra/2.9'.

Add a sample_format parameter to all the readers

M  +153  -121  krita/plugins/formats/tiff/kis_tiff_converter.cc
M  +5    -1    krita/plugins/formats/tiff/kis_tiff_reader.cc
M  +39   -10   krita/plugins/formats/tiff/kis_tiff_reader.h
M  +10   -2    krita/plugins/formats/tiff/kis_tiff_ycbcr_reader.cc
M  +8    -2    krita/plugins/formats/tiff/kis_tiff_ycbcr_reader.h

http://commits.kde.org/calligra/65e0480ee959c5863fcdeaa6563ae175994759de
Comment 8 Halla Rempt 2015-02-20 08:25:17 UTC
Git commit 2e8a8659d99d7ed806bed571dccab94b9276f0d4 by Boudewijn Rempt.
Committed on 20/02/2015 at 08:22.
Pushed by rempt into branch 'calligra/2.9'.

Disable the 32f and 16f formats for Tiff import/export

The code was written to handle 16 and 32 bit integer, but Krita doesn't
support 32 bit integer colorspaces, so we were loading 16f and 32f as
if they contained integers, which has always been wrong.

I haven't yet figured out how to properly load floating point tiff files
yet, and it's unlikely I'll manage before we release 2.9.0, so disable
these formats for now.

M  +8    -8    krita/plugins/formats/tiff/kis_tiff_converter.cc
M  +2    -5    krita/plugins/formats/tiff/kis_tiff_import.cc
M  +5    -5    krita/plugins/formats/tiff/kis_tiff_writer_visitor.cpp

http://commits.kde.org/calligra/2e8a8659d99d7ed806bed571dccab94b9276f0d4
Comment 9 Elle Stone 2015-02-20 12:07:39 UTC
When opening a 32-bit integer tiff that was exported by GIMP, Krita opens it as a 16-bit integer tiff.  A sample tiff can be found here: http://ninedegreesbelow.com/bug-reports/krita-32-bit-floating-point-tiff.html

This was the case before the last commit that disables importing floating point tiffs, and is still the case.
Comment 10 Halla Rempt 2015-02-20 12:33:19 UTC
Well, Krita has never supported 32 bit integer/channel colorspaces, so that's not a big surprise.
Comment 11 Elle Stone 2015-02-20 12:41:32 UTC
Ah, I didn't realize that. Sorry for the noise.
Comment 12 Cyrille Berger 2015-04-14 16:05:50 UTC
Git commit 7576870d6ecab6cdb133b41916ce2bd4734528a9 by Cyrille Berger.
Committed on 14/04/2015 at 16:05.
Pushed by berger into branch 'calligra/2.9'.

Disable opening 32bit float grayscale TIFF since they crash

M  +3    -0    krita/plugins/formats/tiff/kis_tiff_converter.cc

http://commits.kde.org/calligra/7576870d6ecab6cdb133b41916ce2bd4734528a9
Comment 13 Cyrille Berger 2015-05-02 09:50:55 UTC
Git commit d0f249c058b6b9f2accd9185cbbcefa1d81ab38d by Cyrille Berger.
Committed on 02/05/2015 at 09:50.
Pushed by berger into branch 'master'.

fix crash when opening grayscale floating point

M  +36   -13   krita/plugins/formats/tiff/kis_tiff_converter.cc
M  +8    -7    krita/plugins/formats/tiff/kis_tiff_reader.cc
M  +10   -4    krita/plugins/formats/tiff/kis_tiff_reader.h

http://commits.kde.org/calligra/d0f249c058b6b9f2accd9185cbbcefa1d81ab38d
Comment 14 Cyrille Berger 2015-05-02 09:50:55 UTC
Git commit 181a0545e353c4fbc2e2a7fbd8c2aaf45f1296cf by Cyrille Berger.
Committed on 02/05/2015 at 09:50.
Pushed by berger into branch 'master'.

fix order of colors for floating point (it is worth to note that photoshop and gimp are not respecting the TIFF standard in that aspect)

M  +19   -13   krita/plugins/formats/tiff/kis_tiff_converter.cc
M  +12   -7    krita/plugins/formats/tiff/kis_tiff_writer_visitor.cpp

http://commits.kde.org/calligra/181a0545e353c4fbc2e2a7fbd8c2aaf45f1296cf
Comment 15 Halla Rempt 2015-05-02 18:51:08 UTC
Git commit 0c092834b26ccdb777150724cc9ca872a8718414 by Boudewijn Rempt, on behalf of Cyrille Berger.
Committed on 02/05/2015 at 18:50.
Pushed by rempt into branch 'calligra/2.9'.

fix crash when opening grayscale floating point

M  +36   -13   krita/plugins/formats/tiff/kis_tiff_converter.cc
M  +8    -7    krita/plugins/formats/tiff/kis_tiff_reader.cc
M  +10   -4    krita/plugins/formats/tiff/kis_tiff_reader.h

http://commits.kde.org/calligra/0c092834b26ccdb777150724cc9ca872a8718414
Comment 16 Halla Rempt 2015-05-02 18:51:09 UTC
Git commit dc54d8126265ba01a62e14ccccbfdc8d9b724099 by Boudewijn Rempt, on behalf of Cyrille Berger.
Committed on 02/05/2015 at 18:50.
Pushed by rempt into branch 'calligra/2.9'.

fix order of colors for floating point (it is worth to note that photoshop and gimp are not respecting the TIFF standard in that aspect)

M  +19   -13   krita/plugins/formats/tiff/kis_tiff_converter.cc
M  +12   -7    krita/plugins/formats/tiff/kis_tiff_writer_visitor.cpp

http://commits.kde.org/calligra/dc54d8126265ba01a62e14ccccbfdc8d9b724099