22 #if defined (YARP_HAS_PNG)
40 #if defined (YARP_HAS_PNG)
41 FILE* fp = fopen(filename,
"rb");
43 png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
46 yCError(IMAGEFILE) <<
"PNG internal error";
50 png_infop info = png_create_info_struct(png);
53 yCError(IMAGEFILE) <<
"PNG internal error";
57 if (setjmp(png_jmpbuf(png)))
59 yCError(IMAGEFILE) <<
"PNG internal error";
65 png_read_info(png, info);
67 int width = png_get_image_width(png, info);
68 int height = png_get_image_height(png, info);
69 png_byte color_type = png_get_color_type(png, info);
70 png_byte bit_depth = png_get_bit_depth(png, info);
76 png_set_strip_16(png);
78 if (color_type == PNG_COLOR_TYPE_PALETTE)
79 png_set_palette_to_rgb(png);
82 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
83 png_set_expand_gray_1_2_4_to_8(png);
85 if (png_get_valid(png, info, PNG_INFO_tRNS))
86 png_set_tRNS_to_alpha(png);
89 if (color_type == PNG_COLOR_TYPE_RGB ||
90 color_type == PNG_COLOR_TYPE_GRAY ||
91 color_type == PNG_COLOR_TYPE_PALETTE)
92 png_set_filler(png, 0xFF, PNG_FILLER_AFTER);
94 if (color_type == PNG_COLOR_TYPE_GRAY ||
95 color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
96 png_set_gray_to_rgb(png);
98 png_read_update_info(png, info);
100 png_bytep* row_pointers = (png_bytep*)malloc(
sizeof(png_bytep) * height);
101 for (
int y = 0; y < height; y++)
103 row_pointers[y] = (png_byte*)malloc(png_get_rowbytes(png, info));
106 png_read_image(png, row_pointers);
110 for (
int y = 0; y < height; y++)
112 png_bytep row = row_pointers[y];
113 for (
int x = 0; x < width; x++)
115 png_bytep px = &(row[x * 4]);
123 png_destroy_read_struct(&png, &info, NULL);
124 for (
int y = 0; y < height; y++)
126 free(row_pointers[y]);
131 yCError(IMAGEFILE) <<
"PNG library not available/not found";
138 #if defined (YARP_HAS_PNG)
139 yCError(IMAGEFILE) <<
"Not yet implemented";
142 yCError(IMAGEFILE) <<
"PNG library not available/not found";
149 #if defined (YARP_HAS_PNG)
150 yCError(IMAGEFILE) <<
"Not yet implemented";
153 yCError(IMAGEFILE) <<
"PNG library not available/not found";
160 #if defined (YARP_HAS_PNG)
161 yCError(IMAGEFILE) <<
"Not yet implemented";
164 yCError(IMAGEFILE) <<
"PNG library not available/not found";
181 if (fscanf(fp,
"P%c\n", &ch) != 1 || (ch!=
'6'&&ch!=
'5'))
183 yCWarning(IMAGEFILE,
"file is not in pgm/ppm raw format; cannot read");
187 if (ch==
'6') *color = 1;
203 int n=fscanf(fp,
"%d%d%d", width, height, &maxval);
211 yCWarning(IMAGEFILE,
"image is not true-color (24 bit); read failed");
221 int width, height, color, num;
223 fp = fopen(filename,
"rb");
227 yCError(IMAGEFILE,
"Error opening %s, check if file exists.\n", filename);
234 yCError(IMAGEFILE,
"Error reading header, is file a valid ppm/pgm?\n");
244 const int h = tmp.
height();
249 for (
int i = 0; i < h; i++)
251 num += (int)fread((
void *) dst, 1, (size_t) w, fp);
262 const int h = img.
height();
267 for (
int i = 0; i < h; i++)
269 num += (int)fread((
void *) dst, 1, (size_t) w, fp);
280 FILE *fp = fopen(filename.c_str(),
"rb");
286 size_t size_ =
sizeof(float);
290 if (fread(dims,
sizeof(dims), 1, fp) > 0)
292 count_ = (size_t)(dims[0] * dims[1]);
293 dest.
resize(dims[0], dims[1]);
294 br = fread(&dest(0, 0), size_, count_, fp);
303 int width, height, color, num;
305 fp = fopen(filename,
"rb");
309 yCError(IMAGEFILE,
"Error opening %s, check if file exists.\n", filename);
316 yCError(IMAGEFILE,
"Error reading header, is file a valid ppm/pgm?\n");
323 yCError(IMAGEFILE,
"File is grayscale, conversion not yet supported\n");
328 tmpImg.
resize(width, height);
331 const int h = tmpImg.
height();
336 for (
int i = 0; i < h; i++)
338 num += (int)fread((
void *) dst, 1, (size_t) w, fp);
344 return img.
copy(tmpImg);
350 int width, height, color, num;
352 fp = fopen(filename,
"rb");
356 yCError(IMAGEFILE,
"Error opening %s, check if file exists.\n", filename);
363 yCError(IMAGEFILE,
"Error reading header, is file a valid ppm/pgm?\n");
370 yCError(IMAGEFILE,
"File is color, conversion not yet supported\n");
377 const int h = img.
height();
382 for (
int i = 0; i < h; i++)
384 num += (int)fread((
void *) dst, 1, (size_t) w, fp);
397 #if defined (YARP_HAS_PNG)
398 static bool SavePNG(
char *src,
const char *filename,
size_t h,
size_t w,
size_t rowSize, png_byte color_type, png_byte bit_depth)
403 yCError(IMAGEFILE,
"[write_png_file] Cannot write to file a nullptr image");
407 if (filename ==
nullptr)
409 yCError(IMAGEFILE,
"[write_png_file] Filename is nullptr");
413 FILE *fp = fopen(filename,
"wb");
416 yCError(IMAGEFILE,
"[write_png_file] File %s could not be opened for writing", filename);
421 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
424 yCError(IMAGEFILE,
"[write_png_file] png_create_write_struct failed");
429 png_infop info_ptr = png_create_info_struct(png_ptr);
432 yCError(IMAGEFILE,
"[write_png_file] png_create_info_struct failed");
437 png_bytep * row_pointers = (png_bytep*)malloc(
sizeof(png_bytep) * h);
438 for (
size_t y = 0; y < h; y++)
440 row_pointers[y] = (png_bytep)(src)+y*w;
443 if (setjmp(png_jmpbuf(png_ptr)))
445 yCError(IMAGEFILE,
"[write_png_file] Error during init_io");
450 png_init_io(png_ptr, fp);
454 if (setjmp(png_jmpbuf(png_ptr)))
456 yCError(IMAGEFILE,
"[write_png_file] Error during writing header");
460 png_set_IHDR(png_ptr, info_ptr, w, h,
461 bit_depth, color_type, PNG_INTERLACE_NONE,
462 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
464 png_write_info(png_ptr, info_ptr);
468 if (setjmp(png_jmpbuf(png_ptr)))
470 yCError(IMAGEFILE,
"[write_png_file] Error during writing bytes");
475 png_write_image(png_ptr, row_pointers);
479 if (setjmp(png_jmpbuf(png_ptr)))
481 yCError(IMAGEFILE,
"[write_png_file] Error during end of write");
486 png_write_end(png_ptr, NULL);
496 static bool SaveJPG(
char *src,
const char *filename,
int h,
int w,
int rowSize)
500 struct jpeg_compress_struct cinfo;
501 struct jpeg_error_mgr jerr;
503 JSAMPROW row_pointer[1];
506 cinfo.err = jpeg_std_error(&jerr);
507 jpeg_create_compress(&cinfo);
509 if ((outfile = fopen(filename,
"wb")) ==
nullptr)
511 yCError(IMAGEFILE,
"can't write file: %s\n", filename);
514 jpeg_stdio_dest(&cinfo, outfile);
516 cinfo.image_width = w;
517 cinfo.image_height = h;
518 cinfo.input_components = 3;
519 cinfo.in_color_space = JCS_RGB;
520 jpeg_set_defaults(&cinfo);
521 jpeg_set_quality(&cinfo, quality, TRUE);
523 jpeg_start_compress(&cinfo, TRUE);
527 while (cinfo.next_scanline < cinfo.image_height)
529 row_pointer[0] = (JSAMPROW)&src[cinfo.next_scanline * row_stride];
530 (
void)jpeg_write_scanlines(&cinfo, row_pointer, 1);
533 jpeg_finish_compress(&cinfo);
535 jpeg_destroy_compress(&cinfo);
538 yCError(IMAGEFILE) <<
"libjpeg not installed";
543 static bool SavePGM(
char *src,
const char *filename,
int h,
int w,
int rowSize)
545 FILE *fp = fopen(filename,
"wb");
548 yCError(IMAGEFILE,
"cannot open file %s for writing\n", filename);
553 const int inc = rowSize;
555 fprintf(fp,
"P5\n%d %d\n%d\n", w, h, 255);
556 for (
int i = 0; i < h; i++)
558 fwrite((
void *)src, 1, (
size_t)w, fp);
569 static bool SavePPM(
char *src,
const char *filename,
int h,
int w,
int rowSize)
571 FILE *fp = fopen(filename,
"wb");
574 yCError(IMAGEFILE,
"cannot open file %s for writing\n", filename);
579 const int inc = rowSize;
581 fprintf(fp,
"P6\n%d %d\n%d\n", w, h, 255);
582 for (
int i = 0; i < h; i++)
584 fwrite((
void *)src, 1, (
size_t)(w * 3), fp);
602 #if defined (YARP_HAS_PNG)
605 yCError(IMAGEFILE) <<
"YARP was not built with png support";
612 #if defined (YARP_HAS_PNG)
615 yCError(IMAGEFILE) <<
"YARP was not built with png support";
632 FILE *fp = fopen(filename,
"wb");
641 size_t size_ =
sizeof(float);
642 auto count_ = (size_t)(dims[0] * dims[1]);
644 if (fwrite(dims,
sizeof(dims), 1, fp) > 0) {
645 bw = fwrite(&img(0, 0), size_, count_, fp);
658 const char* file_ext = strrchr(src.c_str(),
'.');
659 if (strcmp(file_ext,
".pgm")==0 ||
660 strcmp(file_ext,
".ppm")==0 ||
666 else if(strcmp(file_ext,
".png")==0 ||
671 else if(strcmp(file_ext,
".jpg") == 0 ||
672 strcmp(file_ext,
".jpeg") == 0 ||
675 yCError(IMAGEFILE) <<
"jpeg not yet implemented";
678 yCError(IMAGEFILE) <<
"unsupported file format";
685 const char* file_ext = strrchr(src.c_str(),
'.');
686 if (strcmp(file_ext,
".pgm") == 0 ||
687 strcmp(file_ext,
".ppm") == 0 ||
693 else if (strcmp(file_ext,
".png") == 0 ||
698 else if (strcmp(file_ext,
".jpg") == 0 ||
699 strcmp(file_ext,
".jpeg") == 0 ||
702 yCError(IMAGEFILE) <<
"jpeg not yet implemented";
705 yCError(IMAGEFILE) <<
"unsupported file format";
712 const char* file_ext = strrchr(src.c_str(),
'.');
713 if (strcmp(file_ext,
".pgm") == 0 ||
714 strcmp(file_ext,
".ppm") == 0 ||
726 else if (strcmp(file_ext,
".png") == 0 ||
737 else if (strcmp(file_ext,
".jpg") == 0 ||
738 strcmp(file_ext,
".jpeg") == 0 ||
741 yCError(IMAGEFILE) <<
"jpeg not yet implemented";
744 yCError(IMAGEFILE) <<
"unsupported file format";
750 const char* file_ext = strrchr(src.c_str(),
'.');
751 if (strcmp(file_ext,
".pgm") == 0 ||
752 strcmp(file_ext,
".ppm") == 0 ||
758 else if (strcmp(file_ext,
".png") == 0 ||
763 else if (strcmp(file_ext,
".jpg") == 0 ||
764 strcmp(file_ext,
".jpeg") == 0 ||
767 yCError(IMAGEFILE) <<
"jpeg not yet implemented";
770 yCError(IMAGEFILE) <<
"unsupported file format";
776 const char* file_ext = strrchr(src.c_str(),
'.');
777 if (strcmp(file_ext,
".pgm") == 0 ||
778 strcmp(file_ext,
".ppm") == 0 ||
784 else if (strcmp(file_ext,
".png") == 0 ||
789 else if (strcmp(file_ext,
".jpg") == 0 ||
790 strcmp(file_ext,
".jpeg") == 0 ||
793 yCError(IMAGEFILE) <<
"jpeg not yet implemented";
796 yCError(IMAGEFILE) <<
"unsupported file format";
820 yCError(IMAGEFILE) <<
"Invalid format, operation not supported";
843 yCError(IMAGEFILE) <<
"Invalid format, operation not supported";
867 yCError(IMAGEFILE) <<
"Invalid format, operation not supported";
885 yCError(IMAGEFILE) <<
"Invalid format, operation not supported";
898 yCError(IMAGEFILE) <<
"Invalid format, operation not supported";
930 return write(img,dest);
static bool ImageWriteFloat(ImageOf< PixelFloat > &img, const char *filename)
static bool ImageReadBGR_PxM(ImageOf< PixelBgr > &img, const char *filename)
static bool ImageReadRGB_PNG(ImageOf< PixelRgb > &img, const char *filename)
static bool ImageWriteJPG(ImageOf< PixelRgb > &img, const char *filename)
static bool ImageWriteRGB(ImageOf< PixelRgb > &img, const char *filename)
static bool ImageReadMono_PxM(ImageOf< PixelMono > &img, const char *filename)
static bool ImageReadBGR_PNG(ImageOf< PixelBgr > &img, const char *filename)
static bool ImageReadMono_PNG(ImageOf< PixelMono > &img, const char *filename)
static bool ImageWriteMono(ImageOf< PixelMono > &img, const char *filename)
static bool SaveJPG(char *src, const char *filename, int h, int w, int rowSize)
static bool ReadHeader_PxM(FILE *fp, int *height, int *width, int *color)
static bool ImageReadFloat_PxM(ImageOf< PixelFloat > &dest, const std::string &filename)
static bool SavePGM(char *src, const char *filename, int h, int w, int rowSize)
static bool ImageReadRGB_PxM(ImageOf< PixelRgb > &img, const char *filename)
static bool SavePPM(char *src, const char *filename, int h, int w, int rowSize)
static bool ImageReadFloat_PNG(ImageOf< PixelFloat > &dest, const std::string &filename)
static bool ImageWritePNG(ImageOf< PixelRgb > &img, const char *filename)
size_t getPixelSize() const override
Gets pixel size in memory in bytes.
Base class for storing images.
unsigned char * getPixelAddress(size_t x, size_t y) const
Get address of a pixel in memory.
size_t width() const
Gets width of image in pixels.
size_t getRowSize() const
Size of the underlying image buffer rows.
unsigned char * getRawImage() const
Access to the internal image buffer.
bool copy(const Image &alt)
Copy operator.
void resize(size_t imgWidth, size_t imgHeight)
Reallocate an image to be of a desired size, throwing away its current contents.
size_t height() const
Gets height of image in pixels.
virtual int getPixelCode() const
Gets pixel type identifier.
#define yCError(component,...)
#define yCWarning(component,...)
#define YARP_LOG_COMPONENT(name,...)
An interface to the operating system, including Port based communication.
bool read(ImageOf< PixelRgb > &dest, const std::string &src, image_fileformat format=FORMAT_ANY)
bool write(const ImageOf< PixelRgb > &src, const std::string &dest, image_fileformat format=FORMAT_PPM)