39 #include <visp3/core/vpIoTools.h>
40 #include <visp3/io/vpImageIo.h>
42 #include "private/vpImageIoBackend.h"
44 vpImageIo::vpImageFormatType vpImageIo::getFormat(
const std::string &filename)
46 std::string ext = vpImageIo::getExtension(filename);
48 if (ext.compare(
".PGM") == 0)
50 else if (ext.compare(
".pgm") == 0)
52 else if (ext.compare(
".PPM") == 0)
54 else if (ext.compare(
".ppm") == 0)
56 else if (ext.compare(
".JPG") == 0)
58 else if (ext.compare(
".jpg") == 0)
60 else if (ext.compare(
".JPEG") == 0)
62 else if (ext.compare(
".jpeg") == 0)
64 else if (ext.compare(
".PNG") == 0)
66 else if (ext.compare(
".png") == 0)
69 else if (ext.compare(
".TIFF") == 0)
71 else if (ext.compare(
".tiff") == 0)
73 else if (ext.compare(
".TIF") == 0)
75 else if (ext.compare(
".tif") == 0)
77 else if (ext.compare(
".BMP") == 0)
79 else if (ext.compare(
".bmp") == 0)
81 else if (ext.compare(
".DIB") == 0)
83 else if (ext.compare(
".dib") == 0)
85 else if (ext.compare(
".PBM") == 0)
87 else if (ext.compare(
".pbm") == 0)
89 else if (ext.compare(
".SR") == 0)
91 else if (ext.compare(
".sr") == 0)
93 else if (ext.compare(
".RAS") == 0)
95 else if (ext.compare(
".ras") == 0)
97 else if (ext.compare(
".JP2") == 0)
98 return FORMAT_JPEG2000;
99 else if (ext.compare(
".jp2") == 0)
100 return FORMAT_JPEG2000;
102 return FORMAT_UNKNOWN;
106 std::string vpImageIo::getExtension(
const std::string &filename)
109 size_t dot = filename.find_last_of(
".");
110 std::string ext = filename.substr(dot, filename.size() - 1);
147 std::string message =
"Cannot read file: \"" + std::string(filename) +
"\" doesn't exist";
154 bool try_opencv_reader =
false;
156 switch (getFormat(final_filename)) {
164 readJPEG(I, final_filename, backend);
167 readPNG(I, final_filename, backend);
174 case FORMAT_JPEG2000:
176 try_opencv_reader =
true;
180 if (try_opencv_reader) {
181 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
182 readOpenCV(I, filename);
184 std::string message =
"Cannot read file \"" + filename +
"\": No backend able to support this image format";
223 std::string message =
"Cannot read file: \"" + std::string(filename) +
"\" doesn't exist";
229 bool try_opencv_reader =
false;
231 switch (getFormat(final_filename)) {
239 readJPEG(I, final_filename, backend);
242 readPNG(I, final_filename, backend);
249 case FORMAT_JPEG2000:
251 try_opencv_reader =
true;
255 if (try_opencv_reader) {
256 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
257 readOpenCV(I, filename);
259 std::string message =
"Cannot read file \"" + filename +
"\": No backend able to support this image format";
289 bool try_opencv_writer =
false;
291 switch (getFormat(filename)) {
309 case FORMAT_JPEG2000:
311 try_opencv_writer =
true;
315 if (try_opencv_writer) {
316 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
317 writeOpenCV(I, filename, 90);
319 std::string message =
"Cannot write file \"" + filename +
"\": No backend able to support this image format";
349 bool try_opencv_writer =
false;
351 switch (getFormat(filename)) {
369 case FORMAT_JPEG2000:
371 try_opencv_writer =
true;
375 if (try_opencv_writer) {
376 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
377 writeOpenCV(I, filename, 90);
379 std::string message =
"Cannot write file \"" + filename +
"\": No backend able to support this image format";
398 #if !defined(VISP_HAVE_JPEG)
399 std::string message =
400 "Libjpeg backend is not available to read file \"" + filename +
"\": switch to stb_image backend";
404 #if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
405 std::string message =
406 "OpenCV backend is not available to read file \"" + filename +
"\": switch to stb_image backend";
410 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
412 #elif defined(VISP_HAVE_JPEG)
420 #if defined(VISP_HAVE_JPEG)
421 readJPEGLibjpeg(I, filename);
424 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
425 readOpenCV(I, filename);
428 readStb(I, filename);
430 readSimdlib(I, filename);
447 #if !defined(VISP_HAVE_JPEG)
448 std::string message =
449 "Libjpeg backend is not available to read file \"" + filename +
"\": switch to stb_image backend";
453 #if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
454 std::string message =
455 "OpenCV backend is not available to read file \"" + filename +
"\": switch to stb_image backend";
459 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS)
461 #elif defined(VISP_HAVE_JPEG)
469 #if defined(VISP_HAVE_JPEG)
470 readJPEGLibjpeg(I, filename);
473 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
474 readOpenCV(I, filename);
477 readStb(I, filename);
479 readSimdlib(I, filename);
496 #if !defined(VISP_HAVE_PNG)
497 std::string message =
498 "Libpng backend is not available to read file \"" + filename +
"\": switch to stb_image backend";
502 #if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
503 std::string message =
504 "OpenCV backend is not available to read file \"" + filename +
"\": switch to stb_image backend";
508 #if defined(VISP_HAVE_PNG)
510 #elif defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS)
518 #if defined(VISP_HAVE_PNG)
519 readPNGLibpng(I, filename);
522 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
523 readOpenCV(I, filename);
526 readStb(I, filename);
528 readSimdlib(I, filename);
545 #if !defined(VISP_HAVE_PNG)
546 std::string message =
547 "Libpng backend is not available to read file \"" + filename +
"\": switch to stb_image backend";
551 #if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
552 std::string message =
553 "OpenCV backend is not available to read file \"" + filename +
"\": switch to stb_image backend";
557 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
565 #if defined(VISP_HAVE_PNG)
566 readPNGLibpng(I, filename);
569 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
570 readOpenCV(I, filename);
573 readStb(I, filename);
575 readSimdlib(I, filename);
590 std::string message =
591 "This backend cannot read file \"" + filename +
"\": switch to the default TinyEXR backend";
594 #if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
595 std::string message =
596 "OpenCV backend is not available to read file \"" + filename +
"\": switch to the default TinyEXR backend";
602 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
603 readOpenCV(I, filename);
606 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
607 readEXRTiny(I, filename);
610 std::string message =
611 "TinyEXR backend is not available to read file \"" + filename +
"\": cxx standard should be greater or equal to cxx11";
628 std::string message =
629 "This backend cannot read file \"" + filename +
"\": switch to the default TinyEXR backend";
632 #if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
633 std::string message =
634 "OpenCV backend is not available to read file \"" + filename +
"\": switch to the default TinyEXR backend";
640 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
641 readOpenCV(I, filename);
644 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
645 readEXRTiny(I, filename);
648 std::string message =
649 "TinyEXR backend is not available to read file \"" + filename +
"\": cxx standard should be greater or equal to cxx11";
669 #if !defined(VISP_HAVE_JPEG)
670 std::string message =
"Libjpeg backend is not available to save file \"" + filename +
"\": switch to simd backend";
674 #if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
675 std::string message =
"OpenCV backend is not available to save file \"" + filename +
"\": switch to simd backend";
679 #if defined(VISP_HAVE_JPEG)
681 #elif defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS)
689 #if defined(VISP_HAVE_JPEG)
690 writeJPEGLibjpeg(I, filename, quality);
693 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
694 writeOpenCV(I, filename, quality);
697 writeJPEGSimdlib(I, filename, quality);
699 writeJPEGStb(I, filename, quality);
717 #if !defined(VISP_HAVE_JPEG)
718 std::string message =
"Libjpeg backend is not available to save file \"" + filename +
"\": switch to simd backend";
722 #if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
723 std::string message =
"OpenCV backend is not available to save file \"" + filename +
"\": switch to simd backend";
727 #if defined(VISP_HAVE_JPEG)
729 #elif defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS)
737 #if defined(VISP_HAVE_JPEG)
738 writeJPEGLibjpeg(I, filename, quality);
741 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
742 writeOpenCV(I, filename, quality);
745 writeJPEGSimdlib(I, filename, quality);
747 writeJPEGStb(I, filename, quality);
764 #if !defined(VISP_HAVE_PNG)
765 std::string message =
"Libpng backend is not available to save file \"" + filename +
"\": switch to simd backend";
769 #if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
770 std::string message =
"OpenCV backend is not available to save file \"" + filename +
"\": switch to simd backend";
774 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
782 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
783 writeOpenCV(I, filename, 90);
786 writePNGSimdlib(I, filename);
788 writePNGStb(I, filename);
790 #if defined(VISP_HAVE_PNG)
791 writePNGLibpng(I, filename);
809 #if !defined(VISP_HAVE_PNG)
810 std::string message =
"Libpng backend is not available to save file \"" + filename +
"\": switch to simd backend";
814 #if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
815 std::string message =
"OpenCV backend is not available to save file \"" + filename +
"\": switch to simd backend";
819 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
827 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
828 writeOpenCV(I, filename, 90);
831 writePNGSimdlib(I, filename);
833 writePNGStb(I, filename);
835 #if defined(VISP_HAVE_PNG)
836 writePNGLibpng(I, filename);
852 std::string message =
853 "This backend cannot save file \"" + filename +
"\": switch to the default TinyEXR backend";
856 #if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
858 std::string message =
859 "OpenCV backend is not available to save file \"" + filename +
"\": switch to the default TinyEXR backend";
865 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
866 writeOpenCV(I, filename);
869 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
870 writeEXRTiny(I, filename);
872 std::string message =
873 "TinyEXR backend is not available to save file \"" + filename +
"\": cxx standard should be greater or equal to cxx11";
890 std::string message =
891 "This backend cannot save file \"" + filename +
"\": switch to the default TinyEXR backend";
894 #if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
896 std::string message =
897 "OpenCV backend is not available to save file \"" + filename +
"\": switch to the default TinyEXR backend";
903 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
904 writeOpenCV(I, filename);
907 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
908 writeEXRTiny(I, filename);
910 std::string message =
911 "TinyEXR backend is not available to save file \"" + filename +
"\": cxx standard should be greater or equal to cxx11";
Error that can be emitted by the vpImage class and its derivatives.
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)