
Thread
2003.11.03 16:14 "[Tiff] Re: ask for suggestions....", by Andrey Kiselev
Thanks a lot for your mail. As to my problem, I realy not know how to write the TIFF image with the TIFFWriteScanline. For example, I write the following code just to test whether TIFFWriteScanline can works:
I have attached a sample code to create simple TIFF image with 1 sample per pixel.
OUT_tif = TIFFOpen(OUT_filename, "w");
row=1;
TIFFSetField(OUT_tif, TIFFTAG_IMAGEWIDTH,tileWidth); //tileWidth=256
TIFFSetField(OUT_tif, TIFFTAG_IMAGELENGTH,row);
TIFFSetField(OUT_tif, TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);
TIFFSetField(OUT_tif, TIFFTAG_PHOTOMETRIC,PHOTOMETRIC_RGB);
TIFFSetField(OUT_tif, TIFFTAG_BITSPERSAMPLE,8);
TIFFSetField(OUT_tif, TIFFTAG_SAMPLEFORMAT,3);
TIFFSetField(OUT_tif, TIFFTAG_COMPRESSION,COMPRESSION_NONE);
bufscanline_8= (uint32 *) malloc(tileWidth*3*sizeof(uint32));
for(x=0;x<tileWidth;x++)
*(bufscanline_8+x)=23; // just set the buffer to a constant
TIFFWriteScanline(OUT_tif,bufscanline_8, row,0);
*********
Would you please tell me that what't wrong with the above code? What's the data format of the TIFFWriteScanline? Is the data arrange shoulc be ' RGBRGB.....'? All data is uint32 or not?
All of these completely defined by the TIFFTAG_PHOTOMETRIC, TIFFTAG_BITSPERSAMPLE and TIFFTAG_SAMPLESPERPIXEL tags (BTW, I see you have missed TIFFTAG_SAMPLESPERPIXEL tag. It should be 3 if you want create a RGB image).
Andrey V. Kiselev
Home phone: +7 812 5274898 ICQ# 26871517
#include <stdio.h>
#include "tiffio.h"
#define XSIZE 256
#define YSIZE 256
int main (int argc, char **argv)
{
uint32 image_width, image_height;
float xres, yres;
uint16 spp, bpp, photo, res_unit;
TIFF *out;
int i, j;
char array[XSIZE * YSIZE];
for (j = 0; j < YSIZE; j++)
for(i = 0; i < XSIZE; i++)
array[j * XSIZE + i] = i * j;
out = TIFFOpen("out.tif", "w");
if (!out)
{
fprintf (stderr, "Can't open %s for writing\n", argv[1]);
return 1;
}
image_width = XSIZE;
image_height = YSIZE;
spp = 1; /* Samples per pixel */
bpp = 8; /* Bits per sample */
photo = PHOTOMETRIC_MINISBLACK;
TIFFSetField(out, TIFFTAG_IMAGEWIDTH, image_width / spp);
TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, bpp);
TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, spp);
TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(out, TIFFTAG_PHOTOMETRIC, photo);
TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_BOTLEFT);
/* If you need compression*/
/* TIFFSetField(out, TIFFTAG_COMPRESSION, COMPRESSION_ADOBE_DEFLATE); */
TIFFSetField(out,TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(out, 0));
/* It is good to set resolutions too (but it is not nesessary) */
xres = yres = 100;
res_unit = RESUNIT_INCH;
TIFFSetField(out, TIFFTAG_XRESOLUTION, xres);
TIFFSetField(out, TIFFTAG_YRESOLUTION, yres);
TIFFSetField(out, TIFFTAG_RESOLUTIONUNIT, res_unit);
for (j = 0; j < image_height; j++)
TIFFWriteScanline(out, &array[j * image_width], j, 0);
TIFFClose(out);
return 0;
}