static int cpStrips(TIFF* in, TIFF* out) { tmsize_t bufsize = TIFFStripSize(in); unsigned char *buf = (unsigned char *)_TIFFmalloc(bufsize); if (buf) { tstrip_t s, ns = TIFFNumberOfStrips(in); uint64 *bytecounts; if (!TIFFGetField(in, TIFFTAG_STRIPBYTECOUNTS, &bytecounts)) { fprintf(stderr, "tiffsplit: strip byte counts are missing\n"); return (0); } for (s = 0; s < ns; s++) { if (bytecounts[s] > (uint64)bufsize) { buf = (unsigned char *)_TIFFrealloc(buf, (tmsize_t)bytecounts[s]); if (!buf) return (0); bufsize = (tmsize_t)bytecounts[s]; } if (TIFFReadRawStrip(in, s, buf, (tmsize_t)bytecounts[s]) < 0 || TIFFWriteRawStrip(out, s, buf, (tmsize_t)bytecounts[s]) < 0) { _TIFFfree(buf); return (0); } } _TIFFfree(buf); return (1); } return (0); }
int write_strips(TIFF *tif, const tdata_t array, const tsize_t size) { tstrip_t strip, nstrips; tsize_t stripsize, offset; stripsize = TIFFStripSize(tif); if (!stripsize) { fprintf (stderr, "Wrong size of strip.\n"); return -1; } nstrips = TIFFNumberOfStrips(tif); for (offset = 0, strip = 0; offset < size && strip < nstrips; offset+=stripsize, strip++) { /* * Properly write last strip. */ tsize_t bufsize = size - offset; if (bufsize > stripsize) bufsize = stripsize; if (TIFFWriteEncodedStrip(tif, strip, (char *)array + offset, bufsize) != bufsize) { fprintf (stderr, "Can't write strip %lu.\n", (unsigned long)strip); return -1; } } return 0; }
static int cpStrips(TIFF* in, TIFF* out) { tsize_t bufsize = TIFFStripSize(in); unsigned char *buf = (unsigned char *)_TIFFmalloc(bufsize); if (buf) { tstrip_t s, ns = TIFFNumberOfStrips(in); uint32 *bytecounts; TIFFGetField(in, TIFFTAG_STRIPBYTECOUNTS, &bytecounts); for (s = 0; s < ns; s++) { if (bytecounts[s] > bufsize) { buf = (unsigned char *)_TIFFrealloc(buf, bytecounts[s]); if (!buf) return (0); bufsize = bytecounts[s]; } if (TIFFReadRawStrip(in, s, buf, bytecounts[s]) < 0 || TIFFWriteRawStrip(out, s, buf, bytecounts[s]) < 0) { _TIFFfree(buf); return (0); } } _TIFFfree(buf); return (1); } return (0); }
/* * Get a strip-organized image that has * PlanarConfiguration contiguous if SamplesPerPixel > 1 * or * SamplesPerPixel == 1 */ boolean TIFFRasterImpl::gtStripContig( const RGBvalue* Map, u_long h, u_long w ) { u_char* buf = new u_char[TIFFStripSize(tif_)]; if (buf == nil) { TIFFError(TIFFFileName(tif_), "No space for strip buffer"); return (false); } tileContigRoutine put = pickTileContigCase(Map); u_long y = setorientation(h); int toskew = (int)(orientation_ == ORIENTATION_TOPLEFT ? -w + -w : -w + w); u_long rowsperstrip = (u_long) -1L; TIFFGetField(tif_, TIFFTAG_ROWSPERSTRIP, &rowsperstrip); u_long imagewidth; TIFFGetField(tif_, TIFFTAG_IMAGEWIDTH, &imagewidth); int scanline = TIFFScanlineSize(tif_); int fromskew = (int)(w < imagewidth ? imagewidth - w : 0); for (u_long row = 0; row < h; row += rowsperstrip) { u_int nrow = u_int(row + rowsperstrip > h ? h - row : rowsperstrip); if (TIFFReadEncodedStrip( tif_, TIFFComputeStrip(tif_, row, 0), buf, nrow*scanline) < 0 ) { break; } (this->*put)(raster_ + y*w, buf, Map, w, nrow, fromskew, toskew); y += (orientation_ == ORIENTATION_TOPLEFT ? -nrow : nrow); } delete buf; return true; }
/* * 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) { TIFFError(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); }
static int cpStrips(TIFF* in, TIFF* out) { tsize_t bufsize = TIFFStripSize(in); unsigned char *buf = (unsigned char *)_TIFFmalloc(bufsize); if (buf) { tstrip_t s, ns = TIFFNumberOfStrips(in); uint64 *bytecounts; TIFFGetField(in, TIFFTAG_STRIPBYTECOUNTS, &bytecounts); for (s = 0; s < ns; s++) { if (bytecounts[s] > (uint64) bufsize) { buf = (unsigned char *)_TIFFrealloc(buf, (tmsize_t)bytecounts[s]); if (!buf) goto bad; bufsize = (tmsize_t)bytecounts[s]; } if (TIFFReadRawStrip(in, s, buf, (tmsize_t)bytecounts[s]) < 0 || TIFFWriteRawStrip(out, s, buf, (tmsize_t)bytecounts[s]) < 0) { _TIFFfree(buf); return 0; } } _TIFFfree(buf); return 1; } bad: TIFFError(TIFFFileName(in), "Can't allocate space for strip buffer."); return 0; }
tdata_t tif_ReadContigStripData(TIFF* tif) { tdata_t raster = tif_Malloc(tif); tsize_t result; tsize_t offset; tsize_t stripSize = TIFFStripSize(tif); tstrip_t stripMax = TIFFNumberOfStrips(tif); tstrip_t istrip; if (tif == NULL) return NULL; offset = 0; if (raster != NULL) { for (istrip = 0; istrip < stripMax; istrip++){ result = TIFFReadEncodedStrip (tif, istrip, ((char*)raster)+offset, stripSize); if (result == -1) { printf("Read error on input strip number %d\n", istrip); } offset += result; } } return raster; }
static void svRGBContig(TIFF* tif, uint32* ss, int xsize, int ysize) { register int x, y; tsize_t stripsize = TIFFStripSize(tif); unsigned char *strip = (unsigned char *)_TIFFmalloc(stripsize); for (y = 0; y <= ysize; y += rowsperstrip) { register unsigned char *pp = strip; register uint32 n; n = rowsperstrip; if (n > ysize-y+1) n = ysize-y+1; do { for (x = 0; x <= xsize; x++) { uint32 v = ss[x]; pp[0] = v; pp[1] = v >> 8; pp[2] = v >> 16; pp += 3; } ss += xsize+1; } while (--n); if (TIFFWriteEncodedStrip(tif, TIFFComputeStrip(tif,y,0), strip, stripsize) < 0) break; } _TIFFfree(strip); }
/** Writes single NDArray to the TIFF file. * \param[in] pArray Pointer to the NDArray to be written */ asynStatus NDFileTIFF::writeFile(NDArray *pArray) { unsigned long stripSize; tsize_t nwrite=0; int strip, sizeY; unsigned char *pRed, *pGreen, *pBlue; static const char *functionName = "writeFile"; asynPrint(this->pasynUserSelf, ASYN_TRACE_FLOW, "%s:%s: %lu, %lu\n", driverName, functionName, (unsigned long)pArray->dims[0].size, (unsigned long)pArray->dims[1].size); if (this->output == NULL) { asynPrint(this->pasynUserSelf, ASYN_TRACE_ERROR, "%s:%s NULL TIFF file\n", driverName, functionName); return(asynError); } stripSize = (unsigned long)TIFFStripSize(this->output); TIFFGetField(this->output, TIFFTAG_IMAGELENGTH, &sizeY); switch (this->colorMode) { case NDColorModeMono: case NDColorModeRGB1: nwrite = TIFFWriteEncodedStrip(this->output, 0, pArray->pData, stripSize); break; case NDColorModeRGB2: /* TIFF readers don't support row interleave, put all the red strips first, then all the blue, then green. */ for (strip=0; strip<sizeY; strip++) { pRed = (unsigned char *)pArray->pData + 3*strip*stripSize; pGreen = pRed + stripSize; pBlue = pRed + 2*stripSize; nwrite = TIFFWriteEncodedStrip(this->output, strip, pRed, stripSize); nwrite = TIFFWriteEncodedStrip(this->output, sizeY+strip, pBlue, stripSize); nwrite = TIFFWriteEncodedStrip(this->output, 2*sizeY+strip, pGreen, stripSize); } break; case NDColorModeRGB3: for (strip=0; strip<3; strip++) { nwrite = TIFFWriteEncodedStrip(this->output, strip, (unsigned char *)pArray->pData+stripSize*strip, stripSize); } break; default: asynPrint(this->pasynUserSelf, ASYN_TRACE_ERROR, "%s:%s: unknown color mode %d\n", driverName, functionName, this->colorMode); return(asynError); break; } if (nwrite <= 0) { asynPrint(this->pasynUserSelf, ASYN_TRACE_ERROR, "%s:%s: error writing data to file\n", driverName, functionName); return(asynError); } return(asynSuccess); }
bool CaViewerCore::openTif(ImageInfo info, int nSequence) { static const QString sProgressFormat = QObject::tr("Loading... %p%"); TIFF* tif = TIFFOpen(info.fullName(nSequence).toStdString().c_str(), "r"); quint16 bps, spp; quint32 rps; TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &bps); TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &spp); TIFFGetField(tif, TIFFTAG_ROWSPERSTRIP, &rps); const quint32 stripSize = TIFFNumberOfStrips(tif); const quint32 stripLength = TIFFStripSize(tif); int nPrevImgCount = 0; for(int count = 0; count < nSequence; ++count) nPrevImgCount += info.imageSize(count); if(bps == 16 && spp == 1) // for 16-bit mono channel tif { for(int imgCount = 0; imgCount < info.imageSize(nSequence); ++imgCount) { const int globalCount = nPrevImgCount + imgCount; const int height = globalCount % info.heightSize(); const int time = globalCount / info.heightSize(); cv::Mat_<quint16> img = m_lImage->at(height).at(time); #pragma omp parallel for schedule(static) for(quint32 stripCount = 0; stripCount < stripSize; ++stripCount) { quint16* const stripBuf = reinterpret_cast<quint16*>(new quint8[stripLength]); #pragma omp critical TIFFReadEncodedStrip(tif, stripCount, stripBuf, stripLength); for(quint32 rowInStrip = 0; rowInStrip < rps; ++rowInStrip) { const quint32 imgStrip = rowInStrip + rps * stripCount; quint16* const matData = reinterpret_cast<quint16*>(img.data + img.step * imgStrip); for(int col = 0; col < info.colSize(); ++col) { const quint16 oriValue = stripBuf[col + rowInStrip * info.colSize()]; matData[col] = oriValue; } if(imgStrip == info.rowSize() - 1) break; } } TIFFReadDirectory(tif); emit changedProgressValue(100.0 * globalCount / m_lImage->size(), sProgressFormat); } return true; } else return false; TIFFClose(tif); }
int rut_surface_to_tiff (RutSurface *s, const char *filename) { TIFF *tif = NULL; int rows_per_strip, num_strips, strip, row, i; int result = 1; char *buffer = NULL; assert (s); assert (filename); /* Open TIFF file */ tif = TIFFOpen (filename, "wb"); if (!tif) goto savefail; /* Set TIFF tags */ TIFFSetField (tif, TIFFTAG_IMAGEWIDTH, s->cols); TIFFSetField (tif, TIFFTAG_IMAGELENGTH, s->rows); TIFFSetField (tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_IEEEFP); TIFFSetField (tif, TIFFTAG_BITSPERSAMPLE, 32); TIFFSetField (tif, TIFFTAG_SAMPLESPERPIXEL, 1); TIFFSetField (tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK); rows_per_strip = TIFFDefaultStripSize (tif, 0); TIFFSetField (tif, TIFFTAG_ROWSPERSTRIP, rows_per_strip); num_strips = TIFFNumberOfStrips (tif); /* Copy data into TIFF strips */ buffer = malloc (TIFFStripSize (tif)); for (row = 0, strip = 0; strip < num_strips; strip++) { size_t size = 0; for (i = 0; (i < rows_per_strip) && (row < s->rows); i++, row++) { float *src = RUT_SURFACE_PTR (s, row, 0); char *dest = buffer + i*s->cols*4; memcpy (dest, src, s->cols * 4); size += s->cols*4; } result = (TIFFWriteEncodedStrip (tif, strip, buffer, size) != -1); if (!result) { fprintf (stderr, "Could not write TIFF strip %i/%i to %s\n", strip+1, num_strips, filename); } } /* Clean up */ free (buffer); TIFFClose (tif); return 1; savefail: if (tif) TIFFClose (tif); if (buffer) free (buffer); fprintf (stderr, "Could not save TIFF to %s\n", filename); return 0; }
bool CTiffImg::Create(const char *szFname, unsigned long nWidth, unsigned long nHeight, unsigned long nBPS, unsigned long nPhoto, unsigned long nSamples, float fXRes, float fYRes, bool bCompress) { Close(); m_bRead = false; m_nWidth = nWidth; m_nHeight = nHeight; m_nBitsPerSample = (unsigned short)nBPS; m_nSamples = (unsigned short)nSamples; switch(nPhoto) { case PHOTO_MINISBLACK: if (m_nSamples==3) m_nPhoto = PHOTOMETRIC_RGB; else m_nPhoto = PHOTOMETRIC_MINISBLACK; break; case PHOTO_MINISWHITE: if (m_nSamples==4) m_nPhoto = PHOTOMETRIC_SEPARATED; else m_nPhoto = PHOTOMETRIC_MINISWHITE; break; case PHOTO_CIELAB: m_nPhoto = PHOTOMETRIC_CIELAB; break; } m_hTif = TIFFOpen(szFname, "w"); if (!m_hTif) { TIFFError(szFname,"Can not open output image"); return false; } TIFFSetField(m_hTif, TIFFTAG_IMAGEWIDTH, (uint32) m_nWidth); TIFFSetField(m_hTif, TIFFTAG_IMAGELENGTH, (uint32) m_nHeight); TIFFSetField(m_hTif, TIFFTAG_PHOTOMETRIC, m_nPhoto); TIFFSetField(m_hTif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); TIFFSetField(m_hTif, TIFFTAG_SAMPLESPERPIXEL, m_nSamples); TIFFSetField(m_hTif, TIFFTAG_BITSPERSAMPLE, m_nBitsPerSample); TIFFSetField(m_hTif, TIFFTAG_ROWSPERSTRIP, 1); TIFFSetField(m_hTif, TIFFTAG_COMPRESSION, bCompress ? COMPRESSION_LZW : COMPRESSION_NONE); TIFFSetField(m_hTif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); TIFFSetField(m_hTif, TIFFTAG_XRESOLUTION, fXRes); TIFFSetField(m_hTif, TIFFTAG_YRESOLUTION, fYRes); m_nCurLine = 0; m_nCurStrip = 0; m_nBytesPerLine = TIFFStripSize(m_hTif); return true; }
/* * 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 tiffSize_(TIFF *image, int *W, int *H, int *linestep, BufType *Type) { uint16 bps, spp, sfm; uint32 width; tsize_t stripSize; unsigned long imageOffset; int stripMax; //unsigned long bufferSize, count; // Check that it is of a type that we support if((TIFFGetField(image, TIFFTAG_BITSPERSAMPLE, &bps) == 0) ){ return(42); } MyTRACE( "bits per sample(%d)\n", bps); //float type if((TIFFGetField(image, TIFFTAG_SAMPLEFORMAT, &sfm) == 0) ){ return(42); } MyTRACE( "TIFFTAG_SAMPLEFORMAT(%d)\n", sfm); if (bps == 32 && sfm == SAMPLEFORMAT_IEEEFP) *Type = TYPE_32F; else if (bps == 16 && (sfm == SAMPLEFORMAT_INT)) *Type = TYPE_16S; else if (bps == 16 && (sfm == SAMPLEFORMAT_UINT)) *Type = TYPE_16U; else return 43; if((TIFFGetField(image, TIFFTAG_SAMPLESPERPIXEL, &spp) == 0) || (spp != 1)){ MyTRACE( "Either undefined or unsupported number of samples per pixel\n"); return(42); } // Read in the possibly multiple strips stripSize = TIFFStripSize (image); *linestep = stripSize; stripMax = TIFFNumberOfStrips (image); imageOffset = 0; long height = stripMax; *H = height; // Do whatever it is we do with the buffer -- we dump it in hex if(TIFFGetField(image, TIFFTAG_IMAGEWIDTH, &width) == 0){ MyTRACE( "Image does not define its width\n"); return(42); } *W = width; assert(typeSize(*Type)*width <= stripSize); return 0; }
/* * Setup the raw data buffer used for encoding. */ int TIFFWriteBufferSetup(TIFF *tif, tdata_t bp, tsize_t size) { static const char module[] = "TIFFWriteBufferSetup"; if (tif->tif_rawdata) { if (tif->tif_flags & TIFF_MYBUFFER) { _TIFFfree(tif->tif_rawdata); tif->tif_flags &= ~TIFF_MYBUFFER; } tif->tif_rawdata = NULL; } if (size == (tsize_t) -1) { size = (isTiled(tif) ? tif->tif_tilesize : TIFFStripSize(tif)); /* * Make raw data buffer at least 8K */ if (size < 8 * 1024) size = 8 * 1024; bp = NULL; /* NB: force malloc */ } if (bp == NULL) { bp = _TIFFmalloc(size); if (bp == NULL) { TIFFErrorExt(tif->tif_clientdata, module, "%s: No space for output buffer", tif->tif_name); return (0); } tif->tif_flags |= TIFF_MYBUFFER; } else tif->tif_flags &= ~TIFF_MYBUFFER; tif->tif_rawdata = (tidata_t) bp; tif->tif_rawdatasize = size; tif->tif_rawcc = 0; tif->tif_rawcp = tif->tif_rawdata; tif->tif_flags |= TIFF_BUFFERSETUP; return (1); }
/* * 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); }
/* * 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); }
int read_strips(TIFF *tif, const tdata_t array, const tsize_t size) { tstrip_t strip, nstrips; tsize_t stripsize, offset; tdata_t buf = NULL; stripsize = TIFFStripSize(tif); if (!stripsize) { fprintf (stderr, "Wrong size of strip.\n"); return -1; } buf = _TIFFmalloc(stripsize); if (!buf) { fprintf (stderr, "Can't allocate space for strip buffer.\n"); return -1; } nstrips = TIFFNumberOfStrips(tif); for (offset = 0, strip = 0; offset < size && strip < nstrips; offset+=stripsize, strip++) { /* * Properly read last strip. */ tsize_t bufsize = size - offset; if (bufsize > stripsize) bufsize = stripsize; if (TIFFReadEncodedStrip(tif, strip, buf, -1) != bufsize) { fprintf (stderr, "Can't read strip %lu.\n", (unsigned long)strip); return -1; } if (memcmp(buf, (char *)array + offset, bufsize) != 0) { fprintf (stderr, "Wrong data read for strip %lu.\n", (unsigned long)strip); _TIFFfree(buf); return -1; } } _TIFFfree(buf); return 0; }
_declspec (dllexport) void sinograph(TIFF* tif, const uint32_t slice, const int32_t w, const int32_t l, const int32_t total_quantity, int32_t samples_per_pixel, uint32_t used_quantity, const char* resultPath) { if (slice >= (uint32_t) l) { return; } char pathName[1024]; strcpy_s(pathName, resultPath); strcat_s(pathName, "\\sinograph.tif"); TIFF* resultTif = makeTiffImage(pathName); TIFFSetField(resultTif, TIFFTAG_IMAGEWIDTH, w); TIFFSetField(resultTif, TIFFTAG_IMAGELENGTH, used_quantity); TIFFSetField(resultTif, TIFFTAG_BITSPERSAMPLE, 8); TIFFSetField(resultTif, TIFFTAG_SAMPLESPERPIXEL, samples_per_pixel); TIFFSetField(resultTif, TIFFTAG_ROWSPERSTRIP, 1); TIFFSetField(resultTif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); TIFFSetField(resultTif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); TIFFSetField(resultTif, TIFFTAG_COMPRESSION, COMPRESSION_DEFLATE); TIFFSetField(resultTif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK); int32_t scanlineSize = TIFFScanlineSize(tif); int32_t stripSize = TIFFStripSize(tif); int linesPerStrip = stripSize / scanlineSize; int stripOfInterest = slice / linesPerStrip; // keep integer division here int offsetPixels = slice % linesPerStrip * w; uint8_t* buffer = (uint8_t*) _TIFFmalloc(stripSize); int32_t selector = (total_quantity + used_quantity - 1) / used_quantity; used_quantity = 0; for (int32_t i = 0; i < total_quantity; i++) { if (i % selector != 0) { continue; } TIFFSetDirectory(tif, i); TIFFReadEncodedStrip(tif, stripOfInterest, buffer, stripSize); TIFFWriteScanline(resultTif, buffer + offsetPixels, used_quantity, scanlineSize); ++used_quantity; } _TIFFfree(buffer); TIFFClose(resultTif); }
/** * Constructor. * * @param ifd the directory the tile belongs to. */ Impl(std::shared_ptr<IFD>& ifd): ifd(ifd), tilewidth(), tileheight(), planarconfig(), samples(), tilecount(), nrows(), ncols(), ntiles(), buffersize() { Sentry sentry; ::TIFF *tiff = getTIFF(); // Get basic image metadata. uint32_t imagewidth = ifd->getImageWidth(); uint32_t imageheight = ifd->getImageHeight(); planarconfig = ifd->getPlanarConfiguration(); samples = ifd->getSamplesPerPixel(); tilewidth = ifd->getTileWidth(); tileheight = ifd->getTileHeight(); type = ifd->getTileType(); // Get tile-specific metadata, falling back to // strip-specific metadata if not present. if (type == TILE) { tilecount = TIFFNumberOfTiles(tiff); buffersize = TIFFTileSize(tiff); } else { tilecount = TIFFNumberOfStrips(tiff); buffersize = TIFFStripSize(tiff); } // Compute row and column counts. nrows = imageheight / tileheight; if (imageheight % tileheight) ++nrows; ncols = imagewidth / tilewidth; if (imagewidth % tilewidth) ++ncols; ntiles = nrows * ncols; }
bool CTiffImg::Open(const char *szFname) { Close(); m_bRead = true; m_hTif = TIFFOpen(szFname, "r"); if (!m_hTif) { TIFFError(szFname,"Can not open input image"); return false; } unsigned short nPlanar=PLANARCONFIG_CONTIG; unsigned short nOrientation=ORIENTATION_TOPLEFT; TIFFGetField(m_hTif, TIFFTAG_IMAGEWIDTH, &m_nWidth); TIFFGetField(m_hTif, TIFFTAG_IMAGELENGTH, &m_nHeight); TIFFGetField(m_hTif, TIFFTAG_PHOTOMETRIC, &m_nPhoto); TIFFGetField(m_hTif, TIFFTAG_PLANARCONFIG, &nPlanar); TIFFGetField(m_hTif, TIFFTAG_SAMPLESPERPIXEL, &m_nSamples); TIFFGetField(m_hTif, TIFFTAG_BITSPERSAMPLE, &m_nBitsPerSample); TIFFGetField(m_hTif, TIFFTAG_ROWSPERSTRIP, &m_nRowsPerStrip); TIFFGetField(m_hTif, TIFFTAG_ORIENTATION, &nOrientation); TIFFGetField(m_hTif, TIFFTAG_XRESOLUTION, &m_fXRes); TIFFGetField(m_hTif, TIFFTAG_YRESOLUTION, &m_fYRes); if ((m_nSamples>1 && nPlanar != PLANARCONFIG_CONTIG) || nOrientation != ORIENTATION_TOPLEFT) { Close(); return false; } m_nCurStrip=(unsigned long)-1; m_nCurLine = 0; m_nStripSize = TIFFStripSize(m_hTif); m_pStripBuf = (unsigned char*)malloc(m_nStripSize); m_nBytesPerLine = (m_nWidth * m_nBitsPerSample * m_nSamples + 7)>>3; if (!m_pStripBuf) { Close(); return false; } return true; }
// Write a 6x4 test tiff consisting of two directories void writeStripTiff(const char* fileName) { uint32 height = 4; uint32 width = 6; uint32 rowsPerStrip = height/2; uint16 samplesPerPixel = 3; uint16 bitsPerSample = 8; const unsigned char red[] = {0xFF, 0, 0}; const unsigned char blue[] = {0, 0, 0xFF}; //TIFF* outFile = TIFFStreamOpen("stream", &outStream); TIFF* outFile = TIFFOpen(fileName, "w"); setTiffFields(outFile, width, height, samplesPerPixel, bitsPerSample); TIFFSetField(outFile, TIFFTAG_ROWSPERSTRIP, rowsPerStrip); TIFFSetField(outFile, TIFFTAG_IMAGEDESCRIPTION, "Strip-allocated tiff for unit tests"); tsize_t bufSize = TIFFStripSize(outFile); unsigned char* buf = reinterpret_cast<unsigned char*>(_TIFFmalloc(bufSize)); // first directory is a red strip and a blue strip. setBufToColor(buf, bufSize/samplesPerPixel, red, samplesPerPixel); TIFFWriteEncodedStrip(outFile, 0, buf, bufSize); setBufToColor(buf, bufSize/samplesPerPixel, blue, samplesPerPixel); TIFFWriteEncodedStrip(outFile, 1, buf, bufSize); TIFFWriteDirectory(outFile); setTiffFields(outFile, width, height, samplesPerPixel, bitsPerSample); TIFFSetField(outFile, TIFFTAG_ROWSPERSTRIP, rowsPerStrip); // second directory is a white strip and a black strip _TIFFmemset(buf, 0xFF, bufSize); TIFFWriteEncodedStrip(outFile, 0, buf, bufSize); _TIFFmemset(buf, 0x00, bufSize); TIFFWriteEncodedStrip(outFile, 1, buf, bufSize); TIFFClose(outFile); _TIFFfree(buf); //TIFFSetField(outFile, TIFFTAG_TILEWIDTH, tileWidth); //TIFFSetField(outFile, TIFFTAG_TILELENGTH, tileHeight); }
/****************** Will hopefully remove this function. *********************** * Read image strips and set global variablels. * - NUMBER_OF_ROWS / IMAGE_LENGTH * - NUMBER_OF_COLS / IMAGE_WIDTH * - IMAGE */ void stripIO(TIFF * tif) { uint16 config; TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &config); int stripSize, stripMax; stripSize = TIFFStripSize (tif); stripMax = TIFFNumberOfStrips (tif); int bytesPerStrip; TIFFGetField(tif, TIFFTAG_STRIPBYTECOUNTS, &bytesPerStrip); printf("stripSize : %d stripMax : %d bytesPerStrip : %d\n", stripSize, stripMax, bytesPerStrip); unsigned long bufferSize; bufferSize = stripSize * stripMax; char * buffer; if((buffer = (char *) _TIFFmalloc(bufferSize)) == NULL){ puts("Not enough memory to allocate buffer."); exit(42); } int stripCount, result; unsigned long imageOffset = 0; for(stripCount = 0; stripCount < stripMax; stripCount++) { if((result = TIFFReadRawStrip(tif, stripCount, buffer + imageOffset, stripSize)) == -1){ puts("Read error on input strip."); } } int i; for(stripCount = 0; stripCount < stripMax; stripCount++){ for(i = 0; i < stripSize; i++){ //printf("%d ", atoi(&buffer[stripCount*stripSize + stripSize])); } //printf("\n"); } _TIFFfree(buffer); }
/* * Get a strip-organized image with * SamplesPerPixel > 1 * PlanarConfiguration separated * We assume that all such images are RGB. */ boolean TIFFRasterImpl::gtStripSeparate( const RGBvalue* Map, u_long h, u_long w ) { u_long stripsize = TIFFStripSize(tif_); u_char* buf = new u_char[3*stripsize]; u_char* r = buf; u_char* g = r + stripsize; u_char* b = g + stripsize; tileSeparateRoutine put = pickTileSeparateCase(Map); u_long y = setorientation(h); int toskew = (int)(orientation_ == ORIENTATION_TOPLEFT ? -w + -w : -w + w); u_long rowsperstrip = (u_long) -1L; TIFFGetField(tif_, TIFFTAG_ROWSPERSTRIP, &rowsperstrip); u_long imagewidth; TIFFGetField(tif_, TIFFTAG_IMAGEWIDTH, &imagewidth); int scanline = TIFFScanlineSize(tif_); int fromskew = (int)(w < imagewidth ? imagewidth - w : 0); for (u_long row = 0; row < h; row += rowsperstrip) { u_int nrow = u_int(row + rowsperstrip > h ? h - row : rowsperstrip); if (TIFFReadEncodedStrip( tif_, TIFFComputeStrip(tif_, row, 0), r, nrow*scanline) < 0 ) { break; } if (TIFFReadEncodedStrip( tif_, TIFFComputeStrip(tif_, row, 1), g, nrow*scanline) < 0 ) { break; } if (TIFFReadEncodedStrip( tif_, TIFFComputeStrip(tif_, row, 2), b, nrow*scanline) < 0 ) { break; } (this->*put)(raster_ + y*w, r, g, b, Map, w, nrow, fromskew, toskew); y += (orientation_ == ORIENTATION_TOPLEFT ? -nrow : nrow); } delete buf; return true; }
int ReadTiff(const char * filename, std::vector<unsigned char> * ptr, int * w, int * h, int * depth) { TIFF* tiff = TIFFOpen(filename, "r"); if (!tiff) { std::cerr << "Error: Couldn't open " << filename << " fopen returned 0"; return 0; } uint16 bps, spp; TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, w); TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, h); TIFFGetField(tiff, TIFFTAG_BITSPERSAMPLE, &bps); TIFFGetField(tiff, TIFFTAG_SAMPLESPERPIXEL, &spp); *depth = bps * spp / 8; ptr->resize((*h)*(*w)*(*depth)); if (*depth==4) { if (ptr != nullptr) { if (!TIFFReadRGBAImageOriented(tiff, *w, *h, (uint32*)&((*ptr)[0]), ORIENTATION_TOPLEFT, 0)) { TIFFClose(tiff); return 0; } } } else { for (size_t i=0; i<TIFFNumberOfStrips(tiff); ++i) { if (TIFFReadEncodedStrip(tiff, i, ((uint8*)&((*ptr)[0]))+i*TIFFStripSize(tiff),(tsize_t)-1) == std::numeric_limits<tsize_t>::max()) { TIFFClose(tiff); return 0; } } } TIFFClose(tiff); return 1; }
std::auto_ptr<image_list> STKFormat::read_multi(byte_source* src, ImageFactory* factory) { shift_source moved(src); stk_extend ext; tiff_warn_error twe; tif_holder t = read_client(&moved); std::auto_ptr<image_list> images(new image_list); 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, 1); const uint16 bits_per_sample = tiff_get<uint16>(t, TIFFTAG_BITSPERSAMPLE, 8); const int depth = nr_samples > 1 ? nr_samples : -1; const int strip_size = TIFFStripSize(t.tif); const int n_strips = TIFFNumberOfStrips(t.tif); int32_t n_planes; void* data; TIFFGetField(t.tif, UIC3Tag, &n_planes, &data); int raw_strip_size = 0; for (int st = 0; st != n_strips; ++st) { raw_strip_size += TIFFRawStripSize(t.tif, st); } for (int z = 0; z < n_planes; ++z) { // Monkey patch strip offsets. This is very hacky, but it seems to work! moved.shift(z * raw_strip_size); std::auto_ptr<Image> output(factory->create(bits_per_sample, h, w, depth)); uint8_t* start = output->rowp_as<uint8_t>(0); for (int st = 0; st != n_strips; ++st) { const int offset = TIFFReadEncodedStrip(t.tif, st, start, strip_size); if (offset == -1) { throw CannotReadError("imread.imread._tiff.stk: Error reading strip"); } start += offset; } images->push_back(output); } return images; }
static void svRGBSeparate(TIFF* tif, uint32* ss, int xsize, int ysize) { tsize_t stripsize = TIFFStripSize(tif); unsigned char *rbuf = (unsigned char *)_TIFFmalloc(3*stripsize); unsigned char *gbuf = rbuf + stripsize; unsigned char *bbuf = gbuf + stripsize; register int y; for (y = 0; y <= ysize; y += rowsperstrip) { unsigned char *rp, *gp, *bp; register int x; register uint32 n; n = rowsperstrip; if (n > ysize-y+1) n = ysize-y+1; rp = rbuf; gp = gbuf; bp = bbuf; do { for (x = 0; x <= xsize; x++) { uint32 v = ss[x]; rp[x] = v; gp[x] = v >> 8; bp[x] = v >> 16; } rp += xsize+1, gp += xsize+1, bp += xsize+1; ss += xsize+1; } while (--n); if (TIFFWriteEncodedStrip(tif, TIFFComputeStrip(tif,y,0), rbuf, stripsize) < 0) break; if (TIFFWriteEncodedStrip(tif, TIFFComputeStrip(tif,y,1), gbuf, stripsize) < 0) break; if (TIFFWriteEncodedStrip(tif, TIFFComputeStrip(tif,y,2), bbuf, stripsize) < 0) break; } _TIFFfree(rbuf); }
_declspec (dllexport) uint32_t getTiffTags( int * compression, char * hostComputer, int32_t * imageWidth, int32_t * imageLength, char * software, int* bitness, int* samples_per_pixel, int32_t* scanlineSize, int32_t* stripSize, TIFF* tif) { TIFFGetField(tif, TIFFTAG_COMPRESSION, compression); TIFFGetField(tif, TIFFTAG_HOSTCOMPUTER, hostComputer); TIFFGetField(tif, TIFFTAG_SOFTWARE, software); TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, imageWidth); TIFFGetField(tif, TIFFTAG_IMAGELENGTH, imageLength); TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, bitness); TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, samples_per_pixel); uint32_t tifPages = countTiffPages(tif); *scanlineSize = TIFFScanlineSize(tif); *stripSize = TIFFStripSize(tif); return tifPages; }
int tiff_decode_complex(TIFF *tif, clFFT_Complex *buffer, int offset) { const int strip_size = TIFFStripSize(tif); const int n_strips = TIFFNumberOfStrips(tif); printf("\nstrip_size:%d ; n_strips:%d\n", strip_size, n_strips); int result = 0; int width, height; TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width); TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height); float *temp_buffer = (float *)malloc(strip_size); for (int strip = 0; strip < n_strips; strip++) { result = TIFFReadEncodedStrip(tif, strip, temp_buffer, strip_size); if (result == -1) return 0; int pixels_per_strip = strip_size / sizeof(float); int rows = pixels_per_strip / width; for(int i = 0; i < rows; ++i) { for(int j = offset; j < width; ++j) { buffer[strip * (pixels_per_strip - rows * offset) + i * (width-offset) + j - offset].real = temp_buffer[i * width + j]; buffer[strip * (pixels_per_strip - rows * offset) + i * (width-offset) + j - offset].imag = 0.0; } } } free(temp_buffer); return 1; }
/* * Get a strip-organized image that has * PlanarConfiguration contiguous if SamplesPerPixel > 1 * or * SamplesPerPixel == 1 */ static int gtStripContig(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h) { TIFF* tif = img->tif; tileContigRoutine put = img->put.contig; uint16 orientation; uint32 row, y, nrow; u_char* buf; uint32 rowsperstrip; uint32 imagewidth = img->width; tsize_t scanline; int32 fromskew, toskew; buf = (u_char*) _TIFFmalloc(TIFFStripSize(tif)); if (buf == 0) { TIFFError(TIFFFileName(tif), "No space for strip buffer"); return (0); } 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), buf, nrow*scanline) < 0 && img->stoponerr) break; (*put)(img, raster+y*w, 0, y, w, nrow, fromskew, toskew, buf); y += (orientation == ORIENTATION_TOPLEFT ? -(int32) nrow : (int32) nrow); } _TIFFfree(buf); return (1); }