2002.02.28 19:19 "RE: Must set "ImageWidth" before writing data", by Michael Payne
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include "tiffio.h"
#define OUTFILE "tiffcreate.tif"
#define NUMROWS 10
#define IMAGE_WIDTH 16
#define BYTES_PER_BUF IMAGE_WIDTH/8
unsigned char blackbuf[BYTES_PER_BUF];
unsigned char whitebuf[BYTES_PER_BUF];
void myHandler(const char *module, const char *fmt, va_list ap) {
fprintf(stderr, "%s: ", module);
fprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
}
main(int argc, char *argv[])
{
TIFFSetErrorHandler(myHandler);
do {
TIFF *tif;
int row;
memset(blackbuf, -1, BYTES_PER_BUF);
memset(whitebuf, 0, BYTES_PER_BUF);
if ( !( tif = TIFFOpen(OUTFILE, "w") ) )
{
fprintf(stderr, "%s: Error opening file %s\n", argv[0], OUTFILE);
break;
}
if ( !(TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX3) &&
TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, (unsigned)IMAGE_WIDTH) &&
TIFFSetField(tif, TIFFTAG_IMAGELENGTH, (unsigned)NUMROWS) &&
TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, (unsigned)1) ) )
break;
/* don't forget to set these =) */
TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, 2);
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, 0);
TIFFSetField(tif, TIFFTAG_PLANARCONFIG, 1);
TIFFSetField(tif, TIFFTAG_YRESOLUTION, 196.);
TIFFSetField(tif, TIFFTAG_XRESOLUTION, 204.);
/* changed IMAGEWIDTH to BYTES_PER_BUF */
for (row = 0; row < NUMROWS; ++row)
if ( TIFFWriteEncodedStrip(tif, row, (row % 2) ? blackbuf : whitebuf, BYTES_PER_BUF) == -1) break;
/* put this after you write the strips */
TIFFWriteDirectory(tif);
TIFFClose(tif);
} while(0);
}