This class write an image in a png file. More...
#include <png.hpp>
Classes | |
struct | options |
Parameters of the writing algorithm. More... | |
struct | target_manager |
Target manager that allow us to write in a std::ostream. More... | |
Public Member Functions | |
writer (const image &img) | |
Constructor. | |
writer (const image &img, std::ostream &f, const options &opt=options()) | |
Constructor. | |
void | save (std::ostream &f, const options &opt=options()) const |
Save the image in a PNG file. | |
Private Member Functions | |
void | set_options (png_structp png_ptr, png_infop info_ptr, const options &opt) const |
Set the parameters of the PNG saving structures. | |
void | save_image (png_structp png_ptr, png_infop info_ptr) const |
Save the image in a configured PNG file. | |
void | copy_pixel_line (png_bytep data, unsigned int y) const |
Copy the pixels from the image to an array of bytes. | |
void | create_write_structures (png_structp &png_ptr, png_infop &info_ptr) const |
Initialize the png write structures. | |
Private Attributes | |
const image & | m_image |
The image from which we thake the data to save. | |
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. |
This class write an image in a png file.
Definition at line 113 of file png.hpp.
claw::graphic::png::writer::writer | ( | const image & | img ) |
Constructor.
img | The image in which the data will be stored. |
Definition at line 136 of file png_writer.cpp.
: m_image( img ) { } // png::writer::writer()
claw::graphic::png::writer::writer | ( | const image & | img, |
std::ostream & | f, | ||
const options & | opt = options() |
||
) |
Constructor.
img | The image to save. |
f | The file in which we write the data. |
opt | Saving options. |
Definition at line 150 of file png_writer.cpp.
References claw::graphic::png::save().
void claw::graphic::png::writer::copy_pixel_line | ( | png_bytep | data, |
unsigned int | y | ||
) | const [private] |
Copy the pixels from the image to an array of bytes.
data | (out) The pixels for the PNG image. |
y | Index of the line of the image from which we read the pixels. |
Definition at line 268 of file png_writer.cpp.
References CLAW_PRECOND.
{ CLAW_PRECOND( data ); CLAW_PRECOND( y < m_image.height() ); // four bytes for each pixel in the line for (unsigned int x=0; x!=m_image.width(); ++x, data+=s_rgba_pixel_size) { data[0] = m_image[y][x].components.red; data[1] = m_image[y][x].components.green; data[2] = m_image[y][x].components.blue; data[3] = m_image[y][x].components.alpha; } } // png::writer::copy_pixel_line()
void claw::graphic::png::writer::create_write_structures | ( | png_structp & | png_ptr, |
png_infop & | info_ptr | ||
) | const [private] |
Initialize the png write structures.
png_ptr | PNG file description. |
info_ptr | PNG file informations. |
Definition at line 290 of file png_writer.cpp.
References CLAW_EXCEPTION.
{ png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (png_ptr) { info_ptr = png_create_info_struct(png_ptr); if (!info_ptr) png_destroy_write_struct(&png_ptr, (png_infopp)NULL); } if (!png_ptr || !info_ptr) throw CLAW_EXCEPTION("Can't create PNG write structures."); } // png::writer::create_write_structures()
Save the image in a PNG file.
f | PNG file. |
opt | Saving options. |
Definition at line 163 of file png_writer.cpp.
References claw__graphic__png__target_manager__flush(), claw__graphic__png__target_manager__write(), CLAW_EXCEPTION, and CLAW_PRECOND.
{ CLAW_PRECOND( !!f ); target_manager outfile(f); png_structp png_ptr; png_infop info_ptr; create_write_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_write_struct(&png_ptr, &info_ptr); throw CLAW_EXCEPTION("Invalid PNG file."); } png_set_write_fn( png_ptr, (void *)&outfile, claw__graphic__png__target_manager__write, claw__graphic__png__target_manager__flush ); set_options( png_ptr, info_ptr, opt ); save_image( png_ptr, info_ptr ); png_destroy_write_struct(&png_ptr, &info_ptr); } // png::writer::save()
void claw::graphic::png::writer::save_image | ( | png_structp | png_ptr, |
png_infop | info_ptr | ||
) | const [private] |
Save the image in a configured PNG file.
png_ptr | PNG file description. |
info_ptr | PNG file informations. |
Definition at line 220 of file png_writer.cpp.
References CLAW_PRECOND.
{ 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; 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( data[i], i ); } png_set_rows(png_ptr, info_ptr, data); png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL); } 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::writer::save_image()
void claw::graphic::png::writer::set_options | ( | png_structp | png_ptr, |
png_infop | info_ptr, | ||
const options & | opt | ||
) | const [private] |
Set the parameters of the PNG saving structures.
png_ptr | PNG file description. |
info_ptr | PNG file informations. |
opt | Compression options. |
Definition at line 199 of file png_writer.cpp.
References CLAW_PRECOND, claw::graphic::png::writer::options::compression, and claw::graphic::png::writer::options::interlace.
{ CLAW_PRECOND( png_ptr ); CLAW_PRECOND( info_ptr ); png_set_compression_level( png_ptr, opt.compression ); png_set_IHDR( png_ptr, info_ptr, m_image.width(), m_image.height(), sizeof(pixel_type::component_type) * 8, /* 8 bits per byte */ PNG_COLOR_TYPE_RGB_ALPHA, opt.interlace, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT ); } // png::writer::set_options()
const image& claw::graphic::png::writer::m_image [private] |
const unsigned int claw::graphic::png::writer::s_rgba_pixel_size = 4 [static, private] |