1997.05.05 08:00 "Can't write Tiff directory first", by Pesce Roberto

1997.05.05 08:00 "Can't write Tiff directory first", by Pesce Roberto

hello,

I'm quite a newbie as a LIBTiff user, in fact I only used lilbtiff for reading 'till today and all worked fine; now I have to write some group 4 compressed images but when I tried the writing/compressing capabilities, stealing some code from the samples included with the library, I succeeded in writing but the TIFF directory is appended to the compressed data.

The image is correct and the file is OK, but I'd like to have the directory at the beginning of the file...

Here follows the sample code I used to write the file:

int WriteTIFF(pic,w,h,rmap,gmap,bmap,
                     fname,comp,comment)
     unsigned char *pic;
     int   w,h,comp;
     unsigned char *rmap, *gmap, *bmap;
     char *fname, *comment;
{
  double res;
  TIFF *tif;
  unsigned char *pix;
  int   i,j;
  int sz;
  unsigned short *ppix;
  tdata_t buf;

  tif = TIFFOpen(fname, "w");

  if (!tif) return 0;

  TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, w);
  TIFFSetField(tif, TIFFTAG_IMAGELENGTH, h);
  TIFFSetField(tif, TIFFTAG_COMPRESSION, comp);

  if (comment && strlen(comment)>0) {
    TIFFSetField(tif, TIFFTAG_IMAGEDESCRIPTION, comment);
  }

  if (comp == COMPRESSION_CCITTFAX3)
      TIFFSetField(tif, TIFFTAG_GROUP3OPTIONS,
          GROUP3OPT_2DENCODING+GROUP3OPT_FILLBITS);

  TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
  TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
  TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
  TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, h);

  res = 200;
  TIFFSetField(tif, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB);
  TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, RESUNIT_NONE);
  TIFFSetField(tif, TIFFTAG_XRESOLUTION, res);
  TIFFSetField(tif, TIFFTAG_YRESOLUTION, res);

  TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 1);
  TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE);

  TIFFFlush(tif);
  sz = TIFFStripSize(tif);

  buf = malloc(TIFFStripSize(tif));
  memcpy(buf,pic,sz);

  TIFFWriteEncodedStrip(tif, 0, buf, (tsize_t) sz); 

  free(buf);

  TIFFClose(tif);

  return 0;
}

I've tried to put an explicit call to the TIFFWriteDirectory() before the TIFFFlush() but a warning is raised claiming that I Must set the Image Width before doing that, and that's the first TAG I set.

Many thanxs in advance to anyone that will help me

______________________________________________________

Roberto Pesce
Divisione MAP
* +39-10-6582250
fax +39-10-6582498
* Roberto.Pesce@elsag.it

Elsag Bailey - un'Azienda FINMECCANICA
Via Puccini, 2
16154 Genova - ITALY