annotate DEPENDENCIES/generic/include/boost/gil/extension/io/png_io_private.hpp @ 133:4acb5d8d80b6 tip

Don't fail environmental check if README.md exists (but .txt and no-suffix don't)
author Chris Cannam
date Tue, 30 Jul 2019 12:25:44 +0100
parents 2665513ce2d3
children
rev   line source
Chris@16 1 /*
Chris@16 2 Copyright 2005-2007 Adobe Systems Incorporated
Chris@16 3
Chris@16 4 Use, modification and distribution are subject to the Boost Software License,
Chris@16 5 Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
Chris@16 6 http://www.boost.org/LICENSE_1_0.txt).
Chris@16 7
Chris@16 8 See http://stlab.adobe.com/gil for most recent version including documentation.
Chris@16 9 */
Chris@16 10 /*************************************************************************************************/
Chris@16 11
Chris@16 12 #ifndef GIL_PNG_IO_PRIVATE_H
Chris@16 13 #define GIL_PNG_IO_PRIVATE_H
Chris@16 14
Chris@16 15 /// \file
Chris@16 16 /// \brief Internal support for reading and writing PNG files
Chris@16 17 /// \author Hailin Jin and Lubomir Bourdev \n
Chris@16 18 /// Adobe Systems Incorporated
Chris@16 19 /// \date 2005-2007 \n Last updated August 14, 2007
Chris@16 20
Chris@16 21 #include <algorithm>
Chris@16 22 #include <vector>
Chris@16 23 #include <boost/static_assert.hpp>
Chris@16 24 #include "../../gil_all.hpp"
Chris@16 25 #include "io_error.hpp"
Chris@16 26 #include <png.h>
Chris@16 27
Chris@16 28 namespace boost { namespace gil {
Chris@16 29
Chris@16 30 namespace detail {
Chris@16 31
Chris@16 32 static const std::size_t PNG_BYTES_TO_CHECK = 4;
Chris@16 33
Chris@16 34 // lbourdev: These can be greatly simplified, for example:
Chris@16 35 template <typename Cs> struct png_color_type {BOOST_STATIC_CONSTANT(int,color_type=0);};
Chris@16 36 template<> struct png_color_type<gray_t> { BOOST_STATIC_CONSTANT(int,color_type=PNG_COLOR_TYPE_GRAY); };
Chris@16 37 template<> struct png_color_type<rgb_t> { BOOST_STATIC_CONSTANT(int,color_type=PNG_COLOR_TYPE_RGB); };
Chris@16 38 template<> struct png_color_type<rgba_t> { BOOST_STATIC_CONSTANT(int,color_type=PNG_COLOR_TYPE_RGBA); };
Chris@16 39
Chris@16 40 template <typename Channel,typename ColorSpace> struct png_is_supported {BOOST_STATIC_CONSTANT(bool,value=false);};
Chris@16 41 template <> struct png_is_supported<bits8,gray_t> {BOOST_STATIC_CONSTANT(bool,value=true);};
Chris@16 42 template <> struct png_is_supported<bits8,rgb_t> {BOOST_STATIC_CONSTANT(bool,value=true);};
Chris@16 43 template <> struct png_is_supported<bits8,rgba_t> {BOOST_STATIC_CONSTANT(bool,value=true);};
Chris@16 44 template <> struct png_is_supported<bits16,gray_t> {BOOST_STATIC_CONSTANT(bool,value=true);};
Chris@16 45 template <> struct png_is_supported<bits16,rgb_t> {BOOST_STATIC_CONSTANT(bool,value=true);};
Chris@16 46 template <> struct png_is_supported<bits16,rgba_t> {BOOST_STATIC_CONSTANT(bool,value=true);};
Chris@16 47
Chris@16 48 template <typename Channel> struct png_bit_depth {BOOST_STATIC_CONSTANT(int,bit_depth=sizeof(Channel)*8);};
Chris@16 49
Chris@16 50 template <typename Channel,typename ColorSpace>
Chris@16 51 struct png_read_support_private {
Chris@16 52 BOOST_STATIC_CONSTANT(bool,is_supported=false);
Chris@16 53 BOOST_STATIC_CONSTANT(int,bit_depth=0);
Chris@16 54 BOOST_STATIC_CONSTANT(int,color_type=0);
Chris@16 55 };
Chris@16 56 template <>
Chris@16 57 struct png_read_support_private<bits8,gray_t> {
Chris@16 58 BOOST_STATIC_CONSTANT(bool,is_supported=true);
Chris@16 59 BOOST_STATIC_CONSTANT(int,bit_depth=8);
Chris@16 60 BOOST_STATIC_CONSTANT(int,color_type=PNG_COLOR_TYPE_GRAY);
Chris@16 61 };
Chris@16 62 template <>
Chris@16 63 struct png_read_support_private<bits8,rgb_t> {
Chris@16 64 BOOST_STATIC_CONSTANT(bool,is_supported=true);
Chris@16 65 BOOST_STATIC_CONSTANT(int,bit_depth=8);
Chris@16 66 BOOST_STATIC_CONSTANT(int,color_type=PNG_COLOR_TYPE_RGB);
Chris@16 67 };
Chris@16 68 template <>
Chris@16 69 struct png_read_support_private<bits8,rgba_t> {
Chris@16 70 BOOST_STATIC_CONSTANT(bool,is_supported=true);
Chris@16 71 BOOST_STATIC_CONSTANT(int,bit_depth=8);
Chris@16 72 BOOST_STATIC_CONSTANT(int,color_type=PNG_COLOR_TYPE_RGBA);
Chris@16 73 };
Chris@16 74 template <>
Chris@16 75 struct png_read_support_private<bits16,gray_t> {
Chris@16 76 BOOST_STATIC_CONSTANT(bool,is_supported=true);
Chris@16 77 BOOST_STATIC_CONSTANT(int,bit_depth=16);
Chris@16 78 BOOST_STATIC_CONSTANT(int,color_type=PNG_COLOR_TYPE_GRAY);
Chris@16 79 };
Chris@16 80 template <>
Chris@16 81 struct png_read_support_private<bits16,rgb_t> {
Chris@16 82 BOOST_STATIC_CONSTANT(bool,is_supported=true);
Chris@16 83 BOOST_STATIC_CONSTANT(int,bit_depth=16);
Chris@16 84 BOOST_STATIC_CONSTANT(int,color_type=PNG_COLOR_TYPE_RGB);
Chris@16 85 };
Chris@16 86 template <>
Chris@16 87 struct png_read_support_private<bits16,rgba_t> {
Chris@16 88 BOOST_STATIC_CONSTANT(bool,is_supported=true);
Chris@16 89 BOOST_STATIC_CONSTANT(int,bit_depth=16);
Chris@16 90 BOOST_STATIC_CONSTANT(int,color_type=PNG_COLOR_TYPE_RGBA);
Chris@16 91 };
Chris@16 92
Chris@16 93 template <typename Channel,typename ColorSpace>
Chris@16 94 struct png_write_support_private {
Chris@16 95 BOOST_STATIC_CONSTANT(bool,is_supported=false);
Chris@16 96 BOOST_STATIC_CONSTANT(int,bit_depth=0);
Chris@16 97 BOOST_STATIC_CONSTANT(int,color_type=0);
Chris@16 98 };
Chris@16 99 template <>
Chris@16 100 struct png_write_support_private<bits8,gray_t> {
Chris@16 101 BOOST_STATIC_CONSTANT(bool,is_supported=true);
Chris@16 102 BOOST_STATIC_CONSTANT(int,bit_depth=8);
Chris@16 103 BOOST_STATIC_CONSTANT(int,color_type=PNG_COLOR_TYPE_GRAY);
Chris@16 104 };
Chris@16 105 template <>
Chris@16 106 struct png_write_support_private<bits8,rgb_t> {
Chris@16 107 BOOST_STATIC_CONSTANT(bool,is_supported=true);
Chris@16 108 BOOST_STATIC_CONSTANT(int,bit_depth=8);
Chris@16 109 BOOST_STATIC_CONSTANT(int,color_type=PNG_COLOR_TYPE_RGB);
Chris@16 110 };
Chris@16 111 template <>
Chris@16 112 struct png_write_support_private<bits8,rgba_t> {
Chris@16 113 BOOST_STATIC_CONSTANT(bool,is_supported=true);
Chris@16 114 BOOST_STATIC_CONSTANT(int,bit_depth=8);
Chris@16 115 BOOST_STATIC_CONSTANT(int,color_type=PNG_COLOR_TYPE_RGBA);
Chris@16 116 };
Chris@16 117 template <>
Chris@16 118 struct png_write_support_private<bits16,gray_t> {
Chris@16 119 BOOST_STATIC_CONSTANT(bool,is_supported=true);
Chris@16 120 BOOST_STATIC_CONSTANT(int,bit_depth=16);
Chris@16 121 BOOST_STATIC_CONSTANT(int,color_type=PNG_COLOR_TYPE_GRAY);
Chris@16 122 };
Chris@16 123 template <>
Chris@16 124 struct png_write_support_private<bits16,rgb_t> {
Chris@16 125 BOOST_STATIC_CONSTANT(bool,is_supported=true);
Chris@16 126 BOOST_STATIC_CONSTANT(int,bit_depth=16);
Chris@16 127 BOOST_STATIC_CONSTANT(int,color_type=PNG_COLOR_TYPE_RGB);
Chris@16 128 };
Chris@16 129 template <>
Chris@16 130 struct png_write_support_private<bits16,rgba_t> {
Chris@16 131 BOOST_STATIC_CONSTANT(bool,is_supported=true);
Chris@16 132 BOOST_STATIC_CONSTANT(int,bit_depth=16);
Chris@16 133 BOOST_STATIC_CONSTANT(int,color_type=PNG_COLOR_TYPE_RGBA);
Chris@16 134 };
Chris@16 135
Chris@16 136 class png_reader : public file_mgr {
Chris@16 137 protected:
Chris@16 138 png_structp _png_ptr;
Chris@16 139 png_infop _info_ptr;
Chris@16 140
Chris@16 141 void init() {
Chris@16 142 char buf[PNG_BYTES_TO_CHECK];
Chris@16 143 // read in some of the signature bytes
Chris@16 144 io_error_if(fread(buf, 1, PNG_BYTES_TO_CHECK, get()) != detail::PNG_BYTES_TO_CHECK,
Chris@16 145 "png_check_validity: fail to read file");
Chris@16 146 // compare the first PNG_BYTES_TO_CHECK bytes of the signature.
Chris@16 147 io_error_if(png_sig_cmp((png_bytep)buf, (png_size_t)0, detail::PNG_BYTES_TO_CHECK)!=0,
Chris@16 148 "png_check_validity: invalid png file");
Chris@16 149
Chris@16 150 _png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
Chris@16 151 io_error_if(_png_ptr==NULL,"png_get_file_size: fail to call png_create_write_struct()");
Chris@16 152 // allocate/initialize the image information data
Chris@16 153 _info_ptr = png_create_info_struct(_png_ptr);
Chris@16 154 if (_info_ptr == NULL) {
Chris@16 155 png_destroy_read_struct(&_png_ptr,NULL,NULL);
Chris@16 156 io_error("png_get_file_size: fail to call png_create_info_struct()");
Chris@16 157 }
Chris@16 158 if (setjmp(png_jmpbuf(_png_ptr))) {
Chris@16 159 //free all of the memory associated with the png_ptr and info_ptr
Chris@16 160 png_destroy_read_struct(&_png_ptr, &_info_ptr, NULL);
Chris@16 161 io_error("png_get_file_size: fail to call setjmp()");
Chris@16 162 }
Chris@16 163 png_init_io(_png_ptr, get());
Chris@16 164 png_set_sig_bytes(_png_ptr,PNG_BYTES_TO_CHECK);
Chris@16 165 png_read_info(_png_ptr, _info_ptr);
Chris@16 166 if (little_endian() && png_get_bit_depth(_png_ptr,_info_ptr)>8)
Chris@16 167 png_set_swap(_png_ptr);
Chris@16 168 }
Chris@16 169 public:
Chris@16 170 png_reader(FILE* file ) : file_mgr(file) { init(); }
Chris@16 171 png_reader(const char* filename) : file_mgr(filename, "rb") { init(); }
Chris@16 172
Chris@16 173 ~png_reader() {
Chris@16 174 png_destroy_read_struct(&_png_ptr,&_info_ptr,NULL);
Chris@16 175 }
Chris@16 176 point2<std::ptrdiff_t> get_dimensions() {
Chris@16 177 return point2<std::ptrdiff_t>(png_get_image_width(_png_ptr,_info_ptr),
Chris@16 178 png_get_image_height(_png_ptr,_info_ptr));
Chris@16 179 }
Chris@16 180 template <typename View>
Chris@16 181 void apply(const View& view) {
Chris@16 182 png_uint_32 width, height;
Chris@16 183 int bit_depth, color_type, interlace_type;
Chris@16 184 png_get_IHDR(_png_ptr, _info_ptr,
Chris@16 185 &width, &height,&bit_depth,&color_type,&interlace_type,
Chris@16 186 NULL, NULL);
Chris@16 187 io_error_if(((png_uint_32)view.width()!=width || (png_uint_32)view.height()!= height),
Chris@16 188 "png_read_view: input view size does not match PNG file size");
Chris@16 189
Chris@16 190 if(png_read_support_private<typename channel_type<View>::type,
Chris@16 191 typename color_space_type<View>::type>::bit_depth!=bit_depth ||
Chris@16 192 png_read_support_private<typename channel_type<View>::type,
Chris@16 193 typename color_space_type<View>::type>::color_type!=color_type)
Chris@16 194 io_error("png_read_view: input view type is incompatible with the image type");
Chris@16 195
Chris@16 196 std::vector<pixel<typename channel_type<View>::type,
Chris@16 197 layout<typename color_space_type<View>::type> > > row(width);
Chris@16 198 for(png_uint_32 y=0;y<height;++y) {
Chris@16 199 png_read_row(_png_ptr,(png_bytep)&row.front(),NULL);
Chris@16 200 std::copy(row.begin(),row.end(),view.row_begin(y));
Chris@16 201 }
Chris@16 202 png_read_end(_png_ptr,NULL);
Chris@16 203 }
Chris@16 204
Chris@16 205 template <typename Image>
Chris@16 206 void read_image(Image& im) {
Chris@16 207 im.recreate(get_dimensions());
Chris@16 208 apply(view(im));
Chris@16 209 }
Chris@16 210 };
Chris@16 211
Chris@16 212 // This code will be simplified...
Chris@16 213 template <typename CC>
Chris@16 214 class png_reader_color_convert : public png_reader {
Chris@16 215 private:
Chris@16 216 CC _cc;
Chris@16 217 public:
Chris@16 218 png_reader_color_convert(FILE* file ,CC cc_in) : png_reader(file),_cc(cc_in) {}
Chris@16 219 png_reader_color_convert(FILE* file ) : png_reader(file) {}
Chris@16 220 png_reader_color_convert(const char* filename,CC cc_in) : png_reader(filename),_cc(cc_in) {}
Chris@16 221 png_reader_color_convert(const char* filename) : png_reader(filename) {}
Chris@16 222 template <typename View>
Chris@16 223 void apply(const View& view) {
Chris@16 224 png_uint_32 width, height;
Chris@16 225 int bit_depth, color_type, interlace_type;
Chris@16 226 png_get_IHDR(_png_ptr, _info_ptr,
Chris@16 227 &width, &height,&bit_depth,&color_type,&interlace_type,
Chris@16 228 int_p_NULL, int_p_NULL);
Chris@16 229 io_error_if(((png_uint_32)view.width()!=width || (png_uint_32)view.height()!= height),
Chris@16 230 "png_reader_color_convert::apply(): input view size does not match PNG file size");
Chris@16 231 switch (color_type) {
Chris@16 232 case PNG_COLOR_TYPE_GRAY:
Chris@16 233 switch (bit_depth) {
Chris@16 234 case 8: {
Chris@16 235 std::vector<gray8_pixel_t> row(width);
Chris@16 236 for(png_uint_32 y=0;y<height;++y) {
Chris@16 237 png_read_row(_png_ptr,(png_bytep)&row.front(),NULL);
Chris@16 238 std::transform(row.begin(),row.end(),view.row_begin(y),color_convert_deref_fn<gray8_ref_t,typename View::value_type,CC>(_cc));
Chris@16 239 }
Chris@16 240 break;
Chris@16 241 }
Chris@16 242 case 16: {
Chris@16 243 std::vector<gray16_pixel_t> row(width);
Chris@16 244 for(png_uint_32 y=0;y<height;++y) {
Chris@16 245 png_read_row(_png_ptr,(png_bytep)&row.front(),NULL);
Chris@16 246 std::transform(row.begin(),row.end(),view.row_begin(y),color_convert_deref_fn<gray16_ref_t,typename View::value_type,CC>(_cc));
Chris@16 247 }
Chris@16 248 break;
Chris@16 249 }
Chris@16 250 default: io_error("png_reader_color_convert::apply(): unknown combination of color type and bit depth");
Chris@16 251 }
Chris@16 252 break;
Chris@16 253 case PNG_COLOR_TYPE_RGB:
Chris@16 254 switch (bit_depth) {
Chris@16 255 case 8: {
Chris@16 256 std::vector<rgb8_pixel_t> row(width);
Chris@16 257 for(png_uint_32 y=0;y<height;++y) {
Chris@16 258 png_read_row(_png_ptr,(png_bytep)&row.front(),NULL);
Chris@16 259 std::transform(row.begin(),row.end(),view.row_begin(y),color_convert_deref_fn<rgb8_ref_t,typename View::value_type,CC>(_cc));
Chris@16 260 }
Chris@16 261 break;
Chris@16 262 }
Chris@16 263 case 16: {
Chris@16 264 std::vector<rgb16_pixel_t> row(width);
Chris@16 265 for(png_uint_32 y=0;y<height;++y) {
Chris@16 266 png_read_row(_png_ptr,(png_bytep)&row.front(),NULL);
Chris@16 267 std::transform(row.begin(),row.end(),view.row_begin(y),color_convert_deref_fn<rgb16_ref_t,typename View::value_type,CC>(_cc));
Chris@16 268 }
Chris@16 269 break;
Chris@16 270 }
Chris@16 271 default: io_error("png_reader_color_convert::apply(): unknown combination of color type and bit depth");
Chris@16 272 }
Chris@16 273 break;
Chris@16 274 case PNG_COLOR_TYPE_RGBA:
Chris@16 275 switch (bit_depth) {
Chris@16 276 case 8: {
Chris@16 277 std::vector<rgba8_pixel_t> row(width);
Chris@16 278 for(png_uint_32 y=0;y<height;++y) {
Chris@16 279 png_read_row(_png_ptr,(png_bytep)&row.front(),NULL);
Chris@16 280 std::transform(row.begin(),row.end(),view.row_begin(y),color_convert_deref_fn<rgba8_ref_t,typename View::value_type,CC>(_cc));
Chris@16 281 }
Chris@16 282 break;
Chris@16 283 }
Chris@16 284 case 16: {
Chris@16 285 std::vector<rgba16_pixel_t> row(width);
Chris@16 286 for(png_uint_32 y=0;y<height;++y) {
Chris@16 287 png_read_row(_png_ptr,(png_bytep)&row.front(),NULL);
Chris@16 288 std::transform(row.begin(),row.end(),view.row_begin(y),color_convert_deref_fn<rgba16_ref_t,typename View::value_type,CC>(_cc));
Chris@16 289 }
Chris@16 290 break;
Chris@16 291 }
Chris@16 292 default: io_error("png_reader_color_convert::apply(): unknown combination of color type and bit depth");
Chris@16 293 }
Chris@16 294 break;
Chris@16 295 default: io_error("png_reader_color_convert::apply(): unknown color type");
Chris@16 296 }
Chris@16 297 png_read_end(_png_ptr,NULL);
Chris@16 298 }
Chris@16 299 template <typename Image>
Chris@16 300 void read_image(Image& im) {
Chris@16 301 im.recreate(get_dimensions());
Chris@16 302 apply(view(im));
Chris@16 303 }
Chris@16 304 };
Chris@16 305
Chris@16 306
Chris@16 307 class png_writer : public file_mgr {
Chris@16 308 protected:
Chris@16 309 png_structp _png_ptr;
Chris@16 310 png_infop _info_ptr;
Chris@16 311
Chris@16 312 void init() {
Chris@16 313 _png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
Chris@16 314 io_error_if(!_png_ptr,"png_write_initialize: fail to call png_create_write_struct()");
Chris@16 315 _info_ptr = png_create_info_struct(_png_ptr);
Chris@16 316 if (!_info_ptr) {
Chris@16 317 png_destroy_write_struct(&_png_ptr,NULL);
Chris@16 318 io_error("png_write_initialize: fail to call png_create_info_struct()");
Chris@16 319 }
Chris@16 320 if (setjmp(png_jmpbuf(_png_ptr))) {
Chris@16 321 png_destroy_write_struct(&_png_ptr, &_info_ptr);
Chris@16 322 io_error("png_write_initialize: fail to call setjmp(png_jmpbuf())");
Chris@16 323 }
Chris@16 324 png_init_io(_png_ptr,get());
Chris@16 325 }
Chris@16 326 public:
Chris@16 327 png_writer(FILE* file ) : file_mgr(file) { init(); }
Chris@16 328 png_writer(const char* filename) : file_mgr(filename, "wb") { init(); }
Chris@16 329
Chris@16 330 ~png_writer() {
Chris@16 331 png_destroy_write_struct(&_png_ptr,&_info_ptr);
Chris@16 332 }
Chris@16 333 template <typename View>
Chris@16 334 void apply(const View& view) {
Chris@16 335 png_set_IHDR(_png_ptr, _info_ptr, view.width(), view.height(),
Chris@16 336 png_write_support_private<typename channel_type<View>::type,
Chris@16 337 typename color_space_type<View>::type>::bit_depth,
Chris@16 338 png_write_support_private<typename channel_type<View>::type,
Chris@16 339 typename color_space_type<View>::type>::color_type,
Chris@16 340 PNG_INTERLACE_NONE,
Chris@16 341 PNG_COMPRESSION_TYPE_DEFAULT,PNG_FILTER_TYPE_DEFAULT);
Chris@16 342 png_write_info(_png_ptr,_info_ptr);
Chris@16 343 if (little_endian() &&
Chris@16 344 png_write_support_private<typename channel_type<View>::type,
Chris@16 345 typename color_space_type<View>::type>::bit_depth>8)
Chris@16 346 png_set_swap(_png_ptr);
Chris@16 347 std::vector<pixel<typename channel_type<View>::type,
Chris@16 348 layout<typename color_space_type<View>::type> > > row(view.width());
Chris@16 349 for(int y=0;y<view.height();++y) {
Chris@16 350 std::copy(view.row_begin(y),view.row_end(y),row.begin());
Chris@16 351 png_write_row(_png_ptr,(png_bytep)&row.front());
Chris@16 352 }
Chris@16 353 png_write_end(_png_ptr,_info_ptr);
Chris@16 354 }
Chris@16 355 };
Chris@16 356
Chris@16 357 } // namespace detail
Chris@16 358 } } // namespace boost::gil
Chris@16 359
Chris@16 360 #endif