int main(int argc, char* argv[]) { #if !HAVE_DECL_OPTARG extern int optind; extern char *optarg; #endif int c, status; while ((c = getopt(argc, argv, "c:r:")) != -1) switch (c) { case 'c': /* compression scheme */ if (!processCompressOptions(optarg)) usage(); break; case 'r': /* rows/strip */ rowsperstrip = atoi(optarg); break; case '?': usage(); /*NOTREACHED*/ } if (argc - optind != 2) usage(); makegamtab(GIFGAMMA); filename = argv[optind]; imagename = argv[optind+1]; if ((infile = fopen(imagename, "rb")) != NULL) { int c; fclose(infile); printf("overwrite %s? ", imagename); fflush(stdout); c = getc(stdin); if (c != 'y' && c != 'Y') return (1); } if ((infile = fopen(filename, "rb")) == NULL) { perror(filename); return (1); } status = convert(); fclose(infile); return (status); }
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++ = rmap[ibuf[x]]; *pp++ = gmap[ibuf[x]]; *pp++ = 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++ = rmap[ibuf[x]]; if (!TIFFWriteScanline(out, obuf, row, 0)) goto done; for (pp = obuf, x = 0; x < imagewidth; x++) *pp++ = gmap[ibuf[x]]; if (!TIFFWriteScanline(out, obuf, row, 0)) goto done; for (pp = obuf, x = 0; x < imagewidth; x++) *pp++ = 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); }
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); cpTags(in, out); TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 8); TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 1); TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); 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[]) { uint32 width = 0, length = 0, linebytes, bufsize; uint32 nbands = 1; /* number of bands in input image*/ 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; extern int optind; extern char* optarg; 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 ); lseek(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++) { lseek(fd, hdr_size + (length*band+row)*linebytes, SEEK_SET); 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[]) { TIFF *in, *out; uint16 samplesperpixel, bitspersample = 1, shortv; float floatv; char thing[1024]; uint32 rowsperstrip = (uint32) -1; int onestrip = 0; uint16 fillorder = 0; int c; extern int optind; extern char *optarg; while ((c = getopt(argc, argv, "c:f:r:t:")) != -1) switch (c) { case 'c': /* compression scheme */ if (!processCompressOptions(optarg)) usage(); break; case 'f': /* fill order */ if (streq(optarg, "lsb2msb")) fillorder = FILLORDER_LSB2MSB; else if (streq(optarg, "msb2lsb")) fillorder = FILLORDER_MSB2LSB; else usage(); break; case 'r': /* rows/strip */ rowsperstrip = atoi(optarg); onestrip = 0; break; case 't': threshold = atoi(optarg); if (threshold < 0) threshold = 0; else if (threshold > 255) threshold = 255; break; case '?': usage(); /*NOTREACHED*/ } if (argc - optind < 2) usage(); in = TIFFOpen(argv[optind], "r"); if (in == NULL) return (-1); TIFFGetField(in, TIFFTAG_SAMPLESPERPIXEL, &samplesperpixel); if (samplesperpixel != 1) { fprintf(stderr, "%s: Not a b&w image.\n", argv[0]); return (-1); } TIFFGetField(in, TIFFTAG_BITSPERSAMPLE, &bitspersample); if (bitspersample != 8) { fprintf(stderr, " %s: Sorry, only handle 8-bit samples.\n", argv[0]); return (-1); } out = TIFFOpen(argv[optind+1], "w"); if (out == NULL) return (-1); CopyField(TIFFTAG_IMAGEWIDTH, imagewidth); TIFFGetField(in, TIFFTAG_IMAGELENGTH, &imagelength); TIFFSetField(out, TIFFTAG_IMAGELENGTH, imagelength-1); TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 1); TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 1); TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); TIFFSetField(out, TIFFTAG_COMPRESSION, compression); if (fillorder) TIFFSetField(out, TIFFTAG_FILLORDER, fillorder); else CopyField(TIFFTAG_FILLORDER, shortv); sprintf(thing, "Dithered B&W version of %s", argv[optind]); TIFFSetField(out, TIFFTAG_IMAGEDESCRIPTION, thing); CopyField(TIFFTAG_PHOTOMETRIC, shortv); CopyField(TIFFTAG_ORIENTATION, shortv); CopyField(TIFFTAG_XRESOLUTION, floatv); CopyField(TIFFTAG_YRESOLUTION, floatv); CopyField(TIFFTAG_RESOLUTIONUNIT, shortv); if (onestrip) rowsperstrip = imagelength-1; else rowsperstrip = TIFFDefaultStripSize(out, rowsperstrip); TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, rowsperstrip); switch (compression) { case COMPRESSION_CCITTFAX3: TIFFSetField(out, TIFFTAG_GROUP3OPTIONS, group3options); break; case COMPRESSION_LZW: case COMPRESSION_DEFLATE: if (predictor) TIFFSetField(out, TIFFTAG_PREDICTOR, predictor); break; } fsdither(in, out); TIFFClose(in); TIFFClose(out); return (0); }
int main(int argc, char* argv[]) { uint16 photometric; uint32 rowsperstrip = (uint32) -1; double resolution = -1; unsigned char *buf = NULL; uint32 row; tsize_t linebytes; uint16 spp; TIFF *out; FILE *in; uint32 w, h; int prec; char *infile; int c; extern int optind; extern char* optarg; 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 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, "r" BINMODE); if (in == NULL) { fprintf(stderr, "%s: Can not open.\n", infile); return (-1); } } else { infile = "<stdin>"; in = stdin; } if (getc(in) != 'P') BadPPM(infile); switch (getc(in)) { case '5': /* it's a PGM file */ spp = 1; photometric = PHOTOMETRIC_MINISBLACK; break; case '6': /* it's a PPM file */ spp = 3; photometric = PHOTOMETRIC_RGB; if (compression == COMPRESSION_JPEG && jpegcolormode == JPEGCOLORMODE_RGB) photometric = PHOTOMETRIC_YCBCR; break; default: BadPPM(infile); } if (fscanf(in, " %ld %ld %d", &w, &h, &prec) != 3) BadPPM(infile); if (getc(in) != '\n' || w <= 0 || h <= 0 || prec != 255) BadPPM(infile); out = TIFFOpen(argv[optind], "w"); if (out == NULL) return (-4); TIFFSetField(out, TIFFTAG_IMAGEWIDTH, w); TIFFSetField(out, TIFFTAG_IMAGELENGTH, h); TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, spp); TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 8); 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; } linebytes = spp * w; if (TIFFScanlineSize(out) > linebytes) buf = (unsigned char *)_TIFFmalloc(linebytes); else buf = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(out)); TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(out, rowsperstrip)); 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); }
int main(int argc, char* argv[]) { IMAGE *in; TIFF *out; int c; extern int optind; extern char* optarg; while ((c = getopt(argc, argv, "c:p:r:")) != -1) switch (c) { case 'c': /* compression scheme */ if (!processCompressOptions(optarg)) usage(); break; case 'f': /* fill order */ if (streq(optarg, "lsb2msb")) fillorder = FILLORDER_LSB2MSB; else if (streq(optarg, "msb2lsb")) fillorder = FILLORDER_MSB2LSB; else 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 = iopen(argv[optind], "r"); if (in == NULL) return (-1); out = TIFFOpen(argv[optind+1], "w"); if (out == NULL) return (-2); TIFFSetField(out, TIFFTAG_IMAGEWIDTH, (uint32) in->xsize); TIFFSetField(out, TIFFTAG_IMAGELENGTH, (uint32) in->ysize); TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 8); TIFFSetField(out, TIFFTAG_COMPRESSION, compression); if (in->zsize == 1) photometric = PHOTOMETRIC_MINISBLACK; else photometric = PHOTOMETRIC_RGB; 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; } TIFFSetField(out, TIFFTAG_PHOTOMETRIC, photometric); if (fillorder != 0) TIFFSetField(out, TIFFTAG_FILLORDER, fillorder); TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, in->zsize); if (in->zsize > 3) { uint16 v[1]; v[0] = EXTRASAMPLE_UNASSALPHA; TIFFSetField(out, TIFFTAG_EXTRASAMPLES, 1, v); } TIFFSetField(out, TIFFTAG_MINSAMPLEVALUE, (uint16) in->min); TIFFSetField(out, TIFFTAG_MAXSAMPLEVALUE, (uint16) in->max); TIFFSetField(out, TIFFTAG_PLANARCONFIG, config); if (config != PLANARCONFIG_SEPARATE) TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(out, rowsperstrip)); else /* force 1 row/strip for library limitation */ TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, 1L); if (in->name[0] != '\0') TIFFSetField(out, TIFFTAG_IMAGEDESCRIPTION, in->name); if (config == PLANARCONFIG_CONTIG) cpContig(in, out); else cpSeparate(in, out); (void) iclose(in); (void) TIFFClose(out); return (0); }
int main(int argc, char* argv[]) { uint16 defconfig = (uint16) -1; uint16 deffillorder = 0; uint32 deftilewidth = (uint32) -1; uint32 deftilelength = (uint32) -1; uint32 defrowsperstrip = (uint32) -1; uint32 diroff = 0, p_diroff = 0; TIFF* in; TIFF* out; char mode[10]; char* mp = mode; int c; extern int optind; extern char* optarg; *mp++ = 'w'; *mp = '\0'; while ((c = getopt(argc, argv, "c:f:l:o:z:p:r:w:aistBLMC")) != -1) switch (c) { case 'a': /* append to output */ mode[0] = 'a'; break; case 'c': /* compression scheme */ if (!processCompressOptions(optarg)) usage(); break; case 'f': /* fill order */ if (streq(optarg, "lsb2msb")) deffillorder = FILLORDER_LSB2MSB; else if (streq(optarg, "msb2lsb")) deffillorder = FILLORDER_MSB2LSB; else usage(); break; case 'i': /* ignore errors */ ignore = TRUE; break; case 'l': /* tile length */ outtiled = TRUE; deftilelength = atoi(optarg); break; case 'o': /* initial directory offset */ diroff = strtoul(optarg, NULL, 0); break; case 'z': /* initial directory offset */ p_diroff = strtoul(optarg, NULL, 0); break; case 'p': /* planar configuration */ if (streq(optarg, "separate")) defconfig = PLANARCONFIG_SEPARATE; else if (streq(optarg, "contig")) defconfig = PLANARCONFIG_CONTIG; else usage(); break; case 'r': /* rows/strip */ defrowsperstrip = atoi(optarg); break; case 's': /* generate stripped output */ outtiled = FALSE; break; case 't': /* generate tiled output */ outtiled = TRUE; break; case 'w': /* tile width */ outtiled = TRUE; deftilewidth = atoi(optarg); break; case 'B': *mp++ = 'b'; *mp = '\0'; break; case 'L': *mp++ = 'l'; *mp = '\0'; break; case 'M': *mp++ = 'm'; *mp = '\0'; break; case 'C': *mp++ = 'c'; *mp = '\0'; break; case '?': usage(); /*NOTREACHED*/ } if (argc - optind < 2) usage(); out = TIFFOpen(argv[argc-1], mode); if (out == NULL) return (-2); mode[0] = 'r'; for (; optind < argc-1 ; optind++) { in = TIFFOpen(argv[optind], mode); if (in == NULL) return (-3); if (diroff != 0 && !TIFFSetSubDirectory(in, diroff)) { TIFFError(TIFFFileName(in), "Error, setting subdirectory at %#x", diroff); (void) TIFFClose(out); return (1); } if (p_diroff != 0 && !TIFFSetDirectory(in, p_diroff)) { TIFFError(TIFFFileName(in), "Error, setting subdirectory at %#x", diroff); (void) TIFFClose(out); return (1); } do { config = defconfig; compression = defcompression; predictor = defpredictor; fillorder = deffillorder; rowsperstrip = defrowsperstrip; tilewidth = deftilewidth; tilelength = deftilelength; g3opts = defg3opts; if (!tiffcp(in, out) || !TIFFWriteDirectory(out)) { (void) TIFFClose(out); return (1); } } while (TIFFReadDirectory(in) && p_diroff == 0 ); (void) TIFFClose(in); } (void) TIFFClose(out); return (0); }
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); }
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 main(int argc, char* argv[]) { uint16 defconfig = (uint16) -1; uint16 deffillorder = 0; uint32 deftilewidth = (uint32) -1; uint32 deftilelength = (uint32) -1; uint32 defrowsperstrip = (uint32) -1; uint32 diroff = 0; TIFF* in; TIFF* out; const char* mode = "w"; int c; extern int optind; extern char* optarg; while ((c = getopt(argc, argv, "c:f:l:o:p:r:w:e:g:4:aistd8")) != -1) switch (c) { case 'a': /* append to output */ mode = "a"; break; case '8': /* append to output */ mode = "w8"; break; case 'd': /* down cast 8bit to 4bit */ convert_8_to_4 = 1; break; case 'c': /* compression scheme */ if (!processCompressOptions(optarg)) usage(); break; case 'e': worldfile = optarg; break; case 'f': /* fill order */ if (streq(optarg, "lsb2msb")) deffillorder = FILLORDER_LSB2MSB; else if (streq(optarg, "msb2lsb")) deffillorder = FILLORDER_MSB2LSB; else usage(); break; case 'i': /* ignore errors */ ignore = TRUE; break; case 'g': /* GeoTIFF metadata file */ geofile = optarg; break; case '4': proj4_string = optarg; break; case 'l': /* tile length */ outtiled = TRUE; deftilelength = atoi(optarg); break; case 'o': /* initial directory offset */ diroff = strtoul(optarg, NULL, 0); break; case 'p': /* planar configuration */ if (streq(optarg, "separate")) defconfig = PLANARCONFIG_SEPARATE; else if (streq(optarg, "contig")) defconfig = PLANARCONFIG_CONTIG; else usage(); break; case 'r': /* rows/strip */ defrowsperstrip = atoi(optarg); break; case 's': /* generate stripped output */ outtiled = FALSE; break; case 't': /* generate tiled output */ outtiled = TRUE; break; case 'w': /* tile width */ outtiled = TRUE; deftilewidth = atoi(optarg); break; case '?': usage(); /*NOTREACHED*/ } if (argc - optind < 2) usage(); out = TIFFOpen(argv[argc-1], mode); if (out == NULL) return (-2); for (; optind < argc-1 ; optind++) { in = TIFFOpen(argv[optind], "r"); if (in == NULL) return (-3); if (diroff != 0 && !TIFFSetSubDirectory(in, diroff)) { TIFFError(TIFFFileName(in), "Error, setting subdirectory at %#x", diroff); (void) TIFFClose(out); return (1); } do { config = defconfig; compression = defcompression; predictor = defpredictor; fillorder = deffillorder; rowsperstrip = defrowsperstrip; tilewidth = deftilewidth; tilelength = deftilelength; g3opts = defg3opts; if (!tiffcp(in, out) || !TIFFWriteDirectory(out)) { (void) TIFFClose(out); return (1); } } while (TIFFReadDirectory(in)); (void) TIFFClose(in); } (void) TIFFClose(out); return (0); }