info.hpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2007   Alex Shulgin
00003  *
00004  * This file is part of png++ the C++ wrapper for libpng.  Png++ is free
00005  * software; the exact copying conditions are as follows:
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions are met:
00009  *
00010  * 1. Redistributions of source code must retain the above copyright notice,
00011  * this list of conditions and the following disclaimer.
00012  *
00013  * 2. Redistributions in binary form must reproduce the above copyright
00014  * notice, this list of conditions and the following disclaimer in the
00015  * documentation and/or other materials provided with the distribution.
00016  *
00017  * 3. The name of the author may not be used to endorse or promote products
00018  * derived from this software without specific prior written permission.
00019  *
00020  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00021  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00022  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00023  * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00024  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00025  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00026  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00027  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00028  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00029  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 } // namespace png
00151 
00152 #endif // PNGPP_INFO_HPP_INCLUDED

Generated on Sat Dec 8 13:43:56 2007 for png++ by  doxygen 1.5.3-20071008