Search code examples
c++libtiff

libtiff: How to get values from the TIFFGetField routine?


I have a .tif file and would like to get the image width using libtif. I have tried the following c++ code so far:

TIFF* tif = XTIFFOpen(filenameStr.c_str(), "w");
if (!tif) {
    std::cout << "Error: failed to open file" << std::endl;
}
int32_t width = 0;

int test = TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
std::cout << test << std::endl;

Running the code returns 0 \n ". Thus, I'm able to open the tiff file but using the TIFFGetField routine doesn't seem to work. Can anyone explain what I'm doing wrong? What do I have to do in order to write the width of that .tif file into my variable? Unfortunately, I don't understand the documentation provided. The .tif file is a regular picture that can be opened with app's. Thus, it has to have a width.


Solution

  • The function TIFFGetField() doesn't return a value. Instead it modifies the variable whose address you pass:

    uint32_t width;
    
    TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
    std::cout << width << std::endl;