2003.06.25 12:18 "Pb with TIFFWriteScanline", by Marine Martin-Guillerez

2003.06.25 12:18 "Pb with TIFFWriteScanline", by Marine Martin-Guillerez

For writing an image TIFF i use the following code:

  CDEF (Filename, 256);
  TIFF *ImageTiff, *ImageDst;

  sprintf (Filename, "Image\\20030620_c.tif");
  ImageTiff = TIFFOpen (Filename, "r");
  ImageDst = TIFFOpen ("copy.tif","w");
  if (ImageTiff)
  {
   ULONG FileHeight, FileWidth, Band, FileBits, Compress;
   ULONG TileHeight, TileWidth, PlanarConf;

   FileHeight = FileWidth = Band = FileBits = Compress = 0;
   TileHeight = TileWidth = PlanarConf = 0;

   TIFFGetField (ImageTiff, TIFFTAG_IMAGEWIDTH, &FileWidth);
   TIFFGetField (ImageTiff, TIFFTAG_IMAGELENGTH, &FileHeight);
   TIFFGetField (ImageTiff, TIFFTAG_TILEWIDTH, &TileWidth);
   TIFFGetField (ImageTiff, TIFFTAG_TILELENGTH, &TileHeight);
   TIFFGetField (ImageTiff, TIFFTAG_BITSPERSAMPLE, &FileBits);
   TIFFGetField (ImageTiff, TIFFTAG_SAMPLESPERPIXEL, &Band);
   TIFFGetField (ImageTiff, TIFFTAG_COMPRESSION, &Compress);
   TIFFGetField (ImageTiff, TIFFTAG_PLANARCONFIG, &PlanarConf);


   if (Compress != COMPRESSION_NONE)
    AfxMessageBox ("image compressée", MB_ICONSTOP);
   else
   {
    if (FileBits == 8)
    {
     BYTE *BufImage = new BYTE[FileWidth * FileHeight];
     ULONG x , y;


     // Lecture
     for (y = 0; y < FileHeight; y ++)
      TIFFReadScanline (ImageTiff, BufImage, y);


      // Modification
     for (y = FileHeight / 4; y < (3 * FileHeight / 4); y++)
      for (x = FileWidth / 4; x < (3 * FileWidth / 4); x++)
       BufImage[x + (y * FileWidth)] = 0;


     TIFFSetField (ImageDst, TIFFTAG_IMAGEWIDTH, FileWidth);
     TIFFSetField (ImageDst, TIFFTAG_IMAGELENGTH, FileHeight);
     TIFFSetField (ImageDst, TIFFTAG_BITSPERSAMPLE, FileBits);
     TIFFSetField (ImageDst, TIFFTAG_SAMPLESPERPIXEL, Band);
     TIFFSetField (ImageDst, TIFFTAG_COMPRESSION, Compress);
     TIFFSetField (ImageDst, TIFFTAG_PLANARCONFIG, PlanarConf);
     TIFFWriteDirectory (ImageDst);
     // Ecriture
     for (y = 0; y < FileHeight; y ++)
      TIFFWriteScanline (ImageDst, BufImage, y);


     if (BufImage != NULL)
      delete[] (BufImage);
     BufImage = NULL;
    }
    else if (FileBits == 16)
    {
     USHORT *BufImage = new USHORT[FileWidth * FileHeight];
     ULONG x , y;


     // Lecture
     for (y = 0; y < FileHeight; y ++)
      TIFFReadScanline (ImageTiff, BufImage, y);


      // Modification
     for (y = FileHeight / 4; y < (3 * FileHeight / 4); y++)
      for (x = FileWidth / 4; x < (3 * FileWidth / 4); x++)
       BufImage[x + (y * FileWidth)] = 0;


     // Ecriture
     try
     {
      TIFFSetField (ImageDst, TIFFTAG_IMAGEWIDTH, (int)FileWidth);
      TIFFSetField (ImageDst, TIFFTAG_IMAGELENGTH, FileHeight);
      TIFFSetField (ImageDst, TIFFTAG_BITSPERSAMPLE, FileBits);
      TIFFSetField (ImageDst, TIFFTAG_SAMPLESPERPIXEL, Band);
      TIFFSetField (ImageDst, TIFFTAG_COMPRESSION, Compress);
      TIFFSetField (ImageDst, TIFFTAG_PLANARCONFIG, PlanarConf);
      TIFFWriteDirectory (ImageDst);
      for (y = 0; y < FileHeight; y ++)
       TIFFWriteScanline (ImageDst, BufImage, y);
     }
     catch (...)
     {
      CDEF (tmp, 256);
      sprintf (tmp, "erreur pour y = %d\n", y);
      AfxMessageBox (tmp, MB_ICONSTOP);
     }

     if (BufImage != NULL)
      delete[] (BufImage);
     BufImage = NULL;
    }
    else
    {
     AfxMessageBox ("ce n'est pas un image en niveau de gris", MB_ICONSTOP);
    }
   }
  }
  else
   AfxMessageBox ("pas d'image à ouvrir", MB_ICONSTOP);
  TIFFClose (ImageTiff);
  TIFFClose (ImageDst);

And when i run the application i've the following message:

TIFFWriteScanline: copy.tif: Must set 'ImageWidth" before writing data.

It seems to me i do that with TIFFSetField. I don't know where 's my error.

Someone's can help me, I'm a newbie.
Thanks for all and excuse me for my english, it's not my mother's tongue.

Marine Martin-Guillerez