2023.04.12 15:14 "[Tiff] TIffWriteDirectory() causes problems", by David C. Partridge

2023.04.12 15:14 "[Tiff] TIffWriteDirectory() causes problems", by David C. Partridge

So the code I have to open a TIFF file ends as follows:

                    // 
                    // Now that all the EXIF tags are written, need to write the EXIF 
                    // custom directory into the file... 
                    // 
                    // WriteCustomDirectory returns the actual offset of the EXIF directory. 
                    // 
                    TIFFWriteCustomDirectory(m_tiff, &dir_offset_EXIF); 

                    // Go back to the first (main) directory, and set correct value of the 
                    // EXIFIFD pointer. Note that the directory is reloaded from the file! 
                    // 
                    TIFFSetDirectory(m_tiff, 0); 
                    TIFFSetField(m_tiff, TIFFTAG_EXIFIFD, dir_offset_EXIF); 

             } 
             else 
             { 
                    TIFFClose(m_tiff); 
                    m_tiff = nullptr; 
             }; 
       }; 

       return bResult; 
};

m_tiff should still be valid on exit

I also have code to write the image data:

Which starts like:

bool CTIFFWriter::Write() 

       ZFUNCTRACE_RUNTIME(); 
       bool         bResult = false; 
       bool         bError = false; 

       // 
    // Multipliers of 256.0 and 65536.0 were not correct and resulted in a fully saturated 
       // pixel being written with a value of zero because the value overflowed the data type 
       // which was being stored.   Change the code to use UCHAR_MAX and USHRT_MAX 
       // 

       if (m_tiff) 
       { 
             tmsize_t            scanLineSize; 
             tdata_t                   buff; 

             scanLineSize = TIFFScanlineSize(m_tiff);

That works but ends up with the root IFD being written at the end of the file what TIFFClose calls TIFFReWriteDirectory().

If I insert TIFFWriteDIrectory(m_tiff); immediately after the line that reads TIFFSetField(m_tiff, TIFFTAG_EXIFID, dir_offset_EXIF);

then when I call into Write the value returned by TiffScanlinesSize() is ZERO!

WHY is that and what can/should it do to fix this

Thanks, David