Claw  1.7.3
pcx.hpp
Go to the documentation of this file.
1 /*
2  CLAW - a C++ Library Absolutely Wonderful
3 
4  CLAW is a free library without any particular aim but being useful to
5  anyone.
6 
7  Copyright (C) 2005-2011 Julien Jorge
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 
23  contact: julien.jorge@gamned.org
24 */
30 #ifndef __CLAW_PCX_HPP__
31 #define __CLAW_PCX_HPP__
32 
33 #include <iostream>
34 #include <claw/image.hpp>
35 #include <claw/rle_decoder.hpp>
36 #include <claw/rle_encoder.hpp>
37 #include <claw/color_palette.hpp>
38 #include <claw/types.hpp>
40 
41 namespace claw
42 {
43  namespace graphic
44  {
49  class pcx : public image
50  {
51  private:
52  /* Some convenient renaming. */
53  typedef unsigned_integer_of_size<8>::type u_int_8;
54  typedef unsigned_integer_of_size<16>::type u_int_16;
55 
56  typedef integer_of_size<8>::type int_8;
57  typedef integer_of_size<16>::type int_16;
58 
59  enum format_version
60  {
61  v_2_5 = 0,
62  v_2_8_with_palette = 2,
63  v_2_8_without_palette = 3,
64  v_win = 4,
65  v_3_0 = 5
66  }; // enum format_version
67 
68 # pragma pack (push,1)
69 
70  /*--------------------------------------------------------------------*/
74  class header
75  {
76  public:
78  u_int_8 manufacturer;
79 
81  u_int_8 version;
82 
84  u_int_8 encoded;
85 
87  u_int_8 bpp;
88 
89  struct
90  {
92  u_int_16 x_min;
93 
95  u_int_16 y_min;
96 
98  u_int_16 x_max;
99 
101  u_int_16 y_max;
102 
103  } window;
104 
106  u_int_16 horizontal_dpi;
107 
109  u_int_16 vertical_dpi;
110 
112  rgb_pixel_8 color_map[16];
113 
115  u_int_8 reserved;
116 
118  u_int_8 color_planes;
119 
122  u_int_16 bytes_per_line;
123 
125  u_int_16 palette_info;
126 
128  struct
129  {
131  u_int_16 horizontal;
132 
134  u_int_16 vertical;
135 
136  } screen_size;
137 
139  u_int_8 filler[54];
140  }; // struct header
141 # pragma pack (pop)
142 
143 
144  /*----------------------------------------------------------------------*/
147 
149  typedef std::vector<u_int_8> color_plane_type;
150 
151  public:
152  /*----------------------------------------------------------------------*/
158  class reader
159  {
160  private:
161  /*--------------------------------------------------------------------*/
167 
168  /*--------------------------------------------------------------------*/
172  class rle_pcx_output_buffer
173  {
174  public:
175  rle_pcx_output_buffer( color_plane_type& result );
176 
177  void fill( unsigned int n, u_int_8 pattern );
178  void copy( unsigned int n, rle_pcx_input_buffer& buffer );
179 
180  bool completed() const;
181 
182  private:
184  color_plane_type& m_result;
185 
187  unsigned int m_position;
188 
189  }; // class rle_pcx_output_buffer
190 
191  /*--------------------------------------------------------------------*/
195  class rle_pcx_decoder
196  : public rle_decoder< u_int_8,
197  rle_pcx_input_buffer,
198  rle_pcx_output_buffer >
199  {
200  private:
201  virtual void
202  read_mode( input_buffer_type& input, output_buffer_type& output );
203 
204  }; // class rle_pcx_decoder
205 
206  /*--------------------------------------------------------------------*/
211  class converter_mono
212  {
213  public:
214  void operator()
215  ( const std::vector<color_plane_type>& scanline, image& img,
216  unsigned int y ) const;
217  }; // class converter_mono
218 
219  /*--------------------------------------------------------------------*/
224  class converter_16
225  {
226  public:
227  converter_16( const header& h );
228  void operator()
229  ( const std::vector<color_plane_type>& scanline, image& img,
230  unsigned int y ) const;
231 
232  private:
234  const header& m_header;
235 
236  }; // class converter_16
237 
238  /*--------------------------------------------------------------------*/
243  class converter_256
244  {
245  public:
246  converter_256( const color_palette32& palette );
247  void operator()
248  ( const std::vector<color_plane_type>& scanline, image& img,
249  unsigned int y ) const;
250 
251  private:
253  const color_palette32& m_palette;
254 
255  }; // class converter_256
256 
257  /*--------------------------------------------------------------------*/
262  class converter_true_color
263  {
264  public:
265  void operator()
266  ( const std::vector<color_plane_type>& scanline, image& img,
267  unsigned int y ) const;
268 
269  }; // class converter_true_color
270 
271  public:
272  reader( image& img );
273  reader( image& img, std::istream& f );
274 
275  void load( std::istream& f );
276 
277  private:
278  void check_if_pcx( const header& h ) const;
279 
280  void load_mono( const header& h, std::istream& f );
281  void load_16_color_mapped( const header& h, std::istream& f );
282  void load_true_color( const header& h, std::istream& f );
283  void load_256_color_mapped( const header& h, std::istream& f );
284 
285  void
286  decompress_line( std::istream& f, color_plane_type& scanline ) const;
287 
288  template<typename Converter>
289  void decompress
290  ( const header& h, std::istream& f, const Converter& convert );
291 
292  private:
294  image& m_image;
295 
296  }; // class reader
297 
298  /*----------------------------------------------------------------------*/
303  class writer
304  {
305  public:
306  /*--------------------------------------------------------------------*/
312  {
313  public:
315  typedef u_int_8 pattern_type;
316 
317  public:
318  file_output_buffer( std::ostream& os );
319  void encode( unsigned int n, pattern_type pattern );
320 
321  template<typename Iterator>
322  void raw( Iterator first, Iterator last );
323 
324  unsigned int min_interesting() const;
325  unsigned int max_encodable() const;
326 
327  private:
329  std::ostream& m_stream;
330 
331  }; // class file_output_buffer
332 
333  /*--------------------------------------------------------------------*/
339 
340  public:
341  writer( const image& img );
342  writer( const image& img, std::ostream& f );
343 
344  void save( std::ostream& os ) const;
345 
346  private:
347  void write_header
348  ( std::ostream& os, unsigned int bytes_per_line ) const;
349  void save_rle_true_color
350  ( std::ostream& os, unsigned int bytes_per_line ) const;
351 
352  private:
354  const image& m_image;
355 
356  }; // class writer
357 
358  public:
359  pcx( unsigned int w, unsigned int h );
360  pcx( const image& that );
361  pcx( std::istream& f );
362 
363  void save( std::ostream& os ) const;
364 
365  }; // class pcx
366  } // namespace graphic
367 } // namespace claw
368 
369 #include <claw/impl/pcx_writer.tpp>
370 #include <claw/impl/pcx_reader.tpp>
371 
372 #endif // __CLAW_PCX_HPP__