📄 png_io_private.hpp
字号:
png_uint_32 width, height; int bit_depth, color_type, interlace_type; png_get_IHDR(_png_ptr, _info_ptr, &width, &height,&bit_depth,&color_type,&interlace_type, int_p_NULL, int_p_NULL); io_error_if(((png_uint_32)view.width()!=width || (png_uint_32)view.height()!= height), "png_read_view: input view size does not match PNG file size"); if(png_read_support_private<typename channel_type<View>::type, typename color_space_type<View>::type>::bit_depth!=bit_depth || png_read_support_private<typename channel_type<View>::type, typename color_space_type<View>::type>::color_type!=color_type) io_error("png_read_view: input view type is incompatible with the image type"); std::vector<pixel<typename channel_type<View>::type, layout<typename color_space_type<View>::type> > > row(width); for(png_uint_32 y=0;y<height;++y) { png_read_row(_png_ptr,(png_bytep)&row.front(),NULL); std::copy(row.begin(),row.end(),view.row_begin(y)); } png_read_end(_png_ptr,NULL); } template <typename Image> void read_image(Image& im) { im.recreate(get_dimensions()); apply(view(im)); }};// This code will be simplified...template <typename CC>class png_reader_color_convert : public png_reader {private: CC _cc;public: png_reader_color_convert(FILE* file ,CC cc_in) : png_reader(file),_cc(cc_in) {} png_reader_color_convert(FILE* file ) : png_reader(file) {} png_reader_color_convert(const char* filename,CC cc_in) : png_reader(filename),_cc(cc_in) {} png_reader_color_convert(const char* filename) : png_reader(filename) {} template <typename View> void apply(const View& view) { png_uint_32 width, height; int bit_depth, color_type, interlace_type; png_get_IHDR(_png_ptr, _info_ptr, &width, &height,&bit_depth,&color_type,&interlace_type, int_p_NULL, int_p_NULL); io_error_if(((png_uint_32)view.width()!=width || (png_uint_32)view.height()!= height), "png_reader_color_convert::apply(): input view size does not match PNG file size"); switch (color_type) { case PNG_COLOR_TYPE_GRAY: switch (bit_depth) { case 8: { std::vector<gray8_pixel_t> row(width); for(png_uint_32 y=0;y<height;++y) { png_read_row(_png_ptr,(png_bytep)&row.front(),NULL); std::transform(row.begin(),row.end(),view.row_begin(y),color_convert_deref_fn<gray8_ref_t,typename View::value_type,CC>(_cc)); } break; } case 16: { std::vector<gray16_pixel_t> row(width); for(png_uint_32 y=0;y<height;++y) { png_read_row(_png_ptr,(png_bytep)&row.front(),NULL); std::transform(row.begin(),row.end(),view.row_begin(y),color_convert_deref_fn<gray16_ref_t,typename View::value_type,CC>(_cc)); } break; } default: io_error("png_reader_color_convert::apply(): unknown combination of color type and bit depth"); } break; case PNG_COLOR_TYPE_RGB: switch (bit_depth) { case 8: { std::vector<rgb8_pixel_t> row(width); for(png_uint_32 y=0;y<height;++y) { png_read_row(_png_ptr,(png_bytep)&row.front(),NULL); std::transform(row.begin(),row.end(),view.row_begin(y),color_convert_deref_fn<rgb8_ref_t,typename View::value_type,CC>(_cc)); } break; } case 16: { std::vector<rgb16_pixel_t> row(width); for(png_uint_32 y=0;y<height;++y) { png_read_row(_png_ptr,(png_bytep)&row.front(),NULL); std::transform(row.begin(),row.end(),view.row_begin(y),color_convert_deref_fn<rgb16_ref_t,typename View::value_type,CC>(_cc)); } break; } default: io_error("png_reader_color_convert::apply(): unknown combination of color type and bit depth"); } break; case PNG_COLOR_TYPE_RGBA: switch (bit_depth) { case 8: { std::vector<rgba8_pixel_t> row(width); for(png_uint_32 y=0;y<height;++y) { png_read_row(_png_ptr,(png_bytep)&row.front(),NULL); std::transform(row.begin(),row.end(),view.row_begin(y),color_convert_deref_fn<rgba8_ref_t,typename View::value_type,CC>(_cc)); } break; } case 16: { std::vector<rgba16_pixel_t> row(width); for(png_uint_32 y=0;y<height;++y) { png_read_row(_png_ptr,(png_bytep)&row.front(),NULL); std::transform(row.begin(),row.end(),view.row_begin(y),color_convert_deref_fn<rgba16_ref_t,typename View::value_type,CC>(_cc)); } break; } default: io_error("png_reader_color_convert::apply(): unknown combination of color type and bit depth"); } break; default: io_error("png_reader_color_convert::apply(): unknown color type"); } png_read_end(_png_ptr,NULL); } template <typename Image> void read_image(Image& im) { im.recreate(get_dimensions()); apply(view(im)); }};class png_writer : public file_mgr {protected: png_structp _png_ptr; png_infop _info_ptr; void init() { _png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL); io_error_if(!_png_ptr,"png_write_initialize: fail to call png_create_write_struct()"); _info_ptr = png_create_info_struct(_png_ptr); if (!_info_ptr) { png_destroy_write_struct(&_png_ptr,png_infopp_NULL); io_error("png_write_initialize: fail to call png_create_info_struct()"); } if (setjmp(png_jmpbuf(_png_ptr))) { png_destroy_write_struct(&_png_ptr, &_info_ptr); io_error("png_write_initialize: fail to call setjmp(png_jmpbuf())"); } png_init_io(_png_ptr,get()); }public: png_writer(FILE* file ) : file_mgr(file) { init(); } png_writer(const char* filename) : file_mgr(filename, "wb") { init(); } ~png_writer() { png_destroy_write_struct(&_png_ptr,&_info_ptr); } template <typename View> void apply(const View& view) { png_set_IHDR(_png_ptr, _info_ptr, view.width(), view.height(), png_write_support_private<typename channel_type<View>::type, typename color_space_type<View>::type>::bit_depth, png_write_support_private<typename channel_type<View>::type, typename color_space_type<View>::type>::color_type, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,PNG_FILTER_TYPE_DEFAULT); png_write_info(_png_ptr,_info_ptr); if (little_endian() && png_write_support_private<typename channel_type<View>::type, typename color_space_type<View>::type>::bit_depth>8) png_set_swap(_png_ptr); 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()); png_write_row(_png_ptr,(png_bytep)&row.front()); } png_write_end(_png_ptr,_info_ptr); }};} // namespace detail} } // namespace boost::gil#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -