int main(int argc, char **argv) { TIFF *tif; int i; unsigned char buf[3] = { 0, 127, 255 }; /* Test whether we can write tags. */ tif = TIFFOpen(filename, "w"); if (!tif) { fprintf (stderr, "Can't create test TIFF file %s.\n", filename); return 1; } if (!TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width)) { fprintf (stderr, "Can't set ImageWidth tag.\n"); goto failure; } if (!TIFFSetField(tif, TIFFTAG_IMAGELENGTH, length)) { fprintf (stderr, "Can't set ImageLength tag.\n"); goto failure; } if (!TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8)) { fprintf (stderr, "Can't set BitsPerSample tag.\n"); goto failure; } if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3)) { fprintf (stderr, "Can't set SamplesPerPixel tag.\n"); goto failure; } if (!TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rows_per_strip)) { fprintf (stderr, "Can't set SamplesPerPixel tag.\n"); goto failure; } if (!TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG)) { fprintf (stderr, "Can't set PlanarConfiguration tag.\n"); goto failure; } if (!TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB)) { fprintf (stderr, "Can't set PhotometricInterpretation tag.\n"); goto failure; } for (i = 0; i < NTAGS; i++) { if (!TIFFSetField(tif, long_tags[i].tag, long_tags[i].value)) { fprintf(stderr, "Can't set tag %d.\n", (int)long_tags[i].tag); goto failure; } } /* Write dummy pixel data. */ if (!TIFFWriteScanline(tif, buf, 0, 0) < 0) { fprintf (stderr, "Can't write image data.\n"); goto failure; } TIFFClose(tif); /* Ok, now test whether we can read written values. */ tif = TIFFOpen(filename, "r"); if (!tif) { fprintf (stderr, "Can't open test TIFF file %s.\n", filename); return 1; } if (CheckLongField(tif, TIFFTAG_IMAGEWIDTH, width) < 0) goto failure; if (CheckLongField(tif, TIFFTAG_IMAGELENGTH, length) < 0) goto failure; if (CheckLongField(tif, TIFFTAG_ROWSPERSTRIP, rows_per_strip) < 0) goto failure; for (i = 0; i < NTAGS; i++) { if (CheckLongField(tif, long_tags[i].tag, long_tags[i].value) < 0) goto failure; } TIFFClose(tif); /* All tests passed; delete file and exit with success status. */ unlink(filename); return 0; failure: /* Something goes wrong; close file and return unsuccessful status. */ TIFFClose(tif); unlink(filename); return 1; }
int R_SaveAsTIFF(void *d, int width, int height, unsigned int (*gp)(void *, int, int), int bgr, const char *outfile, int res, int compression) { TIFF *out; int sampleperpixel; tsize_t linebytes; unsigned char *buf, *pscanline; unsigned int col, i, j; int have_alpha = 0; DECLARESHIFTS; for (i = 0; i < height; i++) for (j = 0; j < width; j++) { col = gp(d,i,j); if (GETALPHA(col) < 255) { have_alpha = 1; break; } } sampleperpixel = 3 + have_alpha; out = TIFFOpen(outfile, "w"); if (!out) { warning("unable to open TIFF file '%s'", outfile); return 0; } TIFFSetField(out, TIFFTAG_IMAGEWIDTH, width); TIFFSetField(out, TIFFTAG_IMAGELENGTH, height); TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, sampleperpixel); TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 8); TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB); #if 0 /* Possible compression values COMPRESSION_NONE = 1; COMPRESSION_CCITTRLE = 2; COMPRESSION_CCITTFAX3 = COMPRESSION_CCITT_T4 = 3; COMPRESSION_CCITTFAX4 = COMPRESSION_CCITT_T6 = 4; COMPRESSION_LZW = 5; COMPRESSION_JPEG = 7; COMPRESSION_DEFLATE = 32946; COMPRESSION_ADOBE_DEFLATE = 8; */ TIFFSetField(out, TIFFTAG_COMPRESSION, COMPRESSION_NONE); #endif if(compression > 1) TIFFSetField(out, TIFFTAG_COMPRESSION, compression); if (res > 0) { TIFFSetField(out, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH); TIFFSetField(out, TIFFTAG_XRESOLUTION, (float) res); TIFFSetField(out, TIFFTAG_YRESOLUTION, (float) res); } linebytes = sampleperpixel * width; if (TIFFScanlineSize(out)) buf =(unsigned char *)_TIFFmalloc(linebytes); else buf = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(out)); for (i = 0; i < height; i++) { pscanline = buf; for(j = 0; j < width; j++) { col = gp(d, i, j); *pscanline++ = GETRED(col) ; *pscanline++ = GETGREEN(col) ; *pscanline++ = GETBLUE(col) ; if(have_alpha) *pscanline++ = GETALPHA(col) ; } TIFFWriteScanline(out, buf, i, 0); } TIFFClose(out); _TIFFfree(buf); return 1; }
int main(int argc, char* argv[]) { uint32 width = 0, length = 0, linebytes, bufsize; uint32 nbands = 1; /* number of bands in input image*/ _TIFF_off_t hdr_size = 0; /* size of the header to skip */ TIFFDataType dtype = TIFF_BYTE; int16 depth = 1; /* bytes per pixel in input image */ int swab = 0; /* byte swapping flag */ InterleavingType interleaving = 0; /* interleaving type flag */ uint32 rowsperstrip = (uint32) -1; uint16 photometric = PHOTOMETRIC_MINISBLACK; uint16 config = PLANARCONFIG_CONTIG; uint16 fillorder = FILLORDER_LSB2MSB; int fd; char *outfilename = NULL; TIFF *out; uint32 row, col, band; int c; unsigned char *buf = NULL, *buf1 = NULL; #if !HAVE_DECL_OPTARG extern int optind; extern char* optarg; #endif while ((c = getopt(argc, argv, "c:r:H:w:l:b:d:LMp:si:o:h")) != -1) { switch (c) { case 'c': /* compression scheme */ if (!processCompressOptions(optarg)) usage(); break; case 'r': /* rows/strip */ rowsperstrip = atoi(optarg); break; case 'H': /* size of input image file header */ hdr_size = atoi(optarg); break; case 'w': /* input image width */ width = atoi(optarg); break; case 'l': /* input image length */ length = atoi(optarg); break; case 'b': /* number of bands in input image */ nbands = atoi(optarg); break; case 'd': /* type of samples in input image */ if (strncmp(optarg, "byte", 4) == 0) dtype = TIFF_BYTE; else if (strncmp(optarg, "short", 5) == 0) dtype = TIFF_SHORT; else if (strncmp(optarg, "long", 4) == 0) dtype = TIFF_LONG; else if (strncmp(optarg, "sbyte", 5) == 0) dtype = TIFF_SBYTE; else if (strncmp(optarg, "sshort", 6) == 0) dtype = TIFF_SSHORT; else if (strncmp(optarg, "slong", 5) == 0) dtype = TIFF_SLONG; else if (strncmp(optarg, "float", 5) == 0) dtype = TIFF_FLOAT; else if (strncmp(optarg, "double", 6) == 0) dtype = TIFF_DOUBLE; else dtype = TIFF_BYTE; depth = TIFFDataWidth(dtype); break; case 'L': /* input has lsb-to-msb fillorder */ fillorder = FILLORDER_LSB2MSB; break; case 'M': /* input has msb-to-lsb fillorder */ fillorder = FILLORDER_MSB2LSB; break; case 'p': /* photometric interpretation */ if (strncmp(optarg, "miniswhite", 10) == 0) photometric = PHOTOMETRIC_MINISWHITE; else if (strncmp(optarg, "minisblack", 10) == 0) photometric = PHOTOMETRIC_MINISBLACK; else if (strncmp(optarg, "rgb", 3) == 0) photometric = PHOTOMETRIC_RGB; else if (strncmp(optarg, "cmyk", 4) == 0) photometric = PHOTOMETRIC_SEPARATED; else if (strncmp(optarg, "ycbcr", 5) == 0) photometric = PHOTOMETRIC_YCBCR; else if (strncmp(optarg, "cielab", 6) == 0) photometric = PHOTOMETRIC_CIELAB; else if (strncmp(optarg, "icclab", 6) == 0) photometric = PHOTOMETRIC_ICCLAB; else if (strncmp(optarg, "itulab", 6) == 0) photometric = PHOTOMETRIC_ITULAB; else photometric = PHOTOMETRIC_MINISBLACK; break; case 's': /* do we need to swap bytes? */ swab = 1; break; case 'i': /* type of interleaving */ if (strncmp(optarg, "pixel", 4) == 0) interleaving = PIXEL; else if (strncmp(optarg, "band", 6) == 0) interleaving = BAND; else interleaving = 0; break; case 'o': outfilename = optarg; break; case 'h': usage(); default: break; } } if (argc - optind < 2) usage(); fd = open(argv[optind], O_RDONLY|O_BINARY, 0); if (fd < 0) { fprintf(stderr, "%s: %s: Cannot open input file.\n", argv[0], argv[optind]); return (-1); } if (guessSize(fd, dtype, hdr_size, nbands, swab, &width, &length) < 0) return 1; if (outfilename == NULL) outfilename = argv[optind+1]; out = TIFFOpen(outfilename, "w"); if (out == NULL) { fprintf(stderr, "%s: %s: Cannot open file for output.\n", argv[0], outfilename); return (-1); } TIFFSetField(out, TIFFTAG_IMAGEWIDTH, width); TIFFSetField(out, TIFFTAG_IMAGELENGTH, length); TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, nbands); TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, depth * 8); TIFFSetField(out, TIFFTAG_FILLORDER, fillorder); TIFFSetField(out, TIFFTAG_PLANARCONFIG, config); TIFFSetField(out, TIFFTAG_PHOTOMETRIC, photometric); switch (dtype) { case TIFF_BYTE: case TIFF_SHORT: case TIFF_LONG: TIFFSetField(out, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT); break; case TIFF_SBYTE: case TIFF_SSHORT: case TIFF_SLONG: TIFFSetField(out, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_INT); break; case TIFF_FLOAT: case TIFF_DOUBLE: TIFFSetField(out, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_IEEEFP); break; default: TIFFSetField(out, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_VOID); break; } if (compression == (uint16) -1) compression = COMPRESSION_PACKBITS; TIFFSetField(out, TIFFTAG_COMPRESSION, compression); switch (compression) { case COMPRESSION_JPEG: if (photometric == PHOTOMETRIC_RGB && jpegcolormode == JPEGCOLORMODE_RGB) photometric = PHOTOMETRIC_YCBCR; TIFFSetField(out, TIFFTAG_JPEGQUALITY, quality); TIFFSetField(out, TIFFTAG_JPEGCOLORMODE, jpegcolormode); break; case COMPRESSION_LZW: case COMPRESSION_DEFLATE: if (predictor != 0) TIFFSetField(out, TIFFTAG_PREDICTOR, predictor); break; } switch(interleaving) { case BAND: /* band interleaved data */ linebytes = width * depth; buf = (unsigned char *)_TIFFmalloc(linebytes); break; case PIXEL: /* pixel interleaved data */ default: linebytes = width * nbands * depth; break; } bufsize = width * nbands * depth; buf1 = (unsigned char *)_TIFFmalloc(bufsize); rowsperstrip = TIFFDefaultStripSize(out, rowsperstrip); if (rowsperstrip > length) { rowsperstrip = length; } TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, rowsperstrip ); _TIFF_lseek_f(fd, hdr_size, SEEK_SET); /* Skip the file header */ for (row = 0; row < length; row++) { switch(interleaving) { case BAND: /* band interleaved data */ for (band = 0; band < nbands; band++) { if (_TIFF_lseek_f(fd, hdr_size + (length*band+row)*linebytes, SEEK_SET) == (_TIFF_off_t)-1) { fprintf(stderr, "%s: %s: scanline %lu: seek error.\n", argv[0], argv[optind], (unsigned long) row); break; } if (read(fd, buf, linebytes) < 0) { fprintf(stderr, "%s: %s: scanline %lu: Read error.\n", argv[0], argv[optind], (unsigned long) row); break; } if (swab) /* Swap bytes if needed */ swapBytesInScanline(buf, width, dtype); for (col = 0; col < width; col++) memcpy(buf1 + (col*nbands+band)*depth, buf + col * depth, depth); } break; case PIXEL: /* pixel interleaved data */ default: if (read(fd, buf1, bufsize) < 0) { fprintf(stderr, "%s: %s: scanline %lu: Read error.\n", argv[0], argv[optind], (unsigned long) row); break; } if (swab) /* Swap bytes if needed */ swapBytesInScanline(buf1, width, dtype); break; } if (TIFFWriteScanline(out, buf1, row, 0) < 0) { fprintf(stderr, "%s: %s: scanline %lu: Write error.\n", argv[0], outfilename, (unsigned long) row); break; } } if (buf) _TIFFfree(buf); if (buf1) _TIFFfree(buf1); TIFFClose(out); return (0); }
int main(int argc, char* argv[]) { uint32 width, length; uint16 nbands = 1; /* number of bands in input image */ uint16 depth = 8; /* bits per pixel in input image */ uint32 rowsperstrip = (uint32) -1; uint16 photometric = PHOTOMETRIC_MINISBLACK; int fd = 0; struct stat instat; char *outfilename = NULL, *infilename = NULL; TIFF *out = NULL; BMPFileHeader file_hdr; BMPInfoHeader info_hdr; int bmp_type; uint32 clr_tbl_size, n_clr_elems = 3; unsigned char *clr_tbl; unsigned short *red_tbl = NULL, *green_tbl = NULL, *blue_tbl = NULL; uint32 row, clr; int c; extern int optind; extern char* optarg; while ((c = getopt(argc, argv, "c:r:o:h")) != -1) { switch (c) { case 'c': /* compression scheme */ if (!processCompressOptions(optarg)) usage(); break; case 'r': /* rows/strip */ rowsperstrip = atoi(optarg); break; case 'o': outfilename = optarg; break; case 'h': usage(); default: break; } } if (argc - optind < 2) usage(); if (outfilename == NULL) outfilename = argv[argc-1]; out = TIFFOpen(outfilename, "w"); if (out == NULL) { TIFFError(infilename, "Cannot open file %s for output", outfilename); goto bad3; } while (optind < argc-1) { infilename = argv[optind]; optind++; fd = open(infilename, O_RDONLY|O_BINARY, 0); if (fd < 0) { TIFFError(infilename, "Cannot open input file"); return -1; } read(fd, file_hdr.bType, 2); if(file_hdr.bType[0] != 'B' || file_hdr.bType[1] != 'M') { TIFFError(infilename, "File is not BMP"); goto bad; } /* -------------------------------------------------------------------- */ /* Read the BMPFileHeader. We need iOffBits value only */ /* -------------------------------------------------------------------- */ lseek(fd, 10, SEEK_SET); read(fd, &file_hdr.iOffBits, 4); #ifdef WORDS_BIGENDIAN TIFFSwabLong(&file_hdr.iOffBits); #endif fstat(fd, &instat); file_hdr.iSize = instat.st_size; /* -------------------------------------------------------------------- */ /* Read the BMPInfoHeader. */ /* -------------------------------------------------------------------- */ lseek(fd, BFH_SIZE, SEEK_SET); read(fd, &info_hdr.iSize, 4); #ifdef WORDS_BIGENDIAN TIFFSwabLong(&info_hdr.iSize); #endif if (info_hdr.iSize == BIH_WIN4SIZE) bmp_type = BMPT_WIN4; else if (info_hdr.iSize == BIH_OS21SIZE) bmp_type = BMPT_OS21; else if (info_hdr.iSize == BIH_OS22SIZE || info_hdr.iSize == 16) bmp_type = BMPT_OS22; else bmp_type = BMPT_WIN5; if (bmp_type == BMPT_WIN4 || bmp_type == BMPT_WIN5 || bmp_type == BMPT_OS22) { read(fd, &info_hdr.iWidth, 4); read(fd, &info_hdr.iHeight, 4); read(fd, &info_hdr.iPlanes, 2); read(fd, &info_hdr.iBitCount, 2); read(fd, &info_hdr.iCompression, 4); read(fd, &info_hdr.iSizeImage, 4); read(fd, &info_hdr.iXPelsPerMeter, 4); read(fd, &info_hdr.iYPelsPerMeter, 4); read(fd, &info_hdr.iClrUsed, 4); read(fd, &info_hdr.iClrImportant, 4); #ifdef WORDS_BIGENDIAN TIFFSwabLong((uint32*) &info_hdr.iWidth); TIFFSwabLong((uint32*) &info_hdr.iHeight); TIFFSwabShort((uint16*) &info_hdr.iPlanes); TIFFSwabShort((uint16*) &info_hdr.iBitCount); TIFFSwabLong((uint32*) &info_hdr.iCompression); TIFFSwabLong((uint32*) &info_hdr.iSizeImage); TIFFSwabLong((uint32*) &info_hdr.iXPelsPerMeter); TIFFSwabLong((uint32*) &info_hdr.iYPelsPerMeter); TIFFSwabLong((uint32*) &info_hdr.iClrUsed); TIFFSwabLong((uint32*) &info_hdr.iClrImportant); #endif n_clr_elems = 4; } if (bmp_type == BMPT_OS22) { /* * FIXME: different info in different documents * regarding this! */ n_clr_elems = 3; } if (bmp_type == BMPT_OS21) { int16 iShort; read(fd, &iShort, 2); #ifdef WORDS_BIGENDIAN TIFFSwabShort((uint16*) &iShort); #endif info_hdr.iWidth = iShort; read(fd, &iShort, 2); #ifdef WORDS_BIGENDIAN TIFFSwabShort((uint16*) &iShort); #endif info_hdr.iHeight = iShort; read(fd, &iShort, 2); #ifdef WORDS_BIGENDIAN TIFFSwabShort((uint16*) &iShort); #endif info_hdr.iPlanes = iShort; read(fd, &iShort, 2); #ifdef WORDS_BIGENDIAN TIFFSwabShort((uint16*) &iShort); #endif info_hdr.iBitCount = iShort; info_hdr.iCompression = BMPC_RGB; n_clr_elems = 3; } if (info_hdr.iBitCount != 1 && info_hdr.iBitCount != 4 && info_hdr.iBitCount != 8 && info_hdr.iBitCount != 16 && info_hdr.iBitCount != 24 && info_hdr.iBitCount != 32) { TIFFError(infilename, "Cannot process BMP file with bit count %d", info_hdr.iBitCount); close(fd); return 0; } width = info_hdr.iWidth; length = (info_hdr.iHeight > 0) ? info_hdr.iHeight : -info_hdr.iHeight; switch (info_hdr.iBitCount) { case 1: case 4: case 8: nbands = 1; depth = info_hdr.iBitCount; photometric = PHOTOMETRIC_PALETTE; /* Allocate memory for colour table and read it. */ if (info_hdr.iClrUsed) clr_tbl_size = ((uint32)(1<<depth)<info_hdr.iClrUsed) ? (uint32) (1 << depth) : info_hdr.iClrUsed; else clr_tbl_size = 1 << depth; clr_tbl = (unsigned char *) _TIFFmalloc(n_clr_elems * clr_tbl_size); if (!clr_tbl) { TIFFError(infilename, "Can't allocate space for color table"); goto bad; } lseek(fd, BFH_SIZE + info_hdr.iSize, SEEK_SET); read(fd, clr_tbl, n_clr_elems * clr_tbl_size); red_tbl = (unsigned short*) _TIFFmalloc(((tmsize_t)1)<<depth * sizeof(unsigned short)); if (!red_tbl) { TIFFError(infilename, "Can't allocate space for red component table"); _TIFFfree(clr_tbl); goto bad1; } green_tbl = (unsigned short*) _TIFFmalloc(((tmsize_t)1)<<depth * sizeof(unsigned short)); if (!green_tbl) { TIFFError(infilename, "Can't allocate space for green component table"); _TIFFfree(clr_tbl); goto bad2; } blue_tbl = (unsigned short*) _TIFFmalloc(((tmsize_t)1)<<depth * sizeof(unsigned short)); if (!blue_tbl) { TIFFError(infilename, "Can't allocate space for blue component table"); _TIFFfree(clr_tbl); goto bad3; } for(clr = 0; clr < clr_tbl_size; clr++) { red_tbl[clr] = 257*clr_tbl[clr*n_clr_elems+2]; green_tbl[clr] = 257*clr_tbl[clr*n_clr_elems+1]; blue_tbl[clr] = 257*clr_tbl[clr*n_clr_elems]; } _TIFFfree(clr_tbl); break; case 16: case 24: nbands = 3; depth = info_hdr.iBitCount / nbands; photometric = PHOTOMETRIC_RGB; break; case 32: nbands = 3; depth = 8; photometric = PHOTOMETRIC_RGB; break; default: break; } /* -------------------------------------------------------------------- */ /* Create output file. */ /* -------------------------------------------------------------------- */ TIFFSetField(out, TIFFTAG_IMAGEWIDTH, width); TIFFSetField(out, TIFFTAG_IMAGELENGTH, length); TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, nbands); TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, depth); TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); TIFFSetField(out, TIFFTAG_PHOTOMETRIC, photometric); TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(out, rowsperstrip)); if (red_tbl && green_tbl && blue_tbl) { TIFFSetField(out, TIFFTAG_COLORMAP, red_tbl, green_tbl, blue_tbl); } if (compression == (uint16) -1) compression = COMPRESSION_PACKBITS; TIFFSetField(out, TIFFTAG_COMPRESSION, compression); switch (compression) { case COMPRESSION_JPEG: if (photometric == PHOTOMETRIC_RGB && jpegcolormode == JPEGCOLORMODE_RGB) photometric = PHOTOMETRIC_YCBCR; TIFFSetField(out, TIFFTAG_JPEGQUALITY, quality); TIFFSetField(out, TIFFTAG_JPEGCOLORMODE, jpegcolormode); break; case COMPRESSION_LZW: case COMPRESSION_DEFLATE: if (predictor != 0) TIFFSetField(out, TIFFTAG_PREDICTOR, predictor); break; } /* -------------------------------------------------------------------- */ /* Read uncompressed image data. */ /* -------------------------------------------------------------------- */ if (info_hdr.iCompression == BMPC_RGB) { uint32 offset, size; char *scanbuf; /* XXX: Avoid integer overflow. We can calculate size * in one step using * * size = ((width * info_hdr.iBitCount + 31) & ~31) / 8 * * formulae, but we should check for overflow * conditions during calculation. */ size = width * info_hdr.iBitCount + 31; if (!width || !info_hdr.iBitCount || (size - 31) / info_hdr.iBitCount != width ) { TIFFError(infilename, "Wrong image parameters; can't " "allocate space for scanline buffer"); goto bad3; } size = (size & ~31) / 8; scanbuf = (char *) _TIFFmalloc(size); if (!scanbuf) { TIFFError(infilename, "Can't allocate space for scanline buffer"); goto bad3; } for (row = 0; row < length; row++) { if (info_hdr.iHeight > 0) offset = file_hdr.iOffBits+(length-row-1)*size; else offset = file_hdr.iOffBits + row * size; if (lseek(fd, offset, SEEK_SET) == (off_t)-1) { TIFFError(infilename, "scanline %lu: Seek error", (unsigned long) row); break; } if (read(fd, scanbuf, size) < 0) { TIFFError(infilename, "scanline %lu: Read error", (unsigned long) row); break; } rearrangePixels(scanbuf, width, info_hdr.iBitCount); if (TIFFWriteScanline(out, scanbuf, row, 0)<0) { TIFFError(infilename, "scanline %lu: Write error", (unsigned long) row); break; } } _TIFFfree(scanbuf); /* -------------------------------------------------------------------- */ /* Read compressed image data. */ /* -------------------------------------------------------------------- */ } else if ( info_hdr.iCompression == BMPC_RLE8 || info_hdr.iCompression == BMPC_RLE4 ) { uint32 i, j, k, runlength; uint32 compr_size, uncompr_size; unsigned char *comprbuf; unsigned char *uncomprbuf; compr_size = file_hdr.iSize - file_hdr.iOffBits; uncompr_size = width * length; comprbuf = (unsigned char *) _TIFFmalloc( compr_size ); if (!comprbuf) { TIFFError(infilename, "Can't allocate space for compressed scanline buffer"); goto bad3; } uncomprbuf = (unsigned char *)_TIFFmalloc(uncompr_size); if (!uncomprbuf) { TIFFError(infilename, "Can't allocate space for uncompressed scanline buffer"); goto bad3; } lseek(fd, file_hdr.iOffBits, SEEK_SET); read(fd, comprbuf, compr_size); i = 0; j = 0; if (info_hdr.iBitCount == 8) { /* RLE8 */ while(j < uncompr_size && i < compr_size) { if ( comprbuf[i] ) { runlength = comprbuf[i++]; while( runlength > 0 && j < uncompr_size && i < compr_size ) { uncomprbuf[j++] = comprbuf[i]; runlength--; } i++; } else { i++; if (comprbuf[i] == 0) /* Next scanline */ i++; else if (comprbuf[i] == 1) /* End of image */ break; else if (comprbuf[i] == 2) { /* Move to... */ i++; if (i < compr_size - 1) { j+=comprbuf[i]+comprbuf[i+1]*width; i += 2; } else break; } else { /* Absolute mode */ runlength = comprbuf[i++]; for (k = 0; k < runlength && j < uncompr_size && i < compr_size; k++) uncomprbuf[j++] = comprbuf[i++]; if ( k & 0x01 ) i++; } } } } else { /* RLE4 */ while( j < uncompr_size && i < compr_size ) { if ( comprbuf[i] ) { runlength = comprbuf[i++]; while( runlength > 0 && j < uncompr_size && i < compr_size ) { if ( runlength & 0x01 ) uncomprbuf[j++] = (comprbuf[i] & 0xF0) >> 4; else uncomprbuf[j++] = comprbuf[i] & 0x0F; runlength--; } i++; } else { i++; if (comprbuf[i] == 0) /* Next scanline */ i++; else if (comprbuf[i] == 1) /* End of image */ break; else if (comprbuf[i] == 2) { /* Move to... */ i++; if (i < compr_size - 1) { j+=comprbuf[i]+comprbuf[i+1]*width; i += 2; } else break; } else { /* Absolute mode */ runlength = comprbuf[i++]; for (k = 0; k < runlength && j < uncompr_size && i < compr_size; k++) { if (k & 0x01) uncomprbuf[j++] = comprbuf[i++] & 0x0F; else uncomprbuf[j++] = (comprbuf[i] & 0xF0) >> 4; } if (k & 0x01) i++; } } } }
int write_image(dt_imageio_module_data_t *d_tmp, const char *filename, const void *in_void, void *exif, int exif_len, int imgid, int num, int total) { const dt_imageio_tiff_t *d = (dt_imageio_tiff_t *)d_tmp; uint8_t *profile = NULL; uint32_t profile_len = 0; TIFF *tif = NULL; void *rowdata = NULL; int rc = 1; // default to error if(imgid > 0) { cmsHPROFILE out_profile = dt_colorspaces_get_output_profile(imgid)->profile; cmsSaveProfileToMem(out_profile, 0, &profile_len); if(profile_len > 0) { profile = malloc(profile_len); if(!profile) { rc = 1; goto exit; } cmsSaveProfileToMem(out_profile, profile, &profile_len); } } // Create little endian tiff image tif = TIFFOpen(filename, "wl"); if(!tif) { rc = 1; goto exit; } // http://partners.adobe.com/public/developer/en/tiff/TIFFphotoshop.pdf (dated 2002) // "A proprietary ZIP/Flate compression code (0x80b2) has been used by some" // "software vendors. This code should be considered obsolete. We recommend" // "that TIFF implentations recognize and read the obsolete code but only" // "write the official compression code (0x0008)." // http://www.awaresystems.be/imaging/tiff/tifftags/compression.html // http://www.awaresystems.be/imaging/tiff/tifftags/predictor.html if(d->compress == 1) { TIFFSetField(tif, TIFFTAG_COMPRESSION, (uint16_t)COMPRESSION_ADOBE_DEFLATE); TIFFSetField(tif, TIFFTAG_PREDICTOR, (uint16_t)1); TIFFSetField(tif, TIFFTAG_ZIPQUALITY, (uint16_t)9); } else if(d->compress == 2) { TIFFSetField(tif, TIFFTAG_COMPRESSION, (uint16_t)COMPRESSION_ADOBE_DEFLATE); TIFFSetField(tif, TIFFTAG_PREDICTOR, (uint16_t)2); TIFFSetField(tif, TIFFTAG_ZIPQUALITY, (uint16_t)9); } else if(d->compress == 3) { TIFFSetField(tif, TIFFTAG_COMPRESSION, (uint16_t)COMPRESSION_ADOBE_DEFLATE); if(d->bpp == 32) TIFFSetField(tif, TIFFTAG_PREDICTOR, (uint16_t)3); else TIFFSetField(tif, TIFFTAG_PREDICTOR, (uint16_t)2); TIFFSetField(tif, TIFFTAG_ZIPQUALITY, (uint16_t)9); } else // (d->compress == 0) { TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE); } TIFFSetField(tif, TIFFTAG_FILLORDER, (uint16_t)FILLORDER_MSB2LSB); if(profile != NULL) { TIFFSetField(tif, TIFFTAG_ICCPROFILE, (uint32_t)profile_len, profile); } TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, (uint16_t)3); TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, (uint16_t)d->bpp); TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, (uint16_t)(d->bpp == 32 ? SAMPLEFORMAT_IEEEFP : SAMPLEFORMAT_UINT)); TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, (uint32_t)d->width); TIFFSetField(tif, TIFFTAG_IMAGELENGTH, (uint32_t)d->height); TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, (uint16_t)PHOTOMETRIC_RGB); TIFFSetField(tif, TIFFTAG_PLANARCONFIG, (uint16_t)PLANARCONFIG_CONTIG); TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, (uint32_t)1); TIFFSetField(tif, TIFFTAG_ORIENTATION, (uint16_t)ORIENTATION_TOPLEFT); int resolution = dt_conf_get_int("metadata/resolution"); if(resolution > 0) { TIFFSetField(tif, TIFFTAG_XRESOLUTION, (float)resolution); TIFFSetField(tif, TIFFTAG_YRESOLUTION, (float)resolution); TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, (uint16_t)RESUNIT_INCH); } const size_t rowsize = (d->width * 3) * d->bpp / 8; if((rowdata = malloc(rowsize)) == NULL) { rc = 1; goto exit; } if(d->bpp == 32) { for(int y = 0; y < d->height; y++) { float *in = (float *)in_void + (size_t)4 * y * d->width; float *out = (float *)rowdata; for(int x = 0; x < d->width; x++, in += 4, out += 3) { memcpy(out, in, 3 * sizeof(float)); } if(TIFFWriteScanline(tif, rowdata, y, 0) == -1) { rc = 1; goto exit; } } } else if(d->bpp == 16) { for(int y = 0; y < d->height; y++) { uint16_t *in = (uint16_t *)in_void + (size_t)4 * y * d->width; uint16_t *out = (uint16_t *)rowdata; for(int x = 0; x < d->width; x++, in += 4, out += 3) { memcpy(out, in, 3 * sizeof(uint16_t)); } if(TIFFWriteScanline(tif, rowdata, y, 0) == -1) { rc = 1; goto exit; } } } else { for(int y = 0; y < d->height; y++) { uint8_t *in = (uint8_t *)in_void + (size_t)4 * y * d->width; uint8_t *out = (uint8_t *)rowdata; for(int x = 0; x < d->width; x++, in += 4, out += 3) { memcpy(out, in, 3 * sizeof(uint8_t)); } if(TIFFWriteScanline(tif, rowdata, y, 0) == -1) { rc = 1; goto exit; } } } // success rc = 0; exit: // close the file before adding exif data if(tif) { TIFFClose(tif); tif = NULL; } if(!rc && exif) { rc = dt_exif_write_blob(exif, exif_len, filename, d->compress > 0); // Until we get symbolic error status codes, if rc is 1, return 0 rc = (rc == 1) ? 0 : 1; } free(profile); profile = NULL; free(rowdata); rowdata = NULL; return rc; }
/* * Floyd-Steinberg error propragation with threshold. * This code is stolen from tiffmedian. */ static void fsdither(TIFF* in, TIFF* out) { unsigned char *outline, *inputline, *inptr; short *thisline, *nextline, *tmpptr; register unsigned char *outptr; register short *thisptr, *nextptr; register uint32 i, j; uint32 imax, jmax; int lastline, lastpixel; int bit; tsize_t outlinesize; imax = imagelength - 1; jmax = imagewidth - 1; inputline = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(in)); thisline = (short *)_TIFFmalloc(imagewidth * sizeof (short)); nextline = (short *)_TIFFmalloc(imagewidth * sizeof (short)); outlinesize = TIFFScanlineSize(out); outline = (unsigned char *) _TIFFmalloc(outlinesize); /* * Get first line */ if (TIFFReadScanline(in, inputline, 0, 0) <= 0) return; inptr = inputline; nextptr = nextline; for (j = 0; j < imagewidth; ++j) *nextptr++ = *inptr++; for (i = 1; i < imagelength; ++i) { tmpptr = thisline; thisline = nextline; nextline = tmpptr; lastline = (i == imax); if (TIFFReadScanline(in, inputline, i, 0) <= 0) break; inptr = inputline; nextptr = nextline; for (j = 0; j < imagewidth; ++j) *nextptr++ = *inptr++; thisptr = thisline; nextptr = nextline; _TIFFmemset(outptr = outline, 0, outlinesize); bit = 0x80; for (j = 0; j < imagewidth; ++j) { register int v; lastpixel = (j == jmax); v = *thisptr++; if (v < 0) v = 0; else if (v > 255) v = 255; if (v > threshold) { *outptr |= bit; v -= 255; } bit >>= 1; if (bit == 0) { outptr++; bit = 0x80; } if (!lastpixel) thisptr[0] += v * 7 / 16; if (!lastline) { if (j != 0) nextptr[-1] += v * 3 / 16; *nextptr++ += v * 5 / 16; if (!lastpixel) nextptr[0] += v / 16; } } if (TIFFWriteScanline(out, outline, i-1, 0) < 0) break; } _TIFFfree(inputline); _TIFFfree(thisline); _TIFFfree(nextline); _TIFFfree(outline); }
bool TiffEncoder::writeLibTiff( const Mat& img, const vector<int>& params) { int channels = img.channels(); int width = img.cols, height = img.rows; int depth = img.depth(); int bitsPerChannel = -1; switch (depth) { case CV_8U: { bitsPerChannel = 8; break; } case CV_16U: { bitsPerChannel = 16; break; } default: { return false; } } const int bitsPerByte = 8; size_t fileStep = (width * channels * bitsPerChannel) / bitsPerByte; int rowsPerStrip = (int)((1 << 13)/fileStep); readParam(params, TIFFTAG_ROWSPERSTRIP, rowsPerStrip); if( rowsPerStrip < 1 ) rowsPerStrip = 1; if( rowsPerStrip > height ) rowsPerStrip = height; // do NOT put "wb" as the mode, because the b means "big endian" mode, not "binary" mode. // http://www.remotesensing.org/libtiff/man/TIFFOpen.3tiff.html TIFF* pTiffHandle = TIFFOpen(m_filename.c_str(), "w"); if (!pTiffHandle) { return false; } // defaults for now, maybe base them on params in the future int compression = COMPRESSION_LZW; int predictor = PREDICTOR_HORIZONTAL; readParam(params, TIFFTAG_COMPRESSION, compression); readParam(params, TIFFTAG_PREDICTOR, predictor); int colorspace = channels > 1 ? PHOTOMETRIC_RGB : PHOTOMETRIC_MINISBLACK; if ( !TIFFSetField(pTiffHandle, TIFFTAG_IMAGEWIDTH, width) || !TIFFSetField(pTiffHandle, TIFFTAG_IMAGELENGTH, height) || !TIFFSetField(pTiffHandle, TIFFTAG_BITSPERSAMPLE, bitsPerChannel) || !TIFFSetField(pTiffHandle, TIFFTAG_COMPRESSION, compression) || !TIFFSetField(pTiffHandle, TIFFTAG_PHOTOMETRIC, colorspace) || !TIFFSetField(pTiffHandle, TIFFTAG_SAMPLESPERPIXEL, channels) || !TIFFSetField(pTiffHandle, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG) || !TIFFSetField(pTiffHandle, TIFFTAG_ROWSPERSTRIP, rowsPerStrip) || !TIFFSetField(pTiffHandle, TIFFTAG_PREDICTOR, predictor) ) { TIFFClose(pTiffHandle); return false; } // row buffer, because TIFFWriteScanline modifies the original data! size_t scanlineSize = TIFFScanlineSize(pTiffHandle); AutoBuffer<uchar> _buffer(scanlineSize+32); uchar* buffer = _buffer; if (!buffer) { TIFFClose(pTiffHandle); return false; } for (int y = 0; y < height; ++y) { switch(channels) { case 1: { memcpy(buffer, img.data + img.step * y, scanlineSize); break; } case 3: { if (depth == CV_8U) icvCvt_BGR2RGB_8u_C3R( img.data + img.step*y, 0, buffer, 0, cvSize(width,1) ); else icvCvt_BGR2RGB_16u_C3R( (const ushort*)(img.data + img.step*y), 0, (ushort*)buffer, 0, cvSize(width,1) ); break; } case 4: { if (depth == CV_8U) icvCvt_BGRA2RGBA_8u_C4R( img.data + img.step*y, 0, buffer, 0, cvSize(width,1) ); else icvCvt_BGRA2RGBA_16u_C4R( (const ushort*)(img.data + img.step*y), 0, (ushort*)buffer, 0, cvSize(width,1) ); break; } default: { TIFFClose(pTiffHandle); return false; } } int writeResult = TIFFWriteScanline(pTiffHandle, buffer, y, 0); if (writeResult != 1) { TIFFClose(pTiffHandle); return false; } } TIFFClose(pTiffHandle); return true; }
int FileTIFF::write_frame(VFrame *frame, VFrame *data, FrameWriterUnit *unit) { //printf("FileTIFF::write_frame 1\n"); FileTIFFUnit *tiff_unit = (FileTIFFUnit*)unit; int result = 0; TIFF *stream; tiff_unit->offset = 0; tiff_unit->data = data; tiff_unit->data->set_compressed_size(0); stream = TIFFClientOpen("FileTIFF", "w", (void*)tiff_unit, tiff_read, tiff_write, tiff_seek, tiff_close, tiff_size, tiff_mmap, tiff_unmap); int components, color_model, bits, type, compression; int sampleformat = SAMPLEFORMAT_UINT; int bytesperrow; switch(asset->tiff_cmodel) { case FileTIFF::RGB_888: components = 3; color_model = BC_RGB888; bits = 8; type = TIFF_BYTE; bytesperrow = 3 * asset->width; break; case FileTIFF::RGB_161616: components = 3; color_model = BC_RGB_FLOAT; bits = 16; type = TIFF_SHORT; bytesperrow = 6 * asset->width; break; case FileTIFF::RGBA_8888: components = 4; color_model = BC_RGBA8888; bits = 8; type = TIFF_BYTE; bytesperrow = 4 * asset->width; break; case FileTIFF::RGBA_16161616: components = 4; color_model = BC_RGBA_FLOAT; bits = 16; type = TIFF_SHORT; bytesperrow = 8 * asset->width; break; case FileTIFF::RGB_FLOAT: components = 3; color_model = BC_RGB_FLOAT; bits = 32; type = TIFF_FLOAT; sampleformat = SAMPLEFORMAT_IEEEFP; bytesperrow = 12 * asset->width; break; case FileTIFF::RGBA_FLOAT: components = 4; color_model = BC_RGBA_FLOAT; bits = 32; type = TIFF_FLOAT; sampleformat = SAMPLEFORMAT_IEEEFP; bytesperrow = 16 * asset->width; break; default: components = 3; color_model = BC_RGB888; bits = 8; type = TIFF_BYTE; bytesperrow = 3 * asset->width; break; } switch(asset->tiff_compression) { case FileTIFF::LZW: compression = COMPRESSION_LZW; break; case FileTIFF::PACK_BITS: compression = COMPRESSION_PACKBITS; break; case FileTIFF::DEFLATE: compression = COMPRESSION_DEFLATE; break; case FileTIFF::JPEG: compression = COMPRESSION_JPEG; break; default: compression = COMPRESSION_NONE; break; } TIFFSetField(stream, TIFFTAG_IMAGEWIDTH, asset->width); TIFFSetField(stream, TIFFTAG_IMAGELENGTH, asset->height); TIFFSetField(stream, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); TIFFSetField(stream, TIFFTAG_SAMPLESPERPIXEL, components); TIFFSetField(stream, TIFFTAG_BITSPERSAMPLE, bits); TIFFSetField(stream, TIFFTAG_SAMPLEFORMAT, sampleformat); TIFFSetField(stream, TIFFTAG_COMPRESSION, compression); TIFFSetField(stream, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); TIFFSetField(stream, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(stream, (uint32_t)-1)); // TIFFSetField(stream, TIFFTAG_ROWSPERSTRIP, // (8 * 1024) / bytesperrow); TIFFSetField(stream, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB); if(frame->get_color_model() == color_model) { for(int i = 0; i < asset->height; i++) { TIFFWriteScanline(stream, frame->get_rows()[i], i, 0); } } else { if(tiff_unit->temp && tiff_unit->temp->get_color_model() != color_model) { delete tiff_unit->temp; tiff_unit->temp = 0; } if(!tiff_unit->temp) { tiff_unit->temp = new VFrame(0, asset->width, asset->height, color_model); } cmodel_transfer(tiff_unit->temp->get_rows(), frame->get_rows(), tiff_unit->temp->get_y(), tiff_unit->temp->get_u(), tiff_unit->temp->get_v(), frame->get_y(), frame->get_u(), frame->get_v(), 0, 0, frame->get_w(), frame->get_h(), 0, 0, frame->get_w(), frame->get_h(), frame->get_color_model(), color_model, 0, frame->get_w(), frame->get_w()); for(int i = 0; i < asset->height; i++) { TIFFWriteScanline(stream, tiff_unit->temp->get_rows()[i], i, 0); } } TIFFClose(stream); //printf("FileTIFF::write_frame 10\n"); return result; }
bool wxTIFFHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose ) { TIFF *tif = TIFFwxOpen( stream, "image", "w" ); if (!tif) { if (verbose) wxLogError( _("TIFF: Error saving image.") ); return false; } TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, (uint32)image->GetWidth()); TIFFSetField(tif, TIFFTAG_IMAGELENGTH, (uint32)image->GetHeight()); TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); if ( image->HasOption(wxIMAGE_OPTION_RESOLUTIONX) && image->HasOption(wxIMAGE_OPTION_RESOLUTIONY) ) { TIFFSetField(tif, TIFFTAG_XRESOLUTION, (float)image->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX)); TIFFSetField(tif, TIFFTAG_YRESOLUTION, (float)image->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY)); } int spp = image->GetOptionInt(wxIMAGE_OPTION_SAMPLESPERPIXEL); if ( !spp ) spp = 3; int bpp = image->GetOptionInt(wxIMAGE_OPTION_BITSPERSAMPLE); if ( !bpp ) bpp=8; int compression = image->GetOptionInt(wxIMAGE_OPTION_COMPRESSION); if ( !compression ) { // we can't use COMPRESSION_LZW because current version of libtiff // doesn't implement it ("no longer implemented due to Unisys patent // enforcement") and other compression methods are lossy so we // shouldn't use them by default -- and the only remaining one is none compression = COMPRESSION_NONE; } TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, spp); TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bpp); TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, spp*bpp == 1 ? PHOTOMETRIC_MINISBLACK : PHOTOMETRIC_RGB); TIFFSetField(tif, TIFFTAG_COMPRESSION, compression); // scanlinesize if determined by spp and bpp tsize_t linebytes = (tsize_t)image->GetWidth() * spp * bpp / 8; if ( (image->GetWidth() % 8 > 0) && (spp * bpp < 8) ) linebytes+=1; unsigned char *buf; if (TIFFScanlineSize(tif) > linebytes || (spp * bpp < 24)) { buf = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(tif)); if (!buf) { if (verbose) wxLogError( _("TIFF: Couldn't allocate memory.") ); TIFFClose( tif ); return false; } } else { buf = NULL; } TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP,TIFFDefaultStripSize(tif, (uint32) -1)); unsigned char *ptr = image->GetData(); for ( int row = 0; row < image->GetHeight(); row++ ) { if ( buf ) { if ( spp * bpp > 1 ) { // color image memcpy(buf, ptr, image->GetWidth()); } else // black and white image { for ( int column = 0; column < linebytes; column++ ) { uint8 reverse = 0; for ( int bp = 0; bp < 8; bp++ ) { if ( ptr[column*24 + bp*3] > 0 ) { // check only red as this is sufficient reverse = (uint8)(reverse | 128 >> bp); } } buf[column] = reverse; } } } if ( TIFFWriteScanline(tif, buf ? buf : ptr, (uint32)row, 0) < 0 ) { if (verbose) wxLogError( _("TIFF: Error writing image.") ); TIFFClose( tif ); if (buf) _TIFFfree(buf); return false; } ptr += image->GetWidth()*3; }
int main(int argc, char **argv) { TIFF *tif; int i; unsigned char buf[3] = { 0, 127, 255 }; char *value; /* Test whether we can write tags. */ tif = TIFFOpen(filename, "w"); if (!tif) { fprintf (stderr, "Can't create test TIFF file %s.\n", filename); return 1; } if (!TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, 1)) { fprintf (stderr, "Can't set ImageWidth tag.\n"); goto failure; } if (!TIFFSetField(tif, TIFFTAG_IMAGELENGTH, 1)) { fprintf (stderr, "Can't set ImageLength tag.\n"); goto failure; } if (!TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8)) { fprintf (stderr, "Can't set BitsPerSample tag.\n"); goto failure; } if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3)) { fprintf (stderr, "Can't set SamplesPerPixel tag.\n"); goto failure; } if (!TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG)) { fprintf (stderr, "Can't set PlanarConfiguration tag.\n"); goto failure; } if (!TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB)) { fprintf (stderr, "Can't set PhotometricInterpretation tag.\n"); goto failure; } for (i = 0; i < NTAGS; i++) { if (!TIFFSetField(tif, ascii_tags[i].tag, ascii_tags[i].value)) { fprintf(stderr, "Can't set tag %d.\n", (int)ascii_tags[i].tag); goto failure; } } /* InkNames tag has special form, so we handle it separately. */ if (!TIFFSetField(tif, TIFFTAG_NUMBEROFINKS, 3)) { fprintf (stderr, "Can't set tag %d.\n", TIFFTAG_NUMBEROFINKS); goto failure; } if (!TIFFSetField(tif, TIFFTAG_INKNAMES, ink_names_size, ink_names)) { fprintf (stderr, "Can't set tag %d.\n", TIFFTAG_INKNAMES); goto failure; } /* Write dummy pixel data. */ if (!TIFFWriteScanline(tif, buf, 0, 0) < 0) { fprintf (stderr, "Can't write image data.\n"); goto failure; } TIFFClose(tif); /* Ok, now test whether we can read written values. */ tif = TIFFOpen(filename, "r"); if (!tif) { fprintf (stderr, "Can't open test TIFF file %s.\n", filename); return 1; } for (i = 0; i < NTAGS; i++) { if (!TIFFGetField(tif, ascii_tags[i].tag, &value) || strcmp(value, ascii_tags[i].value)) { fprintf(stderr, "Can't get tag %d.\n", (int)ascii_tags[i].tag); goto failure; } } if (!TIFFGetField(tif, TIFFTAG_INKNAMES, &value) || memcmp(value, ink_names, ink_names_size)) { fprintf (stderr, "Can't get tag %d.\n", TIFFTAG_INKNAMES); goto failure; } TIFFClose(tif); /* All tests passed; delete file and exit with success status. */ unlink(filename); return 0; failure: /* Something goes wrong; close file and return unsuccessful status. */ TIFFClose(tif); unlink(filename); return 1; }
int main(int argc, char *argv[]) { int row; uint8_t image_buffer[1024]; TIFF *tiff_file; struct tm *tm; time_t now; char buf[133]; float x_resolution; float y_resolution; int i; for (i = 0; sequence[i].name; i++) { if ((tiff_file = TIFFOpen(sequence[i].name, "w")) == NULL) exit(2); /* Prepare the directory entry fully before writing the image, or libtiff complains */ TIFFSetField(tiff_file, TIFFTAG_COMPRESSION, COMPRESSION_CCITT_T6); TIFFSetField(tiff_file, TIFFTAG_IMAGEWIDTH, sequence[i].width); TIFFSetField(tiff_file, TIFFTAG_BITSPERSAMPLE, 1); TIFFSetField(tiff_file, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); TIFFSetField(tiff_file, TIFFTAG_SAMPLESPERPIXEL, 1); TIFFSetField(tiff_file, TIFFTAG_ROWSPERSTRIP, -1L); TIFFSetField(tiff_file, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); TIFFSetField(tiff_file, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE); TIFFSetField(tiff_file, TIFFTAG_FILLORDER, FILLORDER_LSB2MSB); x_resolution = sequence[i].x_res/100.0f; y_resolution = sequence[i].y_res/100.0f; TIFFSetField(tiff_file, TIFFTAG_XRESOLUTION, floorf(x_resolution*2.54f + 0.5f)); TIFFSetField(tiff_file, TIFFTAG_YRESOLUTION, floorf(y_resolution*2.54f + 0.5f)); TIFFSetField(tiff_file, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH); TIFFSetField(tiff_file, TIFFTAG_SOFTWARE, "spandsp"); if (gethostname(buf, sizeof(buf)) == 0) TIFFSetField(tiff_file, TIFFTAG_HOSTCOMPUTER, buf); TIFFSetField(tiff_file, TIFFTAG_IMAGEDESCRIPTION, "Blank test image"); TIFFSetField(tiff_file, TIFFTAG_MAKE, "soft-switch.org"); TIFFSetField(tiff_file, TIFFTAG_MODEL, "test data"); time(&now); tm = localtime(&now); sprintf(buf, "%4d/%02d/%02d %02d:%02d:%02d", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec); TIFFSetField(tiff_file, TIFFTAG_DATETIME, buf); TIFFSetField(tiff_file, TIFFTAG_IMAGELENGTH, sequence[i].length); TIFFSetField(tiff_file, TIFFTAG_PAGENUMBER, 0, 1); TIFFSetField(tiff_file, TIFFTAG_CLEANFAXDATA, CLEANFAXDATA_CLEAN); TIFFSetField(tiff_file, TIFFTAG_IMAGEWIDTH, sequence[i].width); /* Write the image first.... */ for (row = 0; row < sequence[i].length; row++) { memset(image_buffer, 0, sequence[i].width/8 + 1); if (TIFFWriteScanline(tiff_file, image_buffer, row, 0) < 0) { printf("Write error at row %d.\n", row); exit(2); } } /* ....then the directory entry, and libtiff is happy. */ TIFFWriteDirectory(tiff_file); TIFFClose(tiff_file); } return 0; }
int main(int argc, char **argv) { char * input_file = NULL; double image_gamma = TIFF_GAMMA; int i, j; TIFF * tif; unsigned char * scan_line; uint16 red[CMSIZE], green[CMSIZE], blue[CMSIZE]; float refblackwhite[2*3]; programName = argv[0]; switch (argc) { case 2: image_gamma = TIFF_GAMMA; input_file = argv[1]; break; case 4: if (!strcmp(argv[1], "-gamma")) { image_gamma = atof(argv[2]); input_file = argv[3]; } else Usage(); break; default: Usage(); } for (i = 0; i < CMSIZE; i++) { if (i == 0) red[i] = green[i] = blue[i] = 0; else { red[i] = ROUND((pow(i / 255.0, 1.0 / image_gamma) * 65535.0)); green[i] = ROUND((pow(i / 255.0, 1.0 / image_gamma) * 65535.0)); blue[i] = ROUND((pow(i / 255.0, 1.0 / image_gamma) * 65535.0)); } } refblackwhite[0] = 0.0; refblackwhite[1] = 255.0; refblackwhite[2] = 0.0; refblackwhite[3] = 255.0; refblackwhite[4] = 0.0; refblackwhite[5] = 255.0; if ((tif = TIFFOpen(input_file, "w")) == NULL) { fprintf(stderr, "can't open %s as a TIFF file\n", input_file); exit(0); } TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, WIDTH); TIFFSetField(tif, TIFFTAG_IMAGELENGTH, HEIGHT); TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8); TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE); TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB); TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3); TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1); TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, RESUNIT_NONE); #ifdef notdef TIFFSetField(tif, TIFFTAG_WHITEPOINT, whitex, whitey); TIFFSetField(tif, TIFFTAG_PRIMARYCHROMATICITIES, primaries); #endif TIFFSetField(tif, TIFFTAG_REFERENCEBLACKWHITE, refblackwhite); TIFFSetField(tif, TIFFTAG_TRANSFERFUNCTION, red, green, blue); scan_line = (unsigned char *) malloc(WIDTH * 3); for (i = 0; i < 255; i++) { for (j = 0; j < 75; j++) { scan_line[j * 3] = 255; scan_line[(j * 3) + 1] = 255 - i; scan_line[(j * 3) + 2] = 255 - i; } for (j = 75; j < 150; j++) { scan_line[j * 3] = 255 - i; scan_line[(j * 3) + 1] = 255; scan_line[(j * 3) + 2] = 255 - i; } for (j = 150; j < 225; j++) { scan_line[j * 3] = 255 - i; scan_line[(j * 3) + 1] = 255 - i; scan_line[(j * 3) + 2] = 255; } for (j = 225; j < 300; j++) { scan_line[j * 3] = (i - 1) / 2; scan_line[(j * 3) + 1] = (i - 1) / 2; scan_line[(j * 3) + 2] = (i - 1) / 2; } for (j = 300; j < 375; j++) { scan_line[j * 3] = 255 - i; scan_line[(j * 3) + 1] = 255; scan_line[(j * 3) + 2] = 255; } for (j = 375; j < 450; j++) { scan_line[j * 3] = 255; scan_line[(j * 3) + 1] = 255 - i; scan_line[(j * 3) + 2] = 255; } for (j = 450; j < 525; j++) { scan_line[j * 3] = 255; scan_line[(j * 3) + 1] = 255; scan_line[(j * 3) + 2] = 255 - i; } TIFFWriteScanline(tif, scan_line, i, 0); } for (i = 255; i < 512; i++) { for (j = 0; j < 75; j++) { scan_line[j * 3] = i; scan_line[(j * 3) + 1] = 0; scan_line[(j * 3) + 2] = 0; } for (j = 75; j < 150; j++) { scan_line[j * 3] = 0; scan_line[(j * 3) + 1] = i; scan_line[(j * 3) + 2] = 0; } for (j = 150; j < 225; j++) { scan_line[j * 3] = 0; scan_line[(j * 3) + 1] = 0; scan_line[(j * 3) + 2] = i; } for (j = 225; j < 300; j++) { scan_line[j * 3] = (i - 1) / 2; scan_line[(j * 3) + 1] = (i - 1) / 2; scan_line[(j * 3) + 2] = (i - 1) / 2; } for (j = 300; j < 375; j++) { scan_line[j * 3] = 0; scan_line[(j * 3) + 1] = i; scan_line[(j * 3) + 2] = i; } for (j = 375; j < 450; j++) { scan_line[j * 3] = i; scan_line[(j * 3) + 1] = 0; scan_line[(j * 3) + 2] = i; } for (j = 450; j < 525; j++) { scan_line[j * 3] = i; scan_line[(j * 3) + 1] = i; scan_line[(j * 3) + 2] = 0; } TIFFWriteScanline(tif, scan_line, i, 0); } free(scan_line); TIFFClose(tif); exit(0); }
CAMLprim value write_tiff_scanline(value tiffh, value buf, value row) { CAMLparam3(tiffh,buf,row); TIFFWriteScanline((TIFF*)tiffh, String_val(buf), Int_val(row), 0); CAMLreturn(Val_unit); }
int main(int argc, char* argv[]) { bool color; int n1, i2, n2, nc, nbuf; unsigned char *grey=NULL; sf_file in=NULL; TIFF *tiffout=NULL; FILE *tiffin=NULL; char *tiffname=NULL, buf[BUFSIZ]; sf_init(argc,argv); in = sf_input("in"); fclose(sf_tempfile(&tiffname,"w")); tiffout = TIFFOpen(tiffname,"wb"); if (tiffout == NULL) sf_error("can't open file %s\n", tiffname); if (SF_UCHAR != sf_gettype(in)) sf_error("Need unsigned char in input"); if (!sf_histint(in,"n1",&n1)) sf_error("No n1= in input"); if (!sf_getbool("color",&color)) color=(bool)(3==n1); if (color) { nc = n1; if (!sf_histint(in,"n2",&n1)) sf_error("No n2= in input"); if (!sf_histint(in,"n3",&n2)) sf_error("No n3= in input"); } else { nc = 1; if (!sf_histint(in,"n2",&n2)) sf_error("No n2= in input"); } grey = sf_ucharalloc (n1*n2*nc); sf_ucharread(grey,n1*n2*nc,in); TIFFSetField(tiffout,TIFFTAG_IMAGEWIDTH,n1); TIFFSetField(tiffout,TIFFTAG_IMAGELENGTH,n2); TIFFSetField(tiffout,TIFFTAG_SAMPLESPERPIXEL,nc); TIFFSetField(tiffout,TIFFTAG_BITSPERSAMPLE,8); TIFFSetField(tiffout,TIFFTAG_ORIENTATION,ORIENTATION_TOPLEFT); TIFFSetField(tiffout,TIFFTAG_PHOTOMETRIC, (3==nc)? PHOTOMETRIC_RGB: PHOTOMETRIC_MINISBLACK); TIFFSetField(tiffout,TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); TIFFSetField(tiffout, TIFFTAG_ROWSPERSTRIP, 1); for (i2 = 0; i2 < n2; i2++) { if (TIFFWriteScanline(tiffout, grey+i2*n1*nc, i2, 0) < 0) sf_error("Trouble writing TIFF file"); } TIFFClose(tiffout); tiffin = fopen(tiffname,"rb"); while (1) { nbuf = fread(buf,1,BUFSIZ,tiffin); if (nbuf <= 0) break; fwrite(buf,1,nbuf,stdout); } fclose(tiffin); unlink(tiffname); exit(0); }
static int create_error_page(TIFF *tiff_file) { uint8_t image_buffer[1728/8 + 1]; int row; int start_pixel; int i; /* ETSI ETS 300 242 B.5.4 Copy quality criteria - the ERROR page. */ for (row = 0; row < 68; row++) { clear_row(image_buffer, 1728); if (TIFFWriteScanline(tiff_file, image_buffer, row, 0) < 0) { printf("Write error at row %d.\n", row); exit(2); } } clear_row(image_buffer, 1728); set_pixel_range(image_buffer, 1, 0, 1727); if (TIFFWriteScanline(tiff_file, image_buffer, row++, 0) < 0) { printf("Write error at row %d.\n", row); exit(2); } clear_row(image_buffer, 1728); if (TIFFWriteScanline(tiff_file, image_buffer, row++, 0) < 0) { printf("Write error at row %d.\n", row); exit(2); } for (i = 0; i < 10; i++) { for (start_pixel = 16; start_pixel <= 1616; start_pixel += 64) { clear_row(image_buffer, 1728); set_pixel_range(image_buffer, 1, start_pixel, start_pixel + 63); if (TIFFWriteScanline(tiff_file, image_buffer, row, 0) < 0) { printf("Write error at row %d.\n", row); exit(2); } row++; } } clear_row(image_buffer, 1728); if (TIFFWriteScanline(tiff_file, image_buffer, row++, 0) < 0) { printf("Write error at row %d.\n", row); exit(2); } clear_row(image_buffer, 1728); set_pixel_range(image_buffer, 1, 0, 1727); if (TIFFWriteScanline(tiff_file, image_buffer, row++, 0) < 0) { printf("Write error at row %d.\n", row); exit(2); } for (row = 332; row < 400; row++) { clear_row(image_buffer, 1728); if (TIFFWriteScanline(tiff_file, image_buffer, row, 0) < 0) { printf("Write error at row %d.\n", row); exit(2); } } return 400; }
void TiffWriter::write_scanline(vector<Byte4>& buf, unsigned int y) { TIFFWriteScanline( (TIFF*)my_tiff, (char*)&buf[0][0], y); }
int classify(float ** rast) { float a, b, c, d, e, f, g, * coeffs, z1, z2, z3, z4, z5, z6, z7, z8, z9, gSqrd, minic, maxic, slope, crosc, slope_tol, curve_tol; int row, col, type; type = 0; //float out [3][NUMBER_OF_ROWS][NUMBER_OF_COLS]; char out [3][NUMBER_OF_ROWS][NUMBER_OF_COLS]; // 3 samples per pixel - 8 bits per sample - rgba char colors [6][3] = { // rgb colors corresponding to landform classifications {160,160,160}, {0,0,0}, {102,178,255}, {76,153,0}, {255,255,102}, {255,102,102} }; g = 1.0; //grid spacing -- assumed to be 1 gSqrd = g*g; slope_tol = 1.0; curve_tol = 0.0001; for(row = 1; row < NUMBER_OF_ROWS-1; row++){ for(col = 1; col < NUMBER_OF_COLS-1; col++) { z1 = rast[row-1][col-1]; z2 = rast[row-1][col ]; z3 = rast[row-1][col+1]; z4 = rast[row ][col-1]; z5 = rast[row ][col ]; z6 = rast[row ][col+1]; z7 = rast[row+1][col-1]; z8 = rast[row+1][col ]; z9 = rast[row+1][col+1]; a = (z1 +z3 +z4 +z6 +z7 +z9) / (6.0*gSqrd) - (z2 +z5 +z8) / (3.0*gSqrd); b = (z1 +z2 +z3 +z7 +z8 +z9) / (6.0*gSqrd) - (z4 +z5 +z6) / (3.0*gSqrd); c = (z3 +z7 -z1 -z9) / (4.0*gSqrd); d = (z3 +z6 +z9 -z1 -z4 -z7) / (6.0*g); e = (z1 +z2 +z3 -z7 -z8 -z9) / (6.0*g); f = (2.0*(z2 +z4 +z6 +z8)-(z1 +z3 +z7 +z9) + 5.0*z5) / 9.0; minic = (-a - b - sqrt((a - b) * (a - b) + c * c)); maxic = (-a - b + sqrt((a - b) * (a - b) + c * c)); slope = RAD2DEG * atan(sqrt((d * d) + (e * e))); crosc = -2.0 * (b * d * d + a * e * e - c * d * e) / (d * d + e * e); /* Case 1: Surface is sloping. Cannot be a peak,pass or pit. Therefore calculate the cross-sectional curvature to characterise as channel, ridge or planar. */ type = FLAT; if (slope > slope_tol) { if (crosc > curve_tol) { type = RIDGE; } else if (crosc < -curve_tol) { type = CHANNEL; } else { type = FLAT; } } else { /* Case 2: Surface has (approximately) vertical slope normal. Feature can be of any type. */ if (maxic > curve_tol) { if (minic > curve_tol) { type = PEAK; } else if (minic < -curve_tol) { type = PASS; } else { type = RIDGE; } } else if (maxic < -curve_tol) { if (minic < -curve_tol) { type = PIT; } } else { if (minic < -curve_tol) { type = CHANNEL; } else if (minic > curve_tol && minic < -curve_tol) { type = FLAT; } } } out[RED ][row][col] = colors[type][RED ]; out[GREEN][row][col] = colors[type][GREEN]; out[BLUE ][row][col] = colors[type][BLUE ]; }//end for }//end for //write tiff out to file TIFF * fdout; if((fdout = TIFFOpen("/Users/Ben/Desktop/out/TM_classified.tif", "w")) == NULL) { PyErr_SetString(PyExc_IOError, "Could not open output file."); return 0; } TIFFSetField(fdout, TIFFTAG_IMAGEWIDTH, IMAGE_WIDTH); TIFFSetField(fdout, TIFFTAG_IMAGELENGTH, IMAGE_LENGTH); TIFFSetField(fdout, TIFFTAG_BITSPERSAMPLE, 32); TIFFSetField(fdout, TIFFTAG_COMPRESSION, COMPRESSION_NONE); TIFFSetField(fdout, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB); //TIFFSetField(fdout, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK); TIFFSetField(fdout, TIFFTAG_RESOLUTIONUNIT, RESUNIT_NONE); TIFFSetField(fdout, TIFFTAG_DATATYPE, 3); TIFFSetField(fdout, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); TIFFSetField(fdout, TIFFTAG_PLANARCONFIG, PLANARCONFIG_SEPARATE); TIFFSetField(fdout, TIFFTAG_SAMPLESPERPIXEL, 3); tsample_t band; for(band = 0; band < 3; band++){ for(row = 0; row < NUMBER_OF_ROWS; row++) { if(TIFFWriteScanline(fdout, out[band][row], row, band) != 1) { PyErr_SetString(PyExc_IOError, "Could not write scanline to file."); return 0; } } } TIFFClose(fdout); return 1; }//end function
void TIFFFormat::do_write(image_list* input, byte_sink* output, const options_map& opts, bool is_multi) { tiff_warn_error twe; tsize_t (*read_function)(thandle_t, void*, tsize_t) = (dynamic_cast<byte_source*>(output) ? tiff_read_from_writer : tiff_no_read); tif_holder t = TIFFClientOpen( "internal", "w", output, read_function, tiff_write, tiff_seek<byte_sink>, tiff_close, tiff_size<byte_sink>, NULL, NULL); std::vector<uint8_t> bufdata; const unsigned n_pages = input->size(); for (unsigned i = 0; i != n_pages; ++i) { Image* im = input->at(i); void* bufp = 0; bool copy_data = false; const uint32 h = im->dim(0); const uint16 photometric = ((im->ndims() == 3 && im->dim(2)) ? PHOTOMETRIC_RGB : PHOTOMETRIC_MINISBLACK); TIFFSetField(t.tif, TIFFTAG_IMAGELENGTH, uint32(h)); TIFFSetField(t.tif, TIFFTAG_IMAGEWIDTH, uint32(im->dim(1))); TIFFSetField(t.tif, TIFFTAG_BITSPERSAMPLE, uint16(im->nbits())); TIFFSetField(t.tif, TIFFTAG_SAMPLESPERPIXEL, uint16(im->dim_or(2, 1))); TIFFSetField(t.tif, TIFFTAG_PHOTOMETRIC, uint16(photometric)); TIFFSetField(t.tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); if (get_optional_bool(opts, "tiff:compress", true)) { TIFFSetField(t.tif, TIFFTAG_COMPRESSION, COMPRESSION_LZW); // For 8 bit images, prediction defaults to false; for 16 bit images, // it defaults to true. This is because compression of raw 16 bit // images is often counter-productive without this flag. See the // discusssion at http://www.asmail.be/msg0055176395.html const bool prediction_default = im->nbits() != 8; if (get_optional_bool(opts, "tiff:horizontal-predictor", prediction_default)) { TIFFSetField(t.tif, TIFFTAG_PREDICTOR, PREDICTOR_HORIZONTAL); if (!copy_data) { bufdata.resize(im->dim(1) * im->nbytes()); bufp = &bufdata[0]; copy_data = true; } } } TIFFSetField(t.tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); const char* meta = get_optional_cstring(opts, "metadata"); if (meta) { TIFFSetField(t.tif, TIFFTAG_IMAGEDESCRIPTION, meta); } options_map::const_iterator x_iter = opts.find("tiff:XResolution"); if (x_iter != opts.end()) { double d; int i; float value; if (x_iter->second.get_int(i)) { value = i; } else if (x_iter->second.get_double(d)) { value = d; } else { throw WriteOptionsError("XResolution must be an integer or floating point value."); } TIFFSetField(t.tif, TIFFTAG_XRESOLUTION, value); } options_map::const_iterator y_iter = opts.find("tiff:YResolution"); if (x_iter != opts.end()) { double d; int i; float value; if (x_iter->second.get_int(i)) { value = i; } else if (x_iter->second.get_double(d)) { value = d; } else { throw WriteOptionsError("YResolution must be an integer or floating point value."); } TIFFSetField(t.tif, TIFFTAG_YRESOLUTION, value); } const uint16_t resolution_unit = get_optional_int(opts, "tiff:XResolutionUnit", uint16_t(-1)); if (resolution_unit != uint16_t(-1)) { TIFFSetField(t.tif, TIFFTAG_RESOLUTIONUNIT, resolution_unit); } if (is_multi) { TIFFSetField(t.tif, TIFFTAG_SUBFILETYPE, FILETYPE_PAGE); TIFFSetField(t.tif, TIFFTAG_PAGENUMBER, i, n_pages); } for (uint32 r = 0; r != h; ++r) { void* rowp = im->rowp(r); if (copy_data) { std::memcpy(bufp, rowp, im->dim(1) * im->nbytes()); rowp = bufp; } if (TIFFWriteScanline(t.tif, rowp, r) == -1) { throw CannotWriteError("imread.imsave._tiff: Error writing TIFF file"); } } if (is_multi) { if (!TIFFWriteDirectory(t.tif)) { throw CannotWriteError("TIFFWriteDirectory failed"); } } } TIFFFlush(t.tif); }
int main(int argc, char *argv[]) { int row; uint8_t image_buffer[8192]; TIFF *tiff_file; struct tm *tm; time_t now; char buf[133]; float x_resolution; float y_resolution; int i; int opt; int compression; int photo_metric; int fill_order; compression = T4_COMPRESSION_ITU_T6; photo_metric = PHOTOMETRIC_MINISWHITE; fill_order = FILLORDER_LSB2MSB; while ((opt = getopt(argc, argv, "126ir")) != -1) { switch (opt) { case '1': compression = T4_COMPRESSION_ITU_T4_1D; break; case '2': compression = T4_COMPRESSION_ITU_T4_2D; break; case '6': compression = T4_COMPRESSION_ITU_T6; break; case 'i': photo_metric = PHOTOMETRIC_MINISBLACK; break; case 'r': fill_order = FILLORDER_MSB2LSB; break; default: //usage(); exit(2); break; } } for (i = 0; sequence[i].name; i++) { if ((tiff_file = TIFFOpen(sequence[i].name, "w")) == NULL) exit(2); /* Prepare the directory entry fully before writing the image, or libtiff complains */ TIFFSetField(tiff_file, TIFFTAG_COMPRESSION, compression); TIFFSetField(tiff_file, TIFFTAG_IMAGEWIDTH, sequence[i].width); TIFFSetField(tiff_file, TIFFTAG_BITSPERSAMPLE, 1); TIFFSetField(tiff_file, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); TIFFSetField(tiff_file, TIFFTAG_SAMPLESPERPIXEL, 1); TIFFSetField(tiff_file, TIFFTAG_ROWSPERSTRIP, -1L); TIFFSetField(tiff_file, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); TIFFSetField(tiff_file, TIFFTAG_PHOTOMETRIC, photo_metric); TIFFSetField(tiff_file, TIFFTAG_FILLORDER, fill_order); x_resolution = sequence[i].x_res/100.0f; y_resolution = sequence[i].y_res/100.0f; TIFFSetField(tiff_file, TIFFTAG_XRESOLUTION, floorf(x_resolution*2.54f + 0.5f)); TIFFSetField(tiff_file, TIFFTAG_YRESOLUTION, floorf(y_resolution*2.54f + 0.5f)); TIFFSetField(tiff_file, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH); TIFFSetField(tiff_file, TIFFTAG_SOFTWARE, "spandsp"); if (gethostname(buf, sizeof(buf)) == 0) TIFFSetField(tiff_file, TIFFTAG_HOSTCOMPUTER, buf); TIFFSetField(tiff_file, TIFFTAG_IMAGEDESCRIPTION, "Blank test image"); TIFFSetField(tiff_file, TIFFTAG_MAKE, "soft-switch.org"); TIFFSetField(tiff_file, TIFFTAG_MODEL, "test data"); time(&now); tm = localtime(&now); sprintf(buf, "%4d/%02d/%02d %02d:%02d:%02d", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec); TIFFSetField(tiff_file, TIFFTAG_DATETIME, buf); TIFFSetField(tiff_file, TIFFTAG_IMAGELENGTH, sequence[i].length); TIFFSetField(tiff_file, TIFFTAG_PAGENUMBER, 0, 1); TIFFSetField(tiff_file, TIFFTAG_CLEANFAXDATA, CLEANFAXDATA_CLEAN); TIFFSetField(tiff_file, TIFFTAG_IMAGEWIDTH, sequence[i].width); TIFFCheckpointDirectory(tiff_file); /* Write the image first.... */ for (row = 0; row < sequence[i].length; row++) { memset(image_buffer, 0, sequence[i].width/8 + 1); if (TIFFWriteScanline(tiff_file, image_buffer, row, 0) < 0) { printf("Write error at row %d.\n", row); exit(2); } } /* ....then the directory entry, and libtiff is happy. */ TIFFWriteDirectory(tiff_file); TIFFClose(tiff_file); } return 0; }
int output_data_to_tif_file(char *file, float *output_data, int xmax_data, int ymax_data, int *labels, int type, int bit_size, int invert){ //Output array output_data to a file. //The array labels tells where to add boundaries, etc. (NULL for none) //(xmax_data,ymax_data)=size of input array (ie, "output_data") //uint32 rowsperstrip=8; uint32 planarconfig=1; uint16 bitspersample=8; uint32 image_width=xmax_data; uint16 photometric=1; float tmp; float onetmp; float array_max,array_min; float scale; tdata_t data_buf; TIFF *tif; int i,j,k; unsigned char *p_char; unsigned short *p_short; // char *datetime; char datetime[50]; time_t rawtime; struct tm * timeinfo; int u; bitspersample=(uint16)bit_size; tif=TIFFOpen(file,"w"); //Set some headers that need to be set TIFFSetField(tif,TIFFTAG_IMAGEWIDTH, image_width); TIFFSetField(tif,TIFFTAG_PLANARCONFIG, planarconfig); TIFFSetField(tif,TIFFTAG_BITSPERSAMPLE, bitspersample); TIFFSetField(tif,TIFFTAG_PHOTOMETRIC, photometric); // TIFFSetField(tif,TIFFTAG_ROWSPERSTRIP, rowsperstrip); //Write out current time into file time(&rawtime); //rawtime=current time since Jan 1, 1970 in seconds timeinfo=localtime(&rawtime); //datetime=asctime(timeinfo); strftime(datetime,sizeof(datetime),"%Y:%m:%d %H:%M:%S",timeinfo); TIFFSetField(tif,TIFFTAG_DATETIME, datetime); //Allocate space for data (either 16-bit or 8-bit) if(bitspersample==8){ data_buf = (unsigned char *)malloc(xmax_data); p_char=data_buf; }else if(bitspersample==16){ data_buf = (unsigned short *)malloc(xmax_data*2); p_short=data_buf; }else{ printf("Bits per sample too high: %i.\n",bitspersample); TIFFClose(tif); return 0; } //Get max and min array_max=0.0; array_min=1.0e15; for(j=0;j<ymax_data;j++){ for(i=0;i<xmax_data;i++){ u=j*xmax_data+i; if(output_data[u]>array_max)array_max=output_data[u]; if(output_data[u]<array_min)array_min=output_data[u]; } } if (array_max>array_min){ scale=1.0/(array_max-array_min); }else{ scale=0.0; } //Value of one degree of grayness: onetmp=1.0; if (bitspersample==8){ onetmp=1.0/(scale*xmax8); } for(j=0;j<ymax_data;j++){ for(i=0;i<xmax_data;i++){ //in bounds u=j*xmax_data+i; //Convert data to 8 bit or 16 bit tmp=output_data[u]; if(invert==1){ //Flip values back from array_max-c[][] if(tmp>0.0)tmp=array_max-tmp; } if (labels!=NULL){ //type determines what set of labels to write out k=labels[u]; if (type==0){ if(k==found_border){ tmp=array_max; }else if(k==found_border_a){ tmp=array_max-onetmp; }else if(k==found_border_b){ tmp=array_max-(2.0*onetmp); }else if(k==found_border_c){ tmp=array_max-(3.0*onetmp); }else if(k==found_border_d){ tmp=array_max-(4.0*onetmp); }else if(k==found_border_e){ tmp=array_max-(5.0*onetmp); }else if(k==found_border_f){ tmp=array_max-(6.0*onetmp); }else if(k==found_border_g){ tmp=array_max-(7.0*onetmp); }else if(k==cell_label){ tmp=array_max-(15.0*onetmp); }else if(k==delete_pixel){ tmp=array_min; } }else if (type==1){ if(labels[u]==found_border){ tmp=array_max; //tmp=8300.0; } }else if (type==2){ if(labels[u]==found_border){ tmp=array_max; }else if (labels[u]==cell_nucleus){ tmp=array_max-5.0; } } } if(bitspersample==8){ *(p_char+i)=(unsigned char) ((tmp-array_min)*scale*xmax8); }else{ if (tmp<0.0) tmp=0.0; if(tmp>xmax16)tmp=xmax16; *(p_short+i)=(unsigned short) tmp; //Original is assumed to be 16-bit data, so just replace it here. } } TIFFWriteScanline(tif,data_buf,j,1); } TIFFClose(tif); free(data_buf); return 1; }
static int cpContig(IMAGE* in, TIFF* out) { tdata_t buf = _TIFFmalloc(TIFFScanlineSize(out)); short *r = NULL; int x, y; if (in->zsize == 3) { short *g, *b; r = (short *)_TIFFmalloc(3 * in->xsize * sizeof (short)); g = r + in->xsize; b = g + in->xsize; for (y = in->ysize-1; y >= 0; y--) { uint8* pp = (uint8*) buf; getrow(in, r, y, 0); getrow(in, g, y, 1); getrow(in, b, y, 2); for (x = 0; x < in->xsize; x++) { pp[0] = r[x]; pp[1] = g[x]; pp[2] = b[x]; pp += 3; } if (TIFFWriteScanline(out, buf, in->ysize-y-1, 0) < 0) goto bad; } } else if (in->zsize == 4) { short *g, *b, *a; r = (short *)_TIFFmalloc(4 * in->xsize * sizeof (short)); g = r + in->xsize; b = g + in->xsize; a = b + in->xsize; for (y = in->ysize-1; y >= 0; y--) { uint8* pp = (uint8*) buf; getrow(in, r, y, 0); getrow(in, g, y, 1); getrow(in, b, y, 2); getrow(in, a, y, 3); for (x = 0; x < in->xsize; x++) { pp[0] = r[x]; pp[1] = g[x]; pp[2] = b[x]; pp[3] = a[x]; pp += 4; } if (TIFFWriteScanline(out, buf, in->ysize-y-1, 0) < 0) goto bad; } } else { uint8* pp = (uint8*) buf; r = (short *)_TIFFmalloc(in->xsize * sizeof (short)); for (y = in->ysize-1; y >= 0; y--) { getrow(in, r, y, 0); for (x = in->xsize-1; x >= 0; x--) pp[x] = r[x]; if (TIFFWriteScanline(out, buf, in->ysize-y-1, 0) < 0) goto bad; } } if (r) _TIFFfree(r); _TIFFfree(buf); return (1); bad: if (r) _TIFFfree(r); _TIFFfree(buf); return (0); }
int main(int argc, char* argv[]) { uint16 bitspersample, shortv; uint32 imagewidth, imagelength; uint16 config = PLANARCONFIG_CONTIG; uint32 rowsperstrip = (uint32) -1; uint16 photometric = PHOTOMETRIC_RGB; uint16 *rmap, *gmap, *bmap; uint32 row; int cmap = -1; TIFF *in, *out; int c; extern int optind; extern char* optarg; while ((c = getopt(argc, argv, "C:c:p:r:")) != -1) switch (c) { case 'C': /* force colormap interpretation */ cmap = atoi(optarg); break; case 'c': /* compression scheme */ if (!processCompressOptions(optarg)) usage(); break; case 'p': /* planar configuration */ if (streq(optarg, "separate")) config = PLANARCONFIG_SEPARATE; else if (streq(optarg, "contig")) config = PLANARCONFIG_CONTIG; else usage(); break; case 'r': /* rows/strip */ rowsperstrip = atoi(optarg); break; case '?': usage(); /*NOTREACHED*/ } if (argc - optind != 2) usage(); in = TIFFOpen(argv[optind], "r"); if (in == NULL) return (-1); if (!TIFFGetField(in, TIFFTAG_PHOTOMETRIC, &shortv) || shortv != PHOTOMETRIC_PALETTE) { fprintf(stderr, "%s: Expecting a palette image.\n", argv[optind]); return (-1); } if (!TIFFGetField(in, TIFFTAG_COLORMAP, &rmap, &gmap, &bmap)) { fprintf(stderr, "%s: No colormap (not a valid palette image).\n", argv[optind]); return (-1); } bitspersample = 0; TIFFGetField(in, TIFFTAG_BITSPERSAMPLE, &bitspersample); if (bitspersample != 8) { fprintf(stderr, "%s: Sorry, can only handle 8-bit images.\n", argv[optind]); return (-1); } out = TIFFOpen(argv[optind+1], "w"); if (out == NULL) return (-2); cpTags(in, out); TIFFGetField(in, TIFFTAG_IMAGEWIDTH, &imagewidth); TIFFGetField(in, TIFFTAG_IMAGELENGTH, &imagelength); if (compression != (uint16)-1) TIFFSetField(out, TIFFTAG_COMPRESSION, compression); else TIFFGetField(in, TIFFTAG_COMPRESSION, &compression); switch (compression) { case COMPRESSION_JPEG: if (jpegcolormode == JPEGCOLORMODE_RGB) photometric = PHOTOMETRIC_YCBCR; else photometric = PHOTOMETRIC_RGB; TIFFSetField(out, TIFFTAG_JPEGQUALITY, quality); TIFFSetField(out, TIFFTAG_JPEGCOLORMODE, jpegcolormode); break; case COMPRESSION_LZW: case COMPRESSION_DEFLATE: if (predictor != 0) TIFFSetField(out, TIFFTAG_PREDICTOR, predictor); break; } TIFFSetField(out, TIFFTAG_PHOTOMETRIC, photometric); TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 3); TIFFSetField(out, TIFFTAG_PLANARCONFIG, config); TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, rowsperstrip = TIFFDefaultStripSize(out, rowsperstrip)); (void) TIFFGetField(in, TIFFTAG_PLANARCONFIG, &shortv); if (cmap == -1) cmap = checkcmap(1<<bitspersample, rmap, gmap, bmap); if (cmap == 16) { /* * Convert 16-bit colormap to 8-bit. */ int i; for (i = (1<<bitspersample)-1; i >= 0; i--) { #define CVT(x) (((x) * 255) / ((1L<<16)-1)) rmap[i] = CVT(rmap[i]); gmap[i] = CVT(gmap[i]); bmap[i] = CVT(bmap[i]); } } { unsigned char *ibuf, *obuf; register unsigned char* pp; register uint32 x; ibuf = (unsigned char*)_TIFFmalloc(TIFFScanlineSize(in)); obuf = (unsigned char*)_TIFFmalloc(TIFFScanlineSize(out)); switch (config) { case PLANARCONFIG_CONTIG: for (row = 0; row < imagelength; row++) { if (!TIFFReadScanline(in, ibuf, row, 0)) goto done; pp = obuf; for (x = 0; x < imagewidth; x++) { *pp++ = (unsigned char) rmap[ibuf[x]]; *pp++ = (unsigned char) gmap[ibuf[x]]; *pp++ = (unsigned char) bmap[ibuf[x]]; } if (!TIFFWriteScanline(out, obuf, row, 0)) goto done; } break; case PLANARCONFIG_SEPARATE: for (row = 0; row < imagelength; row++) { if (!TIFFReadScanline(in, ibuf, row, 0)) goto done; for (pp = obuf, x = 0; x < imagewidth; x++) *pp++ = (unsigned char) rmap[ibuf[x]]; if (!TIFFWriteScanline(out, obuf, row, 0)) goto done; for (pp = obuf, x = 0; x < imagewidth; x++) *pp++ = (unsigned char) gmap[ibuf[x]]; if (!TIFFWriteScanline(out, obuf, row, 0)) goto done; for (pp = obuf, x = 0; x < imagewidth; x++) *pp++ = (unsigned char) bmap[ibuf[x]]; if (!TIFFWriteScanline(out, obuf, row, 0)) goto done; } break; } _TIFFfree(ibuf); _TIFFfree(obuf); } done: (void) TIFFClose(in); (void) TIFFClose(out); return (0); }
static void yuy2tiff(char *outputfile) { unsigned char *yuyv; int z; int sampleperpixel = 3; char cadena[255]; sprintf(cadena, "%s.tif", outputfile); TIFF *out = TIFFOpen(cadena, "w"); TIFFSetField(out, TIFFTAG_IMAGEWIDTH, sw); // set the width of the image TIFFSetField(out, TIFFTAG_IMAGELENGTH, sh); // set the height of the image TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, sampleperpixel); // set number of channels per pixel TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 8); // set the size of the channels TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); // set the origin of the image. // Some other essential fields to set that you do not have to understand for now. TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB); fprintf(stderr, "Convert YUYV frame to TIFF image.\n"); tsize_t linebytes = sampleperpixel * sw; unsigned char *buf = NULL; // buffer used to store the row of pixel information for writing to file // Allocating memory to store the pixels of current row if (TIFFScanlineSize(out) == linebytes) buf = (unsigned char *) _TIFFmalloc(linebytes); else buf = (unsigned char *) _TIFFmalloc(TIFFScanlineSize(out)); // We set the strip size of the file to be size of one row of pixels TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(out, sw * sampleperpixel)); yuyv = buffer; z = 0; uint32 row = 0; for (; row < sh; row++) { int x; unsigned char *ptr = buf; for (x = 0; x < sw; x++) { int r, g, b; int y, u, v; if (!z) y = yuyv[0] << 8; else y = yuyv[2] << 8; u = yuyv[1] - 128; v = yuyv[3] - 128; r = (y + (359 * v)) >> 8; g = (y - (88 * u) - (183 * v)) >> 8; b = (y + (454 * u)) >> 8; *(ptr++) = (r > 255) ? 255 : ((r < 0) ? 0 : r); *(ptr++) = (g > 255) ? 255 : ((g < 0) ? 0 : g); *(ptr++) = (b > 255) ? 255 : ((b < 0) ? 0 : b); if (z++) { z = 0; yuyv += 4; } } if (TIFFWriteScanline(out, buf, row, 0) < 0) { printf("Error TIFF\n"); break; } } TIFFClose(out); if (buf) _TIFFfree(buf); printf("Save %s file\n", cadena); }
void PLTIFFEncoder::DoTiffEncode (PLBmpBase* pBmp, TIFF* tif) { uint32 l, c, image_length, image_width; // iterate over data PLBYTE **pla = pBmp->GetLineArray(); PLASSERT( pla ); image_length = (uint32) pBmp->GetHeight(); image_width = (uint32) pBmp->GetWidth(); switch (pBmp->GetBitsPerPixel()) { case 8: { // first, save the colormap uint16 red[256]; uint16 green[256]; uint16 blue[256]; PLPixel32 * pPal = pBmp->GetPalette(); PLASSERT( pPal ); for (int i = 0; i < pBmp->GetNumColors(); i++, pPal++) { red[i] = pPal->GetR (); green[i] = pPal->GetG (); blue[i] = pPal->GetB (); } SetField( tif, TIFFTAG_COLORMAP, red, green, blue ); } // fall-through case 1: // TODO: a bit of error checking for (l = 0; l < image_length; l++) if(TIFFWriteScanline( tif, pla[l], l, 0 ) < 1) { throw PLTextException(PL_ERRINTERNAL, "TIFFWriteScanline failed"); } break; case 24: { // TODO: check whether (r,g,b) components come in the correct order here... PLBYTE* pBuf = new PLBYTE[3*image_width]; for (l = 0; l < image_length; l++) { for (c = 0; c < image_width; c++) { pBuf[c*3 + 0] = pla[l][c*sizeof(PLPixel24) + PL_RGBA_RED]; pBuf[c*3 + 1] = pla[l][c*sizeof(PLPixel24) + PL_RGBA_GREEN]; pBuf[c*3 + 2] = pla[l][c*sizeof(PLPixel24) + PL_RGBA_BLUE]; } if(TIFFWriteScanline( tif, pBuf, l, 0 ) < 1) { throw PLTextException(PL_ERRINTERNAL, "TIFFWriteScanline failed"); } } delete [] pBuf; } break; case 32: { // TODO: check whether (r,g,b) components come in the correct order here... if (pBmp->HasAlpha()) { uint32 *plBuf = new uint32[image_width]; for (l = 0; l < image_length; l++) { for (c = 0; c < image_width; c++) { // For 32 bit, TIFFLIB wants abgr long word packed... plBuf[c] = ((uint32)(pla[l][c*sizeof(PLPixel32) + PL_RGBA_ALPHA]) << 24) | ((uint32)(pla[l][c*sizeof(PLPixel32) + PL_RGBA_BLUE]) << 16) | ((uint32)(pla[l][c*sizeof(PLPixel32) + PL_RGBA_GREEN]) << 8 ) | ((uint32)(pla[l][c*sizeof(PLPixel32) + PL_RGBA_RED])); } if(TIFFWriteScanline( tif, plBuf, l, 0 ) < 1) { throw PLTextException(PL_ERRINTERNAL, "TIFFWriteScanline failed"); } } delete [] plBuf; } else { PLBYTE* pBuf = new PLBYTE[3*image_width]; for (l = 0; l < image_length; l++) { for (c = 0; c < image_width; c++) { pBuf[c*3 + 0] = pla[l][c*sizeof(PLPixel32) + PL_RGBA_RED]; pBuf[c*3 + 1] = pla[l][c*sizeof(PLPixel32) + PL_RGBA_GREEN]; pBuf[c*3 + 2] = pla[l][c*sizeof(PLPixel32) + PL_RGBA_BLUE]; } if(TIFFWriteScanline( tif, pBuf, l, 0 ) < 1) { throw PLTextException(PL_ERRINTERNAL, "TIFFWriteScanline failed"); } } delete [] pBuf; } } break; default: PLASSERT(false); throw PLTextException(PL_ERRFORMAT_NOT_SUPPORTED, "unsupported bitsPerPixel"); } // we could flush at this point, but TIFFClose will do it anyway }
int main(int argc, char **argv) { int bits_per_pixel = 8, cmsize, i, j, k, gray_index, chunk_size = 32, nchunks = 16; unsigned char * scan_line; uint16 * gray; float refblackwhite[2*1]; TIFF * tif; programName = argv[0]; if (argc != 4) Usage(); if (!strcmp(argv[1], "-depth")) bits_per_pixel = atoi(argv[2]); else Usage(); switch (bits_per_pixel) { case 8: nchunks = 16; chunk_size = 32; break; case 4: nchunks = 4; chunk_size = 128; break; case 2: nchunks = 2; chunk_size = 256; break; default: Usage(); } cmsize = nchunks * nchunks; gray = (uint16 *) malloc(cmsize * sizeof(uint16)); gray[0] = 3000; for (i = 1; i < cmsize; i++) gray[i] = (uint16) (-log10((double) i / (cmsize - 1)) * 1000); refblackwhite[0] = 0.0; refblackwhite[1] = (float)((1L<<bits_per_pixel) - 1); if ((tif = TIFFOpen(argv[3], "w")) == NULL) { fprintf(stderr, "can't open %s as a TIFF file\n", argv[3]); return 0; } TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, WIDTH); TIFFSetField(tif, TIFFTAG_IMAGELENGTH, HEIGHT); TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bits_per_pixel); TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE); TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK); TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1); TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1); TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); TIFFSetField(tif, TIFFTAG_REFERENCEBLACKWHITE, refblackwhite); TIFFSetField(tif, TIFFTAG_TRANSFERFUNCTION, gray); TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, RESUNIT_NONE); scan_line = (unsigned char *) malloc(WIDTH / (8 / bits_per_pixel)); for (i = 0; i < HEIGHT; i++) { for (j = 0, k = 0; j < WIDTH;) { gray_index = (j / chunk_size) + ((i / chunk_size) * nchunks); switch (bits_per_pixel) { case 8: scan_line[k++] = gray_index; j++; break; case 4: scan_line[k++] = (gray_index << 4) + gray_index; j += 2; break; case 2: scan_line[k++] = (gray_index << 6) + (gray_index << 4) + (gray_index << 2) + gray_index; j += 4; break; } } TIFFWriteScanline(tif, scan_line, i, 0); } free(scan_line); TIFFClose(tif); return 0; }
static int create_impress_page(TIFF *tiff_file) { int j; int row; uint8_t *page; /* ETSI ETS 300 242 B.5.2 Printing resolution - the IMPRESS page */ if ((page = malloc(1079*1728/8)) == NULL) return 0; memset(page, 0, 1079*1728/8); set_pixel_range(page, 1, 0, 1727); for (row = 2; row <= 78; row++) { set_pixel_range(page, row, 850, 850 + 27); set_pixel_range(page, row, 850 + 27 + 745, 850 + 27 + 745 + 26); } for (row = 80; row <= 117; row++) { for (j = 0; j < 1728; j += 2) set_pixel(page, row, j); } for (row = 118; row <= 155; row++) { for (j = 1; j < 1728; j += 2) set_pixel(page, row, j); } for (row = 194; row <= 231; row += 2) set_pixel_range(page, row, 0, 1727); for (row = 270; row <= 276; row++) set_pixel_range(page, row, 60, 60 + 1607); for (j = 0; j < 1728; j += 27) set_pixel(page, 315, j); for (row = 354; row <= 480; row++) set_pixel_range(page, row, 209, 768); for (row = 358; row <= 476; row++) clear_pixel_range(page, row, 488, 489); clear_pixel_range(page, 417, 217, 760); for (row = 354; row <= 357; row++) set_pixel_range(page, row, 962, 1521); for (row = 477; row <= 480; row++) set_pixel_range(page, row, 962, 1521); for (row = 358; row <= 476; row++) set_pixel_range(page, row, 962, 969); for (row = 358; row <= 476; row++) set_pixel_range(page, row, 1514, 1521); for (row = 358; row <= 476; row++) set_pixel(page, row, 1241); set_pixel_range(page, 417, 970, 1513); for (row = 354; row <= 1079; row++) set_pixel(page, row, 864); for (row = 157; row <= 926; row++) set_pixel_range(page, row, 884, 899); for (row = 0; row < 1079; row++) { if (TIFFWriteScanline(tiff_file, page + row*1728/8, row, 0) < 0) { printf("Write error at row %d.\n", row); exit(2); } } free(page); return 1079; }
int main(int argc, char* argv[]) { uint16 photometric = 0; uint32 rowsperstrip = (uint32) -1; double resolution = -1; unsigned char *buf = NULL; tsize_t linebytes = 0; uint16 spp = 1; uint16 bpp = 8; TIFF *out; FILE *in; unsigned int w, h, prec, row; char *infile; int c; extern int optind; extern char* optarg; if (argc < 2) { fprintf(stderr, "%s: Too few arguments\n", argv[0]); usage(); } while ((c = getopt(argc, argv, "c:r:R:")) != -1) switch (c) { case 'c': /* compression scheme */ if (!processCompressOptions(optarg)) usage(); break; case 'r': /* rows/strip */ rowsperstrip = atoi(optarg); break; case 'R': /* resolution */ resolution = atof(optarg); break; case '?': usage(); /*NOTREACHED*/ } if (optind + 2 < argc) { fprintf(stderr, "%s: Too many arguments\n", argv[0]); usage(); } /* * If only one file is specified, read input from * stdin; otherwise usage is: ppm2tiff input output. */ if (argc - optind > 1) { infile = argv[optind++]; in = fopen(infile, "rb"); if (in == NULL) { fprintf(stderr, "%s: Can not open.\n", infile); return (-1); } } else { infile = "<stdin>"; in = stdin; #if defined(HAVE_SETMODE) && defined(O_BINARY) setmode(fileno(stdin), O_BINARY); #endif } if (fgetc(in) != 'P') BadPPM(infile); switch (fgetc(in)) { case '4': /* it's a PBM file */ bpp = 1; spp = 1; photometric = PHOTOMETRIC_MINISWHITE; break; case '5': /* it's a PGM file */ bpp = 8; spp = 1; photometric = PHOTOMETRIC_MINISBLACK; break; case '6': /* it's a PPM file */ bpp = 8; spp = 3; photometric = PHOTOMETRIC_RGB; if (compression == COMPRESSION_JPEG && jpegcolormode == JPEGCOLORMODE_RGB) photometric = PHOTOMETRIC_YCBCR; break; default: BadPPM(infile); } /* Parse header */ while(1) { if (feof(in)) BadPPM(infile); c = fgetc(in); /* Skip whitespaces (blanks, TABs, CRs, LFs) */ if (strchr(" \t\r\n", c)) continue; /* Check for comment line */ if (c == '#') { do { c = fgetc(in); } while(!strchr("\r\n", c) || feof(in)); continue; } ungetc(c, in); break; } switch (bpp) { case 1: if (fscanf(in, " %u %u", &w, &h) != 2) BadPPM(infile); if (fgetc(in) != '\n') BadPPM(infile); break; case 8: if (fscanf(in, " %u %u %u", &w, &h, &prec) != 3) BadPPM(infile); if (fgetc(in) != '\n' || prec != 255) BadPPM(infile); break; } out = TIFFOpen(argv[optind], "w"); if (out == NULL) return (-4); TIFFSetField(out, TIFFTAG_IMAGEWIDTH, (uint32) w); TIFFSetField(out, TIFFTAG_IMAGELENGTH, (uint32) h); TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, spp); TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, bpp); TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); TIFFSetField(out, TIFFTAG_PHOTOMETRIC, photometric); TIFFSetField(out, TIFFTAG_COMPRESSION, compression); switch (compression) { case COMPRESSION_JPEG: TIFFSetField(out, TIFFTAG_JPEGQUALITY, quality); TIFFSetField(out, TIFFTAG_JPEGCOLORMODE, jpegcolormode); break; case COMPRESSION_LZW: case COMPRESSION_DEFLATE: if (predictor != 0) TIFFSetField(out, TIFFTAG_PREDICTOR, predictor); break; case COMPRESSION_CCITTFAX3: TIFFSetField(out, TIFFTAG_GROUP3OPTIONS, g3opts); break; } switch (bpp) { case 1: linebytes = (spp * w + (8 - 1)) / 8; if (rowsperstrip == (uint32) -1) { TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, h); } else { TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(out, rowsperstrip)); } break; case 8: linebytes = spp * w; TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(out, rowsperstrip)); break; } if (TIFFScanlineSize(out) > linebytes) buf = (unsigned char *)_TIFFmalloc(linebytes); else buf = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(out)); if (resolution > 0) { TIFFSetField(out, TIFFTAG_XRESOLUTION, resolution); TIFFSetField(out, TIFFTAG_YRESOLUTION, resolution); TIFFSetField(out, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH); } for (row = 0; row < h; row++) { if (fread(buf, linebytes, 1, in) != 1) { fprintf(stderr, "%s: scanline %lu: Read error.\n", infile, (unsigned long) row); break; } if (TIFFWriteScanline(out, buf, row, 0) < 0) break; } (void) TIFFClose(out); if (buf) _TIFFfree(buf); return (0); }
static int create_duration1_page(TIFF *tiff_file) { uint8_t image_buffer[1728/8 + 1]; int row; int i; /* ETSI ETS 300 242 B.5.3 Acceptance of total coded scan line duration - the DURATION1 page */ row = 0; clear_row(image_buffer, 1728); set_pixel_range(image_buffer, 1, 0, 1727); if (TIFFWriteScanline(tiff_file, image_buffer, row++, 0) < 0) { printf("Write error at row %d.\n", row); exit(2); } for ( ; row < 117; row++) { clear_row(image_buffer, 1728); if (TIFFWriteScanline(tiff_file, image_buffer, row, 0) < 0) { printf("Write error at row %d.\n", row); exit(2); } } clear_row(image_buffer, 1728); set_pixel_range(image_buffer, 1, 0, 1727); if (TIFFWriteScanline(tiff_file, image_buffer, row++, 0) < 0) { printf("Write error at row %d.\n", row); exit(2); } clear_row(image_buffer, 1728); for (i = 1; i < 1728; i += 2) set_pixel(image_buffer, 1, i); if (TIFFWriteScanline(tiff_file, image_buffer, row++, 0) < 0) { printf("Write error at row %d.\n", row); exit(2); } clear_row(image_buffer, 1728); set_pixel_range(image_buffer, 1, 0, 1727); if (TIFFWriteScanline(tiff_file, image_buffer, row++, 0) < 0) { printf("Write error at row %d.\n", row); exit(2); } for ( ; row < 236; row++) { clear_row(image_buffer, 1728); if (TIFFWriteScanline(tiff_file, image_buffer, row, 0) < 0) { printf("Write error at row %d.\n", row); exit(2); } } clear_row(image_buffer, 1728); set_pixel_range(image_buffer, 1, 0, 1727); if (TIFFWriteScanline(tiff_file, image_buffer, row, 0) < 0) { printf("Write error at row %d.\n", row); exit(2); } return 237; }
int main(int argc, char* argv[]) { uint32 rowsperstrip = (uint32) -1; TIFF *in, *out; uint32 w, h; uint16 samplesperpixel; uint16 bitspersample; uint16 config; uint16 photometric; uint16* red; uint16* green; uint16* blue; tsize_t rowsize; register uint32 row; register tsample_t s; unsigned char *inbuf, *outbuf; char thing[1024]; int c; extern int optind; extern char *optarg; while ((c = getopt(argc, argv, "c:r:R:G:B:")) != -1) switch (c) { case 'c': /* compression scheme */ if (!processCompressOptions(optarg)) usage(); break; case 'r': /* rows/strip */ rowsperstrip = atoi(optarg); break; case 'R': RED = PCT(atoi(optarg)); break; case 'G': GREEN = PCT(atoi(optarg)); break; case 'B': BLUE = PCT(atoi(optarg)); break; case '?': usage(); /*NOTREACHED*/ } if (argc - optind < 2) usage(); in = TIFFOpen(argv[optind], "r"); if (in == NULL) return (-1); photometric = 0; TIFFGetField(in, TIFFTAG_PHOTOMETRIC, &photometric); if (photometric != PHOTOMETRIC_RGB && photometric != PHOTOMETRIC_PALETTE ) { fprintf(stderr, "%s: Bad photometric; can only handle RGB and Palette images.\n", argv[optind]); return (-1); } TIFFGetField(in, TIFFTAG_SAMPLESPERPIXEL, &samplesperpixel); if (samplesperpixel != 1 && samplesperpixel != 3) { fprintf(stderr, "%s: Bad samples/pixel %u.\n", argv[optind], samplesperpixel); return (-1); } TIFFGetField(in, TIFFTAG_BITSPERSAMPLE, &bitspersample); if (bitspersample != 8) { fprintf(stderr, " %s: Sorry, only handle 8-bit samples.\n", argv[optind]); return (-1); } TIFFGetField(in, TIFFTAG_IMAGEWIDTH, &w); TIFFGetField(in, TIFFTAG_IMAGELENGTH, &h); TIFFGetField(in, TIFFTAG_PLANARCONFIG, &config); out = TIFFOpen(argv[optind+1], "w"); if (out == NULL) return (-1); TIFFSetField(out, TIFFTAG_IMAGEWIDTH, w); TIFFSetField(out, TIFFTAG_IMAGELENGTH, h); TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 8); TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 1); TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); cpTags(in, out); if (compression != (uint16) -1) { TIFFSetField(out, TIFFTAG_COMPRESSION, compression); switch (compression) { case COMPRESSION_JPEG: TIFFSetField(out, TIFFTAG_JPEGQUALITY, quality); TIFFSetField(out, TIFFTAG_JPEGCOLORMODE, jpegcolormode); break; case COMPRESSION_LZW: case COMPRESSION_DEFLATE: if (predictor != 0) TIFFSetField(out, TIFFTAG_PREDICTOR, predictor); break; } } TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK); sprintf(thing, "B&W version of %s", argv[optind]); TIFFSetField(out, TIFFTAG_IMAGEDESCRIPTION, thing); TIFFSetField(out, TIFFTAG_SOFTWARE, "tiff2bw"); outbuf = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(out)); TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(out, rowsperstrip)); #define pack(a,b) ((a)<<8 | (b)) switch (pack(photometric, config)) { case pack(PHOTOMETRIC_PALETTE, PLANARCONFIG_CONTIG): case pack(PHOTOMETRIC_PALETTE, PLANARCONFIG_SEPARATE): TIFFGetField(in, TIFFTAG_COLORMAP, &red, &green, &blue); /* * Convert 16-bit colormap to 8-bit (unless it looks * like an old-style 8-bit colormap). */ if (checkcmap(in, 1<<bitspersample, red, green, blue) == 16) { int i; #define CVT(x) (((x) * 255L) / ((1L<<16)-1)) for (i = (1<<bitspersample)-1; i >= 0; i--) { red[i] = CVT(red[i]); green[i] = CVT(green[i]); blue[i] = CVT(blue[i]); } #undef CVT } inbuf = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(in)); for (row = 0; row < h; row++) { if (TIFFReadScanline(in, inbuf, row, 0) < 0) break; compresspalette(outbuf, inbuf, w, red, green, blue); if (TIFFWriteScanline(out, outbuf, row, 0) < 0) break; } break; case pack(PHOTOMETRIC_RGB, PLANARCONFIG_CONTIG): inbuf = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(in)); for (row = 0; row < h; row++) { if (TIFFReadScanline(in, inbuf, row, 0) < 0) break; compresscontig(outbuf, inbuf, w); if (TIFFWriteScanline(out, outbuf, row, 0) < 0) break; } break; case pack(PHOTOMETRIC_RGB, PLANARCONFIG_SEPARATE): rowsize = TIFFScanlineSize(in); inbuf = (unsigned char *)_TIFFmalloc(3*rowsize); for (row = 0; row < h; row++) { for (s = 0; s < 3; s++) if (TIFFReadScanline(in, inbuf+s*rowsize, row, s) < 0) return (-1); compresssep(outbuf, inbuf, inbuf+rowsize, inbuf+2*rowsize, w); if (TIFFWriteScanline(out, outbuf, row, 0) < 0) break; } break; } #undef pack TIFFClose(out); return (0); }
int main(int argc, char **argv) { int bits_per_pixel = 8, cmsize, i, j, k, cmap_index, chunk_size = 32, nchunks = 16; unsigned char * scan_line; uint16 *red, *green, *blue; TIFF * tif; programName = argv[0]; if (argc != 4) Usage(); if (!strcmp(argv[1], "-depth")) bits_per_pixel = atoi(argv[2]); else Usage(); switch (bits_per_pixel) { case 8: nchunks = 16; chunk_size = 32; break; case 4: nchunks = 4; chunk_size = 128; break; case 2: nchunks = 2; chunk_size = 256; break; case 1: nchunks = 2; chunk_size = 256; break; default: Usage(); } if (bits_per_pixel != 1) { cmsize = nchunks * nchunks; } else { cmsize = 2; } red = (uint16 *) malloc(cmsize * sizeof(uint16)); green = (uint16 *) malloc(cmsize * sizeof(uint16)); blue = (uint16 *) malloc(cmsize * sizeof(uint16)); switch (bits_per_pixel) { case 8: for (i = 0; i < cmsize; i++) { if (i < 32) red[i] = 0; else if (i < 64) red[i] = SCALE(36); else if (i < 96) red[i] = SCALE(73); else if (i < 128) red[i] = SCALE(109); else if (i < 160) red[i] = SCALE(146); else if (i < 192) red[i] = SCALE(182); else if (i < 224) red[i] = SCALE(219); else if (i < 256) red[i] = SCALE(255); if ((i % 32) < 4) green[i] = 0; else if (i < 8) green[i] = SCALE(36); else if ((i % 32) < 12) green[i] = SCALE(73); else if ((i % 32) < 16) green[i] = SCALE(109); else if ((i % 32) < 20) green[i] = SCALE(146); else if ((i % 32) < 24) green[i] = SCALE(182); else if ((i % 32) < 28) green[i] = SCALE(219); else if ((i % 32) < 32) green[i] = SCALE(255); if ((i % 4) == 0) blue[i] = SCALE(0); else if ((i % 4) == 1) blue[i] = SCALE(85); else if ((i % 4) == 2) blue[i] = SCALE(170); else if ((i % 4) == 3) blue[i] = SCALE(255); } break; case 4: red[0] = SCALE(255); green[0] = 0; blue[0] = 0; red[1] = 0; green[1] = SCALE(255); blue[1] = 0; red[2] = 0; green[2] = 0; blue[2] = SCALE(255); red[3] = SCALE(255); green[3] = SCALE(255); blue[3] = SCALE(255); red[4] = 0; green[4] = SCALE(255); blue[4] = SCALE(255); red[5] = SCALE(255); green[5] = 0; blue[5] = SCALE(255); red[6] = SCALE(255); green[6] = SCALE(255); blue[6] = 0; red[7] = 0; green[7] = 0; blue[7] = 0; red[8] = SCALE(176); green[8] = SCALE(224); blue[8] = SCALE(230); red[9] = SCALE(100); green[9] = SCALE(149); blue[9] = SCALE(237); red[10] = SCALE(46); green[10] = SCALE(139); blue[10] = SCALE(87); red[11] = SCALE(160); green[11] = SCALE(82); blue[11] = SCALE(45); red[12] = SCALE(238); green[12] = SCALE(130); blue[12] = SCALE(238); red[13] = SCALE(176); green[13] = SCALE(48); blue[13] = SCALE(96); red[14] = SCALE(50); green[14] = SCALE(205); blue[14] = SCALE(50); red[15] = SCALE(240); green[15] = SCALE(152); blue[15] = SCALE(35); break; case 2: red[0] = SCALE(255); green[0] = 0; blue[0] = 0; red[1] = 0; green[1] = SCALE(255); blue[1] = 0; red[2] = 0; green[2] = 0; blue[2] = SCALE(255); red[3] = SCALE(255); green[3] = SCALE(255); blue[3] = SCALE(255); break; case 1: red[0] = 0; green[0] = 0; blue[0] = 0; red[1] = SCALE(255); green[1] = SCALE(255); blue[1] = SCALE(255); break; } if ((tif = TIFFOpen(argv[3], "w")) == NULL) { fprintf(stderr, "can't open %s as a TIFF file\n", argv[3]); return 0; } TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, WIDTH); TIFFSetField(tif, TIFFTAG_IMAGELENGTH, HEIGHT); TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bits_per_pixel); TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE); TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_PALETTE); TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1); TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1); TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, RESUNIT_NONE); TIFFSetField(tif, TIFFTAG_COLORMAP, red, green, blue); scan_line = (unsigned char *) malloc(WIDTH / (8 / bits_per_pixel)); for (i = 0; i < HEIGHT; i++) { for (j = 0, k = 0; j < WIDTH;) { cmap_index = (j / chunk_size) + ((i / chunk_size) * nchunks); switch (bits_per_pixel) { case 8: scan_line[k++] = cmap_index; j++; break; case 4: scan_line[k++] = (cmap_index << 4) + cmap_index; j += 2; break; case 2: scan_line[k++] = (cmap_index << 6) + (cmap_index << 4) + (cmap_index << 2) + cmap_index; j += 4; break; case 1: scan_line[k++] = ((j / chunk_size) == (i / chunk_size)) ? 0x00 : 0xff; j += 8; break; } } TIFFWriteScanline(tif, scan_line, i, 0); } free(scan_line); TIFFClose(tif); return 0; }