static int read_tiff_greyscale_scanline (TIFF *tiff, tiff_format_t format, tiff_data_config_t *data_config, uint32 row, uint32 scanlineSize, int sample_count, int band, tdata_t *tif_buf) { switch (format) { case SCANLINE_TIFF: if (data_config->planar_config == PLANARCONFIG_CONTIG) { TIFFReadScanline(tiff, tif_buf, row, 0); } else { TIFFReadScanline(tiff, tif_buf, row, band); } break; case STRIP_TIFF: ReadScanline_from_TIFF_Strip(tiff, tif_buf, row, band); break; case TILED_TIFF: ReadScanline_from_TIFF_TileRow(tiff, tif_buf, row, band); break; default: // This code should never execute return FALSE; break; } return TRUE; }
static void tiff_read(unsigned char *dst, unsigned int line, void *data) { struct tiff_state *h = data; int s,on,off; if (h->image) { /* loaded whole image using TIFFReadRGBAImage() */ uint32 *row = h->image + h->width * (h->height - line -1); load_rgba(dst,(unsigned char*)row,h->width); return; } if (h->config == PLANARCONFIG_CONTIG) { TIFFReadScanline(h->tif, h->row, line, 0); } else if (h->config == PLANARCONFIG_SEPARATE) { for (s = 0; s < h->nsamples; s++) TIFFReadScanline(h->tif, h->row, line, s); } switch (h->nsamples) { case 1: if (1 == h->depth) { /* black/white */ on = 0, off = 0; if (PHOTOMETRIC_MINISWHITE == h->photometric) on = 0, off = 255; if (PHOTOMETRIC_MINISBLACK == h->photometric) on = 255, off = 0; #if 0 /* Huh? Does TIFFReadScanline handle this already ??? */ if (FILLORDER_MSB2LSB == h->fillorder) load_bits_msb(dst,(unsigned char*)(h->row),h->width,on,off); else load_bits_lsb(dst,(unsigned char*)(h->row),h->width,on,off); #else load_bits_msb(dst,(unsigned char*)(h->row),h->width,on,off); #endif } else { /* grayscaled */ load_gray(dst,(unsigned char*)(h->row),h->width); } break; case 3: /* rgb */ memcpy(dst,h->row,3*h->width); break; case 4: /* rgb+alpha */ load_rgba(dst,(unsigned char*)(h->row),h->width); break; } }
static int read_tiff_rgb_scanline (TIFF *tiff, tiff_format_t format, tiff_data_config_t *data_config, uint32 row, uint32 scanlineSize, int sample_count, int band_r, int band_g, int band_b, tdata_t *rtif_buf, tdata_t *gtif_buf, tdata_t *btif_buf) { // NOTE: num_bands may not be greater than 1 ...open_tiff_data() decides what to // assign into band_r, band_g, and band_b. They may all be the same, all different, // or some combination depending on what the GUI asked for versus what was available // in the TIFF file. All code called after open_tiff_data() should assume that if // RGB is desired that the 3 band assignments are taken care of appropriate to the // situation. int num_bands = data_config->samples_per_pixel; if (num_bands < 1 || band_r < 0 || band_r > num_bands - 1 || band_g < 0 || band_g > num_bands - 1 || band_b < 0 || band_b > num_bands - 1) { return FALSE; } switch (format) { case SCANLINE_TIFF: if (data_config->planar_config == PLANARCONFIG_CONTIG) { ReadScanline_from_ContiguousRGB_TIFF(tiff, row, sample_count, band_r, band_g, band_b, rtif_buf, gtif_buf, btif_buf); } else { TIFFReadScanline(tiff, rtif_buf, row, band_r); // Red band TIFFReadScanline(tiff, gtif_buf, row, band_g); // Green band TIFFReadScanline(tiff, btif_buf, row, band_b); // Blue band } break; case STRIP_TIFF: ReadScanline_from_TIFF_Strip(tiff, rtif_buf, row, band_r); // Red band ReadScanline_from_TIFF_Strip(tiff, gtif_buf, row, band_g); // Green band ReadScanline_from_TIFF_Strip(tiff, btif_buf, row, band_b); // Blue band break; case TILED_TIFF: ReadScanline_from_TIFF_TileRow(tiff, rtif_buf, row, band_r); // Red band ReadScanline_from_TIFF_TileRow(tiff, gtif_buf, row, band_g); // Green band ReadScanline_from_TIFF_TileRow(tiff, btif_buf, row, band_b); // Blue band break; default: // This code should never execute return FALSE; break; } return TRUE; }
void sph_task::load_texture(TIFF *T, uint32 w, uint32 h, uint16 c, uint16 b) { // Confirm the page format. uint32 W, H; uint16 C, B; TIFFGetField(T, TIFFTAG_IMAGEWIDTH, &W); TIFFGetField(T, TIFFTAG_IMAGELENGTH, &H); TIFFGetField(T, TIFFTAG_BITSPERSAMPLE, &B); TIFFGetField(T, TIFFTAG_SAMPLESPERPIXEL, &C); if (W == w && H == h && B == b && C == c) { // Pad a 24-bit image to 32-bit BGRA. if (c == 3 && b == 8) { if (void *q = malloc(TIFFScanlineSize(T))) { const uint32 S = w * 4 * b / 8; for (uint32 r = 0; r < h; ++r) { TIFFReadScanline(T, q, r, 0); for (int j = w - 1; j >= 0; --j) { uint8 *s = (uint8 *) q + j * c * b / 8; uint8 *d = (uint8 *) p + r * S + j * 4 * b / 8; d[0] = s[2]; d[1] = s[1]; d[2] = s[0]; d[3] = 0xFF; } } free(q); } } else { const uint32 S = (uint32) TIFFScanlineSize(T); for (uint32 r = 0; r < h; ++r) TIFFReadScanline(T, (uint8 *) p + r * S, r, 0); } } }
/* tif load function */ int tif_load(uint16 *image) { int r, c; // height index, width index uint16 s; uint16 *scanline; long int image_offset; if((scanline = (uint16 *)_TIFFmalloc(info->line_size)) == NULL){ fprintf(stderr, "Could not allocate enough memory for the scan buffer!\n"); exit(42); } image_offset = 0; printf("-- loading tif files ... \n"); TIFFSetDirectory(tif, 0); do { if (info->config == PLANARCONFIG_CONTIG){ for(r = 0; r < info->length; r++){ TIFFReadScanline(tif, scanline, r, s); for(c = 0; c < info->width; c++) { image[image_offset + info->width * r + c] = *(scanline + c); } } } else if (info->config == PLANARCONFIG_SEPARATE){ for(s = 0; s < info->spp; s++){ for(r = 0; r < info->length; r++){ TIFFReadScanline(tif, scanline, r, s); for(c = 0; c < info->width; c++) { image[image_offset + info->width * r + c] = *(scanline + c); } } } } image_offset += info->image_size/sizeof(uint16); } while (TIFFReadDirectory(tif)); _TIFFfree(scanline); TIFFClose(tif); return 0; }
static void read_data (UfoTiffReaderPrivate *priv, UfoBuffer *buffer, UfoRequisition *requisition, guint16 bits, guint roi_y, guint roi_height, guint roi_step) { gchar *dst; gsize step; gsize offset; step = requisition->dims[0] * bits / 8; dst = (gchar *) ufo_buffer_get_host_array (buffer, NULL); offset = 0; if (requisition->n_dims == 3) { /* RGB data */ gchar *src; gsize plane_size; plane_size = step * roi_height / roi_step; src = g_new0 (gchar, step * 3); for (guint i = roi_y; i < roi_y + roi_height; i += roi_step) { guint xd = 0; guint xs = 0; TIFFReadScanline (priv->tiff, src, i, 0); for (; xd < requisition->dims[0]; xd += 1, xs += 3) { dst[offset + xd] = src[xs]; dst[offset + plane_size + xd] = src[xs + 1]; dst[offset + 2 * plane_size + xd] = src[xs + 2]; } offset += step; } g_free (src); } else { for (guint i = roi_y; i < roi_y + roi_height; i += roi_step) { TIFFReadScanline (priv->tiff, dst + offset, i, 0); offset += step; } } }
static inline int _read_planar_16(tiff_t *t) { for(uint32_t row = 0; row < t->height; row++) { uint16_t *in = ((uint16_t *)t->buf); float *out = ((float *)t->mipbuf) + (size_t)4 * row * t->width; /* read scanline */ if(TIFFReadScanline(t->tiff, in, row, 0) == -1) return -1; for(uint32_t i = 0; i < t->width; i++, in += t->spp, out += 4) { out[0] = ((float)in[0]) * (1.0f / 65535.0f); if(t->spp == 1) { out[1] = out[2] = out[0]; } else { out[1] = ((float)in[1]) * (1.0f / 65535.0f); out[2] = ((float)in[2]) * (1.0f / 65535.0f); } out[3] = 0; } } return 1; }
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); } }
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); }
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); }
_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 *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; }
/* スキャンラインフォーマット画像の、 1スキャンライン分のみ、読み込む なお、 Compression algorithm does not support random access. つまり、圧縮フォーマットの場合は、 ゼロライン(yy=0)から順にとらないと読み込めない vp_scanlineは読み込み始める場所へのポインター */ int tif_read_scanline( TIF_IMAGE_RW *tp_read, uint32_t yy, void *vp_scanline ) { if (NULL == tp_read ) { pri_funct_err_bttvr( "Error : bad argument, tp_read is NULL." ); return NG; } if (NULL == tp_read->tp_tiff_head ) { pri_funct_err_bttvr( "Error : bad argument, tp_read->tp_tiff_head is NULL." ); return NG; } if (NULL == vp_scanline ) { pri_funct_err_bttvr( "Error : bad argument, vp_scanline is NULL." ); return NG; } if ( -1 == TIFFReadScanline( tp_read->tp_tiff_head, /*(tdata_t)*/vp_scanline, yy, (tsample_t)0 ) ) { pri_funct_err_bttvr( "Error : TIFFReadScanline(%p,%p,%u,0) returns minus.", tp_read->tp_tiff_head, vp_scanline, yy ); return NG; } return OK; }
// ----------------------------------------------------------------------------- // // ----------------------------------------------------------------------------- int TiffUtilities::read16BitImage(TIFF* in, TiffImage* data) { int err = 0; int bytesPerScanLine = TIFFScanlineSize(in); // Allocate a buffer to read each scan line into tdata_t scanBuffer = (unsigned char*)(malloc(bytesPerScanLine)); // Allocate the image buffer data->imageData = malloc(data->width * data->height * 2); unsigned short* dest = static_cast<unsigned short*>(data->imageData); //Loop over each row (scanline) and read the data, then copy the data into the // image buffer. We do it this way because of alignment issues that crop up in // some tiff images. If the scanline size is an odd number tiff will round up // to the next multiple of 4 bytes. for (int i = 0; i < data->height; ++i) { err = TIFFReadScanline(in, scanBuffer, i); ::memcpy(dest, scanBuffer, bytesPerScanLine); dest = dest + data->width; } // Free the memory that was allocated free(scanBuffer); return err; }
/** * Read a TIFF float image. */ static float *readTIFF(TIFF * tif, size_t * nx, size_t * ny) { uint32 w = 0, h = 0, i; uint16 spp = 0, bps = 0, fmt = 0; float *data, *line; TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &w); TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h); TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &spp); TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &bps); TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &fmt); if (spp != 1 || bps != (uint16) sizeof(float) * 8 || fmt != SAMPLEFORMAT_IEEEFP) return NULL; assert((size_t) TIFFScanlineSize(tif) == w * sizeof(float)); data = (float *) malloc(w * h * sizeof(float)); *nx = (size_t) w; *ny = (size_t) h; for (i = 0; i < h; i++) { line = data + i * w; if (TIFFReadScanline(tif, line, i, 0) < 0) { fprintf(stderr, "readTIFF: error reading row %u\n", i); free(data); return NULL; } } return data; }
static int cpData(TIFF *in, TIFF *out, const uint32_t nrow) { tsize_t scSize = TIFFScanlineSize(in); tdata_t buf = _TIFFmalloc(scSize); if (!buf) return 0; _TIFFmemset(buf, 0, scSize); for (uint32_t irow = 0; irow < nrow; irow++) { try { if (TIFFReadScanline(in, buf, irow, 0) < 0) { throw -1; } if (TIFFWriteScanline(out, buf, irow, 0) < 0) { throw -2; } } catch (int err) { if (err == -1) { std::cerr << "Cannot read scanline " << irow; std::cerr << " from " << TIFFFileName(in) << std::endl; } else if (err == -2) { std::cerr << "Cannot write scanline " << irow; std::cerr << " to " << TIFFFileName(out) << std::endl; } else { std::cerr << "Unknown error during row copy" << std::endl; } _TIFFfree(buf); return 0; } } _TIFFfree(buf); return 1; }
static void read_64_bit_data (UfoTiffReaderPrivate *priv, UfoBuffer *buffer, UfoRequisition *requisition, guint roi_y, guint roi_height, guint roi_step) { gdouble *src; gfloat *dst; dst = ufo_buffer_get_host_array (buffer, NULL); src = g_new0 (gdouble, requisition->dims[0]); for (guint i = roi_y; i < roi_y + roi_height; i += roi_step) { TIFFReadScanline (priv->tiff, src, i, 0); for (guint j = 0; j < requisition->dims[0]; j++) dst[j] = (gfloat) src[j]; dst += requisition->dims[0]; } g_free (src); }
static bool scantoimg(img *d, TIFF *T, bool c, bool e) { int n = 1 << (d->n - e); int m = 1 << d->m; int r = 0; float *p; if ((p = (float *) malloc(TIFFScanlineSize(T)))) { for (r = 0; r < n; r++) { if (TIFFReadScanline(T, p, r, 0) == -1) return false; if (c) linetoimgz(r, d, p); else linetoimgr(r, d, p); if (e) { reverse(p, c ? d->p * 2 : d->p, m); if (c) linetoimgz(2 * n - r - 1, d, p); else linetoimgr(2 * n - r - 1, d, p); } } free(p); } return (r == n); }
bool Image_TIFF::read_gs() { if (!tiff_handle) return false; void * buff; buff = _TIFFmalloc(TIFFScanlineSize(tiff_handle)); for (int y = 0, idx=0; y < _height; ++y) { TIFFReadScanline(tiff_handle, buff, y); switch (_depth) { case BIT8: for (int x=0; x<_width; ++x, ++idx) { gs_data_8[idx] = ((uint8*) buff)[x]; } break; case BIT16: for (int x=0; x<_width; ++x, ++idx) { gs_data_16[idx] = ((uint16*) buff)[x]; } break; case BIT32: for (int x=0; x<_width; ++x, ++idx) { gs_data_32[idx] = ((uint32*) buff)[x]; } break; } } _TIFFfree(buff); return true; }
std::auto_ptr<image_list> TIFFFormat::do_read(byte_source* src, ImageFactory* factory, bool is_multi) { tiff_warn_error twe; tif_holder t = read_client(src); std::auto_ptr<image_list> images(new image_list); do { const uint32 h = tiff_get<uint32>(t, TIFFTAG_IMAGELENGTH); const uint32 w = tiff_get<uint32>(t, TIFFTAG_IMAGEWIDTH); const uint16 nr_samples = tiff_get<uint16>(t, TIFFTAG_SAMPLESPERPIXEL); const uint16 bits_per_sample = tiff_get<uint16>(t, TIFFTAG_BITSPERSAMPLE); const int depth = nr_samples > 1 ? nr_samples : -1; std::auto_ptr<Image> output = factory->create(bits_per_sample, h, w, depth); if (ImageWithMetadata* metaout = dynamic_cast<ImageWithMetadata*>(output.get())) { std::string description = tiff_get<std::string>(t, TIFFTAG_IMAGEDESCRIPTION, ""); metaout->set_meta(description); } for (uint32 r = 0; r != h; ++r) { if(TIFFReadScanline(t.tif, output->rowp_as<byte>(r), r) == -1) { throw CannotReadError("imread.imread._tiff: Error reading scanline"); } } images->push_back(output); } while (is_multi && TIFFReadDirectory(t.tif)); return images; }
void PSDataColorSeparate(FILE* fd, TIFF* tif, uint32 w, uint32 h, int nc) { uint32 row; int breaklen = MAXLINE, cc, s, maxs; unsigned char *tf_buf; unsigned char *cp, c; (void) w; tf_buf = (unsigned char *) _TIFFmalloc(tf_bytesperrow); if (tf_buf == NULL) { TIFFError(filename, "No space for scanline buffer"); return; } maxs = (samplesperpixel > nc ? nc : samplesperpixel); for (row = 0; row < h; row++) { for (s = 0; s < maxs; s++) { if (TIFFReadScanline(tif, tf_buf, row, s) < 0) break; for (cp = tf_buf, cc = 0; cc < tf_bytesperrow; cc++) { DOBREAK(breaklen, 1, fd); c = *cp++; PUTHEX(c,fd); } } } _TIFFfree((char *) tf_buf); }
void PSDataColorContig(FILE* fd, TIFF* tif, uint32 w, uint32 h, int nc) { uint32 row; int breaklen = MAXLINE, cc, es = samplesperpixel - nc; unsigned char *tf_buf; unsigned char *cp, c; (void) w; tf_buf = (unsigned char *) _TIFFmalloc(tf_bytesperrow); if (tf_buf == NULL) { TIFFError(filename, "No space for scanline buffer"); return; } for (row = 0; row < h; row++) { if (TIFFReadScanline(tif, tf_buf, row, 0) < 0) break; cp = tf_buf; if (alpha) { int adjust; cc = 0; for (; cc < tf_bytesperrow; cc += samplesperpixel) { DOBREAK(breaklen, nc, fd); /* * For images with alpha, matte against * a white background; i.e. * Cback * (1 - Aimage) * where Cback = 1. */ adjust = 255 - cp[nc]; switch (nc) { case 4: c = *cp++ + adjust; PUTHEX(c,fd); case 3: c = *cp++ + adjust; PUTHEX(c,fd); case 2: c = *cp++ + adjust; PUTHEX(c,fd); case 1: c = *cp++ + adjust; PUTHEX(c,fd); } cp += es; } } else { cc = 0; for (; cc < tf_bytesperrow; cc += samplesperpixel) { DOBREAK(breaklen, nc, fd); switch (nc) { case 4: c = *cp++; PUTHEX(c,fd); case 3: c = *cp++; PUTHEX(c,fd); case 2: c = *cp++; PUTHEX(c,fd); case 1: c = *cp++; PUTHEX(c,fd); } cp += es; } } } _TIFFfree((char *) tf_buf); }
void read_scaline( Buffer& buffer , std::ptrdiff_t row , tsample_t plane ) { io_error_if( TIFFReadScanline( _tiff_file.get() , reinterpret_cast< tdata_t >( &buffer.front() ) , (uint32) row , plane ) == -1 , "Read error." ); }
int main(int argc, char* argv[]) { sf_file out; FILE *tiffin; TIFF *tiffout; char *tiffname, buf[BUFSIZ]; unsigned char *grey; int n1, i2, n2, nbuf; short nc; sf_init(argc,argv); out = sf_output("out"); sf_settype(out,SF_UCHAR); fclose(sf_tempfile(&tiffname,"w")); tiffin = fopen(tiffname,"wb"); while (1) { nbuf = fread(buf,1,BUFSIZ,stdin); if (nbuf <= 0) break; fwrite(buf,1,nbuf,tiffin); } fclose(tiffin); tiffout = TIFFOpen(tiffname,"rb"); /* Find the width and height of the image */ TIFFGetField(tiffout, TIFFTAG_IMAGEWIDTH, &n1); TIFFGetField(tiffout, TIFFTAG_IMAGELENGTH, &n2); TIFFGetField(tiffout,TIFFTAG_SAMPLESPERPIXEL,&nc); if (1==nc) { sf_putint(out,"n1",n1); sf_putint(out,"n2",n2); } else { sf_putint(out,"n1",nc); sf_putint(out,"n2",n1); sf_putint(out,"n3",n2); } grey = sf_ucharalloc(n1*nc); for (i2 = 0; i2 < n2; i2++) { if (TIFFReadScanline(tiffout, grey, i2, 0) < 0) sf_error("Trouble reading TIFF file"); sf_ucharwrite(grey,n1*nc,out); } TIFFClose(tiffout); unlink(tiffname); exit(0); }
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"); } }
oz::cpu_image tiff_read( const std::string& path ) { TIFF *in = 0; try { in = TIFFOpen(path.c_str(), "r"); if (!in) OZ_X() << "Opening file '" << path << "' failed!"; int w,h; if (TIFFGetField(in, TIFFTAG_IMAGEWIDTH, &w) != 1) OZ_X() << "Invalid format!"; if (TIFFGetField(in, TIFFTAG_IMAGELENGTH,&h) != 1) OZ_X() << "Invalid format!"; uint16 bps, photometric, spp; if (TIFFGetField(in, TIFFTAG_BITSPERSAMPLE, &bps) !=1) OZ_X() << "Unsupported format!"; if (TIFFGetField(in, TIFFTAG_PHOTOMETRIC, &photometric) !=1) OZ_X() << "Unsupported format!"; if (TIFFGetField(in, TIFFTAG_SAMPLESPERPIXEL, &spp) !=1) OZ_X() << "Unsupported format!"; /* uint32 profile_size; unsigned char *profile_data; if (TIFFGetField(in, TIFFTAG_ICCPROFILE, &profile_size, &profile_data)) { FILE *f = fopen("C:/Users/jkyprian/Desktop/profile.h", "wt+"); fprintf(f, "%d\n", profile_size); for (unsigned i = 0; i < profile_size; ++i) { if (i % 16 == 0) fprintf(f, "\n"); fprintf(f, "0x%02x, ", profile_data[i]); } fclose(f); } */ //TIFFGetField(in, TIFFTAG_EXTRASAMPLES, &xs, &xs_ptr); //xs=0; //uint16* xs_ptr=0; //if (xs != 1) break; if ((bps != 32) || (photometric != PHOTOMETRIC_RGB) || (spp != 4)) OZ_X() << "Unsupported format!"; //tmsize_t scanlineSize = TIFFScanlineSize(in); oz::cpu_image dst(w, h, oz::FMT_FLOAT4); int j; for (j = 0; j < h; ++j) { if (TIFFReadScanline(in, dst.scan_line<float4>(j), j) !=1 ) break; } if (j != h) OZ_X() << "Reading scan line failed!"; return dst; } catch (std::exception&) { if (in) TIFFClose(in); throw; } }
int TIFFReadFloatScanline(TIFF *T, float *dst, uint32 r) { static tdata_t *src = NULL; static tsize_t len = 0; uint32 w = 0; uint16 c = 0; uint16 b = 0; uint16 s = 0; uint16 p = 0; TIFFGetField(T, TIFFTAG_IMAGEWIDTH, &w); TIFFGetField(T, TIFFTAG_SAMPLESPERPIXEL, &c); TIFFGetField(T, TIFFTAG_BITSPERSAMPLE, &b); TIFFGetField(T, TIFFTAG_SAMPLEFORMAT, &s); TIFFGetField(T, TIFFTAG_PLANARCONFIG, &p); if (p == PLANARCONFIG_CONTIG) { if (len != TIFFScanlineSize(T)) { len = TIFFScanlineSize(T); src = realloc(src, len); } if (src && TIFFReadScanline(T, src, r, 0) > 0) { if ((b == 8) && (s == SAMPLEFORMAT_UINT || s == 0)) for (uint32 i = 0; i < w * c; i++) dst[i] = ((uint8 *) src)[i] / 255.0f; else if ((b == 8) && (s == SAMPLEFORMAT_INT)) for (uint32 i = 0; i < w * c; i++) dst[i] = ((int8 *) src)[i] / 127.0f; else if ((b == 16) && (s == SAMPLEFORMAT_UINT || s == 0)) for (uint32 i = 0; i < w * c; i++) dst[i] = ((uint16 *) src)[i] / 65535.0f; else if ((b == 16) && (s == SAMPLEFORMAT_INT)) for (uint32 i = 0; i < w * c; i++) dst[i] = ((int16 *) src)[i] / 32767.0f; else if ((b == 32) && (s == SAMPLEFORMAT_IEEEFP)) for (uint32 i = 0; i < w * c; i++) dst[i] = ((float *) src)[i]; else return -1; } } 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; }
// // DESCRIPTION: // Load the image into system memory (MImage) /////////////////////////////////////////////////////// MStatus tiffFloatReader::load( MImage& image, unsigned int imageNumber) { MStatus rval = MS::kFailure; #if defined(_TIFF_LIBRARY_AVAILABLE_) if (!fInputFile) return rval; // Configure our Maya image to hold the result image.create( fWidth, fHeight, fChannels, MImage::kFloat); float* outputBuffer = image.floatPixels(); if (outputBuffer == NULL) return rval; // Maya expects images upside down unsigned int row = 0; bool flipVertically = true; if (flipVertically) { outputBuffer += (fHeight-1) * (fWidth * fChannels); for (row = 0; row < fHeight; row++) { TIFFReadScanline (fInputFile, outputBuffer, row); outputBuffer -= (fWidth * fChannels); } } else { for (row = 0; row < fHeight; row++) { TIFFReadScanline (fInputFile, outputBuffer, row); outputBuffer += (fWidth * fChannels); } } rval = MS::kSuccess; #endif return rval; }