S.shin
S.shin

Reputation: 61

32bit tif image read and write using libtiff library. (c++)

I have a problem with reading and saving an 32 bit image using the LibTIFF library.

In my code I have an array of uint32s representing the pixel values of the image.

When I save these values to a tif file, it looks fine. However, when I try to read and save it as a same TIFF file using the LibTIFF library, the image have same overflowed value(842150464.. it too big!!) at all pixel arrays.

As a result of debugging, the image reading parts seems to be no problem. I think writing part is wrong but i can't fix it. (I tried TIFFWriteScanline and TIFFWriteEncodedStrip)

original image and written image(black pixels)

//---------------reading part------------------
TIFF* tif = TIFFOpen(filepath.c_str(), "r");

TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);
uint32* buffer = (uint32 *)malloc(width*height * sizeof(uint32));
TIFFReadRawStrip(tif, 0, buffer, width*height);

//---------------writing part------------------

TIFF *image = TIFFOpen("input.tif", "w");
TIFFSetField(image, TIFFTAG_IMAGEWIDTH, width); 
TIFFSetField(image, TIFFTAG_IMAGELENGTH, height); 
TIFFSetField(image, TIFFTAG_BITSPERSAMPLE, 32); 
TIFFSetField(image, TIFFTAG_SAMPLESPERPIXEL, 1); 
TIFFSetField(image, TIFFTAG_ROWSPERSTRIP, 1);   
TIFFSetField(image, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
TIFFSetField(image, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(image, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
TIFFSetField(image, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
TIFFSetField(image, TIFFTAG_COMPRESSION, COMPRESSION_NONE);

scan_line = (uint32 *)malloc(width*(sizeof(uint32)));

for (int i = 0; i < height; i++) {

    memcpy(scan_line, &buffer[i*width], width * sizeof(uint32));
    TIFFWriteScanline(image, scan_line, i, 0);
}

TIFFClose(image);
TIFFClose(tif);
free(buffer);
free(scan_line);

Upvotes: 3

Views: 5147

Answers (1)

S.shin
S.shin

Reputation: 61

I've fixed the issue. Unlike what I originally expected, there was a problem with the reading part. The function could only read one line of video pixels at a time, and the code was modified by.

for (int i = 0; i < height; i++) //loading the data into a buffer
            {
                TIFFReadScanline(tif, scan_line, i);
                memcpy(&buffer[i*width], scan_line, width * sizeof(uint32));


            }

Upvotes: 3

Related Questions