00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00030 #ifndef __CLAW_TARGA_HPP__
00031 #define __CLAW_TARGA_HPP__
00032
00033 #include <iostream>
00034 #include <claw/image.hpp>
00035 #include <claw/rle_decoder.hpp>
00036 #include <claw/rle_encoder.hpp>
00037 #include <claw/color_palette.hpp>
00038 #include <claw/buffered_istream.hpp>
00039
00040 namespace claw
00041 {
00042 namespace graphic
00043 {
00048 class targa : public image
00049 {
00050 private:
00051
00057 class file_structure
00058 {
00059 public:
00060 enum image_coding
00061 {
00062 color_mapped = 1,
00063 true_color = 2,
00064 black_and_white = 3,
00065 rle_color_mapped = 9,
00066 rle_true_color = 10,
00067 rle_black_and_white = 11
00068 };
00069
00070 # pragma pack (push,1)
00071
00072
00076 class header
00077 {
00078 public:
00079 header();
00080 header( unsigned int w, unsigned int h );
00081
00082 public:
00084 char id_length;
00086 char color_map;
00088 char image_type;
00089
00091 struct
00092 {
00094 unsigned short first_entry_index;
00096 unsigned short length;
00098 unsigned char entry_size;
00099 } color_map_specification;
00100
00102 struct specification
00103 {
00105 unsigned short x_origin;
00107 unsigned short y_origin;
00109 unsigned short width;
00111 unsigned short height;
00113 unsigned char bpp;
00115 unsigned char descriptor;
00116
00117 bool up_down_oriented() const ;
00118 bool left_right_oriented() const ;
00119 unsigned char alpha() const;
00120 } image_specification;
00121 };
00122
00123
00127 struct developer_item
00128 {
00130 unsigned short tag;
00132 unsigned int offset;
00134 unsigned int size;
00135 };
00136
00137
00142 struct extension
00143 {
00144
00145 };
00146
00147
00151 class footer
00152 {
00153 public:
00154 footer();
00155
00156 bool is_valid() const;
00157
00158 public:
00160 unsigned int extension_offset;
00161
00163 unsigned int developer_offset;
00164
00167 char signature[18];
00168
00169 private:
00171 static const std::string s_signature;
00172
00173 };
00174 # pragma pack (pop)
00175
00176 };
00177
00178
00185 struct pixel16
00186 {
00187 };
00188
00189
00196 struct pixel8
00197 {
00198 };
00199
00200
00204 typedef color_palette<rgba_pixel_8> color_palette32;
00205
00206 public:
00207
00213 class reader : private file_structure
00214 {
00215 private:
00216
00224 template<typename Pixel>
00225 class file_input_buffer : public buffered_istream<std::istream>
00226 {
00227 private:
00229 typedef Pixel pixel_type;
00230
00231 public:
00232 file_input_buffer( std::istream& f );
00233 rgba_pixel_8 get_pixel();
00234
00235 };
00236
00237
00245 template<typename Pixel>
00246 class mapped_file_input_buffer:
00247 public buffered_istream<std::istream>
00248 {
00249 private:
00251 typedef Pixel pixel_type;
00252
00253 public:
00254 mapped_file_input_buffer( std::istream& f, const color_palette32& p );
00255 rgba_pixel_8 get_pixel();
00256
00257 private:
00259 const color_palette32& m_palette;
00260
00261 };
00262
00263
00272 template< typename InputBuffer >
00273 class rle_targa_output_buffer
00274 {
00275 private:
00277 typedef rgba_pixel_8 pixel_type;
00278
00280 typedef InputBuffer input_buffer_type;
00281
00282 public:
00283 rle_targa_output_buffer( image& img, bool up_down, bool left_right );
00284
00285 void fill( unsigned int n, rgba_pixel_8 pattern );
00286 void copy( unsigned int n, input_buffer_type& buffer );
00287
00288 bool completed() const;
00289
00290 private:
00291 void adjust_position(int x);
00292
00293 private:
00295 image& m_image;
00296
00298 unsigned int m_x;
00299
00301 unsigned int m_y;
00302
00304 const int m_x_inc;
00305
00307 const int m_y_inc;
00308
00309 };
00310
00311
00324 template< typename InputBuffer,
00325 typename OutputBuffer = rle_targa_output_buffer<InputBuffer> >
00326 class rle_targa_decoder
00327 : public rle_decoder< rgba_pixel_8, InputBuffer, OutputBuffer >
00328 {
00329 public:
00331 typedef InputBuffer input_buffer_type;
00332
00334 typedef OutputBuffer output_buffer_type;
00335
00336 private:
00337 virtual void
00338 read_mode( input_buffer_type& input, output_buffer_type& output );
00339
00340 };
00341
00342
00344 typedef
00345 rle_targa_decoder< file_input_buffer<rgba_pixel_8> > rle32_decoder;
00346
00347
00349 typedef
00350 rle_targa_decoder< file_input_buffer<rgb_pixel_8> > rle24_decoder;
00351
00352
00354 typedef rle_targa_decoder< file_input_buffer<pixel16> > rle16_decoder;
00355
00356
00358 typedef rle_targa_decoder< mapped_file_input_buffer<pixel8> >
00359 rle8_decoder;
00360
00361 public:
00362 reader( image& img );
00363 reader( image& img, std::istream& f );
00364
00365 void load( std::istream& f );
00366
00367 private:
00368 void check_if_targa( std::istream& f ) const;
00369
00370 void load_palette
00371 ( const header& h, std::istream& f, color_palette32& palette ) const;
00372
00373 void load_color_mapped( const header& h, std::istream& f );
00374 void load_rle_color_mapped( const header& h, std::istream& f );
00375 void load_true_color( const header& h, std::istream& f );
00376 void load_rle_true_color( const header& h, std::istream& f );
00377
00378 template<typename Pixel>
00379 void load_color_mapped_raw
00380 ( const header& h, std::istream& f, const color_palette32& palette );
00381
00382 template<typename Decoder>
00383 void decompress_rle_color_mapped
00384 ( const header& h, std::istream& f, const color_palette32& palette );
00385
00386 template<typename Pixel>
00387 void load_true_color_raw( const header& h, std::istream& f );
00388
00389 template<typename Decoder>
00390 void decompress_rle_true_color( const header& h, std::istream& f );
00391
00392 template<typename Pixel>
00393 void
00394 load_palette_content( std::istream& f, color_palette32& palette ) const;
00395
00396 private:
00398 image& m_image;
00399
00400 };
00401
00402
00407 class writer : private file_structure
00408 {
00409 public:
00410
00418 template<typename Pixel>
00419 class file_output_buffer
00420 {
00421 public:
00423 typedef Pixel pixel_type;
00424
00425 typedef pixel_type pattern_type;
00426
00427 public:
00428 file_output_buffer( std::ostream& os );
00429 void encode( unsigned int n, pattern_type pattern );
00430
00431 template<typename Iterator>
00432 void raw( Iterator first, Iterator last );
00433
00434 unsigned int min_interesting() const;
00435 unsigned int max_encodable() const;
00436
00437 void order_pixel_bytes( const pixel_type& p );
00438
00439 private:
00441 std::ostream& m_stream;
00442
00443 };
00444
00445
00454 template<typename Pixel>
00455 class rle_targa_encoder
00456 : public rle_encoder< file_output_buffer<Pixel> >
00457 {
00458 public:
00460 typedef file_output_buffer<Pixel> output_buffer_type;
00461
00462 };
00463
00464
00466 typedef rle_targa_encoder<rgba_pixel_8> rle32_encoder;
00467
00468 public:
00469 writer( const image& img );
00470 writer( const image& img, std::ostream& f, bool rle );
00471
00472 void save( std::ostream& f, bool rle ) const;
00473
00474 private:
00475 void save_true_color( std::ostream& os ) const;
00476 void save_rle_true_color( std::ostream& os ) const;
00477
00478 private:
00480 const image& m_image;
00481
00482 };
00483
00484 public:
00485 targa( unsigned int w, unsigned int h );
00486 targa( const image& that );
00487 targa( std::istream& f );
00488
00489 void save( std::ostream& os, bool rle ) const;
00490
00491 };
00492 }
00493 }
00494
00495 #include <claw/impl/targa_writer.tpp>
00496 #include <claw/impl/targa_reader.tpp>
00497
00498 #endif // __CLAW_TARGA_HPP__