2002.09.30 17:34 "Seek help", by Zhongfu Zhou

2002.09.30 17:34 "Seek help", by Zhongfu Zhou

Dear all,

I want to creat some grayscale images and wrote following code, but the images produced is always wrong. Anyone can kindly have a look to my code and give me some suggestions. Thanks a lot.

#include <stdio.h>
#include <tiffio.h>
#include <math.h>

int main(int argc, char *argv[])
  {
    FILE * pFile;
    uint32 width, height;
    uint32 lSize;
    int buffer[100];
    int i;

    pFile = fopen ( "test4" , "r" );

    if (pFile==NULL) exit (1);

   //Giving values of width and height
    width =  10;
    height = 10;
    lSize =width*height;

   //copy the file into the buffer.

      for (i=0; i<lSize; ++i)
      {
         fscanf (pFile,"%i",&buffer[i]);
         printf ("%i,%i\n", i,buffer[i]);
      }

     if (buffer == NULL) exit (2);


  TIFF *image;

  // Open the Tiff flie
  if ((image = TIFFOpen("output.tif","w")) == NULL)
    {
    printf ("Could not open output.tif for writing \n");
    exit (42);
    }

  //set parameters

  TIFFSetField(image,TIFFTAG_IMAGEWIDTH, 10);
  TIFFSetField(image,TIFFTAG_IMAGELENGTH, 10);
  TIFFSetField(image,TIFFTAG_BITSPERSAMPLE, 8);
  TIFFSetField(image,TIFFTAG_SAMPLESPERPIXEL, 1);
  TIFFSetField(image,TIFFTAG_ROWSPERSTRIP, 10);
  TIFFSetField(image,TIFFTAG_ORIENTATION,ORIENTATION_TOPLEFT);

  TIFFSetField(image,TIFFTAG_COMPRESSION, COMPRESSION_NONE);
  TIFFSetField(image,TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
  //TIFFSetField(image,TIFFTAG_FILLORDER, FILLORDER_MSB2LSB);
  TIFFSetField(image,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);


  TIFFSetField(image,TIFFTAG_XRESOLUTION, 10.0);
  TIFFSetField(image,TIFFTAG_YRESOLUTION, 10.0);
  TIFFSetField(image,TIFFTAG_RESOLUTIONUNIT,RESUNIT_INCH);

  TIFFWriteEncodedStrip(image,0,buffer,100);

    free (buffer);
    fclose(pFile);
    TIFFClose(image);
}

The date matrix of file "test4" I used is:

1 0 1 0 1 0 1 0 1 0
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 0 1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1 0 1
1 1 1 1 1 1 1 1 1 1
1 1 1 0 1 1 1 1 0 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1