📄 tiff_io.hpp
字号:
(std::size_t)(TIFFScanlineSize(_tp)+element_size-1)/element_size); std::vector<rgb16_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<rgb16_ref_t,typename View::value_type,CC>(_cc)); } break; } case 32: { std::size_t element_size=sizeof(rgb32f_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<rgb32f_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<rgb32f_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; } default: { // reads an image in incompatible format via TIFFReadRGBAImage rgba8_image_t rgbaImg(dims); io_error_if(!TIFFReadRGBAImage(_tp, dims.x, dims.y, (uint32*)&gil::view(rgbaImg)(0,0), 0), "tiff_reader_color_convert::unsupported image format"); copy_and_convert_pixels(flipped_up_down_view(const_view(rgbaImg)), view, _cc); } } } template <typename Image> void read_image(Image& im) { im.recreate(get_dimensions()); apply(view(im)); }};class tiff_writer {protected: TIFF* _tp;public: tiff_writer(const char *filename) { io_error_if((_tp=TIFFOpen(filename,"w"))==NULL, "tiff_writer: fail to open file"); } ~tiff_writer() {TIFFClose(_tp);} template <typename View> void apply(const View& view) { io_error_if(TIFFSetField(_tp,TIFFTAG_IMAGELENGTH, view.height())!=1); io_error_if(TIFFSetField(_tp,TIFFTAG_IMAGEWIDTH, view.width())!=1); io_error_if(TIFFSetField(_tp,TIFFTAG_PHOTOMETRIC, tiff_write_support_private<typename channel_type<View>::type, typename color_space_type<View>::type>::color_type)!=1); io_error_if(TIFFSetField(_tp,TIFFTAG_RESOLUTIONUNIT, RESUNIT_NONE)!=1); io_error_if(TIFFSetField(_tp,TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG)!=1); io_error_if(TIFFSetField(_tp,TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT)!=1); io_error_if(TIFFSetField(_tp,TIFFTAG_SAMPLESPERPIXEL,num_channels<View>::value)!=1); io_error_if(TIFFSetField(_tp,TIFFTAG_BITSPERSAMPLE, tiff_write_support_private<typename channel_type<View>::type, typename color_space_type<View>::type>::bit_depth)!=1); io_error_if(TIFFSetField(_tp,TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(_tp, 0))!=1); std::vector<pixel<typename channel_type<View>::type, layout<typename color_space_type<View>::type> > > row(view.width()); for (int y=0;y<view.height();++y) { std::copy(view.row_begin(y),view.row_end(y),row.begin()); io_error_if(TIFFWriteScanline(_tp,&row.front(),y,0)!=1, "tiff_write_view: fail to write file"); } }};} // namespace detail/// \ingroup TIFF_IO/// \brief Determines whether the given view type is supported for readingtemplate <typename View>struct tiff_read_support { BOOST_STATIC_CONSTANT(bool,is_supported= (detail::tiff_read_support_private<typename channel_type<View>::type, typename color_space_type<View>::type>::is_supported)); BOOST_STATIC_CONSTANT(int,bit_depth= (detail::tiff_read_support_private<typename channel_type<View>::type, typename color_space_type<View>::type>::bit_depth)); BOOST_STATIC_CONSTANT(int,color_type= (detail::tiff_read_support_private<typename channel_type<View>::type, typename color_space_type<View>::type>::color_type));};/// \ingroup TIFF_IO/// \brief Returns the width and height of the TIFF file at the specified location./// Throws std::ios_base::failure if the location does not correspond to a valid TIFF fileinline point2<std::ptrdiff_t> tiff_read_dimensions(const char* filename) { detail::tiff_reader m(filename); return m.get_dimensions();}/// \ingroup TIFF_IO/// \brief Returns the width and height of the TIFF file at the specified location./// Throws std::ios_base::failure if the location does not correspond to a valid TIFF fileinline point2<std::ptrdiff_t> tiff_read_dimensions(const std::string& filename) { return tiff_read_dimensions(filename.c_str());}/// \ingroup TIFF_IO/// \brief Loads the image specified by the given tiff image file name into the given view./// Triggers a compile assert if the view color space and channel depth are not supported by the TIFF library or by the I/O extension./// Throws std::ios_base::failure if the file is not a valid TIFF file, or if its color space or channel depth are not /// compatible with the ones specified by View, or if its dimensions don't match the ones of the view.template <typename View>inline void tiff_read_view(const char* filename,const View& view) { BOOST_STATIC_ASSERT(tiff_read_support<View>::is_supported); detail::tiff_reader m(filename); m.apply(view);}/// \ingroup TIFF_IO/// \brief Loads the image specified by the given tiff image file name into the given view.template <typename View>inline void tiff_read_view(const std::string& filename,const View& view) { tiff_read_view(filename.c_str(),view);}/// \ingroup TIFF_IO/// \brief Allocates a new image whose dimensions are determined by the given tiff image file, and loads the pixels into it./// Triggers a compile assert if the image color space or channel depth are not supported by the TIFF library or by the I/O extension./// Throws std::ios_base::failure if the file is not a valid TIFF file, or if its color space or channel depth are not /// compatible with the ones specified by Imagetemplate <typename Image>void tiff_read_image(const char* filename,Image& im) { BOOST_STATIC_ASSERT(tiff_read_support<typename Image::view_t>::is_supported); detail::tiff_reader m(filename); m.read_image(im);}/// \ingroup TIFF_IO/// \brief Allocates a new image whose dimensions are determined by the given tiff image file, and loads the pixels into it.template <typename Image>inline void tiff_read_image(const std::string& filename,Image& im) { tiff_read_image(filename.c_str(),im);}/// \ingroup TIFF_IO/// \brief Loads and color-converts the image specified by the given tiff image file name into the given view./// Throws std::ios_base::failure if the file is not a valid TIFF file, or if its dimensions don't match the ones of the view.template <typename View,typename CC>inline void tiff_read_and_convert_view(const char* filename,const View& view,CC cc) { detail::tiff_reader_color_convert<CC> m(filename,cc); m.apply(view);}/// \ingroup TIFF_IO/// \brief Loads and color-converts the image specified by the given tiff image file name into the given view./// Throws std::ios_base::failure if the file is not a valid TIFF file, or if its dimensions don't match the ones of the view.template <typename View>inline void tiff_read_and_convert_view(const char* filename,const View& view) { detail::tiff_reader_color_convert<default_color_converter> m(filename,default_color_converter()); m.apply(view);}/// \ingroup TIFF_IO/// \brief Loads and color-converts the image specified by the given tiff image file name into the given view.template <typename View,typename CC>inline void tiff_read_and_convert_view(const std::string& filename,const View& view,CC cc) { tiff_read_and_convert_view(filename.c_str(),view,cc);}/// \ingroup TIFF_IO/// \brief Loads and color-converts the image specified by the given tiff image file name into the given view.template <typename View>inline void tiff_read_and_convert_view(const std::string& filename,const View& view) { tiff_read_and_convert_view(filename.c_str(),view);}/// \ingroup TIFF_IO/// \brief Allocates a new image whose dimensions are determined by the given tiff image file, loads and color-converts the pixels into it./// Throws std::ios_base::failure if the file is not a valid TIFF filetemplate <typename Image,typename CC>void tiff_read_and_convert_image(const char* filename,Image& im,CC cc) { detail::tiff_reader_color_convert<CC> m(filename,cc); m.read_image(im);}/// \ingroup TIFF_IO/// \brief Allocates a new image whose dimensions are determined by the given tiff image file, loads and color-converts the pixels into it./// Throws std::ios_base::failure if the file is not a valid TIFF filetemplate <typename Image>void tiff_read_and_convert_image(const char* filename,Image& im) { detail::tiff_reader_color_convert<default_color_converter> m(filename,default_color_converter()); m.read_image(im);}/// \ingroup TIFF_IO/// \brief Allocates a new image whose dimensions are determined by the given tiff image file, loads and color-converts the pixels into it.template <typename Image,typename CC>inline void tiff_read_and_convert_image(const std::string& filename,Image& im,CC cc) { tiff_read_and_convert_image(filename.c_str(),im,cc);}/// \ingroup TIFF_IO/// \brief Allocates a new image whose dimensions are determined by the given tiff image file, loads and color-converts the pixels into it.template <typename Image>inline void tiff_read_and_convert_image(const std::string& filename,Image& im) { tiff_read_and_convert_image(filename.c_str(),im);}/// \ingroup TIFF_IO/// \brief Determines whether the given view type is supported for writingtemplate <typename View>struct tiff_write_support { BOOST_STATIC_CONSTANT(bool,is_supported= (detail::tiff_write_support_private<typename channel_type<View>::type, typename color_space_type<View>::type>::is_supported)); BOOST_STATIC_CONSTANT(int,bit_depth= (detail::tiff_write_support_private<typename channel_type<View>::type, typename color_space_type<View>::type>::bit_depth)); BOOST_STATIC_CONSTANT(int,color_type= (detail::tiff_write_support_private<typename channel_type<View>::type, typename color_space_type<View>::type>::color_type)); BOOST_STATIC_CONSTANT(bool, value=is_supported);};/// \ingroup TIFF_IO/// \brief Saves the view to a tiff file specified by the given tiff image file name./// Triggers a compile assert if the view color space and channel depth are not supported by the TIFF library or by the I/O extension./// Throws std::ios_base::failure if it fails to create the file.template <typename View>inline void tiff_write_view(const char* filename,const View& view) { BOOST_STATIC_ASSERT(tiff_write_support<View>::is_supported); detail::tiff_writer m(filename); m.apply(view);}/// \ingroup TIFF_IO/// \brief Saves the view to a tiff file specified by the given tiff image file name.template <typename View>inline void tiff_write_view(const std::string& filename,const View& view) { tiff_write_view(filename.c_str(),view);}} } // namespace boost::gil#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -