png++
0.2.9
|
00001 /* 00002 * Copyright (C) 2007,2008 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_CONSUMER_HPP_INCLUDED 00032 #define PNGPP_CONSUMER_HPP_INCLUDED 00033 00034 #include <cassert> 00035 #include <stdexcept> 00036 #include <iostream> 00037 #include <istream> 00038 00039 #include "config.hpp" 00040 #include "error.hpp" 00041 #include "streaming_base.hpp" 00042 #include "reader.hpp" 00043 #include "pixel_buffer.hpp" 00044 00045 namespace png 00046 { 00047 00121 template< typename pixel, 00122 class pixcon, 00123 class info_holder = def_image_info_holder, 00124 bool interlacing_supported = false > 00125 class consumer 00126 : public streaming_base< pixel, info_holder > 00127 { 00128 public: 00129 typedef pixel_traits< pixel > traits; 00130 00134 struct transform_identity 00135 { 00136 void operator()(io_base&) const {} 00137 }; 00138 00143 template< typename istream > 00144 void read(istream& stream) 00145 { 00146 read(stream, transform_identity()); 00147 } 00148 00157 template< typename istream, class transformation > 00158 void read(istream& stream, transformation const& transform) 00159 { 00160 reader< istream > rd(stream); 00161 rd.read_info(); 00162 transform(rd); 00163 00164 #if __BYTE_ORDER == __LITTLE_ENDIAN 00165 if (pixel_traits< pixel >::get_bit_depth() == 16) 00166 { 00167 #ifdef PNG_READ_SWAP_SUPPORTED 00168 rd.set_swap(); 00169 #else 00170 throw error("Cannot read 16-bit image: recompile with PNG_READ_SWAP_SUPPORTED."); 00171 #endif 00172 } 00173 #endif 00174 00175 // interlace handling _must_ be set up prior to info update 00176 size_t pass_count; 00177 if (rd.get_interlace_type() != interlace_none) 00178 { 00179 #ifdef PNG_READ_INTERLACING_SUPPORTED 00180 pass_count = rd.set_interlace_handling(); 00181 #else 00182 throw error("Cannot read interlaced image: interlace handling disabled."); 00183 #endif 00184 } 00185 else 00186 { 00187 pass_count = 1; 00188 } 00189 00190 rd.update_info(); 00191 if (rd.get_color_type() != traits::get_color_type() 00192 || rd.get_bit_depth() != traits::get_bit_depth()) 00193 { 00194 throw std::logic_error("color type and/or bit depth mismatch" 00195 " in png::consumer::read()"); 00196 } 00197 00198 this->get_info() = rd.get_image_info(); 00199 00200 pixcon* pixel_con = static_cast< pixcon* >(this); 00201 if (pass_count > 1 && !interlacing_supported) 00202 { 00203 skip_interlaced_rows(rd, pass_count); 00204 pass_count = 1; 00205 } 00206 read_rows(rd, pass_count, pixel_con); 00207 00208 rd.read_end_info(); 00209 } 00210 00211 protected: 00212 typedef streaming_base< pixel, info_holder > base; 00213 00218 explicit consumer(image_info& info) 00219 : base(info) 00220 { 00221 } 00222 00223 private: 00224 template< typename istream > 00225 void skip_interlaced_rows(reader< istream >& rd, size_t pass_count) 00226 { 00227 typedef std::vector< pixel > row; 00228 typedef row_traits< row > row_traits_type; 00229 row dummy_row(this->get_info().get_width()); 00230 for (size_t pass = 1; pass < pass_count; ++pass) 00231 { 00232 rd.read_row(reinterpret_cast< byte* > 00233 (row_traits_type::get_data(dummy_row))); 00234 } 00235 } 00236 00237 template< typename istream > 00238 void read_rows(reader< istream >& rd, size_t pass_count, 00239 pixcon* pixel_con) 00240 { 00241 for (size_t pass = 0; pass < pass_count; ++pass) 00242 { 00243 pixel_con->reset(pass); 00244 00245 for (uint_32 pos = 0; pos < this->get_info().get_height(); ++pos) 00246 { 00247 rd.read_row(pixel_con->get_next_row(pos)); 00248 } 00249 } 00250 } 00251 }; 00252 00253 } // namespace png 00254 00255 #endif // PNGPP_CONSUMER_HPP_INCLUDED