Details
-
Suggestion
-
Resolution: Unresolved
-
P2: Important
-
None
-
5.12.12, 6.4.0
-
None
Description
When attempting to load a TIFF image, QImage fails to load tiff image where PHOTOMETRIC TIFF tag. This tag is not optional as per the documentation, but can be deduced depending on other existing tags or compression type to be able to read the image.
Those Images load correctly using Windows Image Preview, Microsoft Paint, GIMP, and Photoshop.
Defaulting the value of PHOTOMETRIC to say MINISWHITE or MINISBLACK, causes the images to be loaded properly. (GIMP does that)
Problematic code /src/gui/image/qtiffhandler.cpp :
bool QTiffHandlerPrivate::readHeaders(QIODevice *device) { if (headersRead) return true; if (!openForRead(device)) return false; TIFFSetDirectory(tiff, currentDirectory); uint32_t width; uint32_t height; if (!TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &width) || !TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &height) || !TIFFGetField(tiff, TIFFTAG_PHOTOMETRIC, &photometric)) { close(); return false; } // ..... rest of method code }
Suggested fix:
bool QTiffHandlerPrivate::readHeaders(QIODevice *device) { if (headersRead) return true; if (!openForRead(device)) return false; TIFFSetDirectory(tiff, currentDirectory); uint32 width; uint32 height; if (!TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &width) || !TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &height)) { close(); return false; } //@ added default value to photo-metric tag to avoid failure in loading tiff file when the tag is missing. if(!TIFFGetField(tiff, TIFFTAG_PHOTOMETRIC, &photometric)) { //photometric = PHOTOMETRIC_MINISWHITE ; //PHOTOMETRIC_MINISBLACK ; quint16 compression; if (TIFFGetField (tiff, TIFFTAG_COMPRESSION, &compression) && (compression == COMPRESSION_CCITTFAX3 || compression == COMPRESSION_CCITTFAX4 || compression == COMPRESSION_CCITTRLE || compression == COMPRESSION_CCITTRLEW)) { photometric = PHOTOMETRIC_MINISWHITE; } else { photometric = PHOTOMETRIC_MINISBLACK; } } // ..... rest of method code }
Edit: I've attached a sample tiff image file that doesn't have PhotoMetric tag, and sample source code that was used to read and preview tiff image.