png_io_private.hpp 15 KB

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