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         }
00079 
00080         void write() const
00081         {
00082             assert(m_png);
00083             assert(m_info);
00084 
00085             sync_ihdr();
00086             if (! m_palette.empty())
00087             {
00088                 png_set_PLTE(m_png, m_info,
00089                              const_cast< color* >(& m_palette[0]),
00090                              m_palette.size());
00091             }
00092             png_write_info(m_png, m_info);
00093         }
00094 
00095         void update()
00096         {
00097             assert(m_png);
00098             assert(m_info);
00099 
00100             sync_ihdr();
00101             png_read_update_info(m_png, m_info);
00102         }
00103 
00104     protected:
00105         void sync_ihdr(void) const
00106         {
00107             png_set_IHDR(m_png,
00108                          m_info,
00109                          m_width,
00110                          m_height,
00111                          m_bit_depth,
00112                          m_color_type,
00113                          m_interlace_type,
00114                          m_compression_type,
00115                          m_filter_type);
00116         }
00117     };
00118 
00119 } // namespace png
00120 
00121 #endif // PNGPP_INFO_HPP_INCLUDED

Generated on Sun Jul 8 19:33:00 2007 for png++ by  doxygen 1.5.2