Received: from sgi.sgi.com (192.48.153.1) by cerious.com
 with SMTP (Apple Internet Mail Server 1.1.1); Thu, 13 Mar 1997 15:59:51 -0500
Received: from cthulhu.engr.sgi.com (cthulhu.engr.sgi.com [192.26.80.2]) by sgi.sgi.com (950413.SGI.8.6.12/950213.SGI.AUTOCF) via ESMTP id MAA16596; Thu, 13 Mar 1997 12:49:47 -0800
Received: (from daemon@localhost) by cthulhu.engr.sgi.com (950413.SGI.8.6.12/960327.SGI.AUTOCF) id SAA02801 for tiff-list; Thu, 13 Mar 1997 18:44:12 GMT
Received: from sgi.sgi.com (sgi.engr.sgi.com [192.26.80.37]) by cthulhu.engr.sgi.com (950413.SGI.8.6.12/960327.SGI.AUTOCF) via ESMTP id KAA02794 for <tiff-relay@relay.engr.sgi.com>; Thu, 13 Mar 1997 10:44:11 -0800
Received: from eis-net-002.jpl.nasa.gov (eis-net-002.jpl.nasa.gov [137.78.57.20]) by sgi.sgi.com (950413.SGI.8.6.12/950213.SGI.AUTOCF) via ESMTP id KAA11020 for <tiff@sgi.com>; Thu, 13 Mar 1997 10:44:06 -0800
Received: from tazboy.JPL.NASA.GOV (tazboy.jpl.nasa.gov [128.149.24.99]) by eis-net-002.jpl.nasa.gov (8.7.5/8.7.3) with SMTP id KAA02277; Thu, 13 Mar 1997 10:43:46 -0800 (PST)
Received: by tazboy.JPL.NASA.GOV (4.1/SMI-4.1+DXRs2.3)
	id AA10874; Thu, 13 Mar 97 11:40:31 TAN
Date: Thu, 13 Mar 97 11:40:31 TAN
From: ndr@tazboy.jpl.nasa.gov (Niles Ritter)
Message-Id: <9703131840.AA10874@tazboy.JPL.NASA.GOV>
To: mrathi@cs.wmich.edu
Subject: Re:  Question from a Starter..
Cc: tiff@sgi.com
Sender: owner-tiff@relay.engr.SGI.COM
Precedence: bulk

mrathi@cs.wmich.edu (manish rathi) writes,

> [example code below]
>  
>  #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.

>                  TIFFClose(read_tif);
>                  TIFFClose(write_tif);
>                  }                     
>   
>          }

Okay.

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

   http://www-mipl.jpl.nasa.gov/~ndr/tiff

Good Luck!

--Niles.
