15#if defined (YARP_HAS_JPEG)
19#if defined (YARP_HAS_PNG)
23#if defined (YARP_HAS_ZLIB)
42 bool ReadHeader_PxM(FILE* fp,
int* height,
int* width,
int* color);
48#if defined (YARP_HAS_ZLIB)
49 bool ImageReadFloat_CompressedHeaderless(
ImageOf<PixelFloat>& dest,
const std::string& filename);
52 bool SaveJPG(
char* src,
const char* filename,
size_t h,
size_t w,
size_t rowSize);
53 bool SavePGM(
char* src,
const char* filename,
size_t h,
size_t w,
size_t rowSize);
54 bool SavePPM(
char* src,
const char* filename,
size_t h,
size_t w,
size_t rowSize);
55#if defined (YARP_HAS_PNG)
56 bool SavePNG(
char* src,
const char* filename,
size_t h,
size_t w,
size_t rowSize, png_byte color_type, png_byte bit_depth);
58 bool SaveFloatRaw(
char* src,
const char* filename,
size_t h,
size_t w,
size_t rowSize);
59#if defined (YARP_HAS_ZLIB)
60 bool SaveFloatCompressed(
char* src,
const char* filename,
size_t h,
size_t w,
size_t rowSize);
77#if defined (YARP_HAS_JPEG)
79 struct jpeg_decompress_struct cinfo;
80 struct jpeg_error_mgr jerr;
81 jpeg_create_decompress(&cinfo);
82 cinfo.err = jpeg_std_error(&jerr);
84 FILE* fp = fopen(filename,
"rb");
87 yCError(IMAGEFILE) <<
"Error: failed to open" << filename;
91 jpeg_stdio_src(&cinfo, fp);
92 jpeg_read_header(&cinfo, TRUE);
93 jpeg_start_decompress(&cinfo);
95 uint32_t j_width = cinfo.image_width;
96 uint32_t j_height = cinfo.image_height;
97 uint32_t j_ch = cinfo.num_components;
99 uint8_t* j_data = NULL;
100 j_data = (uint8_t*)malloc(
sizeof(uint8_t) * j_width * j_height * j_ch);
103 uint8_t* j_row = j_data;
104 const uint32_t j_stride = j_width * j_ch;
105 for (
size_t y = 0; y < j_height; y++)
107 jpeg_read_scanlines(&cinfo, &j_row, 1);
111 jpeg_finish_decompress(&cinfo);
112 jpeg_destroy_decompress(&cinfo);
115 img.
resize(j_width, j_height);
116 for (
size_t y = 0; y < j_height; y++)
118 for (
size_t x = 0; x < j_width; x++)
121 address[0] = j_data[y * j_width * j_ch + x * j_ch + 0];
122 address[1] = j_data[y * j_width * j_ch + x * j_ch + 1];
123 address[2] = j_data[y * j_width * j_ch + x * j_ch + 2];
132 yCError(IMAGEFILE) <<
"JPG library not available/not found";
139#if defined (YARP_HAS_JPEG)
140 yCError(IMAGEFILE) <<
"Not yet implemented";
143 yCError(IMAGEFILE) <<
"JPG library not available/not found";
150#if defined (YARP_HAS_JPEG)
151 yCError(IMAGEFILE) <<
"Not yet implemented";
154 yCError(IMAGEFILE) <<
"JPG library not available/not found";
164#if defined (YARP_HAS_PNG)
165 FILE* fp = fopen(filename,
"rb");
167 png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
170 yCError(IMAGEFILE) <<
"PNG internal error";
174 png_infop info = png_create_info_struct(png);
177 yCError(IMAGEFILE) <<
"PNG internal error";
181 if (setjmp(png_jmpbuf(png)))
183 yCError(IMAGEFILE) <<
"PNG internal error";
187 png_init_io(png, fp);
189 png_read_info(png, info);
191 int width = png_get_image_width(png, info);
192 int height = png_get_image_height(png, info);
193 png_byte color_type = png_get_color_type(png, info);
194 png_byte bit_depth = png_get_bit_depth(png, info);
199 if (bit_depth == 16) {
200 png_set_strip_16(png);
203 if (color_type == PNG_COLOR_TYPE_PALETTE) {
204 png_set_palette_to_rgb(png);
208 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) {
209 png_set_expand_gray_1_2_4_to_8(png);
212 if (png_get_valid(png, info, PNG_INFO_tRNS)) {
213 png_set_tRNS_to_alpha(png);
217 if (color_type == PNG_COLOR_TYPE_RGB || color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_PALETTE) {
218 png_set_filler(png, 0xFF, PNG_FILLER_AFTER);
221 if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
222 png_set_gray_to_rgb(png);
225 png_read_update_info(png, info);
227 png_bytep* row_pointers = (png_bytep*)malloc(
sizeof(png_bytep) * height);
228 for (
int y = 0; y < height; y++)
230 row_pointers[y] = (png_byte*)malloc(png_get_rowbytes(png, info));
233 png_read_image(png, row_pointers);
237 for (
int y = 0; y < height; y++)
239 png_bytep row = row_pointers[y];
240 for (
int x = 0; x < width; x++)
242 png_bytep px = &(row[x * 4]);
250 png_destroy_read_struct(&png, &info, NULL);
251 for (
int y = 0; y < height; y++)
253 free(row_pointers[y]);
258 yCError(IMAGEFILE) <<
"PNG library not available/not found";
265#if defined (YARP_HAS_PNG)
266 yCError(IMAGEFILE) <<
"Not yet implemented";
269 yCError(IMAGEFILE) <<
"PNG library not available/not found";
276#if defined (YARP_HAS_PNG)
277 yCError(IMAGEFILE) <<
"Not yet implemented";
280 yCError(IMAGEFILE) <<
"PNG library not available/not found";
289bool ReadHeader_PxM(FILE *fp,
int *height,
int *width,
int *color)
297 if (fscanf(fp,
"P%c\n", &ch) != 1 || (ch!=
'6'&&ch!=
'5'))
299 yCWarning(IMAGEFILE,
"file is not in pgm/ppm raw format; cannot read");
321 int n=fscanf(fp,
"%d%d%d", width, height, &maxval);
330 yCWarning(IMAGEFILE,
"image is not true-color (24 bit); read failed");
340 int width, height, color, num;
342 fp = fopen(filename,
"rb");
346 yCError(IMAGEFILE,
"Error opening %s, check if file exists.\n", filename);
350 if (!ReadHeader_PxM(fp, &height, &width, &color))
353 yCError(IMAGEFILE,
"Error reading header, is file a valid ppm/pgm?\n");
363 const int h = tmp.
height();
368 for (
int i = 0; i < h; i++)
370 num += (int)fread((
void *) dst, 1, (
size_t) w, fp);
381 const int h = img.
height();
386 for (
int i = 0; i < h; i++)
388 num += (int)fread((
void *) dst, 1, (
size_t) w, fp);
397#if defined (YARP_HAS_ZLIB)
398bool ImageReadFloat_CompressedHeaderless(
ImageOf<PixelFloat>& dest,
const std::string& filename)
400 FILE* fp = fopen(filename.c_str(),
"rb");
408 fseek(fp, 0, SEEK_END);
409 size_t sizeDataCompressed = ftell(fp);
413 char* dataReadInCompressed =
new char[sizeDataCompressed];
414 br = fread(dataReadInCompressed, 1, sizeDataCompressed, fp);
417 if (br != sizeDataCompressed) {
yError() <<
"problems reading file!";
delete [] dataReadInCompressed;
return false; }
419 size_t h = ((
size_t*)(dataReadInCompressed))[0];
420 size_t w = ((
size_t*)(dataReadInCompressed))[1];
421 size_t hds = 2*
sizeof(
size_t);
428 size_t sizeDataUncompressedExtra = sizeDataUncompressed*2;
430 char* dataUncompressed =
new char[sizeDataUncompressedExtra];
432 int z_result = uncompress((Bytef*) dataUncompressed, (uLongf*)&sizeDataUncompressedExtra, (
const Bytef*)dataReadInCompressed+ hds, sizeDataCompressed- hds);
439 yCError(IMAGEFILE,
"zlib compression: out of memory");
440 delete[] dataUncompressed;
445 yCError(IMAGEFILE,
"zlib compression: output buffer wasn't large enough");
446 delete[] dataUncompressed;
451 yCError(IMAGEFILE,
"zlib compression: file contains corrupted data");
452 delete[] dataUncompressed;
458 for (
size_t i=0; i< sizeDataUncompressed; i++)
460 destbuff[i] = dataUncompressed[i];
463 delete [] dataUncompressed;
470 FILE *fp = fopen(filename.c_str(),
"rb");
476 if (fread(dims,
sizeof(dims), 1, fp) == 0)
489 for (
size_t i = 0; i < h; i++)
491 num += (int)fread((
void*)dst, 1, bytes_to_read_per_row, fp);
501 int width, height, color, num;
503 fp = fopen(filename,
"rb");
507 yCError(IMAGEFILE,
"Error opening %s, check if file exists.\n", filename);
511 if (!ReadHeader_PxM(fp, &height, &width, &color))
514 yCError(IMAGEFILE,
"Error reading header, is file a valid ppm/pgm?\n");
521 yCError(IMAGEFILE,
"File is grayscale, conversion not yet supported\n");
526 tmpImg.
resize(width, height);
529 const int h = tmpImg.
height();
534 for (
int i = 0; i < h; i++)
536 num += (int)fread((
void *) dst, 1, (
size_t) w, fp);
542 return img.
copy(tmpImg);
548 int width, height, color, num;
550 fp = fopen(filename,
"rb");
554 yCError(IMAGEFILE,
"Error opening %s, check if file exists.\n", filename);
558 if (!ReadHeader_PxM(fp, &height, &width, &color))
561 yCError(IMAGEFILE,
"Error reading header, is file a valid ppm/pgm?\n");
568 yCError(IMAGEFILE,
"File is color, conversion not yet supported\n");
575 const int h = img.
height();
580 for (
int i = 0; i < h; i++)
582 num += (int)fread((
void *) dst, 1, (
size_t) w, fp);
595#if defined (YARP_HAS_PNG)
596bool SavePNG(
char *src,
const char *filename,
size_t h,
size_t w,
size_t rowSize, png_byte color_type, png_byte bit_depth)
601 yCError(IMAGEFILE,
"[write_png_file] Cannot write to file a nullptr image");
605 if (filename ==
nullptr)
607 yCError(IMAGEFILE,
"[write_png_file] Filename is nullptr");
611 FILE *fp = fopen(filename,
"wb");
614 yCError(IMAGEFILE,
"[write_png_file] File %s could not be opened for writing", filename);
619 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
622 yCError(IMAGEFILE,
"[write_png_file] png_create_write_struct failed");
627 png_infop info_ptr = png_create_info_struct(png_ptr);
630 yCError(IMAGEFILE,
"[write_png_file] png_create_info_struct failed");
636 if (setjmp(png_jmpbuf(png_ptr)))
638 yCError(IMAGEFILE,
"[write_png_file] Error during init_io");
642 png_init_io(png_ptr, fp);
645 if (setjmp(png_jmpbuf(png_ptr)))
647 yCError(IMAGEFILE,
"[write_png_file] Error during writing header");
651 png_set_IHDR(png_ptr, info_ptr, w, h,
652 bit_depth, color_type, PNG_INTERLACE_NONE,
653 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
656 png_write_info(png_ptr, info_ptr);
659 png_bytep* row_pointers =
new png_bytep[h];
661 for (
size_t y = 0; y < h; y++)
664 row_pointers[y] = (png_bytep)(src) + (y * rowSize);
668 if (setjmp(png_jmpbuf(png_ptr)))
670 yCError(IMAGEFILE,
"[write_png_file] Error during writing bytes");
671 delete [] row_pointers;
672 png_destroy_write_struct(&png_ptr, &info_ptr);
676 png_write_image(png_ptr, row_pointers);
679 if (setjmp(png_jmpbuf(png_ptr)))
681 yCError(IMAGEFILE,
"[write_png_file] Error during end of write");
682 delete [] row_pointers;
683 png_destroy_write_struct(&png_ptr, &info_ptr);
687 png_write_end(png_ptr, info_ptr);
690 delete[] row_pointers;
691 png_destroy_write_struct(&png_ptr, &info_ptr);
697bool SaveJPG(
char *src,
const char *filename,
size_t h,
size_t w,
size_t rowSize)
699#if defined (YARP_HAS_JPEG)
701 struct jpeg_compress_struct cinfo;
702 struct jpeg_error_mgr jerr;
703 FILE * outfile =
nullptr;
704 JSAMPROW row_pointer[1];
707 cinfo.err = jpeg_std_error(&jerr);
708 jpeg_create_compress(&cinfo);
710 if ((outfile = fopen(filename,
"wb")) ==
nullptr)
712 yCError(IMAGEFILE,
"can't write file: %s\n", filename);
715 jpeg_stdio_dest(&cinfo, outfile);
717 cinfo.image_width = w;
718 cinfo.image_height = h;
719 cinfo.input_components = 3;
720 cinfo.in_color_space = JCS_RGB;
721 jpeg_set_defaults(&cinfo);
722 jpeg_set_quality(&cinfo, quality, TRUE);
724 jpeg_start_compress(&cinfo, TRUE);
728 while (cinfo.next_scanline < cinfo.image_height)
730 row_pointer[0] = (JSAMPROW)&src[cinfo.next_scanline * row_stride];
731 (
void)jpeg_write_scanlines(&cinfo, row_pointer, 1);
734 jpeg_finish_compress(&cinfo);
736 jpeg_destroy_compress(&cinfo);
739 yCError(IMAGEFILE) <<
"libjpeg not installed";
744bool SavePGM(
char *src,
const char *filename,
size_t h,
size_t w,
size_t rowSize)
746 FILE *fp = fopen(filename,
"wb");
749 yCError(IMAGEFILE,
"cannot open file %s for writing\n", filename);
754 const int inc = rowSize;
756 fprintf(fp,
"P5\n%zu %zu\n%d\n", w, h, 255);
757 for (
size_t i = 0; i < h; i++)
759 fwrite((
void *)src, 1, (
size_t)w, fp);
770bool SavePPM(
char *src,
const char *filename,
size_t h,
size_t w,
size_t rowSize)
772 FILE *fp = fopen(filename,
"wb");
775 yCError(IMAGEFILE,
"cannot open file %s for writing\n", filename);
780 const int inc = rowSize;
782 fprintf(fp,
"P6\n%zu %zu\n%d\n", w, h, 255);
783 for (
size_t i = 0; i < h; i++)
785 fwrite((
void *)src, 1, (
size_t)(w * 3), fp);
796#if defined (YARP_HAS_ZLIB)
797bool SaveFloatCompressed(
char* src,
const char* filename,
size_t h,
size_t w,
size_t rowSize)
799 size_t sizeDataOriginal=w*h*
sizeof(float);
800 size_t sizeDataCompressed = (sizeDataOriginal * 1.1) + 12;
801 char* dataCompressed = (
char*)malloc(sizeDataCompressed);
803 int z_result = compress((Bytef*) dataCompressed,(uLongf*) &sizeDataCompressed, (Bytef*)src, sizeDataOriginal);
810 yCError(IMAGEFILE,
"zlib compression: out of memory");
815 yCError(IMAGEFILE,
"zlib compression: output buffer wasn't large enough");
820 FILE* fp = fopen(filename,
"wb");
827 size_t dims[2] = { h,w };
829 if (fwrite(dims,
sizeof(dims), 1, fp) > 0) {
830 bw = fwrite((
void*)dataCompressed, sizeDataCompressed, 1, fp);
838bool SaveFloatRaw(
char* src,
const char* filename,
size_t h,
size_t w,
size_t rowSize)
840 FILE* fp = fopen(filename,
"wb");
846 size_t dims[2] = { h,w };
849 size_t size_ =
sizeof(float);
850 auto count_ = (
size_t)(dims[0] * dims[1]);
852 if (fwrite(dims,
sizeof(dims), 1, fp) > 0) {
853 bw = fwrite((
void*)src, size_, count_, fp);
867#if defined (YARP_HAS_PNG)
870 yCError(IMAGEFILE) <<
"YARP was not built with png support";
877#if defined (YARP_HAS_PNG)
880 yCError(IMAGEFILE) <<
"YARP was not built with png support";
902#if defined (YARP_HAS_ZLIB)
905 yCError(IMAGEFILE) <<
"YARP was not built with zlib support";
917 const char* file_ext = strrchr(src.c_str(),
'.');
918 if (file_ext==
nullptr)
920 yCError(IMAGEFILE) <<
"cannot find file extension in file name";
924 if (strcmp(file_ext,
".pgm")==0 ||
925 strcmp(file_ext,
".ppm")==0 ||
929 return ImageReadRGB_PxM(dest,src.c_str());
931 else if(strcmp(file_ext,
".png")==0 ||
934 return ImageReadRGB_PNG(dest, src.c_str());
936 else if(strcmp(file_ext,
".jpg") == 0 ||
937 strcmp(file_ext,
".jpeg") == 0 ||
940 return ImageReadRGB_JPG(dest, src.c_str());
942 yCError(IMAGEFILE) <<
"unsupported file format";
949 const char* file_ext = strrchr(src.c_str(),
'.');
950 if (file_ext ==
nullptr)
952 yCError(IMAGEFILE) <<
"cannot find file extension in file name";
956 if (strcmp(file_ext,
".pgm") == 0 ||
957 strcmp(file_ext,
".ppm") == 0 ||
961 return ImageReadBGR_PxM(dest, src.c_str());
963 else if (strcmp(file_ext,
".png") == 0 ||
966 return ImageReadBGR_PNG(dest, src.c_str());
968 else if (strcmp(file_ext,
".jpg") == 0 ||
969 strcmp(file_ext,
".jpeg") == 0 ||
972 return ImageReadBGR_JPG(dest, src.c_str());
974 yCError(IMAGEFILE) <<
"unsupported file format";
981 const char* file_ext = strrchr(src.c_str(),
'.');
982 if (file_ext ==
nullptr)
984 yCError(IMAGEFILE) <<
"cannot find file extension in file name";
988 if (strcmp(file_ext,
".pgm") == 0 ||
989 strcmp(file_ext,
".ppm") == 0 ||
994 bool ok = ImageReadRGB_PxM(img2, src.c_str());
1001 else if (strcmp(file_ext,
".png") == 0 ||
1005 bool ok = ImageReadRGB_PNG(img2, src.c_str());
1012 else if (strcmp(file_ext,
".jpg") == 0 ||
1013 strcmp(file_ext,
".jpeg") == 0 ||
1017 bool ok = ImageReadRGB_JPG(img2, src.c_str());
1024 yCError(IMAGEFILE) <<
"unsupported file format";
1030 const char* file_ext = strrchr(src.c_str(),
'.');
1031 if (file_ext ==
nullptr)
1033 yCError(IMAGEFILE) <<
"cannot find file extension in file name";
1037 if (strcmp(file_ext,
".pgm") == 0 ||
1038 strcmp(file_ext,
".ppm") == 0 ||
1042 return ImageReadMono_PxM(dest, src.c_str());
1044 else if (strcmp(file_ext,
".png") == 0 ||
1047 return ImageReadMono_PNG(dest, src.c_str());
1049 else if (strcmp(file_ext,
".jpg") == 0 ||
1050 strcmp(file_ext,
".jpeg") == 0 ||
1053 return ImageReadMono_JPG(dest, src.c_str());
1055 yCError(IMAGEFILE) <<
"unsupported file format";
1061 const char* file_ext = strrchr(src.c_str(),
'.');
1062 if (file_ext ==
nullptr)
1064 yCError(IMAGEFILE) <<
"cannot find file extension in file name";
1068 if (strcmp(file_ext,
".float") == 0 ||
1071 return ImageReadFloat_PlainHeaderless(dest, src);
1073 else if (strcmp(file_ext,
".floatzip") == 0 ||
1076#if defined (YARP_HAS_ZLIB)
1077 return ImageReadFloat_CompressedHeaderless(dest, src);
1079 yCError(IMAGEFILE) <<
"YARP was not built with zlib support";
1083 yCError(IMAGEFILE) <<
"unsupported file format";
1107 yCError(IMAGEFILE) <<
"Invalid format, operation not supported";
1130 yCError(IMAGEFILE) <<
"Invalid format, operation not supported";
1154 yCError(IMAGEFILE) <<
"Invalid format, operation not supported";
1172 yCError(IMAGEFILE) <<
"Invalid format, operation not supported";
1181 return ImageWriteFloat_PlainHeaderless(
const_cast<ImageOf<PixelFloat> &
>(src), dest.c_str());
1185 return ImageWriteFloat_CompressedHeaderless(
const_cast<ImageOf<PixelFloat>&
>(src), dest.c_str());
1189 yCError(IMAGEFILE) <<
"Invalid format, operation not supported";
1221 return write(img,dest);
size_t getPixelSize() const override
Gets pixel size in memory in bytes.
Base class for storing images.
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.
size_t getRawImageSize() const
Access to the internal buffer size information (this is how much memory has been allocated for the im...
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.
unsigned char * getPixelAddress(size_t x, size_t y) const
Get address of a pixel in memory.
#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)
@ FORMAT_NUMERIC_COMPRESSED
bool write(const ImageOf< PixelRgb > &src, const std::string &dest, image_fileformat format=FORMAT_PPM)
unsigned char PixelMono
Monochrome pixel type.
float PixelFloat
Floating point pixel type.
Packed RGB pixel type, with pixels stored in reverse order.