1997.03.09 15:34 "Question from a Starter..", by Manish Rathi

1997.03.13 11:40 "Re: Question from a Starter..", by Niles Ritter

 #include <stdio.h>
#include "tiffio.h"
void main() {

        TIFF* read_tif = TIFFOpen("penny.tiff", "r");
        TIFF* write_tif = TIFFOpen("my_penny.tiff", "wa");

        if ( (read_tif == 1) & (write_tif == 1) ) {

Remark 1:

Be careful -- single "&" is a bitwise operator in C; its use is okay here, but in general use "&&" for logical "and".

Remark 2:

TIFFOpen returns a pointer to a TIFF descriptor, and so will be some memory address (probably not 1). If TIFFOpen fails, it will return NULL, so the above test should be changed to:

          if ( (read_tif) && (write_tif) ) {

                int imageWidth, imageLength;
                int x, y;
                int i;
                unsigned int* buf;
                unsigned int** final_buf;

                TIFFGetField(read_tif, TIFFTAG_IMAGEWIDTH, &imageWidth);
                TIFFGetField(read_tif, TIFFTAG_IMAGELENGTH, &imageLength);

                buf = (unsigned int*)
                         malloc( imageWidth * sizeof(unsigned int));

The image may be more than a single byte per pixel, so (assuming that these are strip, not tiled images), you should use TIFFScanlineSize() to determine how much memory to allocate to "buf".

Also, you have opened a new "write_tif" file and are writing to it without first setting the image width, length, bitspersample, photometric, etc. You will need to call TIFFSetField for each of these tags before you can successfully create an image. You will need to set the image compression tag as well, if you want the compression to match the original.

                for ( i = 0; i < imageWidth; i++)
                        final_buf[i] = (unsigned int*)
                                 malloc (imageWidth * sizeof(unsigned int) );

Why read the whole image into memory? Why not just read in a scanline, then write out the scanline, using the same line buffer over again?

Something like:

                  if ( buf != NULL) {
                          int row;
                          for ( row = 0; row < imageLength; row++) {
                                  TIFFReadScanline( read_tif, buf, row, 0 );
                                  TIFFWriteScanline( write_tif,buf, row, 0);

You will want to check the status of these calls as well, just in case they fail.




There are some links to HTML docs on libtiff, visible from my TIFF web page at


Good Luck!