claw  1.9.0
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@stuff-o-matic.com
24 */
30 #ifndef __CLAW_PCX_HPP__
31 #define __CLAW_PCX_HPP__
32 
34 #include <claw/graphic/image.hpp>
35 
37 #include <claw/rle_decoder.hpp>
38 #include <claw/rle_encoder.hpp>
39 #include <claw/types.hpp>
40 
41 #include <iostream>
42 namespace claw
43 {
44  namespace graphic
45  {
50  class pcx : public image
51  {
52  private:
53  /* Some convenient renaming. */
54  typedef unsigned_integer_of_size<8>::type u_int_8;
55  typedef unsigned_integer_of_size<16>::type u_int_16;
56 
57  typedef integer_of_size<8>::type int_8;
58  typedef integer_of_size<16>::type int_16;
59 
60  enum format_version
61  {
62  v_2_5 = 0,
63  v_2_8_with_palette = 2,
64  v_2_8_without_palette = 3,
65  v_win = 4,
66  v_3_0 = 5
67  }; // enum format_version
68 
69 #pragma pack(push, 1)
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 
145 
147  typedef std::vector<u_int_8> color_plane_type;
148 
149  public:
155  class reader
156  {
157  private:
163 
167  class rle_pcx_output_buffer
168  {
169  public:
170  rle_pcx_output_buffer(color_plane_type& result);
171 
172  void fill(unsigned int n, u_int_8 pattern);
173  void copy(unsigned int n, rle_pcx_input_buffer& buffer);
174 
175  bool completed() const;
176 
177  private:
179  color_plane_type& m_result;
180 
182  unsigned int m_position;
183 
184  }; // class rle_pcx_output_buffer
185 
189  class rle_pcx_decoder
190  : public rle_decoder<u_int_8, rle_pcx_input_buffer,
191  rle_pcx_output_buffer>
192  {
193  private:
194  virtual void read_mode(input_buffer_type& input,
195  output_buffer_type& output);
196 
197  }; // class rle_pcx_decoder
198 
203  class converter_mono
204  {
205  public:
206  void operator()(const std::vector<color_plane_type>& scanline,
207  image& img, unsigned int y) const;
208  }; // class converter_mono
209 
214  class converter_16
215  {
216  public:
217  converter_16(const header& h);
218  void operator()(const std::vector<color_plane_type>& scanline,
219  image& img, unsigned int y) const;
220 
221  private:
223  const header& m_header;
224 
225  }; // class converter_16
226 
231  class converter_256
232  {
233  public:
234  converter_256(const color_palette32& palette);
235  void operator()(const std::vector<color_plane_type>& scanline,
236  image& img, unsigned int y) const;
237 
238  private:
240  const color_palette32& m_palette;
241 
242  }; // class converter_256
243 
248  class converter_true_color
249  {
250  public:
251  void operator()(const std::vector<color_plane_type>& scanline,
252  image& img, unsigned int y) const;
253 
254  }; // class converter_true_color
255 
256  public:
257  reader(image& img);
258  reader(image& img, std::istream& f);
259 
260  void load(std::istream& f);
261 
262  private:
263  void check_if_pcx(const header& h) const;
264 
265  void load_mono(const header& h, std::istream& f);
266  void load_16_color_mapped(const header& h, std::istream& f);
267  void load_true_color(const header& h, std::istream& f);
268  void load_256_color_mapped(const header& h, std::istream& f);
269 
270  void decompress_line(std::istream& f,
271  color_plane_type& scanline) const;
272 
273  template <typename Converter>
274  void decompress(const header& h, std::istream& f,
275  const Converter& convert);
276 
277  private:
279  image& m_image;
280 
281  }; // class reader
282 
287  class writer
288  {
289  public:
295  {
296  public:
298  typedef u_int_8 pattern_type;
299 
300  public:
301  file_output_buffer(std::ostream& os);
302  void encode(unsigned int n, pattern_type pattern);
303 
304  template <typename Iterator>
305  void raw(Iterator first, Iterator last);
306 
307  unsigned int min_interesting() const;
308  unsigned int max_encodable() const;
309 
310  private:
312  std::ostream& m_stream;
313 
314  }; // class file_output_buffer
315 
321 
322  public:
323  writer(const image& img);
324  writer(const image& img, std::ostream& f);
325 
326  void save(std::ostream& os) const;
327 
328  private:
329  void write_header(std::ostream& os, unsigned int bytes_per_line) const;
330  void save_rle_true_color(std::ostream& os,
331  unsigned int bytes_per_line) const;
332 
333  private:
335  const image& m_image;
336 
337  }; // class writer
338 
339  public:
340  pcx(unsigned int w, unsigned int h);
341  pcx(const image& that);
342  pcx(std::istream& f);
343 
344  void save(std::ostream& os) const;
345 
346  }; // class pcx
347  }
348 }
349 
350 #include <claw/graphic/pcx_reader.tpp>
351 #include <claw/graphic/pcx_writer.tpp>
352 
353 #endif // __CLAW_PCX_HPP__
pcx(unsigned int w, unsigned int h)
Constructor. Creates an empty image.
Definition: pcx.cpp:38
This class read data from a pcx file and store it in an image.
Definition: pcx.hpp:155
One line in the image.
Definition: image.hpp:61
writer(const image &img)
Constructor.
Definition: pcx_writer.cpp:80
Define the type of an unsigned integer stored with a given number of bits. Template parameters...
Definition: types.hpp:124
A class for pcx pictures.
Definition: pcx.hpp:50
file_output_buffer(std::ostream &os)
Constructor.
Definition: pcx_writer.cpp:36
find_type_by_size< Size, signed_integers >::type type
The integer type that matches the given size.
Definition: types.hpp:114
reader(image &img)
Constructor.
Definition: pcx_reader.cpp:240
Some classes for the raw manipulation of the base types.
u_int_8 pattern_type
The typ of the output patterns.
Definition: pcx.hpp:298
A palette of color, for palettized images.
void fill(const math::rectangle< int > r, const pixel_type &c)
Fill an area of the image with a given color.
Definition: image.cpp:286
The type of the output buffer associated with the file when encoding RLE data.
Definition: pcx.hpp:294
Fuction object to get the first element of a std::pair.
Definition: functional.hpp:42
unsigned int min_interesting() const
Get the minimum number of pixels needed for encoding.
Definition: pcx_writer.cpp:62
A class to help decoding run-length encoded (RLE) streams.
Definition: rle_decoder.hpp:54
This class write an image in a pcx file.
Definition: pcx.hpp:287
A class to help run-length encoding (RLE) streams.
void save(std::ostream &os) const
Save the content of the image in a stream.
Definition: pcx_writer.cpp:99
void save(std::ostream &os) const
Save the content of the image in a stream.
Definition: pcx.cpp:63
A palette of colors, for palettized images.
A class to help decoding run-length encoded (RLE) streams.
This class is made to help reading istreams with a buffer.
void encode(unsigned int n, pattern_type pattern)
Encode a pixel data.
Definition: pcx_writer.cpp:46
rle_encoder< file_output_buffer > rle_pcx_encoder
RLE encoder for pcx format.
Definition: pcx.hpp:320
A class to deal with images.
Definition: image.hpp:50
void load(std::istream &f)
Load an image from a pcx file.
Definition: pcx_reader.cpp:260
A class to help run-length encoding (RLE) streams.
Definition: rle_encoder.hpp:58
This is the main namespace.
Definition: application.hpp:49
unsigned int max_encodable() const
Get the maximum number of pixel a code can encode.
Definition: pcx_writer.cpp:71
A class to deal with images.