static int PredictorSetup(TIFF* tif) { TIFFPredictorState* sp = PredictorState(tif); TIFFDirectory* td = &tif->tif_dir; if (sp->predictor == 1) /* no differencing */ return (1); if (sp->predictor != 2) { TIFFError(tif->tif_name, "\"Predictor\" value %d not supported", sp->predictor); return (0); } if (td->td_bitspersample != 8 && td->td_bitspersample != 16) { TIFFError(tif->tif_name, "Horizontal differencing \"Predictor\" not supported with %d-bit samples", td->td_bitspersample); return (0); } sp->stride = (td->td_planarconfig == PLANARCONFIG_CONTIG ? td->td_samplesperpixel : 1); /* * Calculate the scanline/tile-width size in bytes. */ if (isTiled(tif)) sp->rowsize = TIFFTileRowSize(tif); else sp->rowsize = TIFFScanlineSize(tif); return (1); }
/* * Get a strip-organized image that has * PlanarConfiguration contiguous if SamplesPerPixel > 1 * or * SamplesPerPixel == 1 */ static int gtStripContig(TIFFImageIter* img, void *udata, uint32 w, uint32 h) { TIFF* tif = img->tif; ImageIterTileContigRoutine callback = img->callback.contig; uint16 orientation; uint32 row, nrow; u_char* buf; uint32 rowsperstrip; uint32 imagewidth = img->width; tsize_t scanline; int32 fromskew; buf = (u_char*) _TIFFmalloc(TIFFStripSize(tif)); if (buf == 0) { TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "No space for strip buffer"); return (0); } orientation = img->orientation; TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip); scanline = TIFFScanlineSize(tif); fromskew = (w < imagewidth ? imagewidth - w : 0); for (row = 0; row < h; row += rowsperstrip) { nrow = (row + rowsperstrip > h ? h - row : rowsperstrip); if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, row, 0), buf, nrow*scanline) < 0 && img->stoponerr) break; (*callback)(img, udata, 0, row, w, nrow, fromskew, buf); } _TIFFfree(buf); return (1); }
KDE_EXPORT void kimgio_g3_read( QImageIO *io ) { // This won't work if io is not a QFile ! TIFF *tiff = TIFFOpen(QFile::encodeName(io->fileName()), "r"); if (!tiff) return; uint32 width, height; tsize_t scanlength; if( TIFFGetField( tiff, TIFFTAG_IMAGEWIDTH, &width ) != 1 || TIFFGetField( tiff, TIFFTAG_IMAGELENGTH, &height ) != 1 ) return; scanlength = TIFFScanlineSize(tiff); QImage image(width, height, 1, 0, QImage::BigEndian); if (image.isNull() || scanlength != image.bytesPerLine()) { TIFFClose(tiff); return; } for (uint32 y=0; y < height; y++) TIFFReadScanline(tiff, image.scanLine(y), y); TIFFClose(tiff); io->setImage(image); io->setStatus(0); }
static void read_scanlines(struct tiff_tile *tout, TIFF *tif) { // read all file info struct tiff_info tinfo[1]; get_tiff_info(tinfo, tif); // fill-in output information tout->w = tinfo->w; tout->h = tinfo->h; tout->bps = tinfo->bps; tout->fmt = tinfo->fmt; tout->spp = tinfo->spp; tout->broken = false; // define useful constants int pixel_size = tinfo->spp * tinfo->bps/8; int output_size = tout->w * tout->h * pixel_size; // allocate space for output data tout->data = xmalloc(output_size); // copy scanlines int scanline_size = TIFFScanlineSize(tif); assert(scanline_size == tinfo->w * pixel_size); for (int j = 0; j < tinfo->h; j++) { uint8_t *buf = tout->data + scanline_size*j; int r = TIFFReadScanline(tif, buf, j, 0); if (r < 0) fail("could not read scanline %d", j); } }
_declspec (dllexport) int readGreyImage(TIFF* tif, // TIFF handle - IN const int directory, // page ordinal number - IN uint8_t* buffer) // OUT, caller allocates memory { TIFFSetDirectory(tif, directory); int err = 0; uint32_t w, h, s; size_t npixels; TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &w); TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h); s = TIFFScanlineSize(tif); //TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, bits); npixels = w * h; int numToRead = npixels / s; for (int r = 0; r < numToRead; r++) { err = TIFFReadScanline(tif, buffer+r*s, r); if (err != 1) { return -1*r; } } return err; }
void read_tiff_image(TIFF* tif, IMAGE* image) { tdata_t buf; uint32 image_width, image_height; uint16 photometric; inT16 bpp; inT16 samples_per_pixel = 0; TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &image_width); TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &image_height); TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &bpp); TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &samples_per_pixel); TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &photometric); if (samples_per_pixel > 1) bpp *= samples_per_pixel; // Tesseract's internal representation is 0-is-black, // so if the photometric is 1 (min is black) then high-valued pixels // are 1 (white), otherwise they are 0 (black). uinT8 high_value = photometric == 1; image->create(image_width, image_height, bpp); IMAGELINE line; line.init(image_width); buf = _TIFFmalloc(TIFFScanlineSize(tif)); int bytes_per_line = (image_width*bpp + 7)/8; uinT8* dest_buf = image->get_buffer(); // This will go badly wrong with one of the more exotic tiff formats, // but the majority will work OK. for (int y = 0; y < image_height; ++y) { TIFFReadScanline(tif, buf, y); memcpy(dest_buf, buf, bytes_per_line); dest_buf += bytes_per_line; } if (high_value == 0) invert_image(image); _TIFFfree(buf); }
uint32 _TIFFDefaultStripSize(TIFF* tif, uint32 s) { if ((int32) s < 1) { /* * If RowsPerStrip is unspecified, try to break the * image up into strips that are approximately 8Kbytes. */ tsize_t scanline = TIFFScanlineSize(tif); s = (uint32)(8*1024) / (scanline == 0 ? 1 : scanline); if (s == 0) /* very wide images */ s = 1; } return (s); }
/* * Compute the # bytes in a variable height, row-aligned strip. */ tsize_t TIFFVStripSize(TIFF* tif, uint32 nrows) { TIFFDirectory *td = &tif->tif_dir; if (nrows == (uint32) -1) nrows = td->td_imagelength; #ifdef YCBCR_SUPPORT if (td->td_planarconfig == PLANARCONFIG_CONTIG && td->td_photometric == PHOTOMETRIC_YCBCR && !isUpSampled(tif)) { /* * Packed YCbCr data contain one Cb+Cr for every * HorizontalSampling*VerticalSampling Y values. * Must also roundup width and height when calculating * since images that are not a multiple of the * horizontal/vertical subsampling area include * YCbCr data for the extended image. */ tsize_t w = TIFFroundup(td->td_imagewidth, td->td_ycbcrsubsampling[0]); tsize_t scanline = TIFFhowmany(w*td->td_bitspersample, 8); tsize_t samplingarea = td->td_ycbcrsubsampling[0]*td->td_ycbcrsubsampling[1]; nrows = TIFFroundup(nrows, td->td_ycbcrsubsampling[1]); /* NB: don't need TIFFhowmany here 'cuz everything is rounded */ return ((tsize_t) (nrows*scanline + 2*(nrows*scanline / samplingarea))); } else #endif return ((tsize_t)(nrows * TIFFScanlineSize(tif))); }
void image_write_tif(const char *name, int w, int h, int c, int b, int n, void **p) { TIFF *T = 0; TIFFSetWarningHandler(0); if ((T = TIFFOpen(name, "w"))) { uint32 k, i, s; for (k = 0; k < n; ++k) { TIFFSetField(T, TIFFTAG_IMAGEWIDTH, w); TIFFSetField(T, TIFFTAG_IMAGELENGTH, h); TIFFSetField(T, TIFFTAG_BITSPERSAMPLE, 8*b); TIFFSetField(T, TIFFTAG_SAMPLESPERPIXEL, c); TIFFSetField(T, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB); TIFFSetField(T, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); TIFFSetField(T, TIFFTAG_ICCPROFILE, sRGB_icc_len, sRGB_icc); if (b == 4) TIFFSetField(T, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_IEEEFP); s = (uint32) TIFFScanlineSize(T); for (i = 0; i < h; ++i) TIFFWriteScanline(T, (uint8 *) p[k] + (h - i - 1) * s, i, 0); TIFFWriteDirectory(T); } TIFFClose(T); } }
void *image_read_tif(const char *name, int *w, int *h, int *c, int *b, int n) { TIFF *T = 0; void *p = 0; TIFFSetWarningHandler(0); if ((T = TIFFOpen(name, "r"))) { if ((n == 0) || TIFFSetDirectory(T, n)) { uint32 i, s = (uint32) TIFFScanlineSize(T); uint32 W, H; uint16 B, C; TIFFGetField(T, TIFFTAG_IMAGEWIDTH, &W); TIFFGetField(T, TIFFTAG_IMAGELENGTH, &H); TIFFGetField(T, TIFFTAG_BITSPERSAMPLE, &B); TIFFGetField(T, TIFFTAG_SAMPLESPERPIXEL, &C); if ((p = malloc(H * s))) { for (i = 0; i < H; ++i) TIFFReadScanline(T, (uint8 *) p + i * s, i, 0); *w = (int) W; *h = (int) H; *b = (int) B / 8; *c = (int) C; } } TIFFClose(T); } return p; }
int write_scanlines(TIFF *tif, const tdata_t array, const tsize_t size) { uint32 length, row; tsize_t scanlinesize, offset; (void) size; if (!TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &length)) { fprintf (stderr, "Can't get tag %d.\n", TIFFTAG_IMAGELENGTH); return -1; } scanlinesize = TIFFScanlineSize(tif); if (!scanlinesize) { fprintf (stderr, "Wrong size of scanline.\n"); return -1; } for (offset = 0, row = 0; row < length; offset+=scanlinesize, row++) { if (TIFFWriteScanline(tif, (char *)array + offset, row, 0) < 0) { fprintf (stderr, "Can't write image data at row %lu.\n", (long) row); return -1; } } return 0; }
static int cpSeparate(IMAGE* in, TIFF* out) { tdata_t buf = _TIFFmalloc(TIFFScanlineSize(out)); short *r = (short *)_TIFFmalloc(in->xsize * sizeof (short)); uint8* pp = (uint8*) buf; int x, y, z; for (z = 0; z < in->zsize; z++) { for (y = in->ysize-1; y >= 0; y--) { getrow(in, r, y, z); for (x = 0; x < in->xsize; x++) pp[x] = r[x]; if (TIFFWriteScanline(out, buf, in->ysize-y-1, z) < 0) goto bad; } } _TIFFfree(r); _TIFFfree(buf); return (1); bad: _TIFFfree(r); _TIFFfree(buf); return (0); }
/* * Verify file is writable and that the directory * information is setup properly. In doing the latter * we also "freeze" the state of the directory so * that important information is not changed. */ int TIFFWriteCheck(TIFF* tif, int tiles, const char* module) { if (tif->tif_mode == O_RDONLY) { TIFFErrorExt(tif->tif_clientdata, module, "%s: File not open for writing", tif->tif_name); return (0); } if (tiles ^ isTiled(tif)) { TIFFErrorExt(tif->tif_clientdata, tif->tif_name, tiles ? "Can not write tiles to a stripped image" : "Can not write scanlines to a tiled image"); return (0); } /* * On the first write verify all the required information * has been setup and initialize any data structures that * had to wait until directory information was set. * Note that a lot of our work is assumed to remain valid * because we disallow any of the important parameters * from changing after we start writing (i.e. once * TIFF_BEENWRITING is set, TIFFSetField will only allow * the image's length to be changed). */ if (!TIFFFieldSet(tif, FIELD_IMAGEDIMENSIONS)) { TIFFErrorExt(tif->tif_clientdata, module, "%s: Must set \"ImageWidth\" before writing data", tif->tif_name); return (0); } if (tif->tif_dir.td_samplesperpixel == 1) { /* * Planarconfiguration is irrelevant in case of single band * images and need not be included. We will set it anyway, * because this field is used in other parts of library even * in the single band case. */ if (!TIFFFieldSet(tif, FIELD_PLANARCONFIG)) tif->tif_dir.td_planarconfig = PLANARCONFIG_CONTIG; } else { if (!TIFFFieldSet(tif, FIELD_PLANARCONFIG)) { TIFFErrorExt(tif->tif_clientdata, module, "%s: Must set \"PlanarConfiguration\" before writing data", tif->tif_name); return (0); } } if (tif->tif_dir.td_stripoffset == NULL && !TIFFSetupStrips(tif)) { tif->tif_dir.td_nstrips = 0; TIFFErrorExt(tif->tif_clientdata, module, "%s: No space for %s arrays", tif->tif_name, isTiled(tif) ? "tile" : "strip"); return (0); } tif->tif_tilesize = isTiled(tif) ? TIFFTileSize(tif) : (tsize_t) -1; tif->tif_scanlinesize = TIFFScanlineSize(tif); tif->tif_flags |= TIFF_BEENWRITING; return (1); }
static int PredictorSetup(TIFF* tif) { static const char module[] = "PredictorSetup"; TIFFPredictorState* sp = PredictorState(tif); TIFFDirectory* td = &tif->tif_dir; switch (sp->predictor) /* no differencing */ { case PREDICTOR_NONE: return 1; case PREDICTOR_HORIZONTAL: if (td->td_bitspersample != 8 && td->td_bitspersample != 16 && td->td_bitspersample != 32) { TIFFErrorExt(tif->tif_clientdata, module, "Horizontal differencing \"Predictor\" not supported with %d-bit samples", td->td_bitspersample); return 0; } break; case PREDICTOR_FLOATINGPOINT: if (td->td_sampleformat != SAMPLEFORMAT_IEEEFP) { TIFFErrorExt(tif->tif_clientdata, module, "Floating point \"Predictor\" not supported with %d data format", td->td_sampleformat); return 0; } if (td->td_bitspersample != 16 && td->td_bitspersample != 24 && td->td_bitspersample != 32 && td->td_bitspersample != 64) { /* Should 64 be allowed? */ TIFFErrorExt(tif->tif_clientdata, module, "Floating point \"Predictor\" not supported with %d-bit samples", td->td_bitspersample); return 0; } break; default: TIFFErrorExt(tif->tif_clientdata, module, "\"Predictor\" value %d not supported", sp->predictor); return 0; } sp->stride = (td->td_planarconfig == PLANARCONFIG_CONTIG ? td->td_samplesperpixel : 1); /* * Calculate the scanline/tile-width size in bytes. */ if (isTiled(tif)) sp->rowsize = TIFFTileRowSize(tif); else sp->rowsize = TIFFScanlineSize(tif); if (sp->rowsize == 0) return 0; return 1; }
int tiff_write(char *file, Pic *pic) { TIFF *tif; uint32 samples_per_pixel = 3; uint32 w = pic->nx; uint32 h = pic->nx; uint32 scanline_size = samples_per_pixel * w; uint32 y; char *scanline; tif = TIFFOpen(file, "w"); if( !tif ) return FALSE; TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, w); TIFFSetField(tif, TIFFTAG_IMAGELENGTH, h); /* These are the charateristics of our Pic data */ TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, samples_per_pixel); TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8); TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB); /* * Turn on LZW compression. * Shhhhh! Don't tell Unisys! */ TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_LZW); /* * Predictors: * 1 (default) -- No predictor * 2 -- Horizontal differencing */ TIFFSetField(tif, TIFFTAG_PREDICTOR, 2); if( TIFFScanlineSize(tif) != scanline_size ) { fprintf(stderr, "TIFF: Mismatch with library's expected scanline size!\n"); return FALSE; } TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(tif, -1)); scanline = pic->pix; for(y=0; y<h; y++) { TIFFWriteScanline(tif, scanline, y, 0); scanline += w; } TIFFClose(tif); return TRUE; }
static int putimage( /* write out our image */ uint16 or, uint32 xs, uint32 ys, float xr, float yr, uint16 ru, uby8 *pd ) { register int y; uint32 rowsperstrip; TIFFSetField(tifout, TIFFTAG_COMPRESSION, COMPRESSION_NONE); if (flags & TM_F_BW) { TIFFSetField(tifout, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK); TIFFSetField(tifout, TIFFTAG_SAMPLESPERPIXEL, 1); } else { TIFFSetField(tifout, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB); TIFFSetField(tifout, TIFFTAG_SAMPLESPERPIXEL, 3); } if (rgbp != stdprims) { TIFFSetField(tifout, TIFFTAG_PRIMARYCHROMATICITIES, (float *)rgbp); TIFFSetField(tifout, TIFFTAG_WHITEPOINT, (float *)rgbp[WHT]); } TIFFSetField(tifout, TIFFTAG_BITSPERSAMPLE, 8); TIFFSetField(tifout, TIFFTAG_IMAGEWIDTH, xs); TIFFSetField(tifout, TIFFTAG_IMAGELENGTH, ys); TIFFSetField(tifout, TIFFTAG_RESOLUTIONUNIT, ru); TIFFSetField(tifout, TIFFTAG_COMPRESSION, comp); TIFFSetField(tifout, TIFFTAG_XRESOLUTION, xr); TIFFSetField(tifout, TIFFTAG_YRESOLUTION, yr); TIFFSetField(tifout, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); TIFFSetField(tifout, TIFFTAG_ORIENTATION, or); /* compute good strip size */ rowsperstrip = 8192/TIFFScanlineSize(tifout); if (rowsperstrip < 1) rowsperstrip = 1; TIFFSetField(tifout, TIFFTAG_ROWSPERSTRIP, rowsperstrip); /* write out scanlines */ if (flags & TM_F_BW) { for (y = 0; y < ys; y++) if (TIFFWriteScanline(tifout, pd + y*xs, y, 0) < 0) goto writerr; } else { for (y = 0; y < ys; y++) if (TIFFWriteScanline(tifout, pd + y*3*xs, y, 0) < 0) goto writerr; } return(0); /* all done! */ writerr: fputs("Error writing TIFF output\n", stderr); return(-1); }
/* * Decode a strip of pixels. We break it into rows to * maintain synchrony with the encode algorithm, which * is row by row. */ static int LogLuvDecodeStrip(TIFF* tif, tidata_t bp, tsize_t cc, tsample_t s) { tsize_t rowlen = TIFFScanlineSize(tif); assert(cc%rowlen == 0); while (cc && (*tif->tif_decoderow)(tif, bp, rowlen, s)) bp += rowlen, cc -= rowlen; return (cc == 0); }
/* * Decode a strip of pixels. We break it into rows to * maintain synchrony with the encode algorithm, which * is row by row. */ static int LogLuvDecodeStrip(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s) { tmsize_t rowlen = TIFFScanlineSize(tif); assert(cc%rowlen == 0); while (cc && (*tif->tif_decoderow)(tif, bp, rowlen, s)) bp += rowlen, cc -= rowlen; return (cc == 0); }
/* * Get a strip-organized image with * SamplesPerPixel > 1 * PlanarConfiguration separated * We assume that all such images are RGB. */ static int gtStripSeparate(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h) { TIFF* tif = img->tif; tileSeparateRoutine put = img->put.separate; uint16 orientation; u_char *buf; u_char *r, *g, *b, *a; uint32 row, y, nrow; tsize_t scanline; uint32 rowsperstrip; uint32 imagewidth = img->width; tsize_t stripsize; int32 fromskew, toskew; int alpha = img->alpha; stripsize = TIFFStripSize(tif); r = buf = (u_char *)_TIFFmalloc(4*stripsize); if (buf == 0) { TIFFError(TIFFFileName(tif), "No space for tile buffer"); return (0); } g = r + stripsize; b = g + stripsize; a = b + stripsize; if (!alpha) memset(a, 0xff, stripsize); y = setorientation(img, h); orientation = img->orientation; toskew = -(int32) (orientation == ORIENTATION_TOPLEFT ? w+w : w-w); TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip); scanline = TIFFScanlineSize(tif); fromskew = (w < imagewidth ? imagewidth - w : 0); for (row = 0; row < h; row += rowsperstrip) { nrow = (row + rowsperstrip > h ? h - row : rowsperstrip); if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, row, 0), r, nrow*scanline) < 0 && img->stoponerr) break; if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, row, 1), g, nrow*scanline) < 0 && img->stoponerr) break; if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, row, 2), b, nrow*scanline) < 0 && img->stoponerr) break; if (alpha && (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, row, 3), a, nrow*scanline) < 0 && img->stoponerr)) break; (*put)(img, raster+y*w, 0, y, w, nrow, fromskew, toskew, r, g, b, a); y += (orientation == ORIENTATION_TOPLEFT ? -(int32) nrow : (int32) nrow); } _TIFFfree(buf); return (1); }
int write_tiff (const char path[256], unsigned char *data, int nx, int ny) { TIFF *tif; unsigned char *data1; /* pixel data */ unsigned char *buf, *buf1, *begin, *end; /* scanline buffer */ int y; /* open tiff file */ tif = TIFFOpen(path, "w"); if (tif == NULL) { fprintf (stderr, "Error opening TIFF file: %s\n", path); return(-1); } /* set tiff fields */ TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, nx); TIFFSetField(tif, TIFFTAG_IMAGELENGTH, ny); TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8); TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE); TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK); TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1); TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); /* memory for scanline buffer */ buf = (unsigned char*) malloc (TIFFScanlineSize(tif)); if (buf == NULL) { fprintf (stderr, "Save TIFF roi: Cannot allocate memory "); fprintf (stderr, "for scanline buffer.\n"); return(-1); } /* read the required scanlines and copy the required portion */ data1 = data; begin = buf; end = begin + nx; for (y = 0; y < ny; y++) { for (buf1 = begin; buf1 < end; buf1++) *buf1 = *data1++; TIFFWriteScanline (tif, buf, y, 0); } /* release scanline buffer */ free (buf); /* flush data and close file */ TIFFFlushData(tif); TIFFClose(tif); return (1); }
static void EstimateStripByteCounts(TIFF* tif, TIFFDirEntry* dir, uint16 dircount) { register TIFFDirEntry *dp; register TIFFDirectory *td = &tif->tif_dir; uint16 i; if (td->td_stripbytecount) _TIFFfree(td->td_stripbytecount); td->td_stripbytecount = (uint32*) CheckMalloc(tif, td->td_nstrips * sizeof (uint32), "for \"StripByteCounts\" array"); if (td->td_compression != COMPRESSION_NONE) { uint32 space = (uint32)(sizeof (TIFFHeader) + sizeof (uint16) + (dircount * sizeof (TIFFDirEntry)) + sizeof (uint32)); toff_t filesize = TIFFGetFileSize(tif); uint16 n; /* calculate amount of space used by indirect values */ for (dp = dir, n = dircount; n > 0; n--, dp++) { uint32 cc = dp->tdir_count*tiffDataWidth[dp->tdir_type]; if (cc > sizeof (uint32)) space += cc; } space = filesize - space; if (td->td_planarconfig == PLANARCONFIG_SEPARATE) space /= td->td_samplesperpixel; for (i = 0; i < td->td_nstrips; i++) td->td_stripbytecount[i] = space; /* * This gross hack handles the case were the offset to * the last strip is past the place where we think the strip * should begin. Since a strip of data must be contiguous, * it's safe to assume that we've overestimated the amount * of data in the strip and trim this number back accordingly. */ i--; if (((toff_t)(td->td_stripoffset[i]+td->td_stripbytecount[i])) > filesize) td->td_stripbytecount[i] = filesize - td->td_stripoffset[i]; } else { uint32 rowbytes = TIFFScanlineSize(tif); uint32 rowsperstrip = td->td_imagelength/td->td_stripsperimage; for (i = 0; i < td->td_nstrips; i++) td->td_stripbytecount[i] = rowbytes*rowsperstrip; } TIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS); if (!TIFFFieldSet(tif, FIELD_ROWSPERSTRIP)) td->td_rowsperstrip = td->td_imagelength; }
void *tif_info(char **argv) { int dircount = 0; int bound; info = (struct tiff_info *)malloc(sizeof(struct tiff_info)); tif = TIFFOpen(argv[0], "r"); if(tif == NULL){ fprintf(stderr, "ERROR: Could not open input image!\n"); exit(1); } printf("-- opening tiff file...\n"); do { dircount++; } while (TIFFReadDirectory(tif)); printf("-- %d images in %s\n", dircount, argv[0]); /* global variable */ image_info->page_num = dircount; // input image paramters TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &info->length); TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &info->width); TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &info->bps); TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &info->spp); TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &info->photo_metric); TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &info->config); /* global variablei, buffer length and buffer width will be determined on server side */ image_info->image_size = info->length * info->width; image_info->length = info->length; image_info->width = info->width; printf("-- length = %d, width = %d\n", info->length, info->width); printf("-- bit per sample = %d\n", info->bps); printf("-- sample per pixel = %d\n", info->spp); printf("-- photo metirc = %d\n", info->photo_metric); printf("-- planar config = %d\n", info->config); info->line_size = TIFFScanlineSize(tif); info->image_size = info->line_size * info->length; if(info->spp != 1){ info->spp = 1; printf("-- Warnning:sample per pixel value automatically set to 1!\n"); } return (void *)info; }
klRasterBufferPointer read_tiff (const char* filename) { TIFF* tif = TIFFOpen(filename, "r"); if (tif) { tsize_t scanline; unsigned char* buf; unsigned int width; unsigned int height; unsigned int bpp; TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height); TIFFGetField(tif, TIFFTAG_IMAGEWIDTH,&width); TIFFGetField(tif,TIFFTAG_BITSPERSAMPLE,&bpp); scanline = TIFFScanlineSize(tif); unsigned int widthTemp = scanline /3 ; buf =(unsigned char*) _TIFFmalloc(scanline); unsigned int inbands = 3; klRasterBufferPointer rbp= new klPackedHeapRasterBuffer<unsigned char>(width,height,inbands); size_t outbands=rbp->numBands(); size_t outwidth =rbp->width(); size_t outheight=rbp->height(); size_t outbandStride=rbp->bandStride(); size_t outxStride =rbp->xStride(); size_t outyStride= rbp->yStride(); unsigned char* outbuf=rbp->buffer(); unsigned int row; unsigned int col; for (row = 0; row < height; row++) { TIFFReadScanline(tif, buf, row); memcpy(outbuf+scanline*row ,buf, outyStride); } _TIFFfree(buf); TIFFClose(tif); return rbp; } else { return NULL; } }
void convert_tiff_to_webp_gray(const char* file_name, const char* out_file_name) { TIFF* tif = TIFFOpen(file_name, "r"); if (tif) { uint32 w, h; TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &w); TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h); uint32 imagelength; unsigned char gray_value; TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &imagelength); tsize_t scanline = TIFFScanlineSize(tif); unsigned char *buf = (unsigned char*)_TIFFmalloc(scanline); unsigned char *result = (unsigned char*)malloc(3 * w * h + 1); int iterator = 0; for (uint32 row = 0; row < h; ++row) { TIFFReadScanline(tif, buf, row); for (uint32 col = 0; col < w * 3; col+=3) { gray_value = (unsigned char)(((float)buf[col] + (float)buf[col + 1] + (float)buf[col + 2]) / 3.0 + 0.5); result[iterator] = gray_value; result[iterator + 1] = gray_value; result[iterator + 2] = gray_value; iterator += 3; } } uint8_t* output; FILE *op_file; size_t d_size; d_size = WebPEncodeRGB(result, w, h, 3 * w, 100.0, &output); op_file=fopen(out_file_name,"wb"); fwrite(output,1,(int)d_size, op_file); free(result); _TIFFfree(buf); TIFFClose(tif); } else { printf("I can't open the TIF file\n"); } }
int R2Image:: WriteTIFF(const char *filename) const { #ifndef OMIT_TIFF // Open TIFF file TIFF *out = TIFFOpen(filename, "w"); if (!out) { fprintf(stderr, "Unable to open TIFF file %s\n", filename); return 0; } // Set TIFF parameters TIFFSetField(out, TIFFTAG_IMAGEWIDTH, width); TIFFSetField(out, TIFFTAG_IMAGELENGTH, height); TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_BOTLEFT); TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 3); TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 8); TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB); TIFFSetField(out, TIFFTAG_COMPRESSION, COMPRESSION_NONE); TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, 1); // Allocate data for scan lines int scanline_size = TIFFScanlineSize(out); unsigned char *buf = (unsigned char *)_TIFFmalloc(scanline_size); if (!buf) { fprintf(stderr, "Unable to allocate memory for TIFF scan lines\n"); return 0; } // Write scan lines to TIFF file for (int row = 0; row < height; row++) { const unsigned char *p = Pixels(row); if (TIFFWriteScanline(out, (tdata_t) p, height - row - 1, 0) < 0) { fprintf(stderr, "Unable to write scanline to TIFF image %s\n", filename); return 0; } } // Free data for scan lines _TIFFfree(buf); // Close TIFF file TIFFClose(out); // Return number of bytes written return 1; #else RNFail("TIFF not supported"); return 0; #endif }
static int PackBitsPreEncode(TIFF* tif, tsample_t s) { (void) s; /* * Calculate the scanline/tile-width size in bytes. */ if (isTiled(tif)) tif->tif_data = (tidata_t) TIFFTileRowSize(tif); else tif->tif_data = (tidata_t) TIFFScanlineSize(tif); return (1); }
/* * Compute the # bytes in a variable height, row-aligned strip. */ tsize_t TIFFVStripSize(TIFF* tif, uint32 nrows) { TIFFDirectory *td = &tif->tif_dir; if (nrows == (uint32) -1) nrows = td->td_imagelength; if (td->td_planarconfig == PLANARCONFIG_CONTIG && td->td_photometric == PHOTOMETRIC_YCBCR && !isUpSampled(tif)) { /* * Packed YCbCr data contain one Cb+Cr for every * HorizontalSampling*VerticalSampling Y values. * Must also roundup width and height when calculating * since images that are not a multiple of the * horizontal/vertical subsampling area include * YCbCr data for the extended image. */ uint16 ycbcrsubsampling[2]; tsize_t w, scanline, samplingarea; ycbcrsubsampling[0] = 0; ycbcrsubsampling[1] = 0; TIFFGetField( tif, TIFFTAG_YCBCRSUBSAMPLING, ycbcrsubsampling + 0, ycbcrsubsampling + 1 ); /* make sure we dont get division by 0 due to bad tiffs */ if (!ycbcrsubsampling[0]) ycbcrsubsampling[0] = 1; if (!ycbcrsubsampling[1]) ycbcrsubsampling[1] = 1; samplingarea = ycbcrsubsampling[0]*ycbcrsubsampling[1]; if (samplingarea == 0) { _TIFFError(tif, tif->tif_name, "Invalid YCbCr subsampling"); return 0; } w = TIFFroundup(td->td_imagewidth, ycbcrsubsampling[0]); scanline = TIFFhowmany8(multiply(tif, w, td->td_bitspersample, "TIFFVStripSize")); nrows = TIFFroundup(nrows, ycbcrsubsampling[1]); /* NB: don't need TIFFhowmany here 'cuz everything is rounded */ scanline = multiply(tif, nrows, scanline, "TIFFVStripSize"); return ((tsize_t) summarize(tif, scanline, multiply(tif, 2, scanline / samplingarea, "TIFFVStripSize"), "TIFFVStripSize")); } else return ((tsize_t) multiply(tif, nrows, TIFFScanlineSize(tif), "TIFFVStripSize")); }
/* * Get a strip-organized image with * SamplesPerPixel > 1 * PlanarConfiguration separated * We assume that all such images are RGB. */ static int gtStripSeparate(TIFFImageIter* img, void *udata, uint32 w, uint32 h) { TIFF* tif = img->tif; ImageIterTileSeparateRoutine callback = img->callback.separate; uint16 orientation; u_char *buf; u_char *r, *g, *b, *a; uint32 row, nrow; tsize_t scanline; uint32 rowsperstrip; uint32 imagewidth = img->width; tsize_t stripsize; int32 fromskew; int alpha = img->alpha; stripsize = TIFFStripSize(tif); r = buf = (u_char *)_TIFFmalloc(4*stripsize); if (buf == 0) { TIFFError(TIFFFileName(tif), "No space for tile buffer"); return (0); } g = r + stripsize; b = g + stripsize; a = b + stripsize; if (!alpha) memset(a, 0xff, stripsize); orientation = img->orientation; TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip); scanline = TIFFScanlineSize(tif); fromskew = (w < imagewidth ? imagewidth - w : 0); for (row = 0; row < h; row += rowsperstrip) { nrow = (row + rowsperstrip > h ? h - row : rowsperstrip); if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, row, 0), r, nrow*scanline) < 0 && img->stoponerr) break; if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, row, 1), g, nrow*scanline) < 0 && img->stoponerr) break; if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, row, 2), b, nrow*scanline) < 0 && img->stoponerr) break; if (alpha && (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, row, 3), a, nrow*scanline) < 0 && img->stoponerr)) break; (*callback)(img, udata, 0, row, w, nrow, fromskew, r, g, b, a); } _TIFFfree(buf); return (1); }
/* * read tiff, assumes tiff file has 3 channels per pixel, RGB, * with 8-bit channels * * returns a malloced uint8_t* that needs to be freed, or NULL if failed */ uint8_t *read_tiff8(char *filename, int *width, int *height) { int i; TIFF *tif; uint8_t *result; int scanline_size; if ((tif = TIFFOpen(filename, "r")) == NULL) { fprintf(stderr, "read_tiff: could not open %s\n", filename); fflush(stderr); goto out_no_open; } TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, width); TIFFGetField(tif, TIFFTAG_IMAGELENGTH, height); scanline_size = TIFFScanlineSize(tif); if (scanline_size != 3 * (*width) * sizeof(*result)) { fprintf(stderr, "read_tiff: %s is not in correct format. TIFF file should have 8-bit channels in RGBRGB format.\n", filename); fflush(stderr); goto out_wrong_format; } result = malloc((*height) * scanline_size); if (result == NULL) goto out_nomem; for (i = 0; i < (*height); i++) { if (TIFFReadScanline(tif, result + 3 * (*width) * i, i, 0) == -1) { fprintf(stderr, "read_tiff: error in reading %s row %d\n", filename, i); fflush(stderr); goto out_read_err; } } TIFFClose(tif); return result; out_read_err: free(result); out_nomem: out_wrong_format: TIFFClose(tif); out_no_open: return NULL; }
/* * Get a strip-organized image that has * PlanarConfiguration contiguous if SamplesPerPixel > 1 * or * SamplesPerPixel == 1 * * Hacked from the tif_getimage.c file. * * This is set up to allow us to just copy the data to the raster * for 1-bit bitmaps */ static int getStripContig1Bit(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h) { TIFF* tif = img->tif; tileContigRoutine put = img->put.contig; uint16 orientation; uint32 row, y, nrow, rowstoread; uint32 pos; u_char* buf; uint32 rowsperstrip; uint32 imagewidth = img->width; tsize_t scanline; int32 fromskew, toskew; tstrip_t strip; tsize_t stripsize; u_char* braster = (u_char*)raster; // byte wide raster uint32 wb = WIDTHBYTES(w); int ret = 1; buf = (u_char*) _TIFFmalloc(TIFFStripSize(tif)); if (buf == 0) { TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "No space for strip buffer"); return (0); } y = setorientation(img, h); orientation = img->orientation; toskew = -(int32) (orientation == ORIENTATION_TOPLEFT ? wb+wb : wb-wb); TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip); scanline = TIFFScanlineSize(tif); fromskew = (w < imagewidth ? imagewidth - w : 0)/8; for (row = 0; row < h; row += nrow) { rowstoread = rowsperstrip - (row + img->row_offset) % rowsperstrip; nrow = (row + rowstoread > h ? h - row : rowstoread); strip = TIFFComputeStrip(tif,row+img->row_offset, 0); stripsize = ((row + img->row_offset)%rowsperstrip + nrow) * scanline; if (TIFFReadEncodedStrip(tif, strip, buf, stripsize ) < 0 && img->stoponerr) { ret = 0; break; } pos = ((row + img->row_offset) % rowsperstrip) * scanline; (*put)(img, (uint32*)(braster+y*wb), 0, y, w, nrow, fromskew, toskew, buf + pos); y += (orientation == ORIENTATION_TOPLEFT ?-(int32) nrow : (int32) nrow); } _TIFFfree(buf); return (ret); }