claw  1.9.0
targa.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_TARGA_HPP__
31 #define __CLAW_TARGA_HPP__
32 
34 #include <claw/graphic/image.hpp>
35 
37 #include <claw/rle_decoder.hpp>
38 #include <claw/rle_encoder.hpp>
39 
40 #include <iostream>
41 
42 namespace claw
43 {
44  namespace graphic
45  {
50  class targa : public image
51  {
52  private:
58  class file_structure
59  {
60  public:
61  enum image_coding
62  {
63  color_mapped = 1,
64  true_color = 2,
65  black_and_white = 3,
66  rle_color_mapped = 9,
67  rle_true_color = 10,
68  rle_black_and_white = 11
69  }; // enum image_coding
70 
71 #pragma pack(push, 1)
72 
76  class header
77  {
78  public:
79  header();
80  header(unsigned int w, unsigned int h);
81 
82  public:
84  char id_length;
86  char color_map;
88  char image_type;
89 
91  struct
92  {
94  unsigned short first_entry_index;
96  unsigned short length;
98  unsigned char entry_size;
100 
103  {
105  unsigned short x_origin;
107  unsigned short y_origin;
109  unsigned short width;
111  unsigned short height;
113  unsigned char bpp;
115  unsigned char descriptor;
116 
117  bool up_down_oriented() const;
118  bool left_right_oriented() const;
119  unsigned char alpha() const;
120  }; // struct specification
121 
124  }; // struct header
125 
130  {
132  unsigned short tag;
134  unsigned int offset;
136  unsigned int size;
137  }; // struct developer_item
138 
143  struct extension
144  {}; // struct extension
145 
149  class footer
150  {
151  public:
152  footer();
153 
154  bool is_valid() const;
155 
156  public:
158  unsigned int extension_offset;
159 
161  unsigned int developer_offset;
162 
165  char signature[18];
166 
167  private:
169  static const std::string s_signature;
170 
171  }; // struct footer
172 #pragma pack(pop)
173 
174  }; // class file_structure
175 
182  struct pixel16
183  {}; // struct pixel16
184 
191  struct pixel8
192  {}; // struct pixel8
193 
197  typedef color_palette<rgba_pixel_8> color_palette32;
198 
199  public:
205  class reader : private file_structure
206  {
207  private:
215  template <typename Pixel>
216  class file_input_buffer : public buffered_istream<std::istream>
217  {
218  private:
220  typedef Pixel pixel_type;
221 
222  public:
223  file_input_buffer(std::istream& f);
224  rgba_pixel_8 get_pixel();
225 
226  }; // class file_input_buffer
227 
235  template <typename Pixel>
236  class mapped_file_input_buffer : public buffered_istream<std::istream>
237  {
238  private:
240  typedef Pixel pixel_type;
241 
242  public:
243  mapped_file_input_buffer(std::istream& f, const color_palette32& p);
244  rgba_pixel_8 get_pixel();
245 
246  private:
248  const color_palette32& m_palette;
249 
250  }; // class mapped_file_input_buffer
251 
260  template <typename InputBuffer>
261  class rle_targa_output_buffer
262  {
263  private:
265  typedef rgba_pixel_8 pixel_type;
266 
268  typedef InputBuffer input_buffer_type;
269 
270  public:
271  rle_targa_output_buffer(image& img, bool up_down, bool left_right);
272 
273  void fill(unsigned int n, rgba_pixel_8 pattern);
274  void copy(unsigned int n, input_buffer_type& buffer);
275 
276  bool completed() const;
277 
278  private:
279  void adjust_position(int x);
280 
281  private:
283  image& m_image;
284 
286  unsigned int m_x;
287 
289  unsigned int m_y;
290 
292  const int m_x_inc;
293 
295  const int m_y_inc;
296 
297  }; // class rle_targa_output_buffer
298 
311  template <typename InputBuffer,
312  typename OutputBuffer
313  = rle_targa_output_buffer<InputBuffer> >
314  class rle_targa_decoder
315  : public rle_decoder<rgba_pixel_8, InputBuffer, OutputBuffer>
316  {
317  public:
319  typedef InputBuffer input_buffer_type;
320 
322  typedef OutputBuffer output_buffer_type;
323 
324  private:
325  virtual void read_mode(input_buffer_type& input,
326  output_buffer_type& output);
327 
328  }; // class rle_targa_decoder
329 
331  typedef rle_targa_decoder<file_input_buffer<rgba_pixel_8> >
332  rle32_decoder;
333 
335  typedef rle_targa_decoder<file_input_buffer<rgb_pixel_8> >
336  rle24_decoder;
337 
339  typedef rle_targa_decoder<file_input_buffer<pixel16> > rle16_decoder;
340 
342  typedef rle_targa_decoder<mapped_file_input_buffer<pixel8> >
343  rle8_decoder;
344 
345  public:
346  reader(image& img);
347  reader(image& img, std::istream& f);
348 
349  void load(std::istream& f);
350 
351  private:
352  void check_if_targa(std::istream& f) const;
353 
354  void load_palette(const header& h, std::istream& f,
355  color_palette32& palette) const;
356 
357  void load_color_mapped(const header& h, std::istream& f);
358  void load_rle_color_mapped(const header& h, std::istream& f);
359  void load_true_color(const header& h, std::istream& f);
360  void load_rle_true_color(const header& h, std::istream& f);
361 
362  template <typename Pixel>
363  void load_color_mapped_raw(const header& h, std::istream& f,
364  const color_palette32& palette);
365 
366  template <typename Decoder>
367  void decompress_rle_color_mapped(const header& h, std::istream& f,
368  const color_palette32& palette);
369 
370  template <typename Pixel>
371  void load_true_color_raw(const header& h, std::istream& f);
372 
373  template <typename Decoder>
374  void decompress_rle_true_color(const header& h, std::istream& f);
375 
376  template <typename Pixel>
377  void load_palette_content(std::istream& f,
378  color_palette32& palette) const;
379 
380  private:
382  image& m_image;
383 
384  }; // class reader
385 
390  class writer : private file_structure
391  {
392  public:
400  template <typename Pixel>
402  {
403  public:
405  typedef Pixel pixel_type;
406 
409 
410  public:
411  file_output_buffer(std::ostream& os);
412  void encode(unsigned int n, pattern_type pattern);
413 
414  template <typename Iterator>
415  void raw(Iterator first, Iterator last);
416 
417  unsigned int min_interesting() const;
418  unsigned int max_encodable() const;
419 
425  void order_pixel_bytes(const pixel_type& p);
426 
427  private:
429  std::ostream& m_stream;
430 
431  }; // class file_output_buffer
432 
441  template <typename Pixel>
443  : public rle_encoder<file_output_buffer<Pixel> >
444  {
445  public:
448 
449  }; // class rle_targa_encoder
450 
453 
454  public:
455  writer(const image& img);
456  writer(const image& img, std::ostream& f, bool rle);
457 
458  void save(std::ostream& f, bool rle) const;
459 
460  private:
461  void save_true_color(std::ostream& os) const;
462  void save_rle_true_color(std::ostream& os) const;
463 
464  private:
466  const image& m_image;
467 
468  }; // class writer
469 
470  public:
471  targa(unsigned int w, unsigned int h);
472  targa(const image& that);
473  targa(std::istream& f);
474 
475  void save(std::ostream& os, bool rle) const;
476 
477  }; // class targa
478  }
479 }
480 
481 #include <claw/graphic/targa_reader.tpp>
482 #include <claw/graphic/targa_writer.tpp>
483 
484 #endif // __CLAW_TARGA_HPP__
reader(image &img)
Constructor.
void save(std::ostream &os, bool rle) const
Save the content of the image in a stream.
Definition: targa.cpp:64
A class for targa pictures.
Definition: targa.hpp:50
char color_map
1 if there is a color map, 0 otherwise.
Definition: targa.hpp:86
rgba_pixel pixel_type
The type representing the colors of the pixels in the image.
Definition: image.hpp:55
unsigned char entry_size
Number of bits per enrty.
Definition: targa.hpp:98
void save(std::ostream &f, bool rle) const
Save the content of the image in a stream.
unsigned short tag
Item identifier.
Definition: targa.hpp:132
file_output_buffer< Pixel > output_buffer_type
Type of the output buffer.
Definition: targa.hpp:447
unsigned short first_entry_index
Index of the first color map entry.
Definition: targa.hpp:94
This class read data from a targa file and store it in an image.
Definition: targa.hpp:205
rle_targa_encoder< rgba_pixel_8 > rle32_encoder
RLE encoder for 32 bpp targa images.
Definition: targa.hpp:452
void load(std::istream &f)
Load an image from a targa file.
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
char signature[18]
Footer identier. Must be as long as std::string("TRUEVISION-XFILE.") + 1 (for the last &#39;\0&#39;)...
Definition: targa.hpp:165
RLE encoder for targa format.
Definition: targa.hpp:442
Fuction object to get the first element of a std::pair.
Definition: functional.hpp:42
Item in the developper directory.
Definition: targa.hpp:129
char id_length
Image identifier length.
Definition: targa.hpp:84
This class is made to help reading istreams with a buffer.
A class to help decoding run-length encoded (RLE) streams.
Definition: rle_decoder.hpp:54
A class to help run-length encoding (RLE) streams.
A palette of colors, for palettized images.
unsigned short x_origin
Lower left corner X-origin.
Definition: targa.hpp:105
unsigned int extension_offset
Offset of the extension area.
Definition: targa.hpp:158
void order_pixel_bytes(const pixel_type &p)
Write a pixel in the stream and set its value in the good order.
A class to help decoding run-length encoded (RLE) streams.
unsigned int developer_offset
Offset of the developer directory.
Definition: targa.hpp:161
unsigned int offset
Offset in the file.
Definition: targa.hpp:134
pixel_type pattern_type
The type of the patterns to encode.
Definition: targa.hpp:408
bool is_valid() const
Tell if the data of this footer is valid.
targa(unsigned int w, unsigned int h)
Constructor. Creates an empty image.
Definition: targa.cpp:38
This class is made to help reading istreams with a buffer.
writer(const image &img)
Constructor.
Pixel pixel_type
The type of the pixels in the input buffer.
Definition: targa.hpp:405
unsigned char alpha() const
Number of bits per pixel assigned to alpha chanel.
struct claw::graphic::targa::file_structure::header::@18 color_map_specification
Color map specification.
unsigned short length
Total number of color map entries included.
Definition: targa.hpp:96
A class to deal with images.
Definition: image.hpp:50
A class to help run-length encoding (RLE) streams.
Definition: rle_encoder.hpp:58
This class write an image in a targa file.
Definition: targa.hpp:390
This is the main namespace.
Definition: application.hpp:49
specification image_specification
The specification of the image.
Definition: targa.hpp:123
bool up_down_oriented() const
Is image stored up to down ?
unsigned short y_origin
Lower left corner Y-origin.
Definition: targa.hpp:107
The type of the output buffer associated with the file when encoding RLE data.
Definition: targa.hpp:401
A class to deal with images.
bool left_right_oriented() const
Is image stored left to right ?