39 #include "vpImageIoBackend.h"
40 #include <visp3/core/vpImageConvert.h>
42 #if defined(VISP_HAVE_PNG)
50 #if defined(VISP_HAVE_PNG)
64 if (filename.empty()) {
68 file = fopen(filename.c_str(),
"wb");
70 if (file ==
nullptr) {
75 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
nullptr,
nullptr,
nullptr);
82 png_infop info_ptr = png_create_info_struct(png_ptr);
85 png_destroy_write_struct(&png_ptr,
nullptr);
92 if (setjmp(png_jmpbuf(png_ptr))) {
94 png_destroy_write_struct(&png_ptr, &info_ptr);
101 png_init_io(png_ptr, file);
106 int color_type = PNG_COLOR_TYPE_GRAY;
109 if (setjmp(png_jmpbuf(png_ptr))) {
111 png_destroy_write_struct(&png_ptr, &info_ptr);
116 png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth, color_type, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE,
117 PNG_FILTER_TYPE_BASE);
119 png_write_info(png_ptr, info_ptr);
121 png_bytep *row_ptrs =
new png_bytep[height];
122 for (
unsigned int i = 0; i < height; ++i)
123 row_ptrs[i] =
new png_byte[width];
125 unsigned char *input = (
unsigned char *)I.
bitmap;
127 for (
unsigned int i = 0; i < height; ++i) {
128 png_byte *row = row_ptrs[i];
129 for (
unsigned int j = 0; j < width; ++j) {
135 png_write_image(png_ptr, row_ptrs);
137 png_write_end(png_ptr,
nullptr);
139 for (
unsigned int j = 0; j < height; ++j)
140 delete[] row_ptrs[j];
144 png_destroy_write_struct(&png_ptr, &info_ptr);
156 void writePNGLibpng(
const vpImage<vpRGBa> &I,
const std::string &filename)
161 if (filename.empty()) {
165 file = fopen(filename.c_str(),
"wb");
167 if (file ==
nullptr) {
172 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
nullptr,
nullptr,
nullptr);
179 png_infop info_ptr = png_create_info_struct(png_ptr);
182 png_destroy_write_struct(&png_ptr,
nullptr);
189 if (setjmp(png_jmpbuf(png_ptr))) {
191 png_destroy_write_struct(&png_ptr, &info_ptr);
198 png_init_io(png_ptr, file);
203 int color_type = PNG_COLOR_TYPE_RGB;
206 if (setjmp(png_jmpbuf(png_ptr))) {
208 png_destroy_write_struct(&png_ptr, &info_ptr);
213 png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth, color_type, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE,
214 PNG_FILTER_TYPE_BASE);
216 png_write_info(png_ptr, info_ptr);
218 png_bytep *row_ptrs =
new png_bytep[height];
219 for (
unsigned int i = 0; i < height; ++i)
220 row_ptrs[i] =
new png_byte[3 * width];
222 unsigned char *input = (
unsigned char *)I.
bitmap;
224 for (
unsigned int i = 0; i < height; ++i) {
225 png_byte *row = row_ptrs[i];
226 for (
unsigned int j = 0; j < width; ++j) {
227 row[3 * j] = *(input);
229 row[3 * j + 1] = *(input);
231 row[3 * j + 2] = *(input);
237 png_write_image(png_ptr, row_ptrs);
239 png_write_end(png_ptr,
nullptr);
241 for (
unsigned int j = 0; j < height; ++j)
242 delete[] row_ptrs[j];
246 png_destroy_write_struct(&png_ptr, &info_ptr);
271 if (filename.empty()) {
275 file = fopen(filename.c_str(),
"rb");
277 if (file ==
nullptr) {
282 if (fread(magic, 1,
sizeof(magic), file) !=
sizeof(magic)) {
288 if (png_sig_cmp(magic, 0,
sizeof(magic))) {
296 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
nullptr,
nullptr,
nullptr);
297 if (png_ptr ==
nullptr) {
298 fprintf(stderr,
"error: can't create a png read structure!\n");
304 png_infop info_ptr = png_create_info_struct(png_ptr);
305 if (info_ptr ==
nullptr) {
306 fprintf(stderr,
"error: can't create a png info structure!\n");
308 png_destroy_read_struct(&png_ptr,
nullptr,
nullptr);
314 if (setjmp(png_jmpbuf(png_ptr))) {
316 png_destroy_read_struct(&png_ptr, &info_ptr,
nullptr);
323 png_init_io(png_ptr, file);
326 png_set_sig_bytes(png_ptr,
sizeof(magic));
329 png_read_info(png_ptr, info_ptr);
331 unsigned int width = png_get_image_width(png_ptr, info_ptr);
332 unsigned int height = png_get_image_height(png_ptr, info_ptr);
334 unsigned int bit_depth, channels, color_type;
336 bit_depth = png_get_bit_depth(png_ptr, info_ptr);
337 channels = png_get_channels(png_ptr, info_ptr);
338 color_type = png_get_color_type(png_ptr, info_ptr);
341 if (color_type == PNG_COLOR_TYPE_PALETTE)
342 png_set_palette_to_rgb(png_ptr);
345 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
346 png_set_expand(png_ptr);
351 if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
352 png_set_strip_alpha(png_ptr);
355 png_set_strip_16(png_ptr);
356 else if (bit_depth < 8)
357 png_set_packing(png_ptr);
360 png_read_update_info(png_ptr, info_ptr);
362 channels = png_get_channels(png_ptr, info_ptr);
367 png_bytep *rowPtrs =
new png_bytep[height];
369 unsigned int stride = png_get_rowbytes(png_ptr, info_ptr);
370 unsigned char *data =
new unsigned char[stride * height];
372 for (
unsigned int i = 0; i < height; ++i)
373 rowPtrs[i] = (png_bytep)data + (i * stride);
375 png_read_image(png_ptr, rowPtrs);
378 unsigned char *output;
382 output = (
unsigned char *)I.
bitmap;
383 for (
unsigned int i = 0; i < width * height; ++i) {
384 *(output++) = data[i];
389 output = (
unsigned char *)I.
bitmap;
390 for (
unsigned int i = 0; i < width * height; ++i) {
391 *(output++) = data[i * 2];
396 output = (
unsigned char *)Ic.bitmap;
397 for (
unsigned int i = 0; i < width * height; ++i) {
398 *(output++) = data[i * 3];
399 *(output++) = data[i * 3 + 1];
400 *(output++) = data[i * 3 + 2];
407 output = (
unsigned char *)Ic.bitmap;
408 for (
unsigned int i = 0; i < width * height; ++i) {
409 *(output++) = data[i * 4];
410 *(output++) = data[i * 4 + 1];
411 *(output++) = data[i * 4 + 2];
412 *(output++) = data[i * 4 + 3];
418 delete[](png_bytep) rowPtrs;
420 png_read_end(png_ptr,
nullptr);
421 png_destroy_read_struct(&png_ptr, &info_ptr,
nullptr);
449 if (filename.empty()) {
453 file = fopen(filename.c_str(),
"rb");
455 if (file ==
nullptr) {
460 if (fread(magic, 1,
sizeof(magic), file) !=
sizeof(magic)) {
466 if (png_sig_cmp(magic, 0,
sizeof(magic))) {
473 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
nullptr,
nullptr,
nullptr);
481 png_infop info_ptr = png_create_info_struct(png_ptr);
484 png_destroy_read_struct(&png_ptr,
nullptr,
nullptr);
491 if (setjmp(png_jmpbuf(png_ptr))) {
493 png_destroy_read_struct(&png_ptr, &info_ptr,
nullptr);
500 png_init_io(png_ptr, file);
503 png_set_sig_bytes(png_ptr,
sizeof(magic));
506 png_read_info(png_ptr, info_ptr);
508 unsigned int width = png_get_image_width(png_ptr, info_ptr);
509 unsigned int height = png_get_image_height(png_ptr, info_ptr);
511 unsigned int bit_depth, channels, color_type;
513 bit_depth = png_get_bit_depth(png_ptr, info_ptr);
514 channels = png_get_channels(png_ptr, info_ptr);
515 color_type = png_get_color_type(png_ptr, info_ptr);
518 if (color_type == PNG_COLOR_TYPE_PALETTE)
519 png_set_palette_to_rgb(png_ptr);
522 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
523 png_set_expand(png_ptr);
528 if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
529 png_set_strip_alpha(png_ptr);
532 png_set_strip_16(png_ptr);
533 else if (bit_depth < 8)
534 png_set_packing(png_ptr);
537 png_read_update_info(png_ptr, info_ptr);
539 channels = png_get_channels(png_ptr, info_ptr);
544 png_bytep *rowPtrs =
new png_bytep[height];
546 unsigned int stride = png_get_rowbytes(png_ptr, info_ptr);
547 unsigned char *data =
new unsigned char[stride * height];
549 for (
unsigned int i = 0; i < height; ++i)
550 rowPtrs[i] = (png_bytep)data + (i * stride);
552 png_read_image(png_ptr, rowPtrs);
555 unsigned char *output;
559 output = (
unsigned char *)Ig.bitmap;
560 for (
unsigned int i = 0; i < width * height; ++i) {
561 *(output++) = data[i];
567 output = (
unsigned char *)Ig.bitmap;
568 for (
unsigned int i = 0; i < width * height; ++i) {
569 *(output++) = data[i * 2];
575 output = (
unsigned char *)I.
bitmap;
576 for (
unsigned int i = 0; i < width * height; ++i) {
577 *(output++) = data[i * 3];
578 *(output++) = data[i * 3 + 1];
579 *(output++) = data[i * 3 + 2];
585 output = (
unsigned char *)I.
bitmap;
586 for (
unsigned int i = 0; i < width * height; ++i) {
587 *(output++) = data[i * 4];
588 *(output++) = data[i * 4 + 1];
589 *(output++) = data[i * 4 + 2];
590 *(output++) = data[i * 4 + 3];
595 delete[](png_bytep) rowPtrs;
597 png_read_end(png_ptr,
nullptr);
598 png_destroy_read_struct(&png_ptr, &info_ptr,
nullptr);
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
Error that can be emitted by the vpImage class and its derivatives.
unsigned int getWidth() const
void resize(unsigned int h, unsigned int w)
resize the image : Image initialization
Type * bitmap
points toward the bitmap
unsigned int getHeight() const