41 #include "vpImageIoBackend.h" 42 #include <visp3/core/vpImageConvert.h> 44 #if defined(VISP_HAVE_PNG) 53 #if defined(VISP_HAVE_PNG) 67 if (filename.empty()) {
71 file = fopen(filename.c_str(),
"wb");
78 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
85 png_infop info_ptr = png_create_info_struct(png_ptr);
88 png_destroy_write_struct(&png_ptr, NULL);
95 if (setjmp(png_jmpbuf(png_ptr))) {
97 png_destroy_write_struct(&png_ptr, &info_ptr);
104 png_init_io(png_ptr, file);
109 int color_type = PNG_COLOR_TYPE_GRAY;
112 if (setjmp(png_jmpbuf(png_ptr))) {
114 png_destroy_write_struct(&png_ptr, &info_ptr);
119 png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth, color_type, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE,
120 PNG_FILTER_TYPE_BASE);
122 png_write_info(png_ptr, info_ptr);
124 png_bytep *row_ptrs =
new png_bytep[height];
125 for (
unsigned int i = 0; i < height; i++)
126 row_ptrs[i] =
new png_byte[width];
128 unsigned char *input = (
unsigned char *)I.
bitmap;
130 for (
unsigned int i = 0; i < height; i++) {
131 png_byte *row = row_ptrs[i];
132 for (
unsigned int j = 0; j < width; j++) {
138 png_write_image(png_ptr, row_ptrs);
140 png_write_end(png_ptr, NULL);
142 for (
unsigned int j = 0; j < height; j++)
143 delete[] row_ptrs[j];
147 png_destroy_write_struct(&png_ptr, &info_ptr);
159 void writePNGLibpng(
const vpImage<vpRGBa> &I,
const std::string &filename)
164 if (filename.empty()) {
168 file = fopen(filename.c_str(),
"wb");
175 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
182 png_infop info_ptr = png_create_info_struct(png_ptr);
185 png_destroy_write_struct(&png_ptr, NULL);
192 if (setjmp(png_jmpbuf(png_ptr))) {
194 png_destroy_write_struct(&png_ptr, &info_ptr);
201 png_init_io(png_ptr, file);
206 int color_type = PNG_COLOR_TYPE_RGB;
209 if (setjmp(png_jmpbuf(png_ptr))) {
211 png_destroy_write_struct(&png_ptr, &info_ptr);
216 png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth, color_type, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE,
217 PNG_FILTER_TYPE_BASE);
219 png_write_info(png_ptr, info_ptr);
221 png_bytep *row_ptrs =
new png_bytep[height];
222 for (
unsigned int i = 0; i < height; i++)
223 row_ptrs[i] =
new png_byte[3 * width];
225 unsigned char *input = (
unsigned char *)I.
bitmap;
227 for (
unsigned int i = 0; i < height; i++) {
228 png_byte *row = row_ptrs[i];
229 for (
unsigned int j = 0; j < width; j++) {
230 row[3 * j] = *(input);
232 row[3 * j + 1] = *(input);
234 row[3 * j + 2] = *(input);
240 png_write_image(png_ptr, row_ptrs);
242 png_write_end(png_ptr, NULL);
244 for (
unsigned int j = 0; j < height; j++)
245 delete[] row_ptrs[j];
249 png_destroy_write_struct(&png_ptr, &info_ptr);
274 if (filename.empty()) {
278 file = fopen(filename.c_str(),
"rb");
285 if (fread(magic, 1,
sizeof(magic), file) !=
sizeof(magic)) {
291 if (png_sig_cmp(magic, 0,
sizeof(magic))) {
299 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
300 if (png_ptr == NULL) {
301 fprintf(stderr,
"error: can't create a png read structure!\n");
307 png_infop info_ptr = png_create_info_struct(png_ptr);
308 if (info_ptr == NULL) {
309 fprintf(stderr,
"error: can't create a png info structure!\n");
311 png_destroy_read_struct(&png_ptr, NULL, NULL);
317 if (setjmp(png_jmpbuf(png_ptr))) {
319 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
326 png_init_io(png_ptr, file);
329 png_set_sig_bytes(png_ptr,
sizeof(magic));
332 png_read_info(png_ptr, info_ptr);
334 unsigned int width = png_get_image_width(png_ptr, info_ptr);
335 unsigned int height = png_get_image_height(png_ptr, info_ptr);
337 unsigned int bit_depth, channels, color_type;
339 bit_depth = png_get_bit_depth(png_ptr, info_ptr);
340 channels = png_get_channels(png_ptr, info_ptr);
341 color_type = png_get_color_type(png_ptr, info_ptr);
344 if (color_type == PNG_COLOR_TYPE_PALETTE)
345 png_set_palette_to_rgb(png_ptr);
348 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
349 png_set_expand(png_ptr);
354 if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
355 png_set_strip_alpha(png_ptr);
358 png_set_strip_16(png_ptr);
359 else if (bit_depth < 8)
360 png_set_packing(png_ptr);
363 png_read_update_info(png_ptr, info_ptr);
365 channels = png_get_channels(png_ptr, info_ptr);
370 png_bytep *rowPtrs =
new png_bytep[height];
372 unsigned int stride = png_get_rowbytes(png_ptr, info_ptr);
373 unsigned char *data =
new unsigned char[stride * height];
375 for (
unsigned int i = 0; i < height; i++)
376 rowPtrs[i] = (png_bytep)data + (i * stride);
378 png_read_image(png_ptr, rowPtrs);
381 unsigned char *output;
385 output = (
unsigned char *)I.
bitmap;
386 for (
unsigned int i = 0; i < width * height; i++) {
387 *(output++) = data[i];
392 output = (
unsigned char *)I.
bitmap;
393 for (
unsigned int i = 0; i < width * height; i++) {
394 *(output++) = data[i * 2];
399 output = (
unsigned char *)Ic.bitmap;
400 for (
unsigned int i = 0; i < width * height; i++) {
401 *(output++) = data[i * 3];
402 *(output++) = data[i * 3 + 1];
403 *(output++) = data[i * 3 + 2];
410 output = (
unsigned char *)Ic.bitmap;
411 for (
unsigned int i = 0; i < width * height; i++) {
412 *(output++) = data[i * 4];
413 *(output++) = data[i * 4 + 1];
414 *(output++) = data[i * 4 + 2];
415 *(output++) = data[i * 4 + 3];
421 delete[](png_bytep) rowPtrs;
423 png_read_end(png_ptr, NULL);
424 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
452 if (filename.empty()) {
456 file = fopen(filename.c_str(),
"rb");
463 if (fread(magic, 1,
sizeof(magic), file) !=
sizeof(magic)) {
469 if (png_sig_cmp(magic, 0,
sizeof(magic))) {
476 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
484 png_infop info_ptr = png_create_info_struct(png_ptr);
487 png_destroy_read_struct(&png_ptr, NULL, NULL);
494 if (setjmp(png_jmpbuf(png_ptr))) {
496 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
503 png_init_io(png_ptr, file);
506 png_set_sig_bytes(png_ptr,
sizeof(magic));
509 png_read_info(png_ptr, info_ptr);
511 unsigned int width = png_get_image_width(png_ptr, info_ptr);
512 unsigned int height = png_get_image_height(png_ptr, info_ptr);
514 unsigned int bit_depth, channels, color_type;
516 bit_depth = png_get_bit_depth(png_ptr, info_ptr);
517 channels = png_get_channels(png_ptr, info_ptr);
518 color_type = png_get_color_type(png_ptr, info_ptr);
521 if (color_type == PNG_COLOR_TYPE_PALETTE)
522 png_set_palette_to_rgb(png_ptr);
525 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
526 png_set_expand(png_ptr);
531 if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
532 png_set_strip_alpha(png_ptr);
535 png_set_strip_16(png_ptr);
536 else if (bit_depth < 8)
537 png_set_packing(png_ptr);
540 png_read_update_info(png_ptr, info_ptr);
542 channels = png_get_channels(png_ptr, info_ptr);
547 png_bytep *rowPtrs =
new png_bytep[height];
549 unsigned int stride = png_get_rowbytes(png_ptr, info_ptr);
550 unsigned char *data =
new unsigned char[stride * height];
552 for (
unsigned int i = 0; i < height; i++)
553 rowPtrs[i] = (png_bytep)data + (i * stride);
555 png_read_image(png_ptr, rowPtrs);
558 unsigned char *output;
562 output = (
unsigned char *)Ig.bitmap;
563 for (
unsigned int i = 0; i < width * height; i++) {
564 *(output++) = data[i];
570 output = (
unsigned char *)Ig.bitmap;
571 for (
unsigned int i = 0; i < width * height; i++) {
572 *(output++) = data[i * 2];
578 output = (
unsigned char *)I.
bitmap;
579 for (
unsigned int i = 0; i < width * height; i++) {
580 *(output++) = data[i * 3];
581 *(output++) = data[i * 3 + 1];
582 *(output++) = data[i * 3 + 2];
588 output = (
unsigned char *)I.
bitmap;
589 for (
unsigned int i = 0; i < width * height; i++) {
590 *(output++) = data[i * 4];
591 *(output++) = data[i * 4 + 1];
592 *(output++) = data[i * 4 + 2];
593 *(output++) = data[i * 4 + 3];
598 delete[](png_bytep) rowPtrs;
600 png_read_end(png_ptr, NULL);
601 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
void resize(unsigned int h, unsigned int w)
resize the image : Image initialization
Type * bitmap
points toward the bitmap
Error that can be emited by the vpImage class and its derivates.
unsigned int getHeight() const
unsigned int getWidth() const