44 #include <visp3/core/vpIoTools.h>
45 #include <visp3/io/vpImageIo.h>
47 #include "private/vpImageIoBackend.h"
49 vpImageIo::vpImageFormatType vpImageIo::getFormat(
const std::string &filename)
51 std::string ext = vpImageIo::getExtension(filename);
53 if (ext.compare(
".PGM") == 0)
55 else if (ext.compare(
".pgm") == 0)
57 else if (ext.compare(
".PPM") == 0)
59 else if (ext.compare(
".ppm") == 0)
61 else if (ext.compare(
".JPG") == 0)
63 else if (ext.compare(
".jpg") == 0)
65 else if (ext.compare(
".JPEG") == 0)
67 else if (ext.compare(
".jpeg") == 0)
69 else if (ext.compare(
".PNG") == 0)
71 else if (ext.compare(
".png") == 0)
74 else if (ext.compare(
".TIFF") == 0)
76 else if (ext.compare(
".tiff") == 0)
78 else if (ext.compare(
".TIF") == 0)
80 else if (ext.compare(
".tif") == 0)
82 else if (ext.compare(
".BMP") == 0)
84 else if (ext.compare(
".bmp") == 0)
86 else if (ext.compare(
".DIB") == 0)
88 else if (ext.compare(
".dib") == 0)
90 else if (ext.compare(
".PBM") == 0)
92 else if (ext.compare(
".pbm") == 0)
94 else if (ext.compare(
".SR") == 0)
96 else if (ext.compare(
".sr") == 0)
98 else if (ext.compare(
".RAS") == 0)
100 else if (ext.compare(
".ras") == 0)
101 return FORMAT_RASTER;
102 else if (ext.compare(
".JP2") == 0)
103 return FORMAT_JPEG2000;
104 else if (ext.compare(
".jp2") == 0)
105 return FORMAT_JPEG2000;
107 return FORMAT_UNKNOWN;
111 std::string vpImageIo::getExtension(
const std::string &filename)
114 size_t dot = filename.find_last_of(
".");
115 std::string ext = filename.substr(dot, filename.size() - 1);
152 std::string message =
"Cannot read file: \"" + std::string(filename) +
"\" doesn't exist";
159 bool try_opencv_reader =
false;
161 switch (getFormat(final_filename)) {
169 readJPEG(I, final_filename, backend);
172 readPNG(I, final_filename, backend);
179 case FORMAT_JPEG2000:
181 try_opencv_reader =
true;
185 if (try_opencv_reader) {
186 #if defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100
187 readOpenCV(I, filename);
189 std::string message =
"Cannot read file \"" + filename +
"\": No backend able to support this image format";
228 std::string message =
"Cannot read file: \"" + std::string(filename) +
"\" doesn't exist";
234 bool try_opencv_reader =
false;
236 switch (getFormat(final_filename)) {
244 readJPEG(I, final_filename, backend);
247 readPNG(I, final_filename, backend);
254 case FORMAT_JPEG2000:
256 try_opencv_reader =
true;
260 if (try_opencv_reader) {
261 #if defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100
262 readOpenCV(I, filename);
264 std::string message =
"Cannot read file \"" + filename +
"\": No backend able to support this image format";
294 bool try_opencv_writer =
false;
296 switch (getFormat(filename)) {
314 case FORMAT_JPEG2000:
316 try_opencv_writer =
true;
320 if (try_opencv_writer) {
321 #if defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100
322 writeOpenCV(I, filename, 90);
324 std::string message =
"Cannot write file \"" + filename +
"\": No backend able to support this image format";
354 bool try_opencv_writer =
false;
356 switch (getFormat(filename)) {
374 case FORMAT_JPEG2000:
376 try_opencv_writer =
true;
380 if (try_opencv_writer) {
381 #if defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100
382 writeOpenCV(I, filename, 90);
384 std::string message =
"Cannot write file \"" + filename +
"\": No backend able to support this image format";
403 #if !defined(VISP_HAVE_JPEG)
404 std::string message =
405 "Libjpeg backend is not available to read file \"" + filename +
"\": switch to stb_image backend";
409 #if !(defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100)
410 std::string message =
411 "OpenCV backend is not available to read file \"" + filename +
"\": switch to stb_image backend";
415 #if defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100
417 #elif defined(VISP_HAVE_JPEG)
425 #if defined(VISP_HAVE_JPEG)
426 readJPEGLibjpeg(I, filename);
429 #if defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100
430 readOpenCV(I, filename);
433 readStb(I, filename);
435 readSimdlib(I, filename);
452 #if !defined(VISP_HAVE_JPEG)
453 std::string message =
454 "Libjpeg backend is not available to read file \"" + filename +
"\": switch to stb_image backend";
458 #if !(defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100)
459 std::string message =
460 "OpenCV backend is not available to read file \"" + filename +
"\": switch to stb_image backend";
464 #if defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100
466 #elif defined(VISP_HAVE_JPEG)
474 #if defined(VISP_HAVE_JPEG)
475 readJPEGLibjpeg(I, filename);
478 #if defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100
479 readOpenCV(I, filename);
482 readStb(I, filename);
484 readSimdlib(I, filename);
501 #if !defined(VISP_HAVE_PNG)
502 std::string message =
503 "Libpng backend is not available to read file \"" + filename +
"\": switch to stb_image backend";
507 #if !(defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100)
508 std::string message =
509 "OpenCV backend is not available to read file \"" + filename +
"\": switch to stb_image backend";
513 #if defined(VISP_HAVE_PNG)
515 #elif defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100
523 #if defined(VISP_HAVE_PNG)
524 readPNGLibpng(I, filename);
527 #if defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100
528 readOpenCV(I, filename);
531 readStb(I, filename);
533 readSimdlib(I, filename);
550 #if !defined(VISP_HAVE_PNG)
551 std::string message =
552 "Libpng backend is not available to read file \"" + filename +
"\": switch to stb_image backend";
556 #if !(defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100)
557 std::string message =
558 "OpenCV backend is not available to read file \"" + filename +
"\": switch to stb_image backend";
562 #if defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100
570 #if defined(VISP_HAVE_PNG)
571 readPNGLibpng(I, filename);
574 #if defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100
575 readOpenCV(I, filename);
578 readStb(I, filename);
580 readSimdlib(I, filename);
595 std::string message =
596 "This backend cannot read file \"" + filename +
"\": switch to the default TinyEXR backend";
599 #if !(defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100)
600 std::string message =
601 "OpenCV backend is not available to read file \"" + filename +
"\": switch to the default TinyEXR backend";
607 #if defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100
608 readOpenCV(I, filename);
611 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
612 readEXRTiny(I, filename);
615 std::string message =
616 "TinyEXR backend is not available to read file \"" + filename +
"\": cxx standard should be greater or equal to cxx11";
633 std::string message =
634 "This backend cannot read file \"" + filename +
"\": switch to the default TinyEXR backend";
637 #if !(defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100)
638 std::string message =
639 "OpenCV backend is not available to read file \"" + filename +
"\": switch to the default TinyEXR backend";
645 #if defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100
646 readOpenCV(I, filename);
649 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
650 readEXRTiny(I, filename);
653 std::string message =
654 "TinyEXR backend is not available to read file \"" + filename +
"\": cxx standard should be greater or equal to cxx11";
674 #if !defined(VISP_HAVE_JPEG)
675 std::string message =
"Libjpeg backend is not available to save file \"" + filename +
"\": switch to simd backend";
679 #if !(defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100)
680 std::string message =
"OpenCV backend is not available to save file \"" + filename +
"\": switch to simd backend";
684 #if defined(VISP_HAVE_JPEG)
686 #elif defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100
694 #if defined(VISP_HAVE_JPEG)
695 writeJPEGLibjpeg(I, filename, quality);
698 #if defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100
699 writeOpenCV(I, filename, quality);
702 writeJPEGSimdlib(I, filename, quality);
704 writeJPEGStb(I, filename, quality);
722 #if !defined(VISP_HAVE_JPEG)
723 std::string message =
"Libjpeg backend is not available to save file \"" + filename +
"\": switch to simd backend";
727 #if !(defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100)
728 std::string message =
"OpenCV backend is not available to save file \"" + filename +
"\": switch to simd backend";
732 #if defined(VISP_HAVE_JPEG)
734 #elif defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100
742 #if defined(VISP_HAVE_JPEG)
743 writeJPEGLibjpeg(I, filename, quality);
746 #if defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100
747 writeOpenCV(I, filename, quality);
750 writeJPEGSimdlib(I, filename, quality);
752 writeJPEGStb(I, filename, quality);
769 #if !defined(VISP_HAVE_PNG)
770 std::string message =
"Libpng backend is not available to save file \"" + filename +
"\": switch to simd backend";
774 #if !(defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100)
775 std::string message =
"OpenCV backend is not available to save file \"" + filename +
"\": switch to simd backend";
779 #if defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100
787 #if defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100
788 writeOpenCV(I, filename, 90);
791 writePNGSimdlib(I, filename);
793 writePNGStb(I, filename);
795 #if defined(VISP_HAVE_PNG)
796 writePNGLibpng(I, filename);
814 #if !defined(VISP_HAVE_PNG)
815 std::string message =
"Libpng backend is not available to save file \"" + filename +
"\": switch to simd backend";
819 #if !(defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100)
820 std::string message =
"OpenCV backend is not available to save file \"" + filename +
"\": switch to simd backend";
824 #if defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100
832 #if defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100
833 writeOpenCV(I, filename, 90);
836 writePNGSimdlib(I, filename);
838 writePNGStb(I, filename);
840 #if defined(VISP_HAVE_PNG)
841 writePNGLibpng(I, filename);
857 std::string message =
858 "This backend cannot save file \"" + filename +
"\": switch to the default TinyEXR backend";
861 #if !(defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100)
863 std::string message =
864 "OpenCV backend is not available to save file \"" + filename +
"\": switch to the default TinyEXR backend";
870 #if defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100
871 writeOpenCV(I, filename);
874 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
875 writeEXRTiny(I, filename);
877 std::string message =
878 "TinyEXR backend is not available to save file \"" + filename +
"\": cxx standard should be greater or equal to cxx11";
895 std::string message =
896 "This backend cannot save file \"" + filename +
"\": switch to the default TinyEXR backend";
899 #if !(defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100)
901 std::string message =
902 "OpenCV backend is not available to save file \"" + filename +
"\": switch to the default TinyEXR backend";
908 #if defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100
909 writeOpenCV(I, filename);
912 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
913 writeEXRTiny(I, filename);
915 std::string message =
916 "TinyEXR backend is not available to save file \"" + filename +
"\": cxx standard should be greater or equal to cxx11";
Error that can be emited by the vpImage class and its derivates.
static void writePFM(const vpImage< float > &I, const std::string &filename)
static void readPGM(vpImage< unsigned char > &I, const std::string &filename)
@ IO_STB_IMAGE_BACKEND
Use embedded stb_image library.
@ IO_DEFAULT_BACKEND
Default backend.
@ IO_SIMDLIB_BACKEND
Use embedded simd library.
@ IO_SYSTEM_LIB_BACKEND
Use system libraries like libpng or libjpeg.
@ IO_OPENCV_BACKEND
Use OpenCV.
static void writeJPEG(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND, int quality=90)
static void readJPEG(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
static void readPFM(vpImage< float > &I, const std::string &filename)
static void writePNG(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
static void readPFM_HDR(vpImage< float > &I, const std::string &filename)
static void readPPM(vpImage< unsigned char > &I, const std::string &filename)
static void writePFM_HDR(const vpImage< float > &I, const std::string &filename)
static void readEXR(vpImage< float > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
static void writePGM(const vpImage< unsigned char > &I, const std::string &filename)
static void writeEXR(const vpImage< float > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
static void readPNG(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
static void writePPM(const vpImage< unsigned char > &I, const std::string &filename)
static void write(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)