43 ( png_structp png_ptr, png_bytep data, png_size_t length )
48 self->
write(data, length);
85 ( png_bytep data, png_size_t length )
87 m_output.write( (
char*)data, length *
sizeof(png_byte) );
107 : compression(default_compression), interlace(none)
129 const unsigned int claw::graphic::png::writer::s_rgba_pixel_size = 4;
171 create_write_structures(png_ptr, info_ptr);
173 if (setjmp(png_jmpbuf(png_ptr)))
177 png_destroy_write_struct(&png_ptr, &info_ptr);
181 png_set_write_fn( png_ptr, (
void *)&outfile,
185 set_options( png_ptr, info_ptr, opt );
186 save_image( png_ptr, info_ptr );
188 png_destroy_write_struct(&png_ptr, &info_ptr);
198 void claw::graphic::png::writer::set_options
199 ( png_structp png_ptr, png_infop info_ptr,
const options& opt )
const 204 png_set_compression_level( png_ptr, opt.
compression );
206 png_set_IHDR( png_ptr, info_ptr, m_image.
width(), m_image.
height(),
208 PNG_COLOR_TYPE_RGB_ALPHA,
209 opt.
interlace, PNG_COMPRESSION_TYPE_DEFAULT,
210 PNG_FILTER_TYPE_DEFAULT );
219 void claw::graphic::png::writer::save_image
220 ( png_structp png_ptr, png_infop info_ptr )
const 225 const unsigned int row_length = s_rgba_pixel_size * m_image.
width();
227 (png_bytepp)png_malloc( png_ptr,
sizeof(png_bytep) * m_image.
height() );
232 for (i=0; i!=m_image.
height(); ++i)
234 data[i] = (png_bytep)png_malloc( png_ptr, row_length );
237 throw std::bad_alloc();
239 copy_pixel_line( data[i], i );
242 png_set_rows(png_ptr, info_ptr, data);
243 png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
247 for(
unsigned int j=0; j!=i; ++j)
248 png_free(png_ptr, data[j]);
250 png_free(png_ptr, data);
254 for(i=0; i!=m_image.
height(); ++i)
255 png_free(png_ptr, data[i]);
257 png_free(png_ptr, data);
267 void claw::graphic::png::writer::copy_pixel_line
268 ( png_bytep data,
unsigned int y )
const 274 for (
unsigned int x=0; x!=m_image.
width(); ++x, data+=s_rgba_pixel_size)
276 data[0] = m_image[y][x].components.red;
277 data[1] = m_image[y][x].components.green;
278 data[2] = m_image[y][x].components.blue;
279 data[3] = m_image[y][x].components.alpha;
289 void claw::graphic::png::writer::create_write_structures
290 ( png_structp& png_ptr, png_infop& info_ptr )
const 292 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
296 info_ptr = png_create_info_struct(png_ptr);
299 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
302 if (!png_ptr || !info_ptr)
unsigned int height() const
Gets image's height.
compression_level
Compression level in the interlaced image.
options()
Default constructor.
#define CLAW_EXCEPTION(m)
Create an exception and add the name of the current function to the message.
interlace_type
The algorithm to use to interlace the saved image.
void claw__graphic__png__target_manager__flush(png_structp png_ptr)
Flush the output stream.
void write(png_bytep data, png_size_t length)
Write data in the ouput stream.
unsigned int width() const
Gets image's width.
A class for png pictures.
void save(std::ostream &f, const options &opt=options()) const
Save the image in a PNG file.
Target manager that allow us to write in a std::ostream.
writer(const image &img)
Constructor.
A simple class to use as exception with string message.
void flush()
Flush the output stream.
unsigned char component_type
The type of the components of the color.
Some assert macros to strengthen you code.
compression_level compression
Compression level to use in the saved stream.
target_manager(std::ostream &os)
Constructor.
interlace_type interlace
Interlace method to apply to the saved image.
A class to deal with images.
Parameters of the writing algorithm.
#define CLAW_PRECOND(b)
Abort the program if a precondition is not true.
void claw__graphic__png__target_manager__write(png_structp png_ptr, png_bytep data, png_size_t length)
Write data in the ouput stream.