48 self->
write(data, length);
81 m_output.write((
char*)data, length *
sizeof(png_byte));
111const unsigned int claw::graphic::png::writer::s_rgba_pixel_size = 4;
148 create_write_structures(png_ptr, info_ptr);
150 if(setjmp(png_jmpbuf(png_ptr)))
154 png_destroy_write_struct(&png_ptr, &info_ptr);
158 png_set_write_fn(png_ptr, (
void*)&outfile,
162 set_options(png_ptr, info_ptr, opt);
163 save_image(png_ptr, info_ptr);
165 png_destroy_write_struct(&png_ptr, &info_ptr);
174void claw::graphic::png::writer::set_options(png_structp png_ptr,
176 const options& opt)
const
181 png_set_compression_level(png_ptr, opt.compression);
183 png_set_IHDR(png_ptr, info_ptr, m_image.width(), m_image.height(),
185 PNG_COLOR_TYPE_RGB_ALPHA, opt.interlace,
186 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
194void claw::graphic::png::writer::save_image(png_structp png_ptr,
195 png_infop info_ptr)
const
200 const unsigned int row_length = s_rgba_pixel_size * m_image.width();
202 = (png_bytepp)png_malloc(png_ptr,
sizeof(png_bytep) * m_image.height());
207 for(i = 0; i != m_image.height(); ++i)
209 data[i] = (png_bytep)png_malloc(png_ptr, row_length);
212 throw std::bad_alloc();
214 copy_pixel_line(data[i], i);
217 png_set_rows(png_ptr, info_ptr, data);
218 png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
222 for(
unsigned int j = 0; j != i; ++j)
223 png_free(png_ptr, data[j]);
225 png_free(png_ptr, data);
229 for(i = 0; i != m_image.height(); ++i)
230 png_free(png_ptr, data[i]);
232 png_free(png_ptr, data);
241void claw::graphic::png::writer::copy_pixel_line(png_bytep data,
242 unsigned int y)
const
248 for(
unsigned int x = 0; x != m_image.width(); ++x, data += s_rgba_pixel_size)
250 data[0] = m_image[y][x].components.red;
251 data[1] = m_image[y][x].components.green;
252 data[2] = m_image[y][x].components.blue;
253 data[3] = m_image[y][x].components.alpha;
262void claw::graphic::png::writer::create_write_structures(
263 png_structp& png_ptr, png_infop& info_ptr)
const
265 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
269 info_ptr = png_create_info_struct(png_ptr);
272 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
275 if(!png_ptr || !info_ptr)
Some assert macros to strengthen you code.
#define CLAW_PRECOND(b)
Abort the program if a precondition is not true.
image()
Constructor. Creates an image without datas.
void save(std::ostream &f, const options &opt=options()) const
Save the image in a PNG file.
writer(const image &img)
Constructor.
A simple class to use as exception with string message.
#define CLAW_EXCEPTION(m)
Create an exception and add the name of the current function to the message.
A class for png pictures.
void claw__graphic__png__target_manager__write(png_structp png_ptr, png_bytep data, png_size_t length)
Write data in the ouput stream.
void claw__graphic__png__target_manager__flush(png_structp png_ptr)
Flush the output stream.
Parameters of the writing algorithm.
compression_level compression
Compression level to use in the saved stream.
interlace_type interlace
Interlace method to apply to the saved image.
interlace_type
The algorithm to use to interlace the saved image.
@ none
Saved image won't be interaced.
compression_level
Compression level in the interlaced image.
options()
Default constructor.
Target manager that allow us to write in a std::ostream.
void flush()
Flush the output stream.
void write(png_bytep data, png_size_t length)
Write data in the ouput stream.
target_manager(std::ostream &os)
Constructor.
unsigned char component_type
The type of the components of the color.