#include <png.hpp>
Definition at line 55 of file png.hpp.
Public Member Functions | |
reader (image &img) | |
Constructor. | |
reader (image &img, std::istream &f) | |
Constructor. | |
void | load (std::istream &f) |
Load an image from a png file. | |
Private Member Functions | |
void | read_from_file (std::istream &f) |
Load an image from a png file. | |
void | check_if_png (png_structp png_ptr, std::istream &f) const |
Check that the stream contains a PNG file. | |
void | read_image (png_structp png_ptr, png_infop info_ptr) |
Read the image data of the PNG. | |
void | read_sequential_image (png_structp png_ptr, png_infop info_ptr) |
Read the image data of a non interlaced PNG. | |
void | read_interlaced_image (png_structp png_ptr, png_infop info_ptr, unsigned int passes) |
Read the image data of an interlaced PNG. | |
void | copy_pixel_line (png_bytep data, unsigned int y) |
Copy the pixels taken from the PNG to the image. | |
void | create_read_structures (png_structp &png_ptr, png_infop &info_ptr) const |
Initialize the png read structures. | |
Private Attributes | |
image & | m_image |
The image in which we store the data we read. | |
Static Private Attributes | |
static const unsigned int | s_rgba_pixel_size = 4 |
Size, in bytes, of a red/green/blue/alpha pixel in a png file. | |
Classes | |
struct | source_manager |
Source manager that allow us to read from a std::istream. More... |
claw::graphic::png::reader::reader | ( | image & | img | ) |
Constructor.
img | The image in which the data will be stored. |
Definition at line 90 of file png_reader.cpp.
00091 : m_image( img ) 00092 { 00093 00094 } // png::reader::reader()
claw::graphic::png::reader::reader | ( | image & | img, | |
std::istream & | f | |||
) |
Constructor.
img | The image in which the data will be stored. | |
f | The file from which we read the data. |
Definition at line 103 of file png_reader.cpp.
References load().
void claw::graphic::png::reader::load | ( | std::istream & | f | ) |
Load an image from a png file.
f | PNG file. |
Definition at line 114 of file png_reader.cpp.
References CLAW_PRECOND, and read_from_file().
Referenced by reader().
00115 { 00116 CLAW_PRECOND( !!f ); 00117 00118 std::istream::pos_type init_pos = f.tellg(); 00119 00120 try 00121 { 00122 read_from_file(f); 00123 } 00124 catch(...) 00125 { 00126 f.clear(); 00127 f.seekg( init_pos, std::ios_base::beg ); 00128 throw; 00129 } 00130 } // png::reader::load()
void claw::graphic::png::reader::read_from_file | ( | std::istream & | f | ) | [private] |
Load an image from a png file.
f | PNG file. |
Definition at line 137 of file png_reader.cpp.
References check_if_png(), claw__graphic__png__source_manager__read(), CLAW_EXCEPTION, create_read_structures(), and read_image().
Referenced by load().
00138 { 00139 source_manager infile(f); 00140 png_structp png_ptr; 00141 png_infop info_ptr; 00142 00143 create_read_structures(png_ptr, info_ptr); 00144 00145 if (setjmp(png_jmpbuf(png_ptr))) 00146 { 00147 /* If we get here, we had a problem reading the file */ 00148 /* Free all of the memory associated with the png_ptr and info_ptr */ 00149 png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL); 00150 throw CLAW_EXCEPTION("Invalid PNG file."); 00151 } 00152 00153 check_if_png( png_ptr, f ); 00154 00155 png_set_read_fn( png_ptr, (void *)&infile, 00156 claw__graphic__png__source_manager__read ); 00157 00158 png_set_strip_16(png_ptr); 00159 png_set_gray_1_2_4_to_8(png_ptr); 00160 png_set_packing(png_ptr); 00161 00162 // transform palette index into RGB value 00163 png_set_palette_to_rgb(png_ptr); 00164 00165 // add an alpha value if none 00166 png_set_filler( png_ptr, std::numeric_limits<pixel32::component_type>::max(), 00167 PNG_FILLER_AFTER ); 00168 00169 png_read_info(png_ptr, info_ptr); 00170 read_image( png_ptr, info_ptr ); 00171 00172 png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL); 00173 } // png::reader::read_from_file()
void claw::graphic::png::reader::check_if_png | ( | png_structp | png_ptr, | |
std::istream & | f | |||
) | const [private] |
Check that the stream contains a PNG file.
png_ptr | PNG file description. | |
f | The stream to read from. |
Definition at line 182 of file png_reader.cpp.
References CLAW_EXCEPTION, and CLAW_PRECOND.
Referenced by read_from_file().
00183 { 00184 CLAW_PRECOND( !!f ); 00185 00186 const unsigned int bytes_to_check = 8; 00187 png_byte buffer[bytes_to_check]; 00188 00189 /* Read in some of the signature bytes */ 00190 f.read( (char*)buffer, bytes_to_check * sizeof(png_byte) ); 00191 00192 if ( (png_sig_cmp(buffer, (png_size_t)0, bytes_to_check) != 0) || !f ) 00193 throw CLAW_EXCEPTION( "Not a PNG file." ); 00194 00195 png_set_sig_bytes(png_ptr, bytes_to_check); 00196 } // png::reader::check_if_png()
void claw::graphic::png::reader::read_image | ( | png_structp | png_ptr, | |
png_infop | info_ptr | |||
) | [private] |
Read the image data of the PNG.
png_ptr | PNG file description. | |
info_ptr | PNG file informations. |
Definition at line 205 of file png_reader.cpp.
References CLAW_PRECOND, m_image, read_interlaced_image(), read_sequential_image(), and claw::graphic::image::set_size().
Referenced by read_from_file().
00206 { 00207 CLAW_PRECOND( png_ptr ); 00208 CLAW_PRECOND( info_ptr ); 00209 00210 m_image.set_size( png_get_image_width(png_ptr, info_ptr), 00211 png_get_image_height(png_ptr, info_ptr) ); 00212 00213 if ( png_get_interlace_type(png_ptr, info_ptr) == PNG_INTERLACE_NONE ) 00214 read_sequential_image(png_ptr, info_ptr); 00215 else 00216 read_interlaced_image( png_ptr, info_ptr, 00217 png_set_interlace_handling(png_ptr) ); 00218 } // png::reader::read_image()
void claw::graphic::png::reader::read_sequential_image | ( | png_structp | png_ptr, | |
png_infop | info_ptr | |||
) | [private] |
Read the image data of a non interlaced PNG.
png_ptr | PNG file description. | |
info_ptr | PNG file informations. |
Definition at line 227 of file png_reader.cpp.
References CLAW_PRECOND, copy_pixel_line(), claw::graphic::image::height(), m_image, s_rgba_pixel_size, and claw::graphic::image::width().
Referenced by read_image().
00228 { 00229 CLAW_PRECOND( png_ptr ); 00230 CLAW_PRECOND( info_ptr ); 00231 00232 png_bytep data = 00233 (png_bytep)png_malloc( png_ptr, s_rgba_pixel_size * m_image.width() ); 00234 00235 try 00236 { 00237 for (unsigned int y=0; y!=m_image.height(); ++y) 00238 { 00239 png_read_row(png_ptr, data, NULL); 00240 copy_pixel_line( data, y ); 00241 } 00242 } 00243 catch(...) 00244 { 00245 png_free(png_ptr, data); 00246 throw; 00247 } 00248 00249 png_free(png_ptr, data); 00250 } // png::reader::read_sequential_image()
void claw::graphic::png::reader::read_interlaced_image | ( | png_structp | png_ptr, | |
png_infop | info_ptr, | |||
unsigned int | passes | |||
) | [private] |
Read the image data of an interlaced PNG.
png_ptr | PNG file description. | |
info_ptr | PNG file informations. | |
passes | Number of passes (for interlaced images). |
Definition at line 260 of file png_reader.cpp.
References CLAW_PRECOND, copy_pixel_line(), claw::graphic::image::height(), m_image, s_rgba_pixel_size, and claw::graphic::image::width().
Referenced by read_image().
00261 { 00262 CLAW_PRECOND( passes > 1 ); 00263 CLAW_PRECOND( png_ptr ); 00264 CLAW_PRECOND( info_ptr ); 00265 00266 const unsigned int row_length = s_rgba_pixel_size * m_image.width(); 00267 png_bytepp data = 00268 (png_bytepp)png_malloc( png_ptr, sizeof(png_bytep) * m_image.height() ); 00269 unsigned int i=0; 00270 00271 try 00272 { 00273 for (i=0; i!=m_image.height(); ++i) 00274 { 00275 data[i] = (png_bytep)png_malloc( png_ptr, row_length ); 00276 00277 if (!data[i]) 00278 throw std::bad_alloc(); 00279 00280 copy_pixel_line( data[i], i ); 00281 } 00282 00283 for (unsigned int p=0; p!=passes; ++p) 00284 png_read_rows( png_ptr, data, NULL, m_image.height() ); 00285 00286 for (unsigned int y=0; y!=m_image.height(); ++y) 00287 copy_pixel_line( data[y], y ); 00288 } 00289 catch(...) 00290 { 00291 for(unsigned int j=0; j!=i; ++j) 00292 png_free(png_ptr, data[j]); 00293 00294 png_free(png_ptr, data); 00295 throw; 00296 } 00297 00298 for(i=0; i!=m_image.height(); ++i) 00299 png_free(png_ptr, data[i]); 00300 00301 png_free(png_ptr, data); 00302 } // png::reader::read_interlaced_image()
void claw::graphic::png::reader::copy_pixel_line | ( | png_bytep | data, | |
unsigned int | y | |||
) | [private] |
Copy the pixels taken from the PNG to the image.
data | the pixels from the PNG image. | |
y | Index of the line of the image in which we copy the pixels. |
Definition at line 311 of file png_reader.cpp.
References CLAW_PRECOND, claw::graphic::image::height(), m_image, s_rgba_pixel_size, and claw::graphic::image::width().
Referenced by read_interlaced_image(), and read_sequential_image().
00312 { 00313 CLAW_PRECOND( data ); 00314 CLAW_PRECOND( y < m_image.height() ); 00315 00316 // four bytes for each pixel in the line 00317 for (unsigned int x=0; x!=m_image.width(); ++x, data+=s_rgba_pixel_size) 00318 { 00319 m_image[y][x].components.red = data[0]; 00320 m_image[y][x].components.green = data[1]; 00321 m_image[y][x].components.blue = data[2]; 00322 m_image[y][x].components.alpha = data[3]; 00323 } 00324 } // png::reader::copy_pixel_line()
void claw::graphic::png::reader::create_read_structures | ( | png_structp & | png_ptr, | |
png_infop & | info_ptr | |||
) | const [private] |
Initialize the png read structures.
png_ptr | PNG file description. | |
info_ptr | PNG file informations. |
Definition at line 333 of file png_reader.cpp.
References CLAW_EXCEPTION.
Referenced by read_from_file().
00334 { 00335 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); 00336 00337 if (png_ptr) 00338 { 00339 info_ptr = png_create_info_struct(png_ptr); 00340 00341 if (!info_ptr) 00342 png_destroy_read_struct(&png_ptr, png_infopp_NULL, png_infopp_NULL); 00343 } 00344 00345 if (!png_ptr || !info_ptr) 00346 throw CLAW_EXCEPTION("Can't create PNG read structures."); 00347 } // png::reader::create_read_structures()
image& claw::graphic::png::reader::m_image [private] |
The image in which we store the data we read.
Definition at line 100 of file png.hpp.
Referenced by copy_pixel_line(), read_image(), read_interlaced_image(), and read_sequential_image().
const unsigned int claw::graphic::png::reader::s_rgba_pixel_size = 4 [static, private] |
Size, in bytes, of a red/green/blue/alpha pixel in a png file.
Definition at line 104 of file png.hpp.
Referenced by copy_pixel_line(), read_interlaced_image(), and read_sequential_image().