Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-107682

TIFF image files failed to load if PhotoMetric interpretation tag missing

    XMLWordPrintable

Details

    • Suggestion
    • Resolution: Unresolved
    • P2: Important
    • None
    • 5.12.12, 6.4.0
    • Image formats
    • None
    • Windows

    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.

       

      Attachments

        1. readTiffImage-test-src.zip
          2 kB
        2. 003.tif
          579 kB
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            vgt Eirik Aavitsland
            anbu Ahmed Bedeer
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes