24 #include <core/exception.h>
25 #include <fvutils/writers/png.h>
26 #include <fvutils/color/yuvrgb.h>
33 using namespace fawkes;
35 namespace firevision {
45 PNGWriter::PNGWriter()
77 else if (cspace == YUV422_PLANAR) {
82 throw Exception(
"Color space not supported, can only write YUV422_PLANAR images");
93 throw Exception(
"PNGWriter::write(): Illegal data, width==0 || height == 0 || filename=\"\".");
98 throw Exception(
"Could not open file for writing");
101 png_structp png_ptr = png_create_write_struct
102 (PNG_LIBPNG_VER_STRING,(png_voidp)NULL,
103 (png_error_ptr)NULL, (png_error_ptr)NULL);
105 throw Exception(
"Could not create PNG write struct");
108 png_infop info_ptr = png_create_info_struct(png_ptr);
110 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
111 throw Exception(
"Could not create PNG info struct");
114 if (setjmp(png_jmpbuf(png_ptr))) {
115 png_destroy_write_struct(&png_ptr, &info_ptr);
117 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
118 throw Exception(
"Could not create setjmp");
122 png_init_io(png_ptr, fp);
128 8 , PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
129 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
131 png_write_info(png_ptr, info_ptr);
135 png_byte row[
width*3];
137 unsigned char *yp, *up, *vp;
138 unsigned char y1, y2, u = 0, v = 0;
145 for (
unsigned int i = 0; i <
height; ++i) {
149 for (
unsigned int j = 0; j < (
width / 2); ++j) {
154 pixel_yuv_to_rgb(y1, u, v, &row_p[0], &row_p[1], &row_p[2]);
156 pixel_yuv_to_rgb(y2, u, v, &row_p[0], &row_p[1], &row_p[2]);
160 if ( (
width % 2) == 1 ) {
164 pixel_yuv_to_rgb(y1, u, v, &row_p[0], &row_p[1], &row_p[2]);
171 png_write_row(png_ptr, row);
174 png_write_end(png_ptr, info_ptr);
175 png_destroy_write_struct(&png_ptr, &info_ptr);
Interface to write images.
virtual void write()
Write to file.
Base class for exceptions in Fawkes.
virtual void set_filename(const char *filename)
Set filename.
unsigned int width
The width of the image.
unsigned char * buffer
The image-buffer.
virtual void set_buffer(colorspace_t cspace, unsigned char *buffer)
Set image buffer.
unsigned int height
The height of the image.
char * filename
The complete filename.