Default values for some tags (e.g. for PlanarConfiguration) are not set
Summary
Issue report reference: https://www.asmail.be/msg0054918184.html
I noticed a strange behavior when trying to read the default value of the PlanarConfiguration tag when trying to create a new Tiff file using LibTIFF. When I try running the following code:
TIFF *tif = TIFFOpen("test.tif", "w"); unsigned short planar; TIFFGetFieldDefaulted(tif, TIFFTAG_PLANARCONFIG, &planar); printf("Default Planar value = %d\n", planar);
I get a value of 0 for the planar configuration although the standard specified that the default value is 1 not 0. Also if I try to write to this new Image:
TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, 1); TIFFSetField(tif, TIFFTAG_IMAGELENGTH, 1); TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3); TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8); unsigned char pixel[3] = {255, 255, 255}; TIFFWriteEncodedStrip(tif, 0, pixel, 3); TIFFClose(tif);
I get an error message:
TIFFWriteEncodedStrip: Must set "PlanarConfiguration" before writing data.
Although the standard specifies that the PlanarConfiguration tag is not required.
If the image is opened for reading, however, the default value is 1 even if it doesn't exist in the image which is the expected behavior, so is this a bug or this behavior necessary and if so, what is the reason behind this?
Version
Version: libtiff version 4.4.0 Platform: Windows 10 x64, Visual Studio 2022 with CMake
Analysis
When a new TIFF file is opened, some tags in TIFFDefaultDirectory()
are set to default values. However, td_planarconfig
and some other tags are not set to their defined default values.
The function TIFFVGetFieldDefaulted()
returns the value for td_planarconfig
as "0" even if it was not set before. This is not correct.
When an existing TIFF file is opened, TIFFReadDirectory()
sets planarconfig = 1
before reading the tags from the file with TIFFSetField()
.
This contradicts the statement at TIFFVGetFieldDefaulted()
:
NB: We use the value in the directory, rather than explicit values so that defaults exist only one place in the library -- in TIFFDefaultDirectory.
Note:
Tags with default values should always be read with TIFFGetFieldDefaulted() instead of TIFFGetField()!
Summary
Handling of tags with default values should be improved in LibTIFF.
Tags with default values not yet preset in LibTIFF are:
- TIFFTAG_PLANARCONFIG
- TIFFTAG_COMPRESSION
- TIFFTAG_SUBFILETYPE
- TIFFTAG_MINSAMPLEVALUE
- TIFFTAG_MAXSAMPLEVALUE
- TIFFTAG_EXTRASAMPLES
- ...???...