Search code examples
clibtiff

Reading .tif images in C using libtiff returns one column


I want to use "libtiff" library to read u8-bit pixel intensity values from ".tiff" images . i came across this code and modified it to read 8 bit values as desired and returns correct values for one column only .

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include "tiffio.h"
#define imsize 286628
 int count;
 int count2;
 uint8* im;
 uint32 imagelength;
 uint32 width;
int main(){

im = (uint8*)malloc(imsize*sizeof(uint8));
TIFF* tif = TIFFOpen("image1.tif", "r");
    if (tif) {

        tsize_t scanline;
        tdata_t buf;
        uint32 row;
        uint32 col;


        uint16 nsamples;

        TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &nsamples);
        TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &imagelength);
        TIFFGetField(tif,TIFFTAG_IMAGEWIDTH,&width);

        scanline = TIFFScanlineSize(tif);
        buf = _TIFFmalloc(scanline);
        uint8* data;

        for (row = 0; row < imagelength; row++)
        {
            TIFFReadScanline(tif, buf, row,1);
            count2++;
            for (col = 0; col < scanline; col++)
                data = (uint8*)buf;
                //printf("%d\n",col); remains the same not incrementing
                printf("%d ", *data);//printing for testing need only to copy to an array to access by index
                im[count] = *data;
                count++;
            printf("\n");

        }
        printf("im[1]= %d\n im[2] = %d \n im[3] = %d \n im[286628] = %d\n",im[0],im[1],im[2],im[286627]);
        _TIFFfree(buf);
        TIFFClose(tif);
        free(im);
    }
    printf("num of cols= %d\n",count);
    printf("num of rows = %d\n",count2);//both counts print col size
    printf("width = %d\n",width); //prints row size

    return 0;

}

in the nested forloop if I add brackets , the loops iterate over the correct number of pixels (for this example #of-pixels = 286628, 547x524 image), but with incorrect values . if i keep the brackets removed i get correct values but for the first column (only 547 values).

What changes need to be made to iterate over all the pixels correctly ?

note: i'm trying to obtain a matrix with values as matlabs "imread()"


Solution

  • In the col loop each column does exactly the same thing using data = (uint8*)buf; which seems to be the first column's data. The loop is also missing { braces }.

    Move the line

    data = (uint8*)buf;
    

    outside the column loop and increment it within the loop.

    for (row = 0; row < imagelength; row++)
    {
        TIFFReadScanline(tif, buf, row,1);
        count2++;
        data = (uint8*)buf;                     // move up
        for (col = 0; col < scanline; col++)
        {                                       // add braces
            printf("%d ", *data);
            im[count] = *data;
            count++;
            data++;                             // increment buffer pointer
        }
        printf("\n");
    }