File: targa_reader.cpp

package info (click to toggle)
libclaw 1.7.4-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,080 kB
  • sloc: cpp: 13,287; sh: 227; makefile: 8
file content (404 lines) | stat: -rw-r--r-- 11,912 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
  CLAW - a C++ Library Absolutely Wonderful

  CLAW is a free library without any particular aim but being useful to 
  anyone.

  Copyright (C) 2005-2011 Julien Jorge

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

  contact: julien.jorge@gamned.org
*/
/**
 * \file targa_reader.cpp
 * \brief Implementation of the targa::reader class.
 * \author Julien Jorge
 */
#include <claw/targa.hpp>
#include <claw/exception.hpp>



//********************* targa::reader::file_input_buffer ***********************



namespace claw
{
  namespace graphic
  {
    /*------------------------------------------------------------------------*/
    /**
     * \brief Read a pixel from the file.
     *
     * \remark This method is specialized for the pixels of type
     *         claw::graphic::rgba_pixel_8.
     */
    template< >
    rgba_pixel_8 targa::reader::file_input_buffer<rgba_pixel_8>::get_pixel()
    {
      rgba_pixel_8 result;
      
      if ( this->remaining() < 4 )
        this->read_more(4);
      
      assert( this->remaining() >= 4 );
      
      result.components.blue  = this->get_next();
      result.components.green = this->get_next();
      result.components.red   = this->get_next();
      result.components.alpha = this->get_next();
      
      return result;
    } // targa::reader::file_input_buffer::get_pixel()
  } // namespace graphic
} // namespace claw

namespace claw
{
  namespace graphic
  {
    /*------------------------------------------------------------------------*/
    /**
     * \brief Read a pixel from the file.
     *
     * \remark This method is specialized for the pixels of type
     *         claw::graphic::targa::reader::rgb_pixel_8.
     */
    template< >
    rgba_pixel_8 targa::reader::file_input_buffer<rgb_pixel_8>::get_pixel()
    {
      rgba_pixel_8 result;
      
      if ( this->remaining() < 3 )
        this->read_more(3);
      
      assert( this->remaining() >= 3 );
      
      result.components.blue  = this->get_next();
      result.components.green = this->get_next();
      result.components.red   = this->get_next();
      result.components.alpha =
        std::numeric_limits<claw::graphic::rgba_pixel_8::component_type>::max();
      
      return result;
    } // targa::reader::file_input_buffer::get_pixel()
  } // namespace graphic
} // namespace claw

namespace claw
{
  namespace graphic
  {
    /*------------------------------------------------------------------------*/
    /**
     * \brief Read a pixel from the file.
     *
     * \remark This method is specialized for the pixels of type
     *         claw::graphic::targa::reader::pixel16.
     */
    template< >
    rgba_pixel_8 targa::reader::file_input_buffer<targa::pixel16>::get_pixel()
    {
      rgba_pixel_8 result;

      if ( this->remaining() < 2 )
        this->read_more(2);

      assert( this->remaining() >= 2 );

      unsigned char second_byte  = this->get_next();
      unsigned char first_byte = this->get_next();

      unsigned char r = (first_byte & 0x7C) >> 2;
      unsigned char g =
        ((first_byte & 0x03) << 3) | ((second_byte & 0xE0) >> 5);
      unsigned char b = second_byte & 0x1F;

      result.components.blue  = b * 8;
      result.components.green = g * 8;
      result.components.red   = r * 8;
      result.components.alpha =
        std::numeric_limits<claw::graphic::rgba_pixel_8::component_type>::max();

      return result;
    } // targa::reader::file_input_buffer::get_pixel()
  } // namespace graphic
} // namespace claw







//****************** targa::reader::mapped_file_input_buffer *******************



namespace claw
{
  namespace graphic
  {
    /*------------------------------------------------------------------------*/
    /**
     * \brief Read a pixel from the file.
     *
     * \remark This method is specialized for the pixels of type
     *         claw::graphic::pixel8.
     */
    template< >
    rgba_pixel_8
    targa::reader::mapped_file_input_buffer<targa::pixel8>::get_pixel()
    {
      if ( this->remaining() < 1 )
        this->read_more(1);
      
      assert( this->remaining() >= 1 );
      
      unsigned char index = this->get_next();

      return m_palette[index];
    } // targa::reader::mapped_file_input_buffer::get_pixel()
  } // namespace graphic
} // namespace claw





//****************************** targa::reader *********************************




/*----------------------------------------------------------------------------*/
/**
 * \brief Constructor.
 * \param img The image in which the data will be stored.
 */
claw::graphic::targa::reader::reader( image& img )
  : m_image( img )
{

} // targa::reader::reader()

/*----------------------------------------------------------------------------*/
/**
 * \brief Constructor.
 * \param img The image in which the data will be stored.
 * \param f The file from which we read the data.
 * \post img contains the data from \a f.
 */
claw::graphic::targa::reader::reader( image& img, std::istream& f )
  : m_image( img )
{
  load(f);
} // targa::reader::reader()

/*----------------------------------------------------------------------------*/
/**
 * \brief Load an image from a targa file.
 * \param f Targa file.
 */
void claw::graphic::targa::reader::load( std::istream& f )
{
  CLAW_PRECOND( !!f );
  std::istream::pos_type init_pos = f.tellg();

  try
    {
      check_if_targa(f);

      header h;

      f.read( reinterpret_cast<char*>(&h), sizeof(header) );
      
      if ( f.rdstate() == std::ios_base::goodbit )
        {
          m_image.set_size( h.image_specification.width,
                            h.image_specification.height );
          
          switch(h.image_type)
            {
            case color_mapped: load_color_mapped(h, f); break;
            case rle_color_mapped: load_rle_color_mapped(h, f); break;
            case true_color: load_true_color(h, f); break;
            case rle_true_color: load_rle_true_color(h, f); break;
            default :
              throw claw::bad_format
                ( "targa::reader::targa: unsupported image type" );
            }
        }
      else
        throw claw::bad_format
          ( "claw::targa::reader::targa: can't read header" );
    }
  catch(...)
    {
      f.clear();
      f.seekg( init_pos, std::ios_base::beg );
      throw;
    }
} // targa::reader::load()

/*----------------------------------------------------------------------------*/
/**
 * \brief Check if a stream contains targa data.
 * \param f The stream to check.
 */
void claw::graphic::targa::reader::check_if_targa( std::istream& f ) const
{
  CLAW_PRECOND( !!f );

  std::istream::pos_type init_pos = f.tellg();

  footer foot;

  f.seekg( -(std::istream::off_type)sizeof(footer), std::ios::end );
  f.read( reinterpret_cast<char*>(&foot), sizeof(footer) );
  f.seekg( init_pos , std::ios::beg );
  
  if ( !foot.is_valid() )
    throw CLAW_EXCEPTION( "Not a Targa file." );
} // targa::reader::check_if_targa()

/*----------------------------------------------------------------------------*/
/**
 * \brief Load the color palette.
 * \param h File's header, must have been read before call.
 * \param f Targa file.
 * \param palette (out) The color palette.
 * \pre (h.image_type == color_mapped) || (h.image_type == rle_color_mapped)
 */
void claw::graphic::targa::reader::load_palette
( const header& h, std::istream& f, color_palette32& palette ) const
{
  assert((h.image_type == color_mapped) || (h.image_type == rle_color_mapped));

  switch( h.color_map_specification.entry_size )
    {
    case 16: load_palette_content<pixel16>(f, palette); break;
    case 24: load_palette_content<rgb_pixel_8>(f, palette); break;
    case 32: load_palette_content<rgba_pixel_8>(f, palette); break;
    default: 
      throw claw::bad_format
        ( "targa::reader::load_palette: unsupported entry size" );
    }
} // targa::reader::load_palette()

/*----------------------------------------------------------------------------*/
/**
 * \brief Load a not compressed color mapped targa file.
 * \param h File's header, must have been read before call.
 * \param f Targa file.
 * \pre h.image_type == color_mapped
 */
void claw::graphic::targa::reader::load_color_mapped
( const header& h, std::istream& f )
{
  assert(h.image_type == color_mapped);

  f.seekg( h.id_length, std::ios_base::cur );

  color_palette32 palette( h.color_map_specification.length );
  load_palette( h, f, palette );

  switch(h.image_specification.bpp)
    {
    case 8: load_color_mapped_raw<pixel8>(h, f, palette); break;
    default: 
      throw claw::bad_format
        ( "targa::reader::load_color_mapped: unsupported color depth" );
    }
} // targa::reader::load_color_mapped()

/*----------------------------------------------------------------------------*/
/**
 * \brief Load a RLE color mapped targa file.
 * \param h File's header, must have been read before call.
 * \param f Targa file.
 * \pre h.image_type == color_mapped
 */
void claw::graphic::targa::reader::load_rle_color_mapped
( const header& h, std::istream& f )
{
  assert(h.image_type == rle_color_mapped);

  f.seekg( h.id_length, std::ios_base::cur );

  color_palette32 palette( h.color_map_specification.length );
  load_palette( h, f, palette );

  switch(h.image_specification.bpp)
    {
    case 8: decompress_rle_color_mapped<rle8_decoder>(h, f, palette); break;
    default: 
      throw claw::bad_format
        ( "targa::reader::load_rle_color_mapped: unsupported color depth" );
    }
} // targa::reader::load_color_mapped()

/*----------------------------------------------------------------------------*/
/**
 * \brief Load a not compressed true color targa file.
 * \param h File's header, must have been read before call.
 * \param f Targa file.
 * \pre h.image_type == true_color
 */
void claw::graphic::targa::reader::load_true_color
( const header& h, std::istream& f )
{
  assert(h.image_type == true_color);

  f.seekg( h.id_length, std::ios_base::cur );

  switch(h.image_specification.bpp)
    {
    case 16 : load_true_color_raw<pixel16>(h, f); break;
    case 24 : load_true_color_raw<rgb_pixel_8>(h, f); break;
    case 32 : load_true_color_raw<rgba_pixel_8>(h, f); break;
    default : 
      throw claw::bad_format
        ( "targa::reader::load_true_color: unsupported color depth" );
    }
} // targa::reader::load_true_color()

/*----------------------------------------------------------------------------*/
/**
 * \brief Load a RLE true color targa file.
 * \param h File's header, must have been read before call.
 * \param f Targa file.
 * \pre h.image_type == rle_true_color
 */
void claw::graphic::targa::reader::load_rle_true_color
( const header& h, std::istream& f )
{
  assert(h.image_type == rle_true_color);

  f.seekg( h.id_length, std::ios_base::cur );

  switch(h.image_specification.bpp)
    {
    case 16 : decompress_rle_true_color<rle16_decoder>(h, f); break;
    case 24 : decompress_rle_true_color<rle24_decoder>(h, f); break;
    case 32 : decompress_rle_true_color<rle32_decoder>(h, f); break;
    default : 
      throw claw::bad_format
        ( "targa::reader::load_rle_true_color: unsupported color depth" );
    }
} // targa::reader::load_rle_true_color()