📄 image_io_tiff.cpp
字号:
size_t i; switch (depth) { case 3: for (i = 0; i < width; ++i) { size_t from_idx = from_idx_base + i; size_t to_idx = to_idx_base + i; // TIFFReadRGBAImage() returns data in ABGR image, // packed as a 32-bit value, so we need to pick this // apart here uint32 pixel = raster[from_idx]; data[3 * to_idx + 0] = TIFFGetR(pixel); data[3 * to_idx + 1] = TIFFGetG(pixel); data[3 * to_idx + 2] = TIFFGetB(pixel); } break; case 4: for (i = 0; i < width; ++i) { size_t from_idx = from_idx_base + i; size_t to_idx = to_idx_base + i; // TIFFReadRGBAImage() returns data in ABGR image, // packed as a 32-bit value, so we need to pick this // apart here uint32 pixel = raster[from_idx]; data[4 * to_idx + 0] = TIFFGetR(pixel); data[4 * to_idx + 1] = TIFFGetG(pixel); data[4 * to_idx + 2] = TIFFGetB(pixel); data[4 * to_idx + 3] = TIFFGetA(pixel); } break; } } // clean-up _TIFFfree(raster); TIFFClose(tiff); } catch (...) { if (raster) { _TIFFfree(raster); raster = NULL; } if (tiff) { TIFFClose(tiff); tiff = NULL; } TIFFSetErrorHandler(old_err_handler); TIFFSetWarningHandler(old_warn_handler); // throw to a higher level throw; } TIFFSetErrorHandler(old_err_handler); TIFFSetWarningHandler(old_warn_handler); return image.Release();}//// ReadImage()// read a sub-image from a file. We use a brain-dead implementation that reads// the whole image in, then subsets it. This may change in the future.//CImage* CImageIOTiff::ReadImage(CNcbiIstream& istr, size_t x, size_t y, size_t w, size_t h){ CRef<CImage> image(ReadImage(istr)); return image->GetSubImage(x, y, w, h);}//// WriteImage()// write an image to a file in TIFF format//void CImageIOTiff::WriteImage(const CImage& image, CNcbiOstream& ostr, CImageIO::ECompress){ TIFF* tiff = NULL; TIFFErrorHandler old_err_handler = NULL; TIFFErrorHandler old_warn_handler = NULL; try { if ( !image.GetData() ) { NCBI_THROW(CImageException, eWriteError, "CImageIOTiff::WriteImage(): cannot write empty image"); } old_err_handler = TIFFSetErrorHandler(&s_TiffWriteErrorHandler); old_warn_handler = TIFFSetWarningHandler(&s_TiffWarningHandler); // open our file //ostr.open(file.c_str(), ios::out|ios::binary); tiff = TIFFClientOpen("", "wm", reinterpret_cast<thandle_t>(&ostr), s_TIFFDummyIOHandler, s_TIFFWriteHandler, s_TIFFSeekHandler, s_TIFFCloseHandler, s_TIFFSizeHandler, s_TIFFMapFileHandler, s_TIFFUnmapFileHandler); if ( !tiff ) { NCBI_THROW(CImageException, eWriteError, "CImageIOTiff::WriteImage(): cannot write file"); } // set a bunch of standard fields, defining our image dimensions TIFFSetField(tiff, TIFFTAG_IMAGEWIDTH, image.GetWidth()); TIFFSetField(tiff, TIFFTAG_IMAGELENGTH, image.GetHeight()); TIFFSetField(tiff, TIFFTAG_BITSPERSAMPLE, 8); TIFFSetField(tiff, TIFFTAG_SAMPLESPERPIXEL, image.GetDepth()); TIFFSetField(tiff, TIFFTAG_ROWSPERSTRIP, image.GetHeight()); // TIFF options TIFFSetField(tiff, TIFFTAG_COMPRESSION, COMPRESSION_DEFLATE); TIFFSetField(tiff, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB); TIFFSetField(tiff, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB); TIFFSetField(tiff, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); // write our information TIFFWriteEncodedStrip(tiff, 0, const_cast<void*> (reinterpret_cast<const void*> (image.GetData())), image.GetWidth() * image.GetHeight() * image.GetDepth()); TIFFClose(tiff); TIFFSetErrorHandler(old_err_handler); TIFFSetWarningHandler(old_warn_handler); } catch (...) { // close our file and wipe it from the system if (tiff) { TIFFClose(tiff); tiff = NULL; } // restore the standard error handlers TIFFSetErrorHandler (old_err_handler); TIFFSetWarningHandler(old_warn_handler); throw; }}//// WriteImage()// write a sub-image to a file in TIFF format. This uses a brain-dead// implementation in that we subset the image first, then write the sub-image// out.//void CImageIOTiff::WriteImage(const CImage& image, CNcbiOstream& ostr, size_t x, size_t y, size_t w, size_t h, CImageIO::ECompress compress){ CRef<CImage> subimage(image.GetSubImage(x, y, w, h)); WriteImage(*subimage, ostr, compress);}END_NCBI_SCOPE#else // !HAVE_LIBTIFF//// LIBTIFF functionality not included - we stub out the various needed// functions//BEGIN_NCBI_SCOPECImage* CImageIOTiff::ReadImage(CNcbiIstream&){ NCBI_THROW(CImageException, eUnsupported, "CImageIOTiff::ReadImage(): TIFF format not supported");}CImage* CImageIOTiff::ReadImage(CNcbiIstream&, size_t, size_t, size_t, size_t){ NCBI_THROW(CImageException, eUnsupported, "CImageIOTiff::ReadImage(): TIFF format not supported");}void CImageIOTiff::WriteImage(const CImage&, CNcbiOstream&, CImageIO::ECompress){ NCBI_THROW(CImageException, eUnsupported, "CImageIOTiff::WriteImage(): TIFF format not supported");}void CImageIOTiff::WriteImage(const CImage&, CNcbiOstream&, size_t, size_t, size_t, size_t, CImageIO::ECompress){ NCBI_THROW(CImageException, eUnsupported, "CImageIOTiff::WriteImage(): TIFF format not supported");}END_NCBI_SCOPE#endif // HAVE_LIBTIFF/* * =========================================================================== * $Log: image_io_tiff.cpp,v $ * Revision 1000.4 2004/06/01 19:41:46 gouriano * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.9 * * Revision 1.9 2004/05/17 21:07:58 gorelenk * Added include of PCH ncbi_pch.hpp * * Revision 1.8 2004/02/19 22:57:57 ucko * Accommodate stricter implementations of CT_POS_TYPE. * * Revision 1.7 2003/12/19 20:58:29 dicuccio * Added special case: single-channel TIFF images -> interpret as RGB * * Revision 1.6 2003/12/18 13:50:15 dicuccio * Fixed image reversal bug: TIFFReadRGBAImage() reads the image in raster-order, * not image-order. Fixed setting of correct depth on image read. * * Revision 1.5 2003/12/16 16:16:55 dicuccio * Fixed compiler warnings * * Revision 1.4 2003/12/16 15:49:37 dicuccio * Large re-write of image handling. Added improved error-handling and support * for streams-based i/o (via hooks into each client library). * * Revision 1.3 2003/12/12 17:49:04 dicuccio * Intercept libtiff error messages and translate them into LOG_POST()/exception * where appropriate * * Revision 1.2 2003/11/03 15:19:57 dicuccio * Added optional compression parameter * * Revision 1.1 2003/06/03 15:17:13 dicuccio * Initial revision of image library * * =========================================================================== */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -