2008.11.26 09:37 "[Tiff] Help write 16bites grey level image", by Cristian Perca

2008.11.26 09:37 "[Tiff] Help write 16bites grey level image", by Cristian Perca

 Hi,
I have a problem while trying to write a 16 bites grey level TIFF image. Here is a part of the code. The compilation and the execution are OK but the image I get has the expected size but all pixels are black.
If someone has any idea I would really appreciated.
Thank you

    char *flatBuffer=NULL;
    TIFF *flatTiff=NULL;

flatBuffer=malloc(bufferSize);

/*test: fill the image with average of input images in imageArray*/
   //imageArray is an dynamical array of struct, each object contains an image
//All images are correctly read and object buffers filled
for (count = 0; count < bufferSize; count +=2) {
    uint32 sumPixel=0;
    for(i=0; i<noFiles; i++) {
                  //noFiles is the number of input images == number of created objects

            sumPixel += (*(uint16 *)((imageArray+i) -> buffer + count));

  }

        *(uint16 *)(flatBuffer+count) = (uint16)sumPixel/noFiles;

}

 if((flatTiff = TIFFOpen("flat.tiff", "w")) == NULL){
   fprintf(stderr, "Could not open flat.tif for writing\n");
   exit(42);
}

TIFFSetField(flatTiff, TIFFTAG_IMAGEWIDTH, width);

TIFFSetField(flatTiff, TIFFTAG_IMAGELENGTH, height);

TIFFSetField(flatTiff, TIFFTAG_COMPRESSION, COMPRESSION_NONE);

TIFFSetField(flatTiff, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);

TIFFSetField(flatTiff, TIFFTAG_BITSPERSAMPLE, bps); //bps=16

TIFFSetField(flatTiff, TIFFTAG_SAMPLESPERPIXEL, spp); //spp=1

TIFFSetField(flatTiff, TIFFTAG_ROWSPERSTRIP, height); // put all rows(==height no of rows) in one strip

TIFFSetField(flatTiff, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);

TIFFSetField(flatTiff, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB);

    if( i=TIFFWriteEncodedStrip(flatTiff, 0, flatBuffer, bufferSize) == -1) {
        fprintf(stderr, "Error while writing image to file!");
        exit(42);
    }
//the image has only black pixels, but if I print flatBuffer on the screen I get the expected values:

//printf("%d %d\n", *(uint16 *)(flatBuffer+0), *(uint16 *)(flatBuffer+2));

    TIFFClose(flatTiff);
}