File: png_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 (367 lines) | stat: -rw-r--r-- 11,067 bytes parent folder | download | duplicates (4)
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
/*
  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 png_reader.cpp
 * \brief Implementation of the claw::graphic::png::reader class.
 * \author Julien Jorge
 */
#include <claw/png.hpp>

#include <claw/exception.hpp>
#include <claw/assert.hpp>

#include <limits>

/*----------------------------------------------------------------------------*/
/**
 * \brief Read data from the input stream.
 * \param png_ptr Informations about the PNG we are reading.
 * \param data (out) Array of the bytes we have read.
 * \param length Number of bytes to read.
 */
void claw__graphic__png__source_manager__read
( png_structp png_ptr, png_bytep data, png_size_t length )
{
  claw::graphic::png::reader::source_manager* self =
    (claw::graphic::png::reader::source_manager*)png_get_io_ptr(png_ptr);

  self->read(data, length);
} // claw__graphic__png__source_manager__read()




/*----------------------------------------------------------------------------*/
/**
 * \brief Constructor.
 * \param is The stream we read from.
 */
claw::graphic::png::reader::source_manager::source_manager( std::istream& is )
  : m_input(is)
{
  CLAW_PRECOND( !!is );
} // png::reader::source_manager::source_manager()

/*----------------------------------------------------------------------------*/
/**
 * \brief Read data from the input stream.
 * \param data (out) Array of the bytes we have read.
 * \param length Number of bytes to read.
 */
void claw::graphic::png::reader::source_manager::read
( png_bytep data, png_size_t length )
{
  m_input.read( (char*)data, length * sizeof(png_byte) );
} // png::reader::source_manager::read()




/*----------------------------------------------------------------------------*/
const unsigned int claw::graphic::png::reader::s_rgba_pixel_size = 4;

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

} // png::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::png::reader::reader( image& img, std::istream& f )
  : m_image( img )
{
  load(f);
} // png::reader::reader()

/*----------------------------------------------------------------------------*/
/**
 * \brief Load an image from a png file.
 * \param f PNG file.
 */
void claw::graphic::png::reader::load( std::istream& f )
{
  CLAW_PRECOND( !!f );

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

  try
    {
      read_from_file(f);
    }
  catch(...)
    {
      f.clear();
      f.seekg( init_pos, std::ios_base::beg );
      throw;
    }
} // png::reader::load()

/*----------------------------------------------------------------------------*/
/**
 * \brief Load an image from a png file.
 * \param f PNG file.
 */
void claw::graphic::png::reader::read_from_file( std::istream& f )
{
  source_manager infile(f);
  png_structp png_ptr;
  png_infop info_ptr;

  create_read_structures(png_ptr, info_ptr);

  if (setjmp(png_jmpbuf(png_ptr)))
    {
      /* If we get here, we had a problem reading the file */
      /* Free all of the memory associated with the png_ptr and info_ptr */
      png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
      throw CLAW_EXCEPTION("Invalid PNG file.");
    }
      
  check_if_png( png_ptr, f );

  png_set_read_fn( png_ptr, (void *)&infile,
                   claw__graphic__png__source_manager__read );

  png_read_info(png_ptr, info_ptr);

  png_set_strip_16(png_ptr);
  png_set_expand_gray_1_2_4_to_8(png_ptr);
  png_set_packing(png_ptr);

  png_set_tRNS_to_alpha(png_ptr);

  // transform palette index into RGB value
  png_set_palette_to_rgb(png_ptr);
  
  // add an alpha value if none
  png_set_filler( png_ptr,
                  std::numeric_limits<rgba_pixel_8::component_type>::max(),
                  PNG_FILLER_AFTER );

  png_read_update_info(png_ptr, info_ptr);

  read_image( png_ptr, info_ptr );
      
  png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
} // png::reader::read_from_file()

/*----------------------------------------------------------------------------*/
/**
 * \brief Check that the stream contains a PNG file.
 * \param png_ptr PNG file description.
 * \param f The stream to read from.
 */
void claw::graphic::png::reader::check_if_png
( png_structp png_ptr, std::istream& f ) const
{
  CLAW_PRECOND( !!f );

  const unsigned int bytes_to_check = 8;
  png_byte buffer[bytes_to_check];

  /* Read in some of the signature bytes */
  f.read( (char*)buffer, bytes_to_check * sizeof(png_byte) );

  if ( (png_sig_cmp(buffer, (png_size_t)0, bytes_to_check) != 0) || !f )
    throw CLAW_EXCEPTION( "Not a PNG file." );

  png_set_sig_bytes(png_ptr, bytes_to_check);
} // png::reader::check_if_png()

/*----------------------------------------------------------------------------*/
/**
 * \brief Read the image data of the PNG.
 * \param png_ptr PNG file description.
 * \param info_ptr PNG file informations.
 */
void claw::graphic::png::reader::read_image
( png_structp png_ptr, png_infop info_ptr )
{
  CLAW_PRECOND( png_ptr );
  CLAW_PRECOND( info_ptr );

  m_image.set_size( png_get_image_width(png_ptr, info_ptr),
                    png_get_image_height(png_ptr, info_ptr) );

  if ( png_get_interlace_type(png_ptr, info_ptr) == PNG_INTERLACE_NONE )
    read_sequential_image(png_ptr, info_ptr);
  else
    read_interlaced_image( png_ptr, info_ptr,
                           png_set_interlace_handling(png_ptr) );
} // png::reader::read_image()

/*----------------------------------------------------------------------------*/
/**
 * \brief Read the image data of a non interlaced PNG.
 * \param png_ptr PNG file description.
 * \param info_ptr PNG file informations.
 */
void claw::graphic::png::reader::read_sequential_image
( png_structp png_ptr, png_infop info_ptr )
{
  CLAW_PRECOND( png_ptr );
  CLAW_PRECOND( info_ptr );

  png_bytep data =
    (png_bytep)png_malloc( png_ptr, s_rgba_pixel_size * m_image.width() );
  const png_byte color_type( png_get_color_type(png_ptr, info_ptr) );

  try
    {
      for (unsigned int y=0; y!=m_image.height(); ++y)
        {
          png_read_row(png_ptr, data, NULL);
          copy_pixel_line( color_type, data, y );
        }
    }
  catch(...)
    {
      png_free(png_ptr, data);
      throw;
    }

  png_free(png_ptr, data);
} // png::reader::read_sequential_image()

/*----------------------------------------------------------------------------*/
/**
 * \brief Read the image data of an interlaced PNG.
 * \param png_ptr PNG file description.
 * \param info_ptr PNG file informations.
 * \param passes Number of passes (for interlaced images).
 */
void claw::graphic::png::reader::read_interlaced_image
( png_structp png_ptr, png_infop info_ptr, unsigned int passes )
{
  CLAW_PRECOND( passes > 1 );
  CLAW_PRECOND( png_ptr );
  CLAW_PRECOND( info_ptr );

  const unsigned int row_length = s_rgba_pixel_size * m_image.width();
  png_bytepp data =
    (png_bytepp)png_malloc( png_ptr, sizeof(png_bytep) * m_image.height() );
  unsigned int i=0;
  const png_byte color_type( png_get_color_type(png_ptr, info_ptr) );

  try
    {
      for (i=0; i!=m_image.height(); ++i)
        {
          data[i] = (png_bytep)png_malloc( png_ptr, row_length );

          if (!data[i])
            throw std::bad_alloc();

          copy_pixel_line( color_type, data[i], i );
        }

      for (unsigned int p=0; p!=passes; ++p)
        png_read_rows( png_ptr, data, NULL, m_image.height() );

      for (unsigned int y=0; y!=m_image.height(); ++y)
        copy_pixel_line( color_type, data[y], y );
    }
  catch(...)
    {
      for(unsigned int j=0; j!=i; ++j)
        png_free(png_ptr, data[j]);

      png_free(png_ptr, data);
      throw;
    }

  for(i=0; i!=m_image.height(); ++i)
    png_free(png_ptr, data[i]);

  png_free(png_ptr, data);
} // png::reader::read_interlaced_image()

/*----------------------------------------------------------------------------*/
/**
 * \brief Copy the pixels taken from the PNG to the image.
 * \param color_type The structure of the colors in the data.
 * \param data the pixels from the PNG image.
 * \param y Index of the line of the image in which we copy the pixels.
 */
void
claw::graphic::png::reader::copy_pixel_line
( png_byte color_type, png_bytep data, unsigned int y )
{
  CLAW_PRECOND( data );
  CLAW_PRECOND( y < m_image.height() );

  if ( color_type == PNG_COLOR_TYPE_GRAY_ALPHA ) 
    // There is two bytes for each pixel in the line: the color and the opacity.
    for (unsigned int x=0; x!=m_image.width(); ++x, data += 2)
      {
        m_image[y][x].components.red   = data[0];
        m_image[y][x].components.green = data[0];
        m_image[y][x].components.blue  = data[0];
        m_image[y][x].components.alpha = data[1];
      }
  else
    // There is four bytes for each pixel in the line.
    for (unsigned int x=0; x!=m_image.width(); ++x, data+=s_rgba_pixel_size)
      {
        m_image[y][x].components.red   = data[0];
        m_image[y][x].components.green = data[1];
        m_image[y][x].components.blue  = data[2];
        m_image[y][x].components.alpha = data[3];
      }
} // png::reader::copy_pixel_line()

/*----------------------------------------------------------------------------*/
/**
 * \brief Initialize the png read structures.
 * \param png_ptr PNG file description.
 * \param info_ptr PNG file informations.
 */
void claw::graphic::png::reader::create_read_structures
( png_structp& png_ptr, png_infop& info_ptr ) const
{
  png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);

  if (png_ptr)
    {
      info_ptr = png_create_info_struct(png_ptr);

      if (!info_ptr)
        png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
    }

  if (!png_ptr || !info_ptr)
    throw CLAW_EXCEPTION("Can't create PNG read structures.");
} // png::reader::create_read_structures()