Libtiff: How can I get pixel values OR how can I convert TIFF-files to text files

3.7k views Asked by At

I'm trying to get libtiff to read out tiff files that consist of one strip of about 500x500 32-Bit pixels using the method TIFFReadScanline(tif, buf, row). This gives me tdata_t (??) rows.

How can I write out this buffer as text file or access pixel values (should be doubles)?

My code looks like this:

TIFF* tif = TIFFOpen(c_str2, "r");
uint32 imagelength;
tdata_t buf;
uint32 row;

TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &imagelength);
buf = _TIFFmalloc(TIFFScanlineSize(tif));

for (row = 0; row < imagelength; row++){
        TIFFReadScanline(tif, buf, row);
        myfile << buf << endl;
}

In the last line I try to write write out the whole buffer into a text file, but there are no double values but Hex-Values. When I replace the tdata_t buffer by a char buffer there is ASCII symbol gibberish. I think I should convert the tdata_t buffer to a double or char buffer but how?

It shouldn't be byte-order since libtiff handles this automatically I think.

Any suggestions welcome! Thanks for helping, wish you all a nice weekend!

1

There are 1 answers

2
Martin Beckett On

The << noticed you are outputting types of tdata_t which are probably ints and puts them into hex to make them easier to read.

Just loop over all the elements in a row (in buf) and output them as floats with << (float)buf[element]