00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #ifndef PNGPP_INFO_HPP_INCLUDED
00032 #define PNGPP_INFO_HPP_INCLUDED
00033
00034 #include <cassert>
00035 #include "info_base.hpp"
00036 #include "image_info.hpp"
00037
00038 namespace png
00039 {
00040
00045 class info
00046 : public info_base,
00047 public image_info
00048 {
00049 public:
00050 info(io_base& io, png_struct* png)
00051 : info_base(io, png)
00052 {
00053 }
00054
00055 void read()
00056 {
00057 assert(m_png);
00058 assert(m_info);
00059
00060 png_read_info(m_png, m_info);
00061 png_get_IHDR(m_png,
00062 m_info,
00063 & m_width,
00064 & m_height,
00065 reinterpret_cast< int* >(& m_bit_depth),
00066 reinterpret_cast< int* >(& m_color_type),
00067 reinterpret_cast< int* >(& m_interlace_type),
00068 reinterpret_cast< int* >(& m_compression_type),
00069 reinterpret_cast< int* >(& m_filter_type));
00070
00071 if (png_get_valid(m_png, m_info, chunk_PLTE) == chunk_PLTE)
00072 {
00073 png_color* colors = 0;
00074 int count = 0;
00075 png_get_PLTE(m_png, m_info, & colors, & count);
00076 m_palette.assign(colors, colors + count);
00077 }
00078 #ifdef PNG_tRNS_SUPPORTED
00079 if (png_get_valid(m_png, m_info, chunk_tRNS) == chunk_tRNS)
00080 {
00081 if (m_color_type == color_type_palette)
00082 {
00083 int count;
00084 byte* values;
00085 if (png_get_tRNS(m_png, m_info, & values, & count, NULL)
00086 != PNG_INFO_tRNS)
00087 {
00088 throw error("png_get_tRNS() failed");
00089 }
00090 m_tRNS.assign(values, values + count);
00091 }
00092 }
00093 #endif
00094 }
00095
00096 void write() const
00097 {
00098 assert(m_png);
00099 assert(m_info);
00100
00101 sync_ihdr();
00102 if (m_color_type == color_type_palette)
00103 {
00104 if (! m_palette.empty())
00105 {
00106 png_set_PLTE(m_png, m_info,
00107 const_cast< color* >(& m_palette[0]),
00108 m_palette.size());
00109 }
00110 if (! m_tRNS.empty())
00111 {
00112 #ifdef PNG_tRNS_SUPPORTED
00113 png_set_tRNS(m_png, m_info,
00114 const_cast< byte* >(& m_tRNS[0]),
00115 m_tRNS.size(),
00116 NULL);
00117 #else
00118 throw error("attempted to write tRNS chunk;"
00119 " recompile with PNG_tRNS_SUPPORTED");
00120 #endif
00121 }
00122 }
00123 png_write_info(m_png, m_info);
00124 }
00125
00126 void update()
00127 {
00128 assert(m_png);
00129 assert(m_info);
00130
00131 sync_ihdr();
00132 png_read_update_info(m_png, m_info);
00133 }
00134
00135 protected:
00136 void sync_ihdr(void) const
00137 {
00138 png_set_IHDR(m_png,
00139 m_info,
00140 m_width,
00141 m_height,
00142 m_bit_depth,
00143 m_color_type,
00144 m_interlace_type,
00145 m_compression_type,
00146 m_filter_type);
00147 }
00148 };
00149
00150 }
00151
00152 #endif // PNGPP_INFO_HPP_INCLUDED