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>
39
40namespace 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:
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 /*--------------------------------------------------------------------*/
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
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__
This class is made to help reading istreams with a buffer.
This class is made to help reading istreams with a buffer.
Fuction object to get the first element of a std::pair.
A palette of colors, for palettized images.
A class to deal with images.
Definition image.hpp:50
char signature[18]
Footer identier. Must be as long as std::string("TRUEVISION-XFILE.") + 1 (for the last '\0').
Definition targa.hpp:170
bool is_valid() const
Tell if the data of this footer is valid.
unsigned int developer_offset
Offset of the developer directory.
Definition targa.hpp:166
unsigned int extension_offset
Offset of the extension area.
Definition targa.hpp:163
unsigned char entry_size
Number of bits per enrty.
Definition targa.hpp:98
unsigned short length
Total number of color map entries included.
Definition targa.hpp:96
unsigned short first_entry_index
Index of the first color map entry.
Definition targa.hpp:94
specification image_specification
The specification of the image.
Definition targa.hpp:123
char id_length
Image identifier length.
Definition targa.hpp:84
char color_map
1 if there is a color map, 0 otherwise.
Definition targa.hpp:86
struct claw::graphic::targa::file_structure::header::@18 color_map_specification
Color map specification.
This class read data from a targa file and store it in an image.
Definition targa.hpp:217
void load(std::istream &f)
Load an image from a targa file.
reader(image &img)
Constructor.
The type of the output buffer associated with the file when encoding RLE data.
Definition targa.hpp:423
pixel_type pattern_type
The type of the patterns to encode.
Definition targa.hpp:429
void order_pixel_bytes(const pixel_type &p)
Write a pixel in the stream and set its value in the good order.
Pixel pixel_type
The type of the pixels in the input buffer.
Definition targa.hpp:426
RLE encoder for targa format.
Definition targa.hpp:466
file_output_buffer< Pixel > output_buffer_type
Type of the output buffer.
Definition targa.hpp:469
This class write an image in a targa file.
Definition targa.hpp:411
rle_targa_encoder< rgba_pixel_8 > rle32_encoder
RLE encoder for 32 bpp targa images.
Definition targa.hpp:475
void save(std::ostream &f, bool rle) const
Save the content of the image in a stream.
writer(const image &img)
Constructor.
A class for targa pictures.
Definition targa.hpp:49
targa(unsigned int w, unsigned int h)
Constructor. Creates an empty image.
Definition targa.cpp:39
void save(std::ostream &os, bool rle) const
Save the content of the image in a stream.
Definition targa.cpp:72
A class to help decoding run-length encoded (RLE) streams.
A class to help run-length encoding (RLE) streams.
A palette of color, for palettized images.
A class to deal with images.
This is the main namespace.
Definition algorithm.hpp:34
A class to help decoding run-length encoded (RLE) streams.
A class to help run-length encoding (RLE) streams.
Item in the developper directory.
Definition targa.hpp:131
unsigned int offset
Offset in the file.
Definition targa.hpp:135
unsigned short y_origin
Lower left corner Y-origin.
Definition targa.hpp:107
unsigned short x_origin
Lower left corner X-origin.
Definition targa.hpp:105
unsigned char alpha() const
Number of bits per pixel assigned to alpha chanel.
bool left_right_oriented() const
Is image stored left to right ?