Claw  1.7.3
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@gamned.org
24 */
30 #ifndef __CLAW_TARGA_HPP__
31 #define __CLAW_TARGA_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>
39 
40 namespace claw
41 {
42  namespace graphic
43  {
48  class targa : public image
49  {
50  private:
51  /*----------------------------------------------------------------------*/
57  class file_structure
58  {
59  public:
60  enum image_coding
61  {
62  color_mapped = 1,
63  true_color = 2,
64  black_and_white = 3,
65  rle_color_mapped = 9,
66  rle_true_color = 10,
67  rle_black_and_white = 11
68  }; // enum image_coding
69 
70 # pragma pack (push,1)
71 
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 
126  /*--------------------------------------------------------------------*/
131  {
133  unsigned short tag;
135  unsigned int offset;
137  unsigned int size;
138  }; // struct developer_item
139 
140  /*--------------------------------------------------------------------*/
145  struct extension
146  {
147 
148  }; // struct extension
149 
150  /*--------------------------------------------------------------------*/
154  class footer
155  {
156  public:
157  footer();
158 
159  bool is_valid() const;
160 
161  public:
163  unsigned int extension_offset;
164 
166  unsigned int developer_offset;
167 
170  char signature[18];
171 
172  private:
174  static const std::string s_signature;
175 
176  }; // struct footer
177 # pragma pack (pop)
178 
179  }; // class file_structure
180 
181  /*----------------------------------------------------------------------*/
188  struct pixel16
189  {
190  }; // struct pixel16
191 
192  /*----------------------------------------------------------------------*/
199  struct pixel8
200  {
201  }; // struct pixel8
202 
203  /*----------------------------------------------------------------------*/
207  typedef color_palette<rgba_pixel_8> color_palette32;
208 
209  public:
210  /*----------------------------------------------------------------------*/
216  class reader : private file_structure
217  {
218  private:
219  /*--------------------------------------------------------------------*/
227  template<typename Pixel>
228  class file_input_buffer : public buffered_istream<std::istream>
229  {
230  private:
232  typedef Pixel pixel_type;
233 
234  public:
235  file_input_buffer( std::istream& f );
236  rgba_pixel_8 get_pixel();
237 
238  }; // class file_input_buffer
239 
240  /*--------------------------------------------------------------------*/
248  template<typename Pixel>
249  class mapped_file_input_buffer:
250  public buffered_istream<std::istream>
251  {
252  private:
254  typedef Pixel pixel_type;
255 
256  public:
257  mapped_file_input_buffer( std::istream& f, const color_palette32& p );
258  rgba_pixel_8 get_pixel();
259 
260  private:
262  const color_palette32& m_palette;
263 
264  }; // class mapped_file_input_buffer
265 
266  /*--------------------------------------------------------------------*/
275  template< typename InputBuffer >
276  class rle_targa_output_buffer
277  {
278  private:
280  typedef rgba_pixel_8 pixel_type;
281 
283  typedef InputBuffer input_buffer_type;
284 
285  public:
286  rle_targa_output_buffer( image& img, bool up_down, bool left_right );
287 
288  void fill( unsigned int n, rgba_pixel_8 pattern );
289  void copy( unsigned int n, input_buffer_type& buffer );
290 
291  bool completed() const;
292 
293  private:
294  void adjust_position(int x);
295 
296  private:
298  image& m_image;
299 
301  unsigned int m_x;
302 
304  unsigned int m_y;
305 
307  const int m_x_inc;
308 
310  const int m_y_inc;
311 
312  }; // class rle_targa_output_buffer
313 
314  /*--------------------------------------------------------------------*/
327  template< typename InputBuffer,
328  typename OutputBuffer = rle_targa_output_buffer<InputBuffer> >
329  class rle_targa_decoder
330  : public rle_decoder< rgba_pixel_8, InputBuffer, OutputBuffer >
331  {
332  public:
334  typedef InputBuffer input_buffer_type;
335 
337  typedef OutputBuffer output_buffer_type;
338 
339  private:
340  virtual void
341  read_mode( input_buffer_type& input, output_buffer_type& output );
342 
343  }; // class rle_targa_decoder
344 
345  /*--------------------------------------------------------------------*/
347  typedef
348  rle_targa_decoder< file_input_buffer<rgba_pixel_8> > rle32_decoder;
349 
350  /*--------------------------------------------------------------------*/
352  typedef
353  rle_targa_decoder< file_input_buffer<rgb_pixel_8> > rle24_decoder;
354 
355  /*--------------------------------------------------------------------*/
357  typedef rle_targa_decoder< file_input_buffer<pixel16> > rle16_decoder;
358 
359  /*--------------------------------------------------------------------*/
361  typedef rle_targa_decoder< mapped_file_input_buffer<pixel8> >
362  rle8_decoder;
363 
364  public:
365  reader( image& img );
366  reader( image& img, std::istream& f );
367 
368  void load( std::istream& f );
369 
370  private:
371  void check_if_targa( std::istream& f ) const;
372 
373  void load_palette
374  ( const header& h, std::istream& f, color_palette32& palette ) const;
375 
376  void load_color_mapped( const header& h, std::istream& f );
377  void load_rle_color_mapped( const header& h, std::istream& f );
378  void load_true_color( const header& h, std::istream& f );
379  void load_rle_true_color( const header& h, std::istream& f );
380 
381  template<typename Pixel>
382  void load_color_mapped_raw
383  ( const header& h, std::istream& f, const color_palette32& palette );
384 
385  template<typename Decoder>
386  void decompress_rle_color_mapped
387  ( const header& h, std::istream& f, const color_palette32& palette );
388 
389  template<typename Pixel>
390  void load_true_color_raw( const header& h, std::istream& f );
391 
392  template<typename Decoder>
393  void decompress_rle_true_color( const header& h, std::istream& f );
394 
395  template<typename Pixel>
396  void
397  load_palette_content( std::istream& f, color_palette32& palette ) const;
398 
399  private:
401  image& m_image;
402 
403  }; // class reader
404 
405  /*----------------------------------------------------------------------*/
410  class writer : private file_structure
411  {
412  public:
413  /*--------------------------------------------------------------------*/
421  template<typename Pixel>
423  {
424  public:
426  typedef Pixel pixel_type;
427 
430 
431  public:
432  file_output_buffer( std::ostream& os );
433  void encode( unsigned int n, pattern_type pattern );
434 
435  template<typename Iterator>
436  void raw( Iterator first, Iterator last );
437 
438  unsigned int min_interesting() const;
439  unsigned int max_encodable() const;
440 
446  void order_pixel_bytes( const pixel_type& p );
447 
448  private:
450  std::ostream& m_stream;
451 
452  }; // class file_output_buffer
453 
454  /*--------------------------------------------------------------------*/
463  template<typename Pixel>
465  : public rle_encoder< file_output_buffer<Pixel> >
466  {
467  public:
470 
471  }; // class rle_targa_encoder
472 
473  /*--------------------------------------------------------------------*/
476 
477  public:
478  writer( const image& img );
479  writer( const image& img, std::ostream& f, bool rle );
480 
481  void save( std::ostream& f, bool rle ) const;
482 
483  private:
484  void save_true_color( std::ostream& os ) const;
485  void save_rle_true_color( std::ostream& os ) const;
486 
487  private:
489  const image& m_image;
490 
491  }; // class writer
492 
493  public:
494  targa( unsigned int w, unsigned int h );
495  targa( const image& that );
496  targa( std::istream& f );
497 
498  void save( std::ostream& os, bool rle ) const;
499 
500  }; // class targa
501  } // namespace graphic
502 } // namespace claw
503 
504 #include <claw/impl/targa_writer.tpp>
505 #include <claw/impl/targa_reader.tpp>
506 
507 #endif // __CLAW_TARGA_HPP__
claw::graphic::targa::writer::file_output_buffer::order_pixel_bytes
void order_pixel_bytes(const pixel_type &p)
Write a pixel in the stream and set its value in the good order.
claw::graphic::targa::file_structure::header::specification::y_origin
unsigned short y_origin
Lower left corner Y-origin.
Definition: targa.hpp:107
claw::graphic::targa::writer::file_output_buffer::pixel_type
Pixel pixel_type
The type of the pixels in the input buffer.
Definition: targa.hpp:426
rle_decoder.hpp
A class to help decoding run-length encoded (RLE) streams.
claw::rle_encoder
A class to help run-length encoding (RLE) streams.
Definition: rle_encoder.hpp:58
claw::graphic::targa::file_structure::header::color_map
char color_map
1 if there is a color map, 0 otherwise.
Definition: targa.hpp:86
claw::graphic::targa::file_structure::developer_item::offset
unsigned int offset
Offset in the file.
Definition: targa.hpp:135
claw::graphic::targa::save
void save(std::ostream &os, bool rle) const
Save the content of the image in a stream.
Definition: targa.cpp:72
claw::graphic::targa::writer
This class write an image in a targa file.
Definition: targa.hpp:410
claw::graphic::targa::reader::reader
reader(image &img)
Constructor.
Definition: targa_reader.cpp:193
claw::graphic::targa::file_structure::footer::extension_offset
unsigned int extension_offset
Offset of the extension area.
Definition: targa.hpp:163
claw::first
Fuction object to get the first element of a std::pair.
Definition: functional.hpp:44
claw::graphic::targa::file_structure::header::image_type
char image_type
Image type.
Definition: targa.hpp:88
claw::graphic::rgba_pixel
RGBA pixel.
Definition: pixel.hpp:79
claw::graphic::targa::writer::rle_targa_encoder::output_buffer_type
file_output_buffer< Pixel > output_buffer_type
Type of the output buffer.
Definition: targa.hpp:469
claw::graphic::targa::file_structure::header::specification::left_right_oriented
bool left_right_oriented() const
Is image stored left to right ?
Definition: targa_file_structure.cpp:100
claw::graphic::targa::writer::rle32_encoder
rle_targa_encoder< rgba_pixel_8 > rle32_encoder
RLE encoder for 32 bpp targa images.
Definition: targa.hpp:475
claw::graphic::targa::file_structure::footer
Footer of a targa file.
Definition: targa.hpp:154
claw::graphic::targa::file_structure::header::specification
Image specification.
Definition: targa.hpp:102
claw
This is the main namespace.
Definition: algorithm.hpp:33
claw::graphic::targa::file_structure::header::entry_size
unsigned char entry_size
Number of bits per enrty.
Definition: targa.hpp:98
claw::graphic::targa::file_structure::header
Header of a targa file.
Definition: targa.hpp:76
claw::graphic::targa::file_structure::header::first_entry_index
unsigned short first_entry_index
Index of the first color map entry.
Definition: targa.hpp:94
claw::graphic::targa::file_structure::header::specification::width
unsigned short width
Image width.
Definition: targa.hpp:109
claw::graphic::targa::writer::save
void save(std::ostream &f, bool rle) const
Save the content of the image in a stream.
Definition: targa_writer.cpp:98
buffered_istream.hpp
This class is made to help reading istreams with a buffer.
claw::graphic::targa::reader
This class read data from a targa file and store it in an image.
Definition: targa.hpp:216
claw::graphic::targa::file_structure::extension
Extension area.
Definition: targa.hpp:145
claw::graphic::targa::reader::load
void load(std::istream &f)
Load an image from a targa file.
Definition: targa_reader.cpp:217
claw::graphic::targa::writer::file_output_buffer::pattern_type
pixel_type pattern_type
The type of the patterns to encode.
Definition: targa.hpp:429
claw::graphic::targa::file_structure::header::color_map_specification
struct claw::graphic::targa::file_structure::header::@18 color_map_specification
Color map specification.
claw::graphic::targa::file_structure::header::specification::bpp
unsigned char bpp
Bits per pixel.
Definition: targa.hpp:113
claw::graphic::color_palette
A palette of colors, for palettized images.
Definition: color_palette.hpp:44
claw::graphic::targa::file_structure::developer_item
Item in the developper directory.
Definition: targa.hpp:130
claw::graphic::targa::file_structure::footer::footer
footer()
Constructor.
Definition: targa_file_structure.cpp:131
color_palette.hpp
A palette of color, for palettized images.
claw::graphic::targa::writer::rle_targa_encoder
RLE encoder for targa format.
Definition: targa.hpp:464
claw::graphic::image::fill
void fill(const math::rectangle< int > r, const pixel_type &c)
Fill an area of the image with a given color.
Definition: image.cpp:314
claw::graphic::targa::writer::file_output_buffer
The type of the output buffer associated with the file when encoding RLE data.
Definition: targa.hpp:422
claw::graphic::targa::file_structure::footer::is_valid
bool is_valid() const
Tell if the data of this footer is valid.
Definition: targa_file_structure.cpp:142
claw::graphic::targa::file_structure::header::image_specification
specification image_specification
The specification of the image.
Definition: targa.hpp:123
claw::graphic::targa::file_structure::header::specification::x_origin
unsigned short x_origin
Lower left corner X-origin.
Definition: targa.hpp:105
claw::graphic::targa::file_structure::header::length
unsigned short length
Total number of color map entries included.
Definition: targa.hpp:96
image.hpp
A class to deal with images.
claw::rle_decoder
A class to help decoding run-length encoded (RLE) streams.
Definition: rle_decoder.hpp:54
claw::graphic::targa::targa
targa(unsigned int w, unsigned int h)
Constructor. Creates an empty image.
Definition: targa.cpp:39
claw::graphic::image
A class to deal with images.
Definition: image.hpp:49
claw::graphic::targa
A class for targa pictures.
Definition: targa.hpp:48
claw::graphic::targa::file_structure::header::id_length
char id_length
Image identifier length.
Definition: targa.hpp:84
claw::graphic::image::pixel_type
rgba_pixel pixel_type
The type representing the colors of the pixels in the image.
Definition: image.hpp:53
claw::graphic::targa::file_structure::header::specification::up_down_oriented
bool up_down_oriented() const
Is image stored up to down ?
Definition: targa_file_structure.cpp:89
claw::graphic::targa::writer::writer
writer(const image &img)
Constructor.
Definition: targa_writer.cpp:72
rle_encoder.hpp
A class to help run-length encoding (RLE) streams.
claw::graphic::targa::file_structure::developer_item::tag
unsigned short tag
Item identifier.
Definition: targa.hpp:133
claw::graphic::targa::file_structure::footer::signature
char signature[18]
Footer identier. Must be as long as std::string("TRUEVISION-XFILE.") + 1 (for the last '\0').
Definition: targa.hpp:170
claw::graphic::targa::file_structure::developer_item::size
unsigned int size
Fielf size.
Definition: targa.hpp:137
claw::graphic::targa::file_structure::header::specification::alpha
unsigned char alpha() const
Number of bits per pixel assigned to alpha chanel.
Definition: targa_file_structure.cpp:110
claw::graphic::targa::file_structure::header::header
header()
Default constructor.
Definition: targa_file_structure.cpp:41
claw::graphic::targa::file_structure::header::specification::descriptor
unsigned char descriptor
descriptor.
Definition: targa.hpp:115
claw::graphic::targa::file_structure::footer::developer_offset
unsigned int developer_offset
Offset of the developer directory.
Definition: targa.hpp:166
claw::buffered_istream
This class is made to help reading istreams with a buffer.
Definition: buffered_istream.hpp:42
claw::graphic::targa::file_structure::header::specification::height
unsigned short height
Image height.
Definition: targa.hpp:111