Claw 1.7.0
|
00001 /* 00002 CLAW - a C++ Library Absolutely Wonderful 00003 00004 CLAW is a free library without any particular aim but being useful to 00005 anyone. 00006 00007 Copyright (C) 2005-2011 Julien Jorge 00008 00009 This library is free software; you can redistribute it and/or 00010 modify it under the terms of the GNU Lesser General Public 00011 License as published by the Free Software Foundation; either 00012 version 2.1 of the License, or (at your option) any later version. 00013 00014 This library is distributed in the hope that it will be useful, 00015 but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 Lesser General Public License for more details. 00018 00019 You should have received a copy of the GNU Lesser General Public 00020 License along with this library; if not, write to the Free Software 00021 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00022 00023 contact: julien.jorge@gamned.org 00024 */ 00030 #ifndef __CLAW_BITMAP_HPP__ 00031 #define __CLAW_BITMAP_HPP__ 00032 00033 #include <iostream> 00034 #include <vector> 00035 #include <claw/image.hpp> 00036 #include <claw/rle_decoder.hpp> 00037 #include <claw/color_palette.hpp> 00038 #include <claw/buffered_istream.hpp> 00039 00040 namespace claw 00041 { 00042 namespace graphic 00043 { 00048 class bitmap : public image 00049 { 00050 private: 00056 class file_structure 00057 { 00058 public: 00060 typedef color_palette<rgba_pixel_8> color_palette_type; 00061 00063 enum compression 00064 { 00065 BMP_COMPRESSION_RGB = 0, 00066 BMP_COMPRESSION_RLE8 = 1, 00067 BMP_COMPRESSION_RLE4 = 2, 00068 BMP_COMPRESSION_BITFIELDS = 3 00069 }; 00070 00071 # pragma pack (push,2) 00072 00076 struct header 00077 { 00079 char id[2]; 00080 00082 unsigned int file_size; 00083 00085 unsigned int nop; 00086 00088 unsigned int data_offset; 00089 00091 unsigned int header_size; 00092 00094 unsigned int width; 00095 00097 unsigned int height; 00098 00100 unsigned short layers; 00101 00103 unsigned short bpp; 00104 00106 unsigned int compression; 00107 00109 unsigned int image_size; 00110 00112 unsigned int ppm_x; 00113 00115 unsigned int ppm_y; 00116 00118 unsigned int colors_count; 00119 00121 unsigned int importants_colors; 00122 }; 00123 # pragma pack (pop) 00124 00125 }; // class file_structure 00126 00127 public: 00128 /*----------------------------------------------------------------------*/ 00134 class reader : private file_structure 00135 { 00136 private: 00139 typedef buffered_istream<std::istream> file_input_buffer; 00140 00151 template< bool Coded4bits > 00152 class rle_bitmap_output_buffer 00153 { 00154 public: 00155 rle_bitmap_output_buffer( const color_palette_type& palette, 00156 image& image ); 00157 00158 void fill( unsigned int n, unsigned char pattern ); 00159 void copy( unsigned int n, file_input_buffer& buffer ); 00160 00161 void next_line(); 00162 void delta_move(unsigned char x, unsigned char y); 00163 00164 private: 00166 const color_palette_type& m_palette; 00167 00169 image& m_image; 00170 00172 unsigned int m_x; 00173 00175 unsigned int m_y; 00176 00177 }; // class rle_bitmap_output_buffer 00178 00195 template< typename OutputBuffer > 00196 class rle_bitmap_decoder 00197 : public rle_decoder< char, file_input_buffer, OutputBuffer > 00198 { 00199 public: 00201 typedef OutputBuffer output_buffer_type; 00202 00203 private: 00204 virtual void read_mode( file_input_buffer& input, 00205 output_buffer_type& output ); 00206 }; // class rle_bitmap_decoder 00207 00209 typedef 00210 rle_bitmap_decoder< rle_bitmap_output_buffer<true> > rle4_decoder; 00211 00213 typedef rle_bitmap_decoder< rle_bitmap_output_buffer<false> > 00214 rle8_decoder; 00215 00219 class pixel1_to_pixel32 00220 { 00221 public: 00222 void operator()( scanline& dest, const char* src, 00223 const color_palette_type& palette ) const; 00224 }; // class pixel1_to_pixel32 00225 00229 class pixel4_to_pixel32 00230 { 00231 public: 00232 void operator()( scanline& dest, const char* src, 00233 const color_palette_type& palette ) const; 00234 }; // class pixel4_to_pixel32 00235 00239 class pixel8_to_pixel32 00240 { 00241 public: 00242 void operator()( scanline& dest, const char* src, 00243 const color_palette_type& palette ) const; 00244 }; // class pixel8_to_pixel32 00245 00249 class pixel24_to_pixel32 00250 { 00251 public: 00252 void operator()( scanline& dest, const char* src, 00253 const color_palette_type& palette ) const; 00254 }; // class pixel24_to_pixel32 00255 00256 public: 00257 reader( image& img ); 00258 reader( image& img, std::istream& f ); 00259 00260 void load( std::istream& f ); 00261 00262 private: 00263 void load_palette( const header& h, std::istream& f, 00264 color_palette_type& palette ) const; 00265 00266 void load_1bpp( const header& h, std::istream& f ); 00267 void load_4bpp( const header& h, std::istream& f ); 00268 void load_8bpp( const header& h, std::istream& f ); 00269 void load_24bpp( const header& h, std::istream& f ); 00270 00271 void load_4bpp_rle( const header& h, std::istream& f, 00272 const color_palette_type& palette ); 00273 void load_4bpp_rgb( const header& h, std::istream& f, 00274 const color_palette_type& palette ); 00275 void load_8bpp_rle( const header& h, std::istream& f, 00276 const color_palette_type& palette ); 00277 void load_8bpp_rgb( const header& h, std::istream& f, 00278 const color_palette_type& palette ); 00279 00280 template<typename Convert> 00281 void load_rgb_data( std::istream& f, unsigned int buffer_size, 00282 const color_palette_type& palette, 00283 const Convert& pixel_convert ); 00284 00285 private: 00287 image& m_image; 00288 00289 }; // class reader 00290 00291 /*----------------------------------------------------------------------*/ 00296 class writer : private file_structure 00297 { 00298 public: 00299 writer( const image& img ); 00300 writer( const image& img, std::ostream& f ); 00301 00302 void save( std::ostream& f ) const; 00303 00304 private: 00305 void save_data( std::ostream& f ) const; 00306 00307 void pixel32_to_pixel24( char* dest, const scanline& src ) const; 00308 00309 void init_header( header& h ) const; 00310 00311 private: 00313 const image& m_image; 00314 00315 }; // class writer 00316 00317 public: 00318 bitmap( unsigned int w, unsigned int h ); 00319 bitmap( const image& that ); 00320 bitmap( std::istream& f ); 00321 00322 void save( std::ostream& f ) const; 00323 }; // class bitmap 00324 00325 } // namespace graphic 00326 } // namespace claw 00327 00328 #include <claw/impl/bitmap_reader.tpp> 00329 00330 #endif // __CLAW_BITMAP_HPP__