Claw  1.7.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@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  } image_specification;
121  }; // struct header
122 
123  /*--------------------------------------------------------------------*/
128  {
130  unsigned short tag;
132  unsigned int offset;
134  unsigned int size;
135  }; // struct developer_item
136 
137  /*--------------------------------------------------------------------*/
142  struct extension
143  {
144 
145  }; // struct extension
146 
147  /*--------------------------------------------------------------------*/
151  class footer
152  {
153  public:
154  footer();
155 
156  bool is_valid() const;
157 
158  public:
160  unsigned int extension_offset;
161 
163  unsigned int developer_offset;
164 
167  char signature[18];
168 
169  private:
171  static const std::string s_signature;
172 
173  }; // struct footer
174 # pragma pack (pop)
175 
176  }; // class file_structure
177 
178  /*----------------------------------------------------------------------*/
185  struct pixel16
186  {
187  }; // struct pixel16
188 
189  /*----------------------------------------------------------------------*/
196  struct pixel8
197  {
198  }; // struct pixel8
199 
200  /*----------------------------------------------------------------------*/
204  typedef color_palette<rgba_pixel_8> color_palette32;
205 
206  public:
207  /*----------------------------------------------------------------------*/
213  class reader : private file_structure
214  {
215  private:
216  /*--------------------------------------------------------------------*/
224  template<typename Pixel>
225  class file_input_buffer : public buffered_istream<std::istream>
226  {
227  private:
229  typedef Pixel pixel_type;
230 
231  public:
232  file_input_buffer( std::istream& f );
233  rgba_pixel_8 get_pixel();
234 
235  }; // class file_input_buffer
236 
237  /*--------------------------------------------------------------------*/
245  template<typename Pixel>
246  class mapped_file_input_buffer:
247  public buffered_istream<std::istream>
248  {
249  private:
251  typedef Pixel pixel_type;
252 
253  public:
254  mapped_file_input_buffer( std::istream& f, const color_palette32& p );
255  rgba_pixel_8 get_pixel();
256 
257  private:
259  const color_palette32& m_palette;
260 
261  }; // class mapped_file_input_buffer
262 
263  /*--------------------------------------------------------------------*/
272  template< typename InputBuffer >
273  class rle_targa_output_buffer
274  {
275  private:
277  typedef rgba_pixel_8 pixel_type;
278 
280  typedef InputBuffer input_buffer_type;
281 
282  public:
283  rle_targa_output_buffer( image& img, bool up_down, bool left_right );
284 
285  void fill( unsigned int n, rgba_pixel_8 pattern );
286  void copy( unsigned int n, input_buffer_type& buffer );
287 
288  bool completed() const;
289 
290  private:
291  void adjust_position(int x);
292 
293  private:
295  image& m_image;
296 
298  unsigned int m_x;
299 
301  unsigned int m_y;
302 
304  const int m_x_inc;
305 
307  const int m_y_inc;
308 
309  }; // class rle_targa_output_buffer
310 
311  /*--------------------------------------------------------------------*/
324  template< typename InputBuffer,
325  typename OutputBuffer = rle_targa_output_buffer<InputBuffer> >
326  class rle_targa_decoder
327  : public rle_decoder< rgba_pixel_8, InputBuffer, OutputBuffer >
328  {
329  public:
331  typedef InputBuffer input_buffer_type;
332 
334  typedef OutputBuffer output_buffer_type;
335 
336  private:
337  virtual void
338  read_mode( input_buffer_type& input, output_buffer_type& output );
339 
340  }; // class rle_targa_decoder
341 
342  /*--------------------------------------------------------------------*/
344  typedef
345  rle_targa_decoder< file_input_buffer<rgba_pixel_8> > rle32_decoder;
346 
347  /*--------------------------------------------------------------------*/
349  typedef
350  rle_targa_decoder< file_input_buffer<rgb_pixel_8> > rle24_decoder;
351 
352  /*--------------------------------------------------------------------*/
354  typedef rle_targa_decoder< file_input_buffer<pixel16> > rle16_decoder;
355 
356  /*--------------------------------------------------------------------*/
358  typedef rle_targa_decoder< mapped_file_input_buffer<pixel8> >
359  rle8_decoder;
360 
361  public:
362  reader( image& img );
363  reader( image& img, std::istream& f );
364 
365  void load( std::istream& f );
366 
367  private:
368  void check_if_targa( std::istream& f ) const;
369 
370  void load_palette
371  ( const header& h, std::istream& f, color_palette32& palette ) const;
372 
373  void load_color_mapped( const header& h, std::istream& f );
374  void load_rle_color_mapped( const header& h, std::istream& f );
375  void load_true_color( const header& h, std::istream& f );
376  void load_rle_true_color( const header& h, std::istream& f );
377 
378  template<typename Pixel>
379  void load_color_mapped_raw
380  ( const header& h, std::istream& f, const color_palette32& palette );
381 
382  template<typename Decoder>
383  void decompress_rle_color_mapped
384  ( const header& h, std::istream& f, const color_palette32& palette );
385 
386  template<typename Pixel>
387  void load_true_color_raw( const header& h, std::istream& f );
388 
389  template<typename Decoder>
390  void decompress_rle_true_color( const header& h, std::istream& f );
391 
392  template<typename Pixel>
393  void
394  load_palette_content( std::istream& f, color_palette32& palette ) const;
395 
396  private:
398  image& m_image;
399 
400  }; // class reader
401 
402  /*----------------------------------------------------------------------*/
407  class writer : private file_structure
408  {
409  public:
410  /*--------------------------------------------------------------------*/
418  template<typename Pixel>
420  {
421  public:
423  typedef Pixel pixel_type;
424 
425  typedef pixel_type pattern_type;
426 
427  public:
428  file_output_buffer( std::ostream& os );
429  void encode( unsigned int n, pattern_type pattern );
430 
431  template<typename Iterator>
432  void raw( Iterator first, Iterator last );
433 
434  unsigned int min_interesting() const;
435  unsigned int max_encodable() const;
436 
437  void order_pixel_bytes( const pixel_type& p );
438 
439  private:
441  std::ostream& m_stream;
442 
443  }; // class file_output_buffer
444 
445  /*--------------------------------------------------------------------*/
454  template<typename Pixel>
456  : public rle_encoder< file_output_buffer<Pixel> >
457  {
458  public:
461 
462  }; // class rle_targa_encoder
463 
464  /*--------------------------------------------------------------------*/
467 
468  public:
469  writer( const image& img );
470  writer( const image& img, std::ostream& f, bool rle );
471 
472  void save( std::ostream& f, bool rle ) const;
473 
474  private:
475  void save_true_color( std::ostream& os ) const;
476  void save_rle_true_color( std::ostream& os ) const;
477 
478  private:
480  const image& m_image;
481 
482  }; // class writer
483 
484  public:
485  targa( unsigned int w, unsigned int h );
486  targa( const image& that );
487  targa( std::istream& f );
488 
489  void save( std::ostream& os, bool rle ) const;
490 
491  }; // class targa
492  } // namespace graphic
493 } // namespace claw
494 
497 
498 #endif // __CLAW_TARGA_HPP__