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)
120 : compression(compression_level_), interlace(interlace_)
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)