YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
ImageFile.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
9#include <yarp/os/LogStream.h>
10
11#include <cstdio>
12#include <cstring>
13#include <cstdlib>
14
15#if defined (YARP_HAS_JPEG)
16#include "jpeglib.h"
17#endif
18
19#if defined (YARP_HAS_PNG)
20#include <png.h>
21#endif
22
23#if defined (YARP_HAS_ZLIB)
24#include <zlib.h>
25#endif
26
27using namespace yarp::os;
28using namespace yarp::sig;
29
30namespace
31{
32 YARP_LOG_COMPONENT(IMAGEFILE, "yarp.sig.ImageFile")
33
34 bool ImageReadRGB_JPG(ImageOf<PixelRgb>& img, const char* filename);
35 bool ImageReadBGR_JPG(ImageOf<PixelBgr>& img, const char* filename);
36 bool ImageReadMono_JPG(ImageOf<PixelMono>& img, const char* filename);
37
38 bool ImageReadRGB_PNG(ImageOf<PixelRgb>& img, const char* filename);
39 bool ImageReadBGR_PNG(ImageOf<PixelBgr>& img, const char* filename);
40 bool ImageReadMono_PNG(ImageOf<PixelMono>& img, const char* filename);
41
42 bool ReadHeader_PxM(FILE* fp, int* height, int* width, int* color);
43 bool ImageReadMono_PxM(ImageOf<PixelMono>& img, const char* filename);
44 bool ImageReadRGB_PxM(ImageOf<PixelRgb>& img, const char* filename);
45 bool ImageReadBGR_PxM(ImageOf<PixelBgr>& img, const char* filename);
46
47 bool ImageReadFloat_PlainHeaderless(ImageOf<PixelFloat>& dest, const std::string& filename);
48#if defined (YARP_HAS_ZLIB)
49 bool ImageReadFloat_CompressedHeaderless(ImageOf<PixelFloat>& dest, const std::string& filename);
50#endif
51
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);
57#endif
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);
61#endif
62
63 bool ImageWriteJPG(ImageOf<PixelRgb>& img, const char* filename);
64 bool ImageWritePNG(ImageOf<PixelRgb>& img, const char* filename);
65 bool ImageWritePNG(ImageOf<PixelMono>& img, const char* filename);
66 bool ImageWriteRGB(ImageOf<PixelRgb>& img, const char* filename);
67 bool ImageWriteMono(ImageOf<PixelMono>& img, const char* filename);
68
69 bool ImageWriteFloat_PlainHeaderless(ImageOf<PixelFloat>& img, const char* filename);
70 bool ImageWriteFloat_CompressedHeaderless(ImageOf<PixelFloat>& img, const char* filename);
71
73// private read methods for JPG Files
75bool ImageReadRGB_JPG(ImageOf<PixelRgb>& img, const char* filename)
76{
77#if defined (YARP_HAS_JPEG)
78
79 struct jpeg_decompress_struct cinfo;
80 struct jpeg_error_mgr jerr;
82 cinfo.err = jpeg_std_error(&jerr);
83
84 FILE* fp = fopen(filename, "rb");
85 if (!fp)
86 {
87 yCError(IMAGEFILE) << "Error: failed to open" << filename;
88 return false;
89 }
90
91 jpeg_stdio_src(&cinfo, fp);
92 jpeg_read_header(&cinfo, TRUE);
94
95 uint32_t j_width = cinfo.image_width;
96 uint32_t j_height = cinfo.image_height;
97 uint32_t j_ch = cinfo.num_components;
98
100 j_data = (uint8_t*)malloc(sizeof(uint8_t) * j_width * j_height * j_ch);
101
102 //read line by line
104 const uint32_t j_stride = j_width * j_ch;
105 for (size_t y = 0; y < j_height; y++)
106 {
107 jpeg_read_scanlines(&cinfo, &j_row, 1);
108 j_row += j_stride;
109 }
110
113 fclose(fp);
114
115 img.resize(j_width, j_height);
116 for (size_t y = 0; y < j_height; y++)
117 {
118 for (size_t x = 0; x < j_width; x++)
119 {
120 unsigned char* address = img.getPixelAddress(x, y);
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];
124 }
125 }
126
127 free(j_data);
128 j_data = nullptr;
129
130 return true;
131#else
132 yCError(IMAGEFILE) << "JPG library not available/not found";
133 return false;
134#endif
135}
136
137bool ImageReadBGR_JPG(ImageOf<PixelBgr>& img, const char* filename)
138{
139#if defined (YARP_HAS_JPEG)
140 yCError(IMAGEFILE) << "Not yet implemented";
141 return false;
142#else
143 yCError(IMAGEFILE) << "JPG library not available/not found";
144 return false;
145#endif
146}
147
148bool ImageReadMono_JPG(ImageOf<PixelMono>& img, const char* filename)
149{
150#if defined (YARP_HAS_JPEG)
151 yCError(IMAGEFILE) << "Not yet implemented";
152 return false;
153#else
154 yCError(IMAGEFILE) << "JPG library not available/not found";
155 return false;
156#endif
157}
158
160// private read methods for PNG Files
162bool ImageReadRGB_PNG(ImageOf<PixelRgb>& img, const char* filename)
163{
164#if defined (YARP_HAS_PNG)
165 FILE* fp = fopen(filename, "rb");
166 if (!fp)
167 {
168 yCError(IMAGEFILE) << "Error: failed to open" << filename;
169 return false;
170 }
171
173 if (!png)
174 {
175 fclose(fp);
176 yCError(IMAGEFILE) << "PNG internal error";
177 return false;
178 }
179
181 if (!info)
182 {
183 fclose(fp);
184 yCError(IMAGEFILE) << "PNG internal error";
185 return false;
186 }
187
188 if (setjmp(png_jmpbuf(png)))
189 {
190 fclose(fp);
191 yCError(IMAGEFILE) << "PNG internal error";
192 return false;
193 }
194
196
198
199 int width = png_get_image_width(png, info);
200 int height = png_get_image_height(png, info);
203
204 // Read any color_type into 8bit depth, RGBA format.
205 // See http://www.libpng.org/pub/png/libpng-manual.txt
206
207 if (bit_depth == 16) {
209 }
210
213 }
214
215 // PNG_COLOR_TYPE_GRAY_ALPHA is always 8 or 16bit depth.
218 }
219
222 }
223
224 // These color_type don't have an alpha channel then fill it with 0xff.
227 }
228
231 }
232
234
235 png_bytep* row_pointers = (png_bytep*)malloc(sizeof(png_bytep) * height);
236 for (int y = 0; y < height; y++)
237 {
239 }
240
242 fclose(fp);
243
244 img.resize(width,height);
245 for (int y = 0; y < height; y++)
246 {
248 for (int x = 0; x < width; x++)
249 {
250 png_bytep px = &(row[x * 4]);
251 unsigned char* address = img.getPixelAddress(x,y);
252 address[0] = px[0];
253 address[1] = px[1];
254 address[2] = px[2];
255 }
256 }
257
259 for (int y = 0; y < height; y++)
260 {
261 free(row_pointers[y]);
262 }
264 return true;
265#else
266 yCError(IMAGEFILE) << "PNG library not available/not found";
267 return false;
268#endif
269}
270
271bool ImageReadBGR_PNG(ImageOf<PixelBgr>& img, const char* filename)
272{
273#if defined (YARP_HAS_PNG)
274 yCError(IMAGEFILE) << "Not yet implemented";
275 return false;
276#else
277 yCError(IMAGEFILE) << "PNG library not available/not found";
278 return false;
279#endif
280}
281
282bool ImageReadMono_PNG(ImageOf<PixelMono>& img, const char* filename)
283{
284#if defined (YARP_HAS_PNG)
285 yCError(IMAGEFILE) << "Not yet implemented";
286 return false;
287#else
288 yCError(IMAGEFILE) << "PNG library not available/not found";
289 return false;
290#endif
291}
292
294// private read methods for PGM/PPM Files
296
297bool ReadHeader_PxM(FILE *fp, int *height, int *width, int *color)
298{
299 char ch;
300 int maxval;
301
302 *color = 0;
303
305 if (fscanf(fp, "P%c\n", &ch) != 1 || (ch!='6'&&ch!='5'))
306 {
307 yCWarning(IMAGEFILE, "file is not in pgm/ppm raw format; cannot read");
308 return false;
309 }
310
311 if (ch == '6') {
312 *color = 1;
313 }
314
315 // skip comments
316 ch = fgetc(fp);
317 while (ch == '#')
318 {
319 do
320 {
321 ch = fgetc(fp);
322 }
323 while (ch != '\n');
324 ch = fgetc(fp);
325 }
326 ungetc(ch, fp);
327
329 int n=fscanf(fp, "%d%d%d", width, height, &maxval);
330 if (n != 3) {
331 return false;
332 }
333
334 fgetc(fp);
335 if (maxval != 255)
336 {
337 //die("image is not true-color (24 bit); read failed");
338 yCWarning(IMAGEFILE, "image is not true-color (24 bit); read failed");
339 return false;
340 }
341
342 return true;
343}
344
345
346bool ImageReadRGB_PxM(ImageOf<PixelRgb> &img, const char *filename)
347{
348 int width, height, color, num;
349 FILE *fp=nullptr;
350 fp = fopen(filename, "rb");
351 if(!fp)
352 {
353 yCError(IMAGEFILE, "Error opening %s, check if file exists.\n", filename);
354 return false;
355 }
356
357 if (!ReadHeader_PxM(fp, &height, &width, &color))
358 {
359 fclose (fp);
360 yCError(IMAGEFILE, "Error reading header, is file a valid ppm/pgm?\n");
361 return false;
362 }
363
364 if (!color)
365 {
367 tmp.resize(width,height);
368
369 const int w = tmp.width() * tmp.getPixelSize();
370 const int h = tmp.height();
371 const int pad = tmp.getRowSize();
372 unsigned char *dst = tmp.getRawImage ();
373
374 num = 0;
375 for (int i = 0; i < h; i++)
376 {
377 num += (int)fread((void *) dst, 1, (size_t) w, fp);
378 dst += pad;
379 }
380 fclose(fp);
381 img.copy(tmp);
382 return true;
383 }
384
385 img.resize(width,height);
386
387 const int w = img.width() * img.getPixelSize();
388 const int h = img.height();
389 const int pad = img.getRowSize();
390 unsigned char *dst = img.getRawImage ();
391
392 num = 0;
393 for (int i = 0; i < h; i++)
394 {
395 num += (int)fread((void *) dst, 1, (size_t) w, fp);
396 dst += pad;
397 }
398
399 fclose(fp);
400
401 return true;
402}
403
404#if defined (YARP_HAS_ZLIB)
405bool ImageReadFloat_CompressedHeaderless(ImageOf<PixelFloat>& dest, const std::string& filename)
406{
407 FILE* fp = fopen(filename.c_str(), "rb");
408 if (!fp)
409 {
410 yCError(IMAGEFILE) << "Error: failed to open" << filename;
411 return false;
412 }
413
414 size_t br = 0;
415
416 //get the file size
417 fseek(fp, 0, SEEK_END);
418 size_t sizeDataCompressed = ftell(fp);
419 rewind(fp);
420
421 //read the compressed data
424 fclose(fp);
425
426 if (br != sizeDataCompressed) { yError() << "problems reading file!"; delete [] dataReadInCompressed; return false; }
427
428 size_t h = ((size_t*)(dataReadInCompressed))[0]; //byte 0
429 size_t w = ((size_t*)(dataReadInCompressed))[1]; //byte 8, because size_t is 8 bytes long
430 size_t hds = 2* sizeof(size_t); //16 bytes
431
432 dest.resize(w, h);
433 unsigned char* destbuff = dest.getRawImage();
434 //this is the size of the image
435 size_t sizeDataUncompressed = dest.getRawImageSize();
436 //this is the size of the buffer. Extra space is required for temporary operations (I choose arbitrarily *2)
438
440
442 switch (z_result)
443 {
444 case Z_OK:
445 break;
446
447 case Z_MEM_ERROR:
448 yCError(IMAGEFILE, "zlib compression: out of memory");
449 delete[] dataUncompressed;
450 return false;
451 break;
452
453 case Z_BUF_ERROR:
454 yCError(IMAGEFILE, "zlib compression: output buffer wasn't large enough");
455 delete[] dataUncompressed;
456 return false;
457 break;
458
459 case Z_DATA_ERROR:
460 yCError(IMAGEFILE, "zlib compression: file contains corrupted data");
461 delete[] dataUncompressed;
462 return false;
463 break;
464 }
465
466 //here I am copy only the size of the image, obviously the extra space is not needed anymore.
467 for (size_t i=0; i< sizeDataUncompressed; i++)
468 {
470 }
471
472 delete [] dataUncompressed;
473 return true;
474}
475#endif
476
477bool ImageReadFloat_PlainHeaderless(ImageOf<PixelFloat>& dest, const std::string& filename)
478{
479 FILE *fp = fopen(filename.c_str(), "rb");
480 if (!fp)
481 {
482 yCError(IMAGEFILE) << "Error: failed to open" << filename;
483 return false;
484 }
485
486 size_t dims[2];
487 if (fread(dims, sizeof(dims), 1, fp) == 0)
488 {
489 fclose(fp);
490 return false;
491 }
492
493 size_t h = dims[0];
494 size_t w = dims[1];
495 dest.resize(w, h);
496 size_t pad = dest.getRowSize();
497 size_t bytes_to_read_per_row = w* dest.getPixelSize();
498 unsigned char* dst = dest.getRawImage();
499 size_t num = 0;
500 for (size_t i = 0; i < h; i++)
501 {
502 num += (int)fread((void*)dst, 1, bytes_to_read_per_row, fp);
503 dst += pad;
504 }
505
506 fclose(fp);
507 return (num > 0);
508}
509
510bool ImageReadBGR_PxM(ImageOf<PixelBgr> &img, const char *filename)
511{
512 int width, height, color, num;
513 FILE *fp=nullptr;
514 fp = fopen(filename, "rb");
515 if(!fp)
516 {
517 yCError(IMAGEFILE, "Error opening %s, check if file exists.\n", filename);
518 return false;
519 }
520
521 if (!ReadHeader_PxM(fp, &height, &width, &color))
522 {
523 fclose (fp);
524 yCError(IMAGEFILE, "Error reading header, is file a valid ppm/pgm?\n");
525 return false;
526 }
527
528 if (!color)
529 {
530 fclose(fp);
531 yCError(IMAGEFILE, "File is grayscale, conversion not yet supported\n");
532 return false;
533 }
534
536 tmpImg.resize(width, height);
537
538 const int w = tmpImg.width() * img.getPixelSize();
539 const int h = tmpImg.height();
540 const int pad = tmpImg.getRowSize();
541 unsigned char *dst = tmpImg.getRawImage ();
542
543 num = 0;
544 for (int i = 0; i < h; i++)
545 {
546 num += (int)fread((void *) dst, 1, (size_t) w, fp);
547 dst += pad;
548 }
549
550 fclose(fp);
551
552 return img.copy(tmpImg);
553}
554
555
556bool ImageReadMono_PxM(ImageOf<PixelMono> &img, const char *filename)
557{
558 int width, height, color, num;
559 FILE *fp=nullptr;
560 fp = fopen(filename, "rb");
561 if(!fp)
562 {
563 yCError(IMAGEFILE, "Error opening %s, check if file exists.\n", filename);
564 return false;
565 }
566
567 if (!ReadHeader_PxM(fp, &height, &width, &color))
568 {
569 fclose (fp);
570 yCError(IMAGEFILE, "Error reading header, is file a valid ppm/pgm?\n");
571 return false;
572 }
573
574 if (color)
575 {
576 fclose(fp);
577 yCError(IMAGEFILE, "File is color, conversion not yet supported\n");
578 return false;
579 }
580
581 img.resize(width,height);
582
583 const int w = img.width() * img.getPixelSize();
584 const int h = img.height();
585 const int pad = img.getRowSize();
586 unsigned char *dst = img.getRawImage ();
587
588 num = 0;
589 for (int i = 0; i < h; i++)
590 {
591 num += (int)fread((void *) dst, 1, (size_t) w, fp);
592 dst += pad;
593 }
594
595 fclose(fp);
596
597 return true;
598}
599
601// private write methods
603
604#if defined (YARP_HAS_PNG)
605bool SavePNG(char *src, const char *filename, size_t h, size_t w, size_t rowSize, png_byte color_type, png_byte bit_depth)
606{
607 // create file
608 if (src == nullptr)
609 {
610 yCError(IMAGEFILE, "[write_png_file] Cannot write to file a nullptr image");
611 return false;
612 }
613
614 if (filename == nullptr)
615 {
616 yCError(IMAGEFILE, "[write_png_file] Filename is nullptr");
617 return false;
618 }
619
620 FILE *fp = fopen(filename, "wb");
621 if (!fp)
622 {
623 yCError(IMAGEFILE, "[write_png_file] File %s could not be opened for writing", filename);
624 return false;
625 }
626
627 // initialize stuff
629 if (!png_ptr)
630 {
631 yCError(IMAGEFILE, "[write_png_file] png_create_write_struct failed");
632 fclose(fp);
633 return false;
634 }
635
637 if (!info_ptr)
638 {
639 yCError(IMAGEFILE, "[write_png_file] png_create_info_struct failed");
640 fclose(fp);
641 return false;
642 }
643
644 //init io
646 {
647 yCError(IMAGEFILE, "[write_png_file] Error during init_io");
648 fclose(fp);
649 return false;
650 }
652
653 // write header
655 {
656 yCError(IMAGEFILE, "[write_png_file] Error during writing header");
657 fclose(fp);
658 return false;
659 }
663
664 //write info
666
667 //allocate data space
669
670 for (size_t y = 0; y < h; y++)
671 {
672 //this is an array of pointers. Each element points to a row of the image
673 row_pointers[y] = (png_bytep)(src) + (y * rowSize);
674 }
675
676 // write bytes
678 {
679 yCError(IMAGEFILE, "[write_png_file] Error during writing bytes");
680 delete [] row_pointers;
682 fclose(fp);
683 return false;
684 }
686
687 // end write
689 {
690 yCError(IMAGEFILE, "[write_png_file] Error during end of write");
691 delete [] row_pointers;
693 fclose(fp);
694 return false;
695 }
697
698 // finished. cleanup allocation
699 delete[] row_pointers;
701 fclose(fp);
702 return true;
703}
704#endif
705
706bool SaveJPG(char *src, const char *filename, size_t h, size_t w, size_t rowSize)
707{
708#if defined (YARP_HAS_JPEG)
709 int quality = 100;
710 struct jpeg_compress_struct cinfo;
711 struct jpeg_error_mgr jerr;
712 FILE * outfile = nullptr;
714 int row_stride;
715
716 cinfo.err = jpeg_std_error(&jerr);
717 jpeg_create_compress(&cinfo);
718
719 if ((outfile = fopen(filename, "wb")) == nullptr)
720 {
721 yCError(IMAGEFILE, "can't write file: %s\n", filename);
722 return false;
723 }
724 jpeg_stdio_dest(&cinfo, outfile);
725
726 cinfo.image_width = w;
727 cinfo.image_height = h;
728 cinfo.input_components = 3;
729 cinfo.in_color_space = JCS_RGB;
730 jpeg_set_defaults(&cinfo);
731 jpeg_set_quality(&cinfo, quality, TRUE);
732
733 jpeg_start_compress(&cinfo, TRUE);
734
735 row_stride = w * 3;
736
737 while (cinfo.next_scanline < cinfo.image_height)
738 {
739 row_pointer[0] = (JSAMPROW)&src[cinfo.next_scanline * row_stride];
741 }
742
743 jpeg_finish_compress(&cinfo);
745 jpeg_destroy_compress(&cinfo);
746 return true;
747#else
748 yCError(IMAGEFILE) << "libjpeg not installed";
749 return false;
750#endif
751}
752
753bool SavePGM(char *src, const char *filename, size_t h, size_t w, size_t rowSize)
754{
755 FILE *fp = fopen(filename, "wb");
756 if (!fp)
757 {
758 yCError(IMAGEFILE, "cannot open file %s for writing\n", filename);
759 return false;
760 }
761 else
762 {
763 const int inc = rowSize;
764
765 fprintf(fp, "P5\n%zu %zu\n%d\n", w, h, 255);
766 for (size_t i = 0; i < h; i++)
767 {
768 fwrite((void *)src, 1, (size_t)w, fp);
769 src += inc;
770 }
771
772 fclose(fp);
773 }
774
775 return true;
776}
777
778
779bool SavePPM(char *src, const char *filename, size_t h, size_t w, size_t rowSize)
780{
781 FILE *fp = fopen(filename, "wb");
782 if (!fp)
783 {
784 yCError(IMAGEFILE, "cannot open file %s for writing\n", filename);
785 return false;
786 }
787 else
788 {
789 const int inc = rowSize;//YARPSimpleOperation::ComputePadding (w*3, YarpImageAlign) + w * 3;
790
791 fprintf(fp, "P6\n%zu %zu\n%d\n", w, h, 255);
792 for (size_t i = 0; i < h; i++)
793 {
794 fwrite((void *)src, 1, (size_t)(w * 3), fp);
795 src += inc;
796 }
797
799 fclose(fp);
800 }
801
802 return true;
803}
804
805#if defined (YARP_HAS_ZLIB)
806bool SaveFloatCompressed(char* src, const char* filename, size_t h, size_t w, size_t rowSize)
807{
808 size_t sizeDataOriginal=w*h*sizeof(float);
809 size_t sizeDataCompressed = (sizeDataOriginal * 1.1) + 12;
811
813 switch (z_result)
814 {
815 case Z_OK:
816 break;
817
818 case Z_MEM_ERROR:
819 yCError(IMAGEFILE, "zlib compression: out of memory");
820 return false;
821 break;
822
823 case Z_BUF_ERROR:
824 yCError(IMAGEFILE, "zlib compression: output buffer wasn't large enough");
825 return false;
826 break;
827 }
828
829 FILE* fp = fopen(filename, "wb");
830 if (!fp)
831 {
832 yCError(IMAGEFILE, "Unable to open file");
833 return false;
834 }
835
836 size_t bw = 0;
837 size_t dims[2] = { h,w };
838
839 if (fwrite(dims, sizeof(dims), 1, fp) > 0) {
841 }
842
843 fclose(fp);
844 return (bw > 0);
845}
846#endif
847
848bool SaveFloatRaw(char* src, const char* filename, size_t h, size_t w, size_t rowSize)
849{
850 FILE* fp = fopen(filename, "wb");
851 if (!fp)
852 {
853 yCError(IMAGEFILE, "Unable to open file");
854 return false;
855 }
856
857 size_t dims[2] = { h,w };
858
859 size_t bw = 0;
860 size_t size_ = sizeof(float);
861 auto count_ = (size_t)(dims[0] * dims[1]);
862
863 if (fwrite(dims, sizeof(dims), 1, fp) > 0) {
864 bw = fwrite((void*)src, size_, count_, fp);
865 }
866
867 fclose(fp);
868 return (bw > 0);
869}
870
871bool ImageWriteJPG(ImageOf<PixelRgb>& img, const char *filename)
872{
873 return SaveJPG((char*)img.getRawImage(), filename, img.height(), img.width(), img.getRowSize());
874}
875
876bool ImageWritePNG(ImageOf<PixelRgb>& img, const char *filename)
877{
878#if defined (YARP_HAS_PNG)
879 return SavePNG((char*)img.getRawImage(), filename, img.height(), img.width(), img.getRowSize(), PNG_COLOR_TYPE_RGB, 8);
880#else
881 yCError(IMAGEFILE) << "YARP was not built with png support";
882 return false;
883#endif
884}
885
886bool ImageWritePNG(ImageOf<PixelMono>& img, const char *filename)
887{
888#if defined (YARP_HAS_PNG)
889 return SavePNG((char*)img.getRawImage(), filename, img.height(), img.width(), img.getRowSize(), PNG_COLOR_TYPE_GRAY, 8);
890#else
891 yCError(IMAGEFILE) << "YARP was not built with png support";
892 return false;
893#endif
894}
895
896bool ImageWriteRGB(ImageOf<PixelRgb>& img, const char *filename)
897{
898 return SavePPM((char*)img.getRawImage(),filename,img.height(),img.width(),img.getRowSize());
899}
900
901bool ImageWriteMono(ImageOf<PixelMono>& img, const char *filename)
902{
903 return SavePGM((char*)img.getRawImage(), filename, img.height(), img.width(), img.getRowSize());
904}
905
906bool ImageWriteFloat_PlainHeaderless(ImageOf<PixelFloat>& img, const char *filename)
907{
908 return SaveFloatRaw((char*)img.getRawImage(), filename, img.height(), img.width(), img.getRowSize());
909}
910
911bool ImageWriteFloat_CompressedHeaderless(ImageOf<PixelFloat>& img, const char* filename)
912{
913#if defined (YARP_HAS_ZLIB)
914 return SaveFloatCompressed((char*)img.getRawImage(), filename, img.height(), img.width(), img.getRowSize());
915#else
916 yCError(IMAGEFILE) << "YARP was not built with zlib support";
917 return false;
918#endif
919}
920} // namespace
921
923// public read methods
925
926bool file::read(ImageOf<PixelRgb> & dest, const std::string& src, image_fileformat format)
927{
928 const char* file_ext = strrchr(src.c_str(), '.');
929 if (file_ext==nullptr)
930 {
931 yCError(IMAGEFILE) << "cannot find file extension in file name";
932 return false;
933 }
934
935 if (strcmp(file_ext, ".pgm")==0 ||
936 strcmp(file_ext, ".ppm")==0 ||
937 format == FORMAT_PGM ||
938 format == FORMAT_PPM)
939 {
940 return ImageReadRGB_PxM(dest,src.c_str());
941 }
942 else if(strcmp(file_ext, ".png")==0 ||
943 format == FORMAT_PNG)
944 {
945 return ImageReadRGB_PNG(dest, src.c_str());
946 }
947 else if(strcmp(file_ext, ".jpg") == 0 ||
948 strcmp(file_ext, ".jpeg") == 0 ||
949 format == FORMAT_JPG)
950 {
951 return ImageReadRGB_JPG(dest, src.c_str());
952 }
953 yCError(IMAGEFILE) << "unsupported file format";
954 return false;
955}
956
957
958bool file::read(ImageOf<PixelBgr> & dest, const std::string& src, image_fileformat format)
959{
960 const char* file_ext = strrchr(src.c_str(), '.');
961 if (file_ext == nullptr)
962 {
963 yCError(IMAGEFILE) << "cannot find file extension in file name";
964 return false;
965 }
966
967 if (strcmp(file_ext, ".pgm") == 0 ||
968 strcmp(file_ext, ".ppm") == 0 ||
969 format == FORMAT_PGM ||
970 format == FORMAT_PPM)
971 {
972 return ImageReadBGR_PxM(dest, src.c_str());
973 }
974 else if (strcmp(file_ext, ".png") == 0 ||
975 format == FORMAT_PNG)
976 {
977 return ImageReadBGR_PNG(dest, src.c_str());
978 }
979 else if (strcmp(file_ext, ".jpg") == 0 ||
980 strcmp(file_ext, ".jpeg") == 0 ||
981 format == FORMAT_JPG)
982 {
983 return ImageReadBGR_JPG(dest, src.c_str());
984 }
985 yCError(IMAGEFILE) << "unsupported file format";
986 return false;
987}
988
989
990bool file::read(ImageOf<PixelRgba> & dest, const std::string& src, image_fileformat format)
991{
992 const char* file_ext = strrchr(src.c_str(), '.');
993 if (file_ext == nullptr)
994 {
995 yCError(IMAGEFILE) << "cannot find file extension in file name";
996 return false;
997 }
998
999 if (strcmp(file_ext, ".pgm") == 0 ||
1000 strcmp(file_ext, ".ppm") == 0 ||
1001 format == FORMAT_PGM ||
1002 format == FORMAT_PPM)
1003 {
1005 bool ok = ImageReadRGB_PxM(img2, src.c_str());
1006 if (ok)
1007 {
1008 dest.copy(img2);
1009 }
1010 return ok;
1011 }
1012 else if (strcmp(file_ext, ".png") == 0 ||
1013 format == FORMAT_PNG)
1014 {
1016 bool ok = ImageReadRGB_PNG(img2, src.c_str());
1017 if (ok)
1018 {
1019 dest.copy(img2);
1020 }
1021 return ok;
1022 }
1023 else if (strcmp(file_ext, ".jpg") == 0 ||
1024 strcmp(file_ext, ".jpeg") == 0 ||
1025 format == FORMAT_JPG)
1026 {
1028 bool ok = ImageReadRGB_JPG(img2, src.c_str());
1029 if (ok)
1030 {
1031 dest.copy(img2);
1032 }
1033 return ok;
1034 }
1035 yCError(IMAGEFILE) << "unsupported file format";
1036 return false;
1037}
1038
1039bool file::read(ImageOf<PixelMono> & dest, const std::string& src, image_fileformat format)
1040{
1041 const char* file_ext = strrchr(src.c_str(), '.');
1042 if (file_ext == nullptr)
1043 {
1044 yCError(IMAGEFILE) << "cannot find file extension in file name";
1045 return false;
1046 }
1047
1048 if (strcmp(file_ext, ".pgm") == 0 ||
1049 strcmp(file_ext, ".ppm") == 0 ||
1050 format == FORMAT_PGM ||
1051 format == FORMAT_PPM)
1052 {
1053 return ImageReadMono_PxM(dest, src.c_str());
1054 }
1055 else if (strcmp(file_ext, ".png") == 0 ||
1056 format == FORMAT_PNG)
1057 {
1058 return ImageReadMono_PNG(dest, src.c_str());
1059 }
1060 else if (strcmp(file_ext, ".jpg") == 0 ||
1061 strcmp(file_ext, ".jpeg") == 0 ||
1062 format == FORMAT_JPG)
1063 {
1064 return ImageReadMono_JPG(dest, src.c_str());
1065 }
1066 yCError(IMAGEFILE) << "unsupported file format";
1067 return false;
1068}
1069
1070bool file::read(ImageOf<PixelFloat>& dest, const std::string& src, image_fileformat format)
1071{
1072 const char* file_ext = strrchr(src.c_str(), '.');
1073 if (file_ext == nullptr)
1074 {
1075 yCError(IMAGEFILE) << "cannot find file extension in file name";
1076 return false;
1077 }
1078
1079 if (strcmp(file_ext, ".float") == 0 ||
1080 format == FORMAT_NUMERIC)
1081 {
1082 return ImageReadFloat_PlainHeaderless(dest, src);
1083 }
1084 else if (strcmp(file_ext, ".floatzip") == 0 ||
1085 format == FORMAT_NUMERIC_COMPRESSED)
1086 {
1087#if defined (YARP_HAS_ZLIB)
1088 return ImageReadFloat_CompressedHeaderless(dest, src);
1089#else
1090 yCError(IMAGEFILE) << "YARP was not built with zlib support";
1091 return false;
1092#endif
1093 }
1094 yCError(IMAGEFILE) << "unsupported file format";
1095 return false;
1096}
1097
1101
1102bool file::write(const ImageOf<PixelRgb> & src, const std::string& dest, image_fileformat format)
1103{
1104 if (format == FORMAT_PPM)
1105 {
1106 return ImageWriteRGB(const_cast<ImageOf<PixelRgb> &>(src), dest.c_str());
1107 }
1108 else if (format == FORMAT_JPG)
1109 {
1110 return ImageWriteJPG(const_cast<ImageOf<PixelRgb> &>(src), dest.c_str());
1111 }
1112 else if (format == FORMAT_PNG)
1113 {
1114 return ImageWritePNG(const_cast<ImageOf<PixelRgb> &>(src), dest.c_str());
1115 }
1116 else
1117 {
1118 yCError(IMAGEFILE) << "Invalid format, operation not supported";
1119 return false;
1120 }
1121}
1122
1123bool file::write(const ImageOf<PixelBgr> & src, const std::string& dest, image_fileformat format)
1124{
1126 imgRGB.copy(src);
1127 if (format == FORMAT_PPM)
1128 {
1129 return ImageWriteRGB(const_cast<ImageOf<PixelRgb> &>(imgRGB), dest.c_str());
1130 }
1131 else if (format == FORMAT_JPG)
1132 {
1133 return ImageWriteJPG(const_cast<ImageOf<PixelRgb> &>(imgRGB), dest.c_str());
1134 }
1135 else if (format == FORMAT_PNG)
1136 {
1137 return ImageWritePNG(const_cast<ImageOf<PixelRgb> &>(imgRGB), dest.c_str());
1138 }
1139 else
1140 {
1141 yCError(IMAGEFILE) << "Invalid format, operation not supported";
1142 return false;
1143 }
1144}
1145
1146
1147bool file::write(const ImageOf<PixelRgba> & src, const std::string& dest, image_fileformat format)
1148{
1150 imgRGB.copy(src);
1151 if (format == FORMAT_PPM)
1152 {
1153 return ImageWriteRGB(const_cast<ImageOf<PixelRgb> &>(imgRGB), dest.c_str());
1154 }
1155 else if (format == FORMAT_JPG)
1156 {
1157 return ImageWriteJPG(const_cast<ImageOf<PixelRgb> &>(imgRGB), dest.c_str());
1158 }
1159 else if (format == FORMAT_PNG)
1160 {
1161 return ImageWritePNG(const_cast<ImageOf<PixelRgb> &>(imgRGB), dest.c_str());
1162 }
1163 else
1164 {
1165 yCError(IMAGEFILE) << "Invalid format, operation not supported";
1166 return false;
1167 }
1168}
1169
1170
1171bool file::write(const ImageOf<PixelMono> & src, const std::string& dest, image_fileformat format)
1172{
1173 if (format == FORMAT_PGM)
1174 {
1175 return ImageWriteMono(const_cast<ImageOf<PixelMono> &>(src), dest.c_str());
1176 }
1177 else if (format == FORMAT_PNG)
1178 {
1179 return ImageWritePNG(const_cast<ImageOf<PixelMono> &>(src), dest.c_str());
1180 }
1181 else
1182 {
1183 yCError(IMAGEFILE) << "Invalid format, operation not supported";
1184 return false;
1185 }
1186}
1187
1188bool file::write(const ImageOf<PixelFloat>& src, const std::string& dest, image_fileformat format)
1189{
1190 if (format == FORMAT_NUMERIC)
1191 {
1192 return ImageWriteFloat_PlainHeaderless(const_cast<ImageOf<PixelFloat> &>(src), dest.c_str());
1193 }
1194 else if (format == FORMAT_NUMERIC_COMPRESSED)
1195 {
1196 return ImageWriteFloat_CompressedHeaderless(const_cast<ImageOf<PixelFloat>&>(src), dest.c_str());
1197 }
1198 else
1199 {
1200 yCError(IMAGEFILE) << "Invalid format, operation not supported";
1201 return false;
1202 }
1203}
1204
1205bool file::write(const Image& src, const std::string& dest, image_fileformat format)
1206{
1207 int code=src.getPixelCode();
1208 if (code == VOCAB_PIXEL_MONO)
1209 {
1210 return write(static_cast<const ImageOf<PixelMono>&>(src), dest, format);
1211 }
1212 else if (code == VOCAB_PIXEL_MONO_FLOAT)
1213 {
1214 return write(static_cast<const ImageOf<PixelFloat>&>(src), dest, format);
1215 }
1216 else if (code == VOCAB_PIXEL_BGR)
1217 {
1218 return write(static_cast<const ImageOf<PixelBgr>&>(src), dest, format);
1219 }
1220 else if (code == VOCAB_PIXEL_RGB)
1221 {
1222 return write(static_cast<const ImageOf<PixelRgb>&>(src), dest, format);
1223 }
1224 else if (code == VOCAB_PIXEL_RGBA)
1225 {
1226 return write(static_cast<const ImageOf<PixelRgba>&>(src), dest, format);
1227 }
1228 else
1229 {
1231 img.copy(src);
1232 return write(img,dest);
1233 }
1234}
1235
1236
@ VOCAB_PIXEL_RGBA
Definition Image.h:45
@ VOCAB_PIXEL_BGR
Definition Image.h:49
@ VOCAB_PIXEL_MONO_FLOAT
Definition Image.h:53
@ VOCAB_PIXEL_MONO
Definition Image.h:42
@ VOCAB_PIXEL_RGB
Definition Image.h:44
#define yError(...)
Definition Log.h:361
A mini-server for performing network communication in the background.
void write(bool forceStrict=false)
Write the current object being returned by BufferedPort::prepare.
Typed image class.
Definition Image.h:603
Base class for storing images.
Definition Image.h:79
virtual int getPixelCode() const
Gets pixel type identifier.
Definition Image.cpp:375
#define yCError(component,...)
#define yCWarning(component,...)
#define YARP_LOG_COMPONENT(name,...)
STL namespace.
An interface to the operating system, including Port based communication.
Definition OSTestMsg.cpp:13
bool read(ImageOf< PixelRgb > &dest, const std::string &src, image_fileformat format=FORMAT_ANY)
@ FORMAT_NUMERIC_COMPRESSED
Definition ImageFile.h:22
bool write(const ImageOf< PixelRgb > &src, const std::string &dest, image_fileformat format=FORMAT_PPM)