39 #include "vpImageIoBackend.h"
40 #include <visp3/core/vpImageConvert.h>
42 #if defined(VISP_HAVE_PNG)
50 #if defined(VISP_HAVE_PNG)
65 if (filename.empty()) {
69 file = fopen(filename.c_str(),
"wb");
71 if (file ==
nullptr) {
76 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);
91 if (setjmp(png_jmpbuf(png_ptr))) {
93 png_destroy_write_struct(&png_ptr, &info_ptr);
99 png_init_io(png_ptr, file);
104 int color_type = PNG_COLOR_TYPE_GRAY;
107 if (setjmp(png_jmpbuf(png_ptr))) {
109 png_destroy_write_struct(&png_ptr, &info_ptr);
113 png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth, color_type, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE,
114 PNG_FILTER_TYPE_BASE);
116 png_write_info(png_ptr, info_ptr);
118 png_bytep *row_ptrs =
new png_bytep[height];
119 for (
unsigned int i = 0; i < height; ++i)
120 row_ptrs[i] =
new png_byte[width];
122 unsigned char *input = (
unsigned char *)I.
bitmap;
124 for (
unsigned int i = 0; i < height; ++i) {
125 png_byte *row = row_ptrs[i];
126 for (
unsigned int j = 0; j < width; ++j) {
132 png_write_image(png_ptr, row_ptrs);
134 png_write_end(png_ptr,
nullptr);
136 for (
unsigned int j = 0; j < height; ++j)
137 delete[] row_ptrs[j];
141 png_destroy_write_struct(&png_ptr, &info_ptr);
153 void writePNGLibpng(
const vpImage<vpRGBa> &I,
const std::string &filename)
158 if (filename.empty()) {
162 file = fopen(filename.c_str(),
"wb");
164 if (file ==
nullptr) {
169 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
nullptr,
nullptr,
nullptr);
175 png_infop info_ptr = png_create_info_struct(png_ptr);
178 png_destroy_write_struct(&png_ptr,
nullptr);
184 if (setjmp(png_jmpbuf(png_ptr))) {
186 png_destroy_write_struct(&png_ptr, &info_ptr);
192 png_init_io(png_ptr, file);
197 int color_type = PNG_COLOR_TYPE_RGB;
200 if (setjmp(png_jmpbuf(png_ptr))) {
202 png_destroy_write_struct(&png_ptr, &info_ptr);
206 png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth, color_type, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE,
207 PNG_FILTER_TYPE_BASE);
209 png_write_info(png_ptr, info_ptr);
211 png_bytep *row_ptrs =
new png_bytep[height];
212 for (
unsigned int i = 0; i < height; ++i)
213 row_ptrs[i] =
new png_byte[3 * width];
215 unsigned char *input = (
unsigned char *)I.
bitmap;
217 for (
unsigned int i = 0; i < height; ++i) {
218 png_byte *row = row_ptrs[i];
219 for (
unsigned int j = 0; j < width; ++j) {
220 row[3 * j] = *(input);
222 row[3 * j + 1] = *(input);
224 row[3 * j + 2] = *(input);
230 png_write_image(png_ptr, row_ptrs);
232 png_write_end(png_ptr,
nullptr);
234 for (
unsigned int j = 0; j < height; ++j)
235 delete[] row_ptrs[j];
239 png_destroy_write_struct(&png_ptr, &info_ptr);
264 if (filename.empty()) {
268 file = fopen(filename.c_str(),
"rb");
270 if (file ==
nullptr) {
275 if (fread(magic, 1,
sizeof(magic), file) !=
sizeof(magic)) {
281 if (png_sig_cmp(magic, 0,
sizeof(magic))) {
289 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
nullptr,
nullptr,
nullptr);
290 if (png_ptr ==
nullptr) {
291 fprintf(stderr,
"error: can't create a png read structure!\n");
297 png_infop info_ptr = png_create_info_struct(png_ptr);
298 if (info_ptr ==
nullptr) {
299 fprintf(stderr,
"error: can't create a png info structure!\n");
301 png_destroy_read_struct(&png_ptr,
nullptr,
nullptr);
307 if (setjmp(png_jmpbuf(png_ptr))) {
309 png_destroy_read_struct(&png_ptr, &info_ptr,
nullptr);
315 png_init_io(png_ptr, file);
318 png_set_sig_bytes(png_ptr,
sizeof(magic));
321 png_read_info(png_ptr, info_ptr);
323 unsigned int width = png_get_image_width(png_ptr, info_ptr);
324 unsigned int height = png_get_image_height(png_ptr, info_ptr);
326 unsigned int bit_depth, channels, color_type;
328 bit_depth = png_get_bit_depth(png_ptr, info_ptr);
329 channels = png_get_channels(png_ptr, info_ptr);
330 color_type = png_get_color_type(png_ptr, info_ptr);
333 if (color_type == PNG_COLOR_TYPE_PALETTE)
334 png_set_palette_to_rgb(png_ptr);
337 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
338 png_set_expand(png_ptr);
343 if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
344 png_set_strip_alpha(png_ptr);
347 png_set_strip_16(png_ptr);
348 else if (bit_depth < 8)
349 png_set_packing(png_ptr);
352 png_read_update_info(png_ptr, info_ptr);
354 channels = png_get_channels(png_ptr, info_ptr);
359 png_bytep *rowPtrs =
new png_bytep[height];
361 unsigned int stride = png_get_rowbytes(png_ptr, info_ptr);
362 unsigned char *data =
new unsigned char[stride * height];
364 for (
unsigned int i = 0; i < height; ++i)
365 rowPtrs[i] = (png_bytep)data + (i * stride);
367 png_read_image(png_ptr, rowPtrs);
370 unsigned char *output;
374 output = (
unsigned char *)I.
bitmap;
375 for (
unsigned int i = 0; i < width * height; ++i) {
376 *(output++) = data[i];
381 output = (
unsigned char *)I.
bitmap;
382 for (
unsigned int i = 0; i < width * height; ++i) {
383 *(output++) = data[i * 2];
388 output = (
unsigned char *)Ic.bitmap;
389 for (
unsigned int i = 0; i < width * height; ++i) {
390 *(output++) = data[i * 3];
391 *(output++) = data[i * 3 + 1];
392 *(output++) = data[i * 3 + 2];
399 output = (
unsigned char *)Ic.bitmap;
400 for (
unsigned int i = 0; i < width * height; ++i) {
401 *(output++) = data[i * 4];
402 *(output++) = data[i * 4 + 1];
403 *(output++) = data[i * 4 + 2];
404 *(output++) = data[i * 4 + 3];
410 delete[](png_bytep) rowPtrs;
412 png_read_end(png_ptr,
nullptr);
413 png_destroy_read_struct(&png_ptr, &info_ptr,
nullptr);
441 if (filename.empty()) {
445 file = fopen(filename.c_str(),
"rb");
447 if (file ==
nullptr) {
452 if (fread(magic, 1,
sizeof(magic), file) !=
sizeof(magic)) {
458 if (png_sig_cmp(magic, 0,
sizeof(magic))) {
465 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
nullptr,
nullptr,
nullptr);
472 png_infop info_ptr = png_create_info_struct(png_ptr);
475 png_destroy_read_struct(&png_ptr,
nullptr,
nullptr);
481 if (setjmp(png_jmpbuf(png_ptr))) {
483 png_destroy_read_struct(&png_ptr, &info_ptr,
nullptr);
489 png_init_io(png_ptr, file);
492 png_set_sig_bytes(png_ptr,
sizeof(magic));
495 png_read_info(png_ptr, info_ptr);
497 unsigned int width = png_get_image_width(png_ptr, info_ptr);
498 unsigned int height = png_get_image_height(png_ptr, info_ptr);
500 unsigned int bit_depth, channels, color_type;
502 bit_depth = png_get_bit_depth(png_ptr, info_ptr);
503 channels = png_get_channels(png_ptr, info_ptr);
504 color_type = png_get_color_type(png_ptr, info_ptr);
507 if (color_type == PNG_COLOR_TYPE_PALETTE)
508 png_set_palette_to_rgb(png_ptr);
511 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
512 png_set_expand(png_ptr);
517 if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
518 png_set_strip_alpha(png_ptr);
521 png_set_strip_16(png_ptr);
522 else if (bit_depth < 8)
523 png_set_packing(png_ptr);
526 png_read_update_info(png_ptr, info_ptr);
528 channels = png_get_channels(png_ptr, info_ptr);
533 png_bytep *rowPtrs =
new png_bytep[height];
535 unsigned int stride = png_get_rowbytes(png_ptr, info_ptr);
536 unsigned char *data =
new unsigned char[stride * height];
538 for (
unsigned int i = 0; i < height; ++i)
539 rowPtrs[i] = (png_bytep)data + (i * stride);
541 png_read_image(png_ptr, rowPtrs);
544 unsigned char *output;
548 output = (
unsigned char *)Ig.bitmap;
549 for (
unsigned int i = 0; i < width * height; ++i) {
550 *(output++) = data[i];
556 output = (
unsigned char *)Ig.bitmap;
557 for (
unsigned int i = 0; i < width * height; ++i) {
558 *(output++) = data[i * 2];
564 output = (
unsigned char *)I.
bitmap;
565 for (
unsigned int i = 0; i < width * height; ++i) {
566 *(output++) = data[i * 3];
567 *(output++) = data[i * 3 + 1];
568 *(output++) = data[i * 3 + 2];
574 output = (
unsigned char *)I.
bitmap;
575 for (
unsigned int i = 0; i < width * height; ++i) {
576 *(output++) = data[i * 4];
577 *(output++) = data[i * 4 + 1];
578 *(output++) = data[i * 4 + 2];
579 *(output++) = data[i * 4 + 3];
584 delete[](png_bytep) rowPtrs;
586 png_read_end(png_ptr,
nullptr);
587 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