21 #if defined (YARP_HAS_PNG) 33 static bool ReadHeader(FILE *fp,
int *height,
int *width,
int *color)
41 if (fscanf(fp,
"P%c\n", &ch) != 1 || (ch!=
'6'&&ch!=
'5'))
43 yWarning(
"file is not in pgm/ppm raw format; cannot read");
47 if (ch==
'6') *color = 1;
63 int n=fscanf(fp,
"%d%d%d", width, height, &maxval);
71 yWarning(
"image is not true-color (24 bit); read failed");
81 int width, height, color, num;
83 fp = fopen(filename,
"rb");
87 yError(
"Error opening %s, check if file exists.\n", filename);
94 yError(
"Error reading header, is file a valid ppm/pgm?\n");
104 const int h = tmp.
height();
109 for (
int i = 0; i < h; i++)
111 num += (int)fread((
void *) dst, 1, (size_t) w, fp);
122 const int h = img.
height();
127 for (
int i = 0; i < h; i++)
129 num += (int)fread((
void *) dst, 1, (size_t) w, fp);
140 FILE *fp = fopen(filename.c_str(),
"rb");
146 size_t size_ =
sizeof(float);
150 if (fread(dims,
sizeof(dims), 1, fp) > 0)
152 count_ = (size_t)(dims[0] * dims[1]);
153 dest.
resize(dims[0], dims[1]);
154 br = fread(&dest(0, 0), size_, count_, fp);
163 int width, height, color, num;
165 fp = fopen(filename,
"rb");
169 yError(
"Error opening %s, check if file exists.\n", filename);
176 yError(
"Error reading header, is file a valid ppm/pgm?\n");
183 yError(
"File is grayscale, conversion not yet supported\n");
188 tmpImg.
resize(width, height);
191 const int h = tmpImg.
height();
196 for (
int i = 0; i < h; i++)
198 num += (int)fread((
void *) dst, 1, (size_t) w, fp);
204 return img.
copy(tmpImg);
210 int width, height, color, num;
212 fp = fopen(filename,
"rb");
216 yError(
"Error opening %s, check if file exists.\n", filename);
223 yError(
"Error reading header, is file a valid ppm/pgm?\n");
230 yError(
"File is color, conversion not yet supported\n");
237 const int h = img.
height();
242 for (
int i = 0; i < h; i++)
244 num += (int)fread((
void *) dst, 1, (size_t) w, fp);
257 #if defined (YARP_HAS_PNG) 258 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)
263 yError(
"[write_png_file] Cannot write to file a nullptr image");
267 if (filename ==
nullptr)
269 yError(
"[write_png_file] Filename is nullptr");
273 FILE *fp = fopen(filename,
"wb");
276 yError(
"[write_png_file] File %s could not be opened for writing", filename);
281 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
284 yError(
"[write_png_file] png_create_write_struct failed");
289 png_infop info_ptr = png_create_info_struct(png_ptr);
292 yError(
"[write_png_file] png_create_info_struct failed");
297 png_bytep * row_pointers = (png_bytep*)malloc(
sizeof(png_bytep) * h);
298 for (
size_t y = 0; y < h; y++)
300 row_pointers[y] = (png_bytep)(src)+y*w;
303 if (setjmp(png_jmpbuf(png_ptr)))
305 yError(
"[write_png_file] Error during init_io");
310 png_init_io(png_ptr, fp);
314 if (setjmp(png_jmpbuf(png_ptr)))
316 yError(
"[write_png_file] Error during writing header");
320 png_set_IHDR(png_ptr, info_ptr, w, h,
321 bit_depth, color_type, PNG_INTERLACE_NONE,
322 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
324 png_write_info(png_ptr, info_ptr);
328 if (setjmp(png_jmpbuf(png_ptr)))
330 yError(
"[write_png_file] Error during writing bytes");
335 png_write_image(png_ptr, row_pointers);
339 if (setjmp(png_jmpbuf(png_ptr)))
341 yError(
"[write_png_file] Error during end of write");
346 png_write_end(png_ptr, NULL);
356 static bool SaveJPG(
char *src,
const char *filename,
int h,
int w,
int rowSize)
360 struct jpeg_compress_struct cinfo;
361 struct jpeg_error_mgr jerr;
363 JSAMPROW row_pointer[1];
366 cinfo.err = jpeg_std_error(&jerr);
367 jpeg_create_compress(&cinfo);
369 if ((outfile = fopen(filename,
"wb")) ==
nullptr)
371 yError(
"can't write file: %s\n", filename);
374 jpeg_stdio_dest(&cinfo, outfile);
376 cinfo.image_width = w;
377 cinfo.image_height = h;
378 cinfo.input_components = 3;
379 cinfo.in_color_space = JCS_RGB;
380 jpeg_set_defaults(&cinfo);
381 jpeg_set_quality(&cinfo, quality, TRUE);
383 jpeg_start_compress(&cinfo, TRUE);
387 while (cinfo.next_scanline < cinfo.image_height)
389 row_pointer[0] = (JSAMPROW)&src[cinfo.next_scanline * row_stride];
390 (
void)jpeg_write_scanlines(&cinfo, row_pointer, 1);
393 jpeg_finish_compress(&cinfo);
395 jpeg_destroy_compress(&cinfo);
398 yError() <<
"libjpeg not installed";
403 static bool SavePGM(
char *src,
const char *filename,
int h,
int w,
int rowSize)
405 FILE *fp = fopen(filename,
"wb");
408 yError(
"cannot open file %s for writing\n", filename);
413 const int inc = rowSize;
415 fprintf(fp,
"P5\n%d %d\n%d\n", w, h, 255);
416 for (
int i = 0; i < h; i++)
418 fwrite((
void *)src, 1, (
size_t)w, fp);
429 static bool SavePPM(
char *src,
const char *filename,
int h,
int w,
int rowSize)
431 FILE *fp = fopen(filename,
"wb");
434 yError(
"cannot open file %s for writing\n", filename);
439 const int inc = rowSize;
441 fprintf(fp,
"P6\n%d %d\n%d\n", w, h, 255);
442 for (
int i = 0; i < h; i++)
444 fwrite((
void *)src, 1, (
size_t)(w * 3), fp);
462 #if defined (YARP_HAS_PNG) 465 yError() <<
"YARP was not built with png support";
472 #if defined (YARP_HAS_PNG) 475 yError() <<
"YARP was not built with png support";
492 FILE *fp = fopen(filename,
"wb");
501 size_t size_ =
sizeof(float);
502 auto count_ = (size_t)(dims[0] * dims[1]);
504 if (fwrite(dims,
sizeof(dims), 1, fp) > 0) {
505 bw = fwrite(&img(0, 0), size_, count_, fp);
569 yError() <<
"Invalid format, operation not supported";
592 yError() <<
"Invalid format, operation not supported";
616 yError() <<
"Invalid format, operation not supported";
634 yError() <<
"Invalid format, operation not supported";
647 yError() <<
"Invalid format, operation not supported";
679 return write(img,dest);
bool write(const ImageOf< PixelRgb > &src, const std::string &dest, image_fileformat format=FORMAT_PPM)
unsigned char * getRawImage() const
Access to the internal image buffer.
static bool ImageWriteFloat(ImageOf< PixelFloat > &img, const char *filename)
static bool ImageWritePNG(ImageOf< PixelRgb > &img, const char *filename)
bool read(ImageOf< PixelRgb > &dest, const std::string &src, image_fileformat format=FORMAT_ANY)
bool copy(const Image &alt)
Copy operator.
static bool ImageReadRGB(ImageOf< PixelRgb > &img, const char *filename)
static bool ImageReadFloat(ImageOf< PixelFloat > &dest, const std::string &filename)
size_t getRowSize() const
Size of the underlying image buffer rows.
static bool SavePGM(char *src, const char *filename, int h, int w, int rowSize)
static bool ReadHeader(FILE *fp, int *height, int *width, int *color)
size_t width() const
Gets width of image in pixels.
size_t getPixelSize() const override
Gets pixel size in memory in bytes.
Base class for storing images.
void resize(size_t imgWidth, size_t imgHeight)
Reallocate an image to be of a desired size, throwing away its current contents.
static bool ImageReadBGR(ImageOf< PixelBgr > &img, const char *filename)
static bool SavePPM(char *src, const char *filename, int h, int w, int rowSize)
static bool SaveJPG(char *src, const char *filename, int h, int w, int rowSize)
size_t height() const
Gets height of image in pixels.
static bool ImageWriteRGB(ImageOf< PixelRgb > &img, const char *filename)
static bool ImageWriteMono(ImageOf< PixelMono > &img, const char *filename)
An interface to the operating system, including Port based communication.
static bool ImageReadMono(ImageOf< PixelMono > &img, const char *filename)
static bool ImageWriteJPG(ImageOf< PixelRgb > &img, const char *filename)
virtual int getPixelCode() const
Gets pixel type identifier.