📄 tiff_io.hpp
字号:
/* Copyright 2005-2007 Adobe Systems Incorporated Use, modification and distribution are subject to the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt). See http://opensource.adobe.com/gil for most recent version including documentation.*//*************************************************************************************************/#ifndef GIL_TIFF_IO_H#define GIL_TIFF_IO_H/// \file/// \brief Support for reading and writing TIFF files/// Requires libtiff!/// \author Hailin Jin and Lubomir Bourdev \n/// Adobe Systems Incorporated/// \date 2005-2007 \n Last updated September 24, 2006#include <vector>#include <string>#include <algorithm>#include <boost/static_assert.hpp>#include <tiffio.h>#include "../../gil_all.hpp"#include "io_error.hpp"namespace boost { namespace gil {namespace detail {template <typename Channel,typename ColorSpace>struct tiff_read_support_private { BOOST_STATIC_CONSTANT(bool,is_supported=false); BOOST_STATIC_CONSTANT(int,bit_depth=0); BOOST_STATIC_CONSTANT(int,color_type=0);};template <>struct tiff_read_support_private<bits8,gray_t> { BOOST_STATIC_CONSTANT(bool,is_supported=true); BOOST_STATIC_CONSTANT(int,bit_depth=8); BOOST_STATIC_CONSTANT(int,color_type=PHOTOMETRIC_MINISBLACK);};template <>struct tiff_read_support_private<bits8,rgb_t> { BOOST_STATIC_CONSTANT(bool,is_supported=true); BOOST_STATIC_CONSTANT(int,bit_depth=8); BOOST_STATIC_CONSTANT(int,color_type=PHOTOMETRIC_RGB);};template <>struct tiff_read_support_private<bits16,gray_t> { BOOST_STATIC_CONSTANT(bool,is_supported=true); BOOST_STATIC_CONSTANT(int,bit_depth=16); BOOST_STATIC_CONSTANT(int,color_type=PHOTOMETRIC_MINISBLACK);};template <>struct tiff_read_support_private<bits16,rgb_t> { BOOST_STATIC_CONSTANT(bool,is_supported=true); BOOST_STATIC_CONSTANT(int,bit_depth=16); BOOST_STATIC_CONSTANT(int,color_type=PHOTOMETRIC_RGB);};template <>struct tiff_read_support_private<bits32f,gray_t> { BOOST_STATIC_CONSTANT(bool,is_supported=true); BOOST_STATIC_CONSTANT(int,bit_depth=32); BOOST_STATIC_CONSTANT(int,color_type=PHOTOMETRIC_MINISBLACK);};template <>struct tiff_read_support_private<bits32f,rgb_t> { BOOST_STATIC_CONSTANT(bool,is_supported=true); BOOST_STATIC_CONSTANT(int,bit_depth=32); BOOST_STATIC_CONSTANT(int,color_type=PHOTOMETRIC_RGB);};template <typename Channel,typename ColorSpace>struct tiff_write_support_private { BOOST_STATIC_CONSTANT(bool,is_supported=false); BOOST_STATIC_CONSTANT(int,bit_depth=0); BOOST_STATIC_CONSTANT(int,color_type=0);};template <>struct tiff_write_support_private<bits8,gray_t> { BOOST_STATIC_CONSTANT(bool,is_supported=true); BOOST_STATIC_CONSTANT(int,bit_depth=8); BOOST_STATIC_CONSTANT(int,color_type=PHOTOMETRIC_MINISBLACK);};template <>struct tiff_write_support_private<bits8,rgb_t> { BOOST_STATIC_CONSTANT(bool,is_supported=true); BOOST_STATIC_CONSTANT(int,bit_depth=8); BOOST_STATIC_CONSTANT(int,color_type=PHOTOMETRIC_RGB);};template <>struct tiff_write_support_private<bits16,gray_t> { BOOST_STATIC_CONSTANT(bool,is_supported=true); BOOST_STATIC_CONSTANT(int,bit_depth=16); BOOST_STATIC_CONSTANT(int,color_type=PHOTOMETRIC_MINISBLACK);};template <>struct tiff_write_support_private<bits16,rgb_t> { BOOST_STATIC_CONSTANT(bool,is_supported=true); BOOST_STATIC_CONSTANT(int,bit_depth=16); BOOST_STATIC_CONSTANT(int,color_type=PHOTOMETRIC_RGB);};template <>struct tiff_write_support_private<bits32f,gray_t> { BOOST_STATIC_CONSTANT(bool,is_supported=true); BOOST_STATIC_CONSTANT(int,bit_depth=32); BOOST_STATIC_CONSTANT(int,color_type=PHOTOMETRIC_MINISBLACK);};template <>struct tiff_write_support_private<bits32f,rgb_t> { BOOST_STATIC_CONSTANT(bool,is_supported=true); BOOST_STATIC_CONSTANT(int,bit_depth=32); BOOST_STATIC_CONSTANT(int,color_type=PHOTOMETRIC_RGB);};class tiff_reader {protected: TIFF *_tp;public: tiff_reader(const char* filename) { io_error_if((_tp=TIFFOpen(filename,"r"))==NULL, "tiff_reader: fail to open file"); } ~tiff_reader() { TIFFClose(_tp); } template <typename View> void apply(const View& view) { unsigned short bps,photometric; point2<std::ptrdiff_t> dims=get_dimensions(); io_error_if(TIFFGetField(_tp,TIFFTAG_BITSPERSAMPLE,&bps)!=1); io_error_if(TIFFGetField(_tp,TIFFTAG_PHOTOMETRIC,&photometric)!=1); io_error_if(dims!=view.dimensions(), "tiff_read_view: input view size does not match TIFF file size"); io_error_if(tiff_read_support_private<typename channel_type<View>::type, typename color_space_type<View>::type>::bit_depth!=bps || tiff_read_support_private<typename channel_type<View>::type, typename color_space_type<View>::type>::color_type!=photometric, "tiff_read_view: input view type is incompatible with the image type"); std::size_t element_size=sizeof(pixel<typename channel_type<View>::type, layout<typename color_space_type<View>::type> >); std::size_t size_to_allocate = (std::max)((std::size_t)view.width(), (std::size_t)(TIFFScanlineSize(_tp)+element_size-1)/element_size); std::vector<pixel<typename channel_type<View>::type, layout<typename color_space_type<View>::type> > > row(size_to_allocate); for (int y=0;y<view.height();++y) { io_error_if(TIFFReadScanline(_tp,&row.front(), y)!=1); std::copy(row.begin(),row.begin()+view.width(),view.row_begin(y)); } } point2<std::ptrdiff_t> get_dimensions() { int w,h; io_error_if(TIFFGetField(_tp,TIFFTAG_IMAGEWIDTH, &w)!=1); io_error_if(TIFFGetField(_tp,TIFFTAG_IMAGELENGTH,&h)!=1); return point2<std::ptrdiff_t>(w,h); } template <typename Image> void read_image(Image& im) { im.recreate(get_dimensions()); apply(view(im)); }};// This code will be simplified...template <typename CC> class tiff_reader_color_convert : public tiff_reader {private: CC _cc;public: tiff_reader_color_convert(const char* filename) : tiff_reader(filename) {} tiff_reader_color_convert(const char* filename,CC cc_in) : tiff_reader(filename),_cc(cc_in) {} template <typename View> void apply(const View& view) { point2<std::ptrdiff_t> dims=get_dimensions(); unsigned short bps,photometric; io_error_if(TIFFGetField(_tp,TIFFTAG_BITSPERSAMPLE,&bps)!=1); io_error_if(TIFFGetField(_tp,TIFFTAG_PHOTOMETRIC,&photometric)!=1); io_error_if(dims!=view.dimensions(), "tiff_reader_color_convert::apply(): input view size does not match TIFF file size"); switch (photometric) { case PHOTOMETRIC_MINISBLACK: { switch (bps) { case 8: { std::size_t element_size=sizeof(gray8_pixel_t); std::size_t size_to_allocate = (std::max)((std::size_t)view.width(), (std::size_t)(TIFFScanlineSize(_tp)+element_size-1)/element_size); std::vector<gray8_pixel_t> row(size_to_allocate); for (int y=0;y<view.height();++y) { io_error_if(TIFFReadScanline(_tp,&row.front(), y)!=1); std::transform(row.begin(),row.begin()+view.width(),view.row_begin(y), color_convert_deref_fn<gray8_ref_t,typename View::value_type,CC>(_cc)); } break; } case 16: { std::size_t element_size=sizeof(gray16_pixel_t); std::size_t size_to_allocate = (std::max)((std::size_t)view.width(), (std::size_t)(TIFFScanlineSize(_tp)+element_size-1)/element_size); std::vector<gray16_pixel_t> row(size_to_allocate); for (int y=0;y<view.height();++y) { io_error_if(TIFFReadScanline(_tp,&row.front(), y)!=1); std::transform(row.begin(),row.begin()+view.width(),view.row_begin(y), color_convert_deref_fn<gray16_ref_t,typename View::value_type,CC>(_cc)); } break; } case 32: { std::size_t element_size=sizeof(gray32f_pixel_t); std::size_t size_to_allocate = (std::max)((std::size_t)view.width(), (std::size_t)(TIFFScanlineSize(_tp)+element_size-1)/element_size); std::vector<gray32f_pixel_t> row(size_to_allocate); for (int y=0;y<view.height();++y) { io_error_if(TIFFReadScanline(_tp,&row.front(), y)!=1); std::transform(row.begin(),row.begin()+view.width(),view.row_begin(y), color_convert_deref_fn<gray32f_ref_t,typename View::value_type,CC>(_cc)); } break; } default: io_error("tiff_reader_color_convert::apply(): unknown combination of color type and bit depth"); } break; } case PHOTOMETRIC_RGB: { switch (bps) { case 8: { std::size_t element_size=sizeof(rgb8_pixel_t); std::size_t size_to_allocate = (std::max)((std::size_t)view.width(), (std::size_t)(TIFFScanlineSize(_tp)+element_size-1)/element_size); std::vector<rgb8_pixel_t> row(size_to_allocate); for (int y=0;y<view.height();++y) { io_error_if(TIFFReadScanline(_tp,&row.front(), y)!=1); std::transform(row.begin(),row.begin()+view.width(),view.row_begin(y), color_convert_deref_fn<rgb8_ref_t,typename View::value_type,CC>(_cc)); } break; } case 16: { std::size_t element_size=sizeof(rgb16_pixel_t); std::size_t size_to_allocate = (std::max)((std::size_t)view.width(),
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -