Chris@16: /* Chris@16: Copyright 2005-2007 Adobe Systems Incorporated Chris@16: Chris@16: Use, modification and distribution are subject to the Boost Software License, Chris@16: Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at Chris@16: http://www.boost.org/LICENSE_1_0.txt). Chris@16: Chris@16: See http://stlab.adobe.com/gil for most recent version including documentation. Chris@16: */ Chris@16: /*************************************************************************************************/ Chris@16: Chris@16: #ifndef GIL_PNG_IO_PRIVATE_H Chris@16: #define GIL_PNG_IO_PRIVATE_H Chris@16: Chris@16: /// \file Chris@16: /// \brief Internal support for reading and writing PNG files Chris@16: /// \author Hailin Jin and Lubomir Bourdev \n Chris@16: /// Adobe Systems Incorporated Chris@16: /// \date 2005-2007 \n Last updated August 14, 2007 Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include "../../gil_all.hpp" Chris@16: #include "io_error.hpp" Chris@16: #include Chris@16: Chris@16: namespace boost { namespace gil { Chris@16: Chris@16: namespace detail { Chris@16: Chris@16: static const std::size_t PNG_BYTES_TO_CHECK = 4; Chris@16: Chris@16: // lbourdev: These can be greatly simplified, for example: Chris@16: template struct png_color_type {BOOST_STATIC_CONSTANT(int,color_type=0);}; Chris@16: template<> struct png_color_type { BOOST_STATIC_CONSTANT(int,color_type=PNG_COLOR_TYPE_GRAY); }; Chris@16: template<> struct png_color_type { BOOST_STATIC_CONSTANT(int,color_type=PNG_COLOR_TYPE_RGB); }; Chris@16: template<> struct png_color_type { BOOST_STATIC_CONSTANT(int,color_type=PNG_COLOR_TYPE_RGBA); }; Chris@16: Chris@16: template struct png_is_supported {BOOST_STATIC_CONSTANT(bool,value=false);}; Chris@16: template <> struct png_is_supported {BOOST_STATIC_CONSTANT(bool,value=true);}; Chris@16: template <> struct png_is_supported {BOOST_STATIC_CONSTANT(bool,value=true);}; Chris@16: template <> struct png_is_supported {BOOST_STATIC_CONSTANT(bool,value=true);}; Chris@16: template <> struct png_is_supported {BOOST_STATIC_CONSTANT(bool,value=true);}; Chris@16: template <> struct png_is_supported {BOOST_STATIC_CONSTANT(bool,value=true);}; Chris@16: template <> struct png_is_supported {BOOST_STATIC_CONSTANT(bool,value=true);}; Chris@16: Chris@16: template struct png_bit_depth {BOOST_STATIC_CONSTANT(int,bit_depth=sizeof(Channel)*8);}; Chris@16: Chris@16: template Chris@16: struct png_read_support_private { Chris@16: BOOST_STATIC_CONSTANT(bool,is_supported=false); Chris@16: BOOST_STATIC_CONSTANT(int,bit_depth=0); Chris@16: BOOST_STATIC_CONSTANT(int,color_type=0); Chris@16: }; Chris@16: template <> Chris@16: struct png_read_support_private { Chris@16: BOOST_STATIC_CONSTANT(bool,is_supported=true); Chris@16: BOOST_STATIC_CONSTANT(int,bit_depth=8); Chris@16: BOOST_STATIC_CONSTANT(int,color_type=PNG_COLOR_TYPE_GRAY); Chris@16: }; Chris@16: template <> Chris@16: struct png_read_support_private { Chris@16: BOOST_STATIC_CONSTANT(bool,is_supported=true); Chris@16: BOOST_STATIC_CONSTANT(int,bit_depth=8); Chris@16: BOOST_STATIC_CONSTANT(int,color_type=PNG_COLOR_TYPE_RGB); Chris@16: }; Chris@16: template <> Chris@16: struct png_read_support_private { Chris@16: BOOST_STATIC_CONSTANT(bool,is_supported=true); Chris@16: BOOST_STATIC_CONSTANT(int,bit_depth=8); Chris@16: BOOST_STATIC_CONSTANT(int,color_type=PNG_COLOR_TYPE_RGBA); Chris@16: }; Chris@16: template <> Chris@16: struct png_read_support_private { Chris@16: BOOST_STATIC_CONSTANT(bool,is_supported=true); Chris@16: BOOST_STATIC_CONSTANT(int,bit_depth=16); Chris@16: BOOST_STATIC_CONSTANT(int,color_type=PNG_COLOR_TYPE_GRAY); Chris@16: }; Chris@16: template <> Chris@16: struct png_read_support_private { Chris@16: BOOST_STATIC_CONSTANT(bool,is_supported=true); Chris@16: BOOST_STATIC_CONSTANT(int,bit_depth=16); Chris@16: BOOST_STATIC_CONSTANT(int,color_type=PNG_COLOR_TYPE_RGB); Chris@16: }; Chris@16: template <> Chris@16: struct png_read_support_private { Chris@16: BOOST_STATIC_CONSTANT(bool,is_supported=true); Chris@16: BOOST_STATIC_CONSTANT(int,bit_depth=16); Chris@16: BOOST_STATIC_CONSTANT(int,color_type=PNG_COLOR_TYPE_RGBA); Chris@16: }; Chris@16: Chris@16: template Chris@16: struct png_write_support_private { Chris@16: BOOST_STATIC_CONSTANT(bool,is_supported=false); Chris@16: BOOST_STATIC_CONSTANT(int,bit_depth=0); Chris@16: BOOST_STATIC_CONSTANT(int,color_type=0); Chris@16: }; Chris@16: template <> Chris@16: struct png_write_support_private { Chris@16: BOOST_STATIC_CONSTANT(bool,is_supported=true); Chris@16: BOOST_STATIC_CONSTANT(int,bit_depth=8); Chris@16: BOOST_STATIC_CONSTANT(int,color_type=PNG_COLOR_TYPE_GRAY); Chris@16: }; Chris@16: template <> Chris@16: struct png_write_support_private { Chris@16: BOOST_STATIC_CONSTANT(bool,is_supported=true); Chris@16: BOOST_STATIC_CONSTANT(int,bit_depth=8); Chris@16: BOOST_STATIC_CONSTANT(int,color_type=PNG_COLOR_TYPE_RGB); Chris@16: }; Chris@16: template <> Chris@16: struct png_write_support_private { Chris@16: BOOST_STATIC_CONSTANT(bool,is_supported=true); Chris@16: BOOST_STATIC_CONSTANT(int,bit_depth=8); Chris@16: BOOST_STATIC_CONSTANT(int,color_type=PNG_COLOR_TYPE_RGBA); Chris@16: }; Chris@16: template <> Chris@16: struct png_write_support_private { Chris@16: BOOST_STATIC_CONSTANT(bool,is_supported=true); Chris@16: BOOST_STATIC_CONSTANT(int,bit_depth=16); Chris@16: BOOST_STATIC_CONSTANT(int,color_type=PNG_COLOR_TYPE_GRAY); Chris@16: }; Chris@16: template <> Chris@16: struct png_write_support_private { Chris@16: BOOST_STATIC_CONSTANT(bool,is_supported=true); Chris@16: BOOST_STATIC_CONSTANT(int,bit_depth=16); Chris@16: BOOST_STATIC_CONSTANT(int,color_type=PNG_COLOR_TYPE_RGB); Chris@16: }; Chris@16: template <> Chris@16: struct png_write_support_private { Chris@16: BOOST_STATIC_CONSTANT(bool,is_supported=true); Chris@16: BOOST_STATIC_CONSTANT(int,bit_depth=16); Chris@16: BOOST_STATIC_CONSTANT(int,color_type=PNG_COLOR_TYPE_RGBA); Chris@16: }; Chris@16: Chris@16: class png_reader : public file_mgr { Chris@16: protected: Chris@16: png_structp _png_ptr; Chris@16: png_infop _info_ptr; Chris@16: Chris@16: void init() { Chris@16: char buf[PNG_BYTES_TO_CHECK]; Chris@16: // read in some of the signature bytes Chris@16: io_error_if(fread(buf, 1, PNG_BYTES_TO_CHECK, get()) != detail::PNG_BYTES_TO_CHECK, Chris@16: "png_check_validity: fail to read file"); Chris@16: // compare the first PNG_BYTES_TO_CHECK bytes of the signature. Chris@16: io_error_if(png_sig_cmp((png_bytep)buf, (png_size_t)0, detail::PNG_BYTES_TO_CHECK)!=0, Chris@16: "png_check_validity: invalid png file"); Chris@16: Chris@16: _png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL); Chris@16: io_error_if(_png_ptr==NULL,"png_get_file_size: fail to call png_create_write_struct()"); Chris@16: // allocate/initialize the image information data Chris@16: _info_ptr = png_create_info_struct(_png_ptr); Chris@16: if (_info_ptr == NULL) { Chris@16: png_destroy_read_struct(&_png_ptr,NULL,NULL); Chris@16: io_error("png_get_file_size: fail to call png_create_info_struct()"); Chris@16: } Chris@16: if (setjmp(png_jmpbuf(_png_ptr))) { Chris@16: //free all of the memory associated with the png_ptr and info_ptr Chris@16: png_destroy_read_struct(&_png_ptr, &_info_ptr, NULL); Chris@16: io_error("png_get_file_size: fail to call setjmp()"); Chris@16: } Chris@16: png_init_io(_png_ptr, get()); Chris@16: png_set_sig_bytes(_png_ptr,PNG_BYTES_TO_CHECK); Chris@16: png_read_info(_png_ptr, _info_ptr); Chris@16: if (little_endian() && png_get_bit_depth(_png_ptr,_info_ptr)>8) Chris@16: png_set_swap(_png_ptr); Chris@16: } Chris@16: public: Chris@16: png_reader(FILE* file ) : file_mgr(file) { init(); } Chris@16: png_reader(const char* filename) : file_mgr(filename, "rb") { init(); } Chris@16: Chris@16: ~png_reader() { Chris@16: png_destroy_read_struct(&_png_ptr,&_info_ptr,NULL); Chris@16: } Chris@16: point2 get_dimensions() { Chris@16: return point2(png_get_image_width(_png_ptr,_info_ptr), Chris@16: png_get_image_height(_png_ptr,_info_ptr)); Chris@16: } Chris@16: template Chris@16: void apply(const View& view) { Chris@16: png_uint_32 width, height; Chris@16: int bit_depth, color_type, interlace_type; Chris@16: png_get_IHDR(_png_ptr, _info_ptr, Chris@16: &width, &height,&bit_depth,&color_type,&interlace_type, Chris@16: NULL, NULL); Chris@16: io_error_if(((png_uint_32)view.width()!=width || (png_uint_32)view.height()!= height), Chris@16: "png_read_view: input view size does not match PNG file size"); Chris@16: Chris@16: if(png_read_support_private::type, Chris@16: typename color_space_type::type>::bit_depth!=bit_depth || Chris@16: png_read_support_private::type, Chris@16: typename color_space_type::type>::color_type!=color_type) Chris@16: io_error("png_read_view: input view type is incompatible with the image type"); Chris@16: Chris@16: std::vector::type, Chris@16: layout::type> > > row(width); Chris@16: for(png_uint_32 y=0;y Chris@16: void read_image(Image& im) { Chris@16: im.recreate(get_dimensions()); Chris@16: apply(view(im)); Chris@16: } Chris@16: }; Chris@16: Chris@16: // This code will be simplified... Chris@16: template Chris@16: class png_reader_color_convert : public png_reader { Chris@16: private: Chris@16: CC _cc; Chris@16: public: Chris@16: png_reader_color_convert(FILE* file ,CC cc_in) : png_reader(file),_cc(cc_in) {} Chris@16: png_reader_color_convert(FILE* file ) : png_reader(file) {} Chris@16: png_reader_color_convert(const char* filename,CC cc_in) : png_reader(filename),_cc(cc_in) {} Chris@16: png_reader_color_convert(const char* filename) : png_reader(filename) {} Chris@16: template Chris@16: void apply(const View& view) { Chris@16: png_uint_32 width, height; Chris@16: int bit_depth, color_type, interlace_type; Chris@16: png_get_IHDR(_png_ptr, _info_ptr, Chris@16: &width, &height,&bit_depth,&color_type,&interlace_type, Chris@16: int_p_NULL, int_p_NULL); Chris@16: io_error_if(((png_uint_32)view.width()!=width || (png_uint_32)view.height()!= height), Chris@16: "png_reader_color_convert::apply(): input view size does not match PNG file size"); Chris@16: switch (color_type) { Chris@16: case PNG_COLOR_TYPE_GRAY: Chris@16: switch (bit_depth) { Chris@16: case 8: { Chris@16: std::vector row(width); Chris@16: for(png_uint_32 y=0;y(_cc)); Chris@16: } Chris@16: break; Chris@16: } Chris@16: case 16: { Chris@16: std::vector row(width); Chris@16: for(png_uint_32 y=0;y(_cc)); Chris@16: } Chris@16: break; Chris@16: } Chris@16: default: io_error("png_reader_color_convert::apply(): unknown combination of color type and bit depth"); Chris@16: } Chris@16: break; Chris@16: case PNG_COLOR_TYPE_RGB: Chris@16: switch (bit_depth) { Chris@16: case 8: { Chris@16: std::vector row(width); Chris@16: for(png_uint_32 y=0;y(_cc)); Chris@16: } Chris@16: break; Chris@16: } Chris@16: case 16: { Chris@16: std::vector row(width); Chris@16: for(png_uint_32 y=0;y(_cc)); Chris@16: } Chris@16: break; Chris@16: } Chris@16: default: io_error("png_reader_color_convert::apply(): unknown combination of color type and bit depth"); Chris@16: } Chris@16: break; Chris@16: case PNG_COLOR_TYPE_RGBA: Chris@16: switch (bit_depth) { Chris@16: case 8: { Chris@16: std::vector row(width); Chris@16: for(png_uint_32 y=0;y(_cc)); Chris@16: } Chris@16: break; Chris@16: } Chris@16: case 16: { Chris@16: std::vector row(width); Chris@16: for(png_uint_32 y=0;y(_cc)); Chris@16: } Chris@16: break; Chris@16: } Chris@16: default: io_error("png_reader_color_convert::apply(): unknown combination of color type and bit depth"); Chris@16: } Chris@16: break; Chris@16: default: io_error("png_reader_color_convert::apply(): unknown color type"); Chris@16: } Chris@16: png_read_end(_png_ptr,NULL); Chris@16: } Chris@16: template Chris@16: void read_image(Image& im) { Chris@16: im.recreate(get_dimensions()); Chris@16: apply(view(im)); Chris@16: } Chris@16: }; Chris@16: Chris@16: Chris@16: class png_writer : public file_mgr { Chris@16: protected: Chris@16: png_structp _png_ptr; Chris@16: png_infop _info_ptr; Chris@16: Chris@16: void init() { Chris@16: _png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL); Chris@16: io_error_if(!_png_ptr,"png_write_initialize: fail to call png_create_write_struct()"); Chris@16: _info_ptr = png_create_info_struct(_png_ptr); Chris@16: if (!_info_ptr) { Chris@16: png_destroy_write_struct(&_png_ptr,NULL); Chris@16: io_error("png_write_initialize: fail to call png_create_info_struct()"); Chris@16: } Chris@16: if (setjmp(png_jmpbuf(_png_ptr))) { Chris@16: png_destroy_write_struct(&_png_ptr, &_info_ptr); Chris@16: io_error("png_write_initialize: fail to call setjmp(png_jmpbuf())"); Chris@16: } Chris@16: png_init_io(_png_ptr,get()); Chris@16: } Chris@16: public: Chris@16: png_writer(FILE* file ) : file_mgr(file) { init(); } Chris@16: png_writer(const char* filename) : file_mgr(filename, "wb") { init(); } Chris@16: Chris@16: ~png_writer() { Chris@16: png_destroy_write_struct(&_png_ptr,&_info_ptr); Chris@16: } Chris@16: template Chris@16: void apply(const View& view) { Chris@16: png_set_IHDR(_png_ptr, _info_ptr, view.width(), view.height(), Chris@16: png_write_support_private::type, Chris@16: typename color_space_type::type>::bit_depth, Chris@16: png_write_support_private::type, Chris@16: typename color_space_type::type>::color_type, Chris@16: PNG_INTERLACE_NONE, Chris@16: PNG_COMPRESSION_TYPE_DEFAULT,PNG_FILTER_TYPE_DEFAULT); Chris@16: png_write_info(_png_ptr,_info_ptr); Chris@16: if (little_endian() && Chris@16: png_write_support_private::type, Chris@16: typename color_space_type::type>::bit_depth>8) Chris@16: png_set_swap(_png_ptr); Chris@16: std::vector::type, Chris@16: layout::type> > > row(view.width()); Chris@16: for(int y=0;y