Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
vpImageIo.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2025 by Inria. All rights reserved.
4 *
5 * This software is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * See the file LICENSE.txt at the root directory of this source
10 * distribution for additional information about the GNU GPL.
11 *
12 * For using ViSP with software that can not be combined with the GNU
13 * GPL, please contact Inria about acquiring a ViSP Professional
14 * Edition License.
15 *
16 * See https://visp.inria.fr for more information.
17 *
18 * This software was developed at:
19 * Inria Rennes - Bretagne Atlantique
20 * Campus Universitaire de Beaulieu
21 * 35042 Rennes Cedex
22 * France
23 *
24 * If you have questions regarding the use of this file, please contact
25 * Inria at visp@inria.fr
26 *
27 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29 *
30 * Description:
31 * Read/write images.
32 */
33
38
39#include <visp3/core/vpIoTools.h>
40#include <visp3/io/vpImageIo.h>
41
42#include "private/vpImageIoBackend.h"
43
44#ifdef ENABLE_VISP_NAMESPACE
45using namespace VISP_NAMESPACE_NAME;
46#endif
47
48vpImageIo::vpImageFormatType vpImageIo::getFormat(const std::string &filename)
49{
50 std::string ext = vpIoTools::toLowerCase(vpIoTools::getFileExtension(filename));
51
52 if (ext.find("pgm") != std::string::npos)
53 return FORMAT_PGM;
54 else if (ext.find("ppm") != std::string::npos)
55 return FORMAT_PPM;
56 else if (ext.find("jpg") != std::string::npos)
57 return FORMAT_JPEG;
58 else if (ext.find("jpeg") != std::string::npos)
59 return FORMAT_JPEG;
60 else if (ext.find("png") != std::string::npos)
61 return FORMAT_PNG;
62 // Formats supported by opencv
63 else if (ext.find("tiff") != std::string::npos)
64 return FORMAT_TIFF;
65 else if (ext.find("tif") != std::string::npos)
66 return FORMAT_TIFF;
67 else if (ext.find("bmp") != std::string::npos)
68 return FORMAT_BMP;
69 else if (ext.find("dib") != std::string::npos)
70 return FORMAT_DIB;
71 else if (ext.find("pbm") != std::string::npos)
72 return FORMAT_PBM;
73 else if (ext.find("sr") != std::string::npos)
74 return FORMAT_RASTER;
75 else if (ext.find("ras") != std::string::npos)
76 return FORMAT_RASTER;
77 else if (ext.find("jp2") != std::string::npos)
78 return FORMAT_JPEG2000;
79 else if (ext.find("exr") != std::string::npos)
80 return FORMAT_EXR;
81 else if (ext.find("pfm") != std::string::npos)
82 return FORMAT_PFM;
83 else
84 return FORMAT_UNKNOWN;
85}
86
116void vpImageIo::read(vpImage<unsigned char> &I, const std::string &filename, int backend)
117{
118 bool exist = vpIoTools::checkFilename(filename);
119 if (!exist) {
120 const std::string message = "Cannot read file: \"" + std::string(filename) + "\" doesn't exist";
122 }
123
124 // Allows to use ~ symbol or env variables in path
125 std::string final_filename = vpIoTools::path(filename);
126
127 bool try_opencv_reader = false;
128
129 switch (getFormat(final_filename)) {
130 case FORMAT_PGM:
131 readPGM(I, final_filename);
132 break;
133 case FORMAT_PPM:
134 readPPM(I, final_filename);
135 break;
136 case FORMAT_JPEG:
137 readJPEG(I, final_filename, backend);
138 break;
139 case FORMAT_PNG:
140 readPNG(I, final_filename, backend);
141 break;
142 case FORMAT_TIFF:
143 case FORMAT_BMP:
144 case FORMAT_DIB:
145 case FORMAT_PBM:
146 case FORMAT_RASTER:
147 case FORMAT_JPEG2000:
148 case FORMAT_UNKNOWN:
149 try_opencv_reader = true;
150 break;
151 case FORMAT_EXR:
152 case FORMAT_PFM:
153 throw(vpException(vpException::badValue, "vpImage<uchar> cannot be used with file '%s' extension does not match", final_filename.c_str()));
154 }
155
156 if (try_opencv_reader) {
157#if defined(VISP_HAVE_OPENCV) && \
158 (((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || \
159 ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC)))
160 readOpenCV(I, filename);
161#else
162 const std::string message = "Cannot read file \"" + filename + "\": No backend able to support this image format";
164#endif
165 }
166}
167
197void vpImageIo::read(vpImage<vpRGBa> &I, const std::string &filename, int backend)
198{
199 bool exist = vpIoTools::checkFilename(filename);
200 if (!exist) {
201 const std::string message = "Cannot read file: \"" + std::string(filename) + "\" doesn't exist";
203 }
204 // Allows to use ~ symbol or env variables in path
205 std::string final_filename = vpIoTools::path(filename);
206
207 bool try_opencv_reader = false;
208
209 switch (getFormat(final_filename)) {
210 case FORMAT_PGM:
211 readPGM(I, final_filename);
212 break;
213 case FORMAT_PPM:
214 readPPM(I, final_filename);
215 break;
216 case FORMAT_JPEG:
217 readJPEG(I, final_filename, backend);
218 break;
219 case FORMAT_PNG:
220 readPNG(I, final_filename, backend);
221 break;
222 case FORMAT_TIFF:
223 case FORMAT_BMP:
224 case FORMAT_DIB:
225 case FORMAT_PBM:
226 case FORMAT_RASTER:
227 case FORMAT_JPEG2000:
228 case FORMAT_UNKNOWN:
229 try_opencv_reader = true;
230 break;
231 default:
232 throw(vpException(vpException::badValue, "vpImage<vpRGBa> cannot be used with file '%s', extension does not match.", final_filename.c_str()));
233 }
234
235 if (try_opencv_reader) {
236#if defined(VISP_HAVE_OPENCV) && \
237 (((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || \
238 ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC)))
239 readOpenCV(I, filename);
240#else
241 const std::string message = "Cannot read file \"" + filename + "\": No backend able to support this image format";
243#endif
244 }
245}
246
266void vpImageIo::read(vpImage<float> &I, const std::string &filename)
267{
268 bool exist = vpIoTools::checkFilename(filename);
269 if (!exist) {
270 const std::string message = "Cannot read file: \"" + std::string(filename) + "\" doesn't exist";
272 }
273
274 // Allows to use ~ symbol or env variables in path
275 std::string final_filename = vpIoTools::path(filename);
276
277 bool try_opencv_reader = false;
278
279 switch (getFormat(final_filename)) {
280 case FORMAT_EXR:
281#ifdef VISP_HAVE_OPENCV
282 try_opencv_reader = true;
283#else
284#if defined(VISP_HAVE_TINYEXR)
285 readEXRTiny(I, filename);
286#else
287 throw(vpException(vpException::ioError, "Trying to read EXR files when neither OpenCV nor Tiny EXR is installed"));
288#endif
289#endif
290 break;
291 case FORMAT_PFM:
292 readPFM(I, filename);
293 break;
294 case FORMAT_TIFF:
295 case FORMAT_UNKNOWN:
296 try_opencv_reader = true;
297 break;
298 default:
299 throw(vpException(vpException::ioError, "Extension of file %s does not match a valid format for vpImage<float>", final_filename.c_str()));
300 }
301
302 if (try_opencv_reader) {
303#if defined(VISP_HAVE_OPENCV) && \
304 (((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || \
305 ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC)))
306 readOpenCV(I, filename);
307#else
308 const std::string message = "Cannot read file \"" + filename + "\": No backend able to support this image format";
310#endif
311 }
312}
313
336void vpImageIo::write(const vpImage<unsigned char> &I, const std::string &filename, int backend)
337{
338 bool try_opencv_writer = false;
339
340 switch (getFormat(filename)) {
341 case FORMAT_PGM:
342 writePGM(I, filename);
343 break;
344 case FORMAT_PPM:
345 writePPM(I, filename);
346 break;
347 case FORMAT_JPEG:
348 writeJPEG(I, filename, backend);
349 break;
350 case FORMAT_PNG:
351 writePNG(I, filename, backend);
352 break;
353 case FORMAT_TIFF:
354 case FORMAT_BMP:
355 case FORMAT_DIB:
356 case FORMAT_PBM:
357 case FORMAT_RASTER:
358 case FORMAT_JPEG2000:
359 case FORMAT_UNKNOWN:
360 default:
361 try_opencv_writer = true;
362 break;
363 }
364
365 if (try_opencv_writer) {
366#if defined(VISP_HAVE_OPENCV) && \
367 (((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || \
368 ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC)))
369 writeOpenCV(I, filename, 90);
370#else
371 const std::string message = "Cannot write file \"" + filename + "\": No backend able to support this image format";
373#endif
374 }
375}
376
399void vpImageIo::write(const vpImage<vpRGBa> &I, const std::string &filename, int backend)
400{
401 bool try_opencv_writer = false;
402
403 switch (getFormat(filename)) {
404 case FORMAT_PGM:
405 writePGM(I, filename);
406 break;
407 case FORMAT_PPM:
408 writePPM(I, filename);
409 break;
410 case FORMAT_JPEG:
411 writeJPEG(I, filename, backend);
412 break;
413 case FORMAT_PNG:
414 writePNG(I, filename, backend);
415 break;
416 case FORMAT_TIFF:
417 case FORMAT_BMP:
418 case FORMAT_DIB:
419 case FORMAT_PBM:
420 case FORMAT_RASTER:
421 case FORMAT_JPEG2000:
422 case FORMAT_UNKNOWN:
423 default:
424 try_opencv_writer = true;
425 break;
426 }
427
428 if (try_opencv_writer) {
429#if defined(VISP_HAVE_OPENCV) && \
430 (((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || \
431 ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC)))
432 writeOpenCV(I, filename, 90);
433#else
434 const std::string message = "Cannot write file \"" + filename + "\": No backend able to support this image format";
436#endif
437 }
438}
439
448// Strategy based on benchmark: see https://github.com/lagadic/visp/pull/1004
449// Default: 1. opencv, 2. system, 3. stb_image
450void vpImageIo::readJPEG(vpImage<unsigned char> &I, const std::string &filename, int backend)
451{
452 if (backend == IO_SYSTEM_LIB_BACKEND) {
453#if !defined(VISP_HAVE_JPEG)
454 // Libjpeg backend is not available to read file \"" + filename + "\": switch to stb_image backend
455 backend = IO_STB_IMAGE_BACKEND;
456#endif
457 }
458 else if (backend == IO_OPENCV_BACKEND) {
459#if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
460 // OpenCV backend is not available to read file \"" + filename + "\": switch to stb_image backend
461 backend = IO_STB_IMAGE_BACKEND;
462#endif
463 }
464 else if (backend == IO_DEFAULT_BACKEND) {
465#if defined(VISP_HAVE_OPENCV) && \
466 (((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || \
467 ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC)))
468 backend = IO_OPENCV_BACKEND;
469#elif defined(VISP_HAVE_JPEG)
470 backend = IO_SYSTEM_LIB_BACKEND;
471#elif defined(VISP_HAVE_SIMDLIB)
472 backend = IO_SIMDLIB_BACKEND;
473#elif defined(VISP_HAVE_STBIMAGE)
474 backend = IO_STB_IMAGE_BACKEND;
475#else
476 (void)I;
477 (void)filename;
478 const std::string message = "Cannot read file \"" + filename + "\": no backend available";
480#endif
481 }
482
483 if (backend == IO_SYSTEM_LIB_BACKEND) {
484#if defined(VISP_HAVE_JPEG)
485 readJPEGLibjpeg(I, filename);
486#else
487 (void)I;
488 (void)filename;
489 const std::string message = "Cannot read file \"" + filename + "\": jpeg library backend is not available";
491#endif
492 }
493 else if (backend == IO_OPENCV_BACKEND) {
494#if defined(VISP_HAVE_OPENCV) && \
495 (((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || \
496 ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC)))
497 readOpenCV(I, filename);
498#else
499 (void)I;
500 (void)filename;
501 const std::string message = "Cannot read file \"" + filename + "\": OpenCV library backend is not available";
503#endif
504 }
505 else if (backend == IO_STB_IMAGE_BACKEND) {
506#if defined(VISP_HAVE_STBIMAGE)
507 readStb(I, filename);
508#else
509 (void)I;
510 (void)filename;
511 const std::string message = "Cannot read file \"" + filename + "\": stb_image backend is not available";
513#endif
514 }
515 else if (backend == IO_SIMDLIB_BACKEND) {
516#if defined(VISP_HAVE_SIMDLIB)
517 readSimdlib(I, filename);
518#else
519 (void)I;
520 (void)filename;
521 const std::string message = "Cannot read file \"" + filename + "\": Simd library backend is not available";
523#endif
524 }
525}
526
535// Strategy based on benchmark: see https://github.com/lagadic/visp/pull/1004
536// Default: 1. opencv, 2. system, 3. stb_image
537void vpImageIo::readJPEG(vpImage<vpRGBa> &I, const std::string &filename, int backend)
538{
539 if (backend == IO_SYSTEM_LIB_BACKEND) {
540#if !defined(VISP_HAVE_JPEG)
541 // Libjpeg backend is not available to read file \"" + filename + "\": switch to stb_image backend";
542 backend = IO_STB_IMAGE_BACKEND;
543#endif
544 }
545 else if (backend == IO_OPENCV_BACKEND) {
546#if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
547 // OpenCV backend is not available to read file \"" + filename + "\": switch to stb_image backend";
548 backend = IO_STB_IMAGE_BACKEND;
549#endif
550 }
551 else if (backend == IO_DEFAULT_BACKEND) {
552#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS)
553 backend = IO_OPENCV_BACKEND;
554#elif defined(VISP_HAVE_JPEG)
555 backend = IO_SYSTEM_LIB_BACKEND;
556#elif defined(VISP_HAVE_SIMDLIB)
557 backend = IO_SIMDLIB_BACKEND;
558#elif defined(VISP_HAVE_STBIMAGE)
559 backend = IO_STB_IMAGE_BACKEND;
560#else
561 (void)I;
562 (void)filename;
563 const std::string message = "Cannot read file \"" + filename + "\": no backend available";
565#endif
566 }
567
568 if (backend == IO_SYSTEM_LIB_BACKEND) {
569#if defined(VISP_HAVE_JPEG)
570 readJPEGLibjpeg(I, filename);
571#else
572 (void)I;
573 (void)filename;
574 const std::string message = "Cannot read file \"" + filename + "\": jpeg library backend is not available";
576#endif
577 }
578 else if (backend == IO_OPENCV_BACKEND) {
579#if defined(VISP_HAVE_OPENCV) && \
580 (((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || \
581 ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC)))
582 readOpenCV(I, filename);
583#else
584 (void)I;
585 (void)filename;
586 const std::string message = "Cannot read file \"" + filename + "\": OpenCV library backend is not available";
588#endif
589 }
590 else if (backend == IO_STB_IMAGE_BACKEND) {
591#if defined(VISP_HAVE_STBIMAGE)
592 readStb(I, filename);
593#else
594 (void)I;
595 (void)filename;
596 const std::string message = "Cannot read file \"" + filename + "\": stb_image backend is not available";
598#endif
599 }
600 else if (backend == IO_SIMDLIB_BACKEND) {
601#if defined(VISP_HAVE_SIMDLIB)
602 readSimdlib(I, filename);
603#else
604 (void)I;
605 (void)filename;
606 const std::string message = "Cannot read file \"" + filename + "\": Simd library backend is not available";
608#endif
609 }
610}
611
620// Strategy based on benchmark: see https://github.com/lagadic/visp/pull/1004
621// Default: 1. system, 2. opencv, 3. stb_image
622void vpImageIo::readPNG(vpImage<unsigned char> &I, const std::string &filename, int backend)
623{
624 if (backend == IO_SYSTEM_LIB_BACKEND) {
625#if !defined(VISP_HAVE_PNG)
626 // Libpng backend is not available to read file \"" + filename + "\": switch to stb_image backend";
627 backend = IO_STB_IMAGE_BACKEND;
628#endif
629 }
630 else if (backend == IO_OPENCV_BACKEND) {
631#if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
632 // OpenCV backend is not available to read file \"" + filename + "\": switch to stb_image backend";
633 backend = IO_STB_IMAGE_BACKEND;
634#endif
635 }
636 else if (backend == IO_DEFAULT_BACKEND) {
637#if defined(VISP_HAVE_PNG)
638 backend = IO_SYSTEM_LIB_BACKEND;
639#elif defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS)
640 backend = IO_OPENCV_BACKEND;
641#elif defined(VISP_HAVE_SIMDLIB)
642 backend = IO_SIMDLIB_BACKEND;
643#elif defined(VISP_HAVE_STBIMAGE)
644 backend = IO_STB_IMAGE_BACKEND;
645#else
646 (void)I;
647 (void)filename;
648 const std::string message = "Cannot read file \"" + filename + "\": no backend available";
650#endif
651 }
652
653 if (backend == IO_SYSTEM_LIB_BACKEND) {
654#if defined(VISP_HAVE_PNG)
655 readPNGLibpng(I, filename);
656#else
657 (void)I;
658 (void)filename;
659 const std::string message = "Cannot read file \"" + filename + "\": png library backend is not available";
661#endif
662 }
663 else if (backend == IO_OPENCV_BACKEND) {
664#if defined(VISP_HAVE_OPENCV) && \
665 (((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || \
666 ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC)))
667 readOpenCV(I, filename);
668#else
669 (void)I;
670 (void)filename;
671 const std::string message = "Cannot read file \"" + filename + "\": OpenCV library backend is not available";
673#endif
674 }
675 else if (backend == IO_STB_IMAGE_BACKEND) {
676#if defined(VISP_HAVE_STBIMAGE)
677 readStb(I, filename);
678#else
679 (void)I;
680 (void)filename;
681 const std::string message = "Cannot read file \"" + filename + "\": stb_image backend is not available";
683#endif
684 }
685 else if (backend == IO_SIMDLIB_BACKEND) {
686#if defined(VISP_HAVE_SIMDLIB)
687 readSimdlib(I, filename);
688#else
689 (void)I;
690 (void)filename;
691 const std::string message = "Cannot read file \"" + filename + "\": Simd library backend is not available";
693#endif
694 }
695}
696
705// Strategy based on benchmark: see https://github.com/lagadic/visp/pull/1004
706// Default: 1. opencv, 2. stb_image
707void vpImageIo::readPNG(vpImage<vpRGBa> &I, const std::string &filename, int backend)
708{
709 if (backend == IO_SYSTEM_LIB_BACKEND) {
710#if !defined(VISP_HAVE_PNG)
711 // Libpng backend is not available to read file \"" + filename + "\": switch to stb_image backend";
712 backend = IO_STB_IMAGE_BACKEND;
713#endif
714 }
715 else if (backend == IO_OPENCV_BACKEND) {
716#if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
717 // OpenCV backend is not available to read file \"" + filename + "\": switch to stb_image backend";
718 backend = IO_STB_IMAGE_BACKEND;
719#endif
720 }
721 else if (backend == IO_DEFAULT_BACKEND) {
722#if defined(VISP_HAVE_OPENCV) && \
723 (((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || \
724 ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC)))
725 backend = IO_OPENCV_BACKEND;
726#elif defined(VISP_HAVE_SIMDLIB)
727 backend = IO_SIMDLIB_BACKEND;
728#elif defined(VISP_HAVE_STBIMAGE)
729 backend = IO_STB_IMAGE_BACKEND;
730#else
731 (void)I;
732 (void)filename;
733 const std::string message = "Cannot read file \"" + filename + "\": no backend available";
735#endif
736 }
737
738 if (backend == IO_SYSTEM_LIB_BACKEND) {
739#if defined(VISP_HAVE_PNG)
740 readPNGLibpng(I, filename);
741#else
742 (void)I;
743 (void)filename;
744 const std::string message = "Cannot read file \"" + filename + "\": png library backend is not available";
746#endif
747 }
748 else if (backend == IO_OPENCV_BACKEND) {
749#if defined(VISP_HAVE_OPENCV) && \
750 (((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || \
751 ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC)))
752 readOpenCV(I, filename);
753#else
754 (void)I;
755 (void)filename;
756 const std::string message = "Cannot read file \"" + filename + "\": OpenCV library backend is not available";
758#endif
759 }
760 else if (backend == IO_STB_IMAGE_BACKEND) {
761#if defined(VISP_HAVE_STBIMAGE)
762 readStb(I, filename);
763#else
764 (void)I;
765 (void)filename;
766 const std::string message = "Cannot read file \"" + filename + "\": stb_image backend is not available";
768#endif
769 }
770 else if (backend == IO_SIMDLIB_BACKEND) {
771#if defined(VISP_HAVE_SIMDLIB)
772 readSimdlib(I, filename);
773#else
774 (void)I;
775 (void)filename;
776 const std::string message = "Cannot read file \"" + filename + "\": Simd library backend is not available";
778#endif
779 }
780}
781
790void vpImageIo::readEXR(vpImage<float> &I, const std::string &filename, int backend)
791{
792 if (backend == IO_SYSTEM_LIB_BACKEND || backend == IO_SIMDLIB_BACKEND || backend == IO_STB_IMAGE_BACKEND) {
793 // This backend cannot read file \"" + filename + "\": switch to the default TinyEXR backend
794 backend = IO_DEFAULT_BACKEND;
795 }
796 else if (backend == IO_OPENCV_BACKEND) {
797#if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
798 // OpenCV backend is not available to read file \"" + filename + "\": switch to the default TinyEXR backend
799 backend = IO_DEFAULT_BACKEND;
800#endif
801 }
802 else if (backend == IO_DEFAULT_BACKEND) {
803#if !defined(VISP_HAVE_TINYEXR)
804 // TinyEXR backend is not available to read file \"" + filename + "\": switch to the OpenCV backend
805 backend = IO_OPENCV_BACKEND;
806#endif
807 }
808
809 if (backend == IO_OPENCV_BACKEND) {
810#if defined(VISP_HAVE_OPENCV) && \
811 (((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || \
812 ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC)))
813 readOpenCV(I, filename);
814#else
815 (void)I;
816 (void)filename;
817 const std::string message = "Cannot read file \"" + filename + "\": OpenCV backend is not available";
819#endif
820 }
821 else if (backend == IO_DEFAULT_BACKEND) {
822#if defined(VISP_HAVE_TINYEXR)
823 readEXRTiny(I, filename);
824#else
825 (void)I;
826 (void)filename;
827 const std::string message = "Cannot read file \"" + filename + "\": Default TinyEXR backend is not available";
829#endif
830 }
831}
832
841void vpImageIo::readEXR(vpImage<vpRGBf> &I, const std::string &filename, int backend)
842{
843 if (backend == IO_SYSTEM_LIB_BACKEND || backend == IO_SIMDLIB_BACKEND || backend == IO_STB_IMAGE_BACKEND) {
844 // This backend cannot read file \"" + filename + "\": switch to the default TinyEXR backend
845 backend = IO_DEFAULT_BACKEND;
846 }
847 else if (backend == IO_OPENCV_BACKEND) {
848#if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
849 // OpenCV backend is not available to read file \"" + filename + "\": switch to the default TinyEXR backend
850 backend = IO_DEFAULT_BACKEND;
851#endif
852 }
853 else if (backend == IO_DEFAULT_BACKEND) {
854#if !defined(VISP_HAVE_TINYEXR)
855 // TinyEXR backend is not available to read file \"" + filename + "\": switch to the OpenCV backend
856 backend = IO_OPENCV_BACKEND;
857#endif
858 }
859
860 if (backend == IO_OPENCV_BACKEND) {
861#if defined(VISP_HAVE_OPENCV) && \
862 (((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || \
863 ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC)))
864 readOpenCV(I, filename);
865#else
866 (void)I;
867 (void)filename;
868 const std::string message = "Cannot read file \"" + filename + "\": OpenCV backend is not available";
870#endif
871 }
872 else if (backend == IO_DEFAULT_BACKEND) {
873#if defined(VISP_HAVE_TINYEXR)
874 readEXRTiny(I, filename);
875#else
876 (void)I;
877 (void)filename;
878 const std::string message = "Cannot read file \"" + filename + "\": TinyEXR backend is not available";
880#endif
881 }
882}
883
893// Strategy based on benchmark: see https://github.com/lagadic/visp/pull/1004
894// Default: 1. system, 2. opencv, 3. simd
895void vpImageIo::writeJPEG(const vpImage<unsigned char> &I, const std::string &filename, int backend, int quality)
896{
897 if (backend == IO_SYSTEM_LIB_BACKEND) {
898#if !defined(VISP_HAVE_JPEG)
899#if defined(VISP_HAVE_SIMDLIB)
900 // Libjpeg backend is not available to save file \"" + filename + "\": switch to simd backend
901 backend = IO_SIMDLIB_BACKEND;
902#else
903 // Libjpeg backend is not available to save file \"" + filename + "\": switch to stb_image backend
904 backend = IO_STB_IMAGE_BACKEND;
905#endif
906#endif
907 }
908 else if (backend == IO_OPENCV_BACKEND) {
909#if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
910#if defined(VISP_HAVE_SIMDLIB)
911 // OpenCV backend is not available to save file \"" + filename + "\": switch to simd backend
912 backend = IO_SIMDLIB_BACKEND;
913#else
914 // OpenCV backend is not available to save file \"" + filename + "\": switch to stb_image backend
915 backend = IO_STB_IMAGE_BACKEND;
916#endif
917#endif
918 }
919 else if (backend == IO_DEFAULT_BACKEND) {
920#if defined(VISP_HAVE_JPEG)
921 backend = IO_SYSTEM_LIB_BACKEND;
922#elif defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS)
923 backend = IO_OPENCV_BACKEND;
924#elif defined(VISP_HAVE_SIMDLIB)
925 backend = IO_SIMDLIB_BACKEND;
926#elif defined(VISP_HAVE_STBIMAGE)
927 backend = IO_STB_IMAGE_BACKEND;
928#else
929 (void)I;
930 (void)filename;
931 const std::string message = "Cannot save file \"" + filename + "\": no available backend";
933#endif
934 }
935
936 if (backend == IO_SYSTEM_LIB_BACKEND) {
937#if defined(VISP_HAVE_JPEG)
938 writeJPEGLibjpeg(I, filename, quality);
939#else
940 (void)I;
941 (void)filename;
942 (void)quality;
943 const std::string message = "Cannot save file \"" + filename + "\": jpeg backend is not available";
945#endif
946 }
947 else if (backend == IO_OPENCV_BACKEND) {
948#if defined(VISP_HAVE_OPENCV) && \
949 (((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || \
950 ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC)))
951 writeOpenCV(I, filename, quality);
952#else
953 (void)I;
954 (void)filename;
955 (void)quality;
956 const std::string message = "Cannot save file \"" + filename + "\": OpenCV backend is not available";
958#endif
959 }
960 else if (backend == IO_SIMDLIB_BACKEND) {
961#if defined(VISP_HAVE_SIMDLIB)
962 writeJPEGSimdlib(I, filename, quality);
963#else
964 (void)I;
965 (void)filename;
966 (void)quality;
967 const std::string message = "Cannot save file \"" + filename + "\": Simd library backend is not available";
969#endif
970 }
971 else if (backend == IO_STB_IMAGE_BACKEND) {
972#if defined(VISP_HAVE_STBIMAGE)
973 writeJPEGStb(I, filename, quality);
974#else
975 (void)I;
976 (void)filename;
977 (void)quality;
978 const std::string message = "Cannot save file \"" + filename + "\": stb_image backend is not available";
980#endif
981 }
982}
983
993// Strategy based on benchmark: see https://github.com/lagadic/visp/pull/1004
994// Default: 1. system, 2. opencv, , 3. simd
995void vpImageIo::writeJPEG(const vpImage<vpRGBa> &I, const std::string &filename, int backend, int quality)
996{
997 if (backend == IO_SYSTEM_LIB_BACKEND) {
998#if !defined(VISP_HAVE_JPEG)
999#if defined(VISP_HAVE_SIMDLIB)
1000 // Libjpeg backend is not available to save file \"" + filename + "\": switch to simd backend
1001 backend = IO_SIMDLIB_BACKEND;
1002#else
1003 // Libjpeg backend is not available to save file \"" + filename + "\": switch to stb_image backend
1004 backend = IO_STB_IMAGE_BACKEND;
1005#endif
1006#endif
1007 }
1008 else if (backend == IO_OPENCV_BACKEND) {
1009#if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
1010#if defined(VISP_HAVE_SIMDLIB)
1011 // OpenCV backend is not available to save file \"" + filename + "\": switch to simd backend
1012 backend = IO_SIMDLIB_BACKEND;
1013#else
1014 // OpenCV backend is not available to save file \"" + filename + "\": switch to stb_image backend
1015 backend = IO_STB_IMAGE_BACKEND;
1016#endif
1017#endif
1018 }
1019 else if (backend == IO_DEFAULT_BACKEND) {
1020#if defined(VISP_HAVE_JPEG)
1021 backend = IO_SYSTEM_LIB_BACKEND;
1022#elif defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS)
1023 backend = IO_OPENCV_BACKEND;
1024#elif defined(VISP_HAVE_SIMDLIB)
1025 backend = IO_SIMDLIB_BACKEND;
1026#elif defined(VISP_HAVE_STBIMAGE)
1027 backend = IO_STB_IMAGE_BACKEND;
1028#else
1029 (void)I;
1030 (void)filename;
1031 const std::string message = "Cannot save file \"" + filename + "\": no backend available";
1033#endif
1034 }
1035
1036 if (backend == IO_SYSTEM_LIB_BACKEND) {
1037#if defined(VISP_HAVE_JPEG)
1038 writeJPEGLibjpeg(I, filename, quality);
1039#else
1040 (void)I;
1041 (void)filename;
1042 (void)quality;
1043 const std::string message = "Cannot save file \"" + filename + "\": jpeg library backend is not available";
1045#endif
1046 }
1047 else if (backend == IO_OPENCV_BACKEND) {
1048#if defined(VISP_HAVE_OPENCV) && \
1049 (((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || \
1050 ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC)))
1051 writeOpenCV(I, filename, quality);
1052#else
1053 (void)I;
1054 (void)filename;
1055 (void)quality;
1056 const std::string message = "Cannot save file \"" + filename + "\": OpenCV library backend is not available";
1058#endif
1059 }
1060 else if (backend == IO_SIMDLIB_BACKEND) {
1061#if defined(VISP_HAVE_SIMDLIB)
1062 writeJPEGSimdlib(I, filename, quality);
1063#else
1064 (void)I;
1065 (void)filename;
1066 (void)quality;
1067 const std::string message = "Cannot save file \"" + filename + "\": Simd library backend is not available";
1069#endif
1070 }
1071 else if (backend == IO_STB_IMAGE_BACKEND) {
1072#if defined(VISP_HAVE_STBIMAGE)
1073 writeJPEGStb(I, filename, quality);
1074#else
1075 (void)I;
1076 (void)filename;
1077 (void)quality;
1078 const std::string message = "Cannot save file \"" + filename + "\": stb_image backend is not available";
1080#endif
1081 }
1082}
1083
1092// Strategy based on benchmark: see https://github.com/lagadic/visp/pull/1004
1093// Default: 1. opencv, 2. simd
1094void vpImageIo::writePNG(const vpImage<unsigned char> &I, const std::string &filename, int backend)
1095{
1096 if (backend == IO_SYSTEM_LIB_BACKEND) {
1097#if !defined(VISP_HAVE_PNG)
1098#if defined(VISP_HAVE_SIMDLIB)
1099 // Libpng backend is not available to save file \"" + filename + "\": switch to simd backend
1100 backend = IO_SIMDLIB_BACKEND;
1101#else
1102 // Libpng backend is not available to save file \"" + filename + "\": switch to stb_image backend
1103 backend = IO_STB_IMAGE_BACKEND;
1104#endif
1105#endif
1106 }
1107 else if (backend == IO_OPENCV_BACKEND) {
1108#if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
1109#if defined(VISP_HAVE_SIMDLIB)
1110 // OpenCV backend is not available to save file \"" + filename + "\": switch to simd backend
1111 backend = IO_SIMDLIB_BACKEND;
1112#else
1113 // OpenCV backend is not available to save file \"" + filename + "\": switch to stb_image backend
1114 backend = IO_STB_IMAGE_BACKEND;
1115#endif
1116#endif
1117 }
1118 else if (backend == IO_DEFAULT_BACKEND) {
1119#if defined(VISP_HAVE_OPENCV) && \
1120 (((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || \
1121 ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC)))
1122 backend = IO_OPENCV_BACKEND;
1123#elif defined(VISP_HAVE_SIMDLIB)
1124 backend = IO_SIMDLIB_BACKEND;
1125#elif defined(VISP_HAVE_STBIMAGE)
1126 backend = IO_STB_IMAGE_BACKEND;
1127#else
1128 (void)I;
1129 (void)filename;
1130 const std::string message = "Cannot save file \"" + filename + "\": no backend available";
1132#endif
1133 }
1134
1135 if (backend == IO_OPENCV_BACKEND) {
1136#if defined(VISP_HAVE_OPENCV) && \
1137 (((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || \
1138 ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC)))
1139 writeOpenCV(I, filename, 90);
1140#else
1141 (void)I;
1142 (void)filename;
1143 const std::string message = "Cannot save file \"" + filename + "\": OpenCV library backend is not available";
1145#endif
1146 }
1147 else if (backend == IO_SIMDLIB_BACKEND) {
1148#if defined(VISP_HAVE_SIMDLIB)
1149 writePNGSimdlib(I, filename);
1150#else
1151 (void)I;
1152 (void)filename;
1153 const std::string message = "Cannot save file \"" + filename + "\": Simd library backend is not available";
1155#endif
1156 }
1157 else if (backend == IO_STB_IMAGE_BACKEND) {
1158#if defined(VISP_HAVE_STBIMAGE)
1159 writePNGStb(I, filename);
1160#else
1161 (void)I;
1162 (void)filename;
1163 const std::string message = "Cannot save file \"" + filename + "\": stb_image backend is not available";
1165#endif
1166 }
1167 else if (backend == IO_SYSTEM_LIB_BACKEND) {
1168#if defined(VISP_HAVE_PNG)
1169 writePNGLibpng(I, filename);
1170#else
1171 (void)I;
1172 (void)filename;
1173 const std::string message = "Cannot save file \"" + filename + "\": png library backend is not available";
1175#endif
1176 }
1177}
1178
1187// Strategy based on benchmark: see https://github.com/lagadic/visp/pull/1004
1188// Default: 1. opencv, 2. system, 3. simd
1189void vpImageIo::writePNG(const vpImage<vpRGBa> &I, const std::string &filename, int backend)
1190{
1191 if (backend == IO_SYSTEM_LIB_BACKEND) {
1192#if !defined(VISP_HAVE_PNG)
1193#if defined(VISP_HAVE_SIMDLIB)
1194 // Libpng backend is not available to save file \"" + filename + "\": switch to simd backend
1195 backend = IO_SIMDLIB_BACKEND;
1196#else
1197 // Libpng backend is not available to save file \"" + filename + "\": switch to stb_image backend
1198 backend = IO_STB_IMAGE_BACKEND;
1199#endif
1200#endif
1201 }
1202 else if (backend == IO_OPENCV_BACKEND) {
1203#if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
1204#if defined(VISP_HAVE_SIMDLIB)
1205 // OpenCV backend is not available to save file \"" + filename + "\": switch to simd backend
1206 backend = IO_SIMDLIB_BACKEND;
1207#else
1208 // OpenCV backend is not available to save file \"" + filename + "\": switch to stb_image backend
1209 backend = IO_STB_IMAGE_BACKEND;
1210#endif
1211#endif
1212 }
1213 else if (backend == IO_DEFAULT_BACKEND) {
1214#if defined(VISP_HAVE_OPENCV) && \
1215 (((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || \
1216 ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC)))
1217 backend = IO_OPENCV_BACKEND;
1218#elif defined(VISP_HAVE_SIMDLIB)
1219 backend = IO_SIMDLIB_BACKEND;
1220#elif defined(VISP_HAVE_STBIMAGE)
1221 backend = IO_STB_IMAGE_BACKEND;
1222#else
1223 (void)I;
1224 (void)filename;
1225 const std::string message = "Cannot save file \"" + filename + "\": no backend available";
1227#endif
1228 }
1229
1230 if (backend == IO_OPENCV_BACKEND) {
1231#if defined(VISP_HAVE_OPENCV) && \
1232 (((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || \
1233 ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC)))
1234 writeOpenCV(I, filename, 90);
1235#else
1236 (void)I;
1237 (void)filename;
1238 const std::string message = "Cannot save file \"" + filename + "\": OpenCV backend is not available";
1240#endif
1241 }
1242 else if (backend == IO_SIMDLIB_BACKEND) {
1243#if defined(VISP_HAVE_SIMDLIB)
1244 writePNGSimdlib(I, filename);
1245#else
1246 (void)I;
1247 (void)filename;
1248 const std::string message = "Cannot save file \"" + filename + "\": Simd library backend is not available";
1250#endif
1251 }
1252 else if (backend == IO_STB_IMAGE_BACKEND) {
1253#if defined(VISP_HAVE_STBIMAGE)
1254 writePNGStb(I, filename);
1255#else
1256 (void)I;
1257 (void)filename;
1258 const std::string message = "Cannot save file \"" + filename + "\": stb_image backend is not available";
1260#endif
1261 }
1262 else if (backend == IO_SYSTEM_LIB_BACKEND) {
1263#if defined(VISP_HAVE_PNG)
1264 writePNGLibpng(I, filename);
1265#else
1266 (void)I;
1267 (void)filename;
1268 const std::string message = "Cannot save file \"" + filename + "\": libpng backend is not available";
1270#endif
1271 }
1272}
1273
1282void vpImageIo::writeEXR(const vpImage<float> &I, const std::string &filename, int backend)
1283{
1284 if (backend == IO_SYSTEM_LIB_BACKEND || backend == IO_SIMDLIB_BACKEND || backend == IO_STB_IMAGE_BACKEND) {
1285 // This backend cannot save file \"" + filename + "\": switch to the default TinyEXR backend
1286 backend = IO_DEFAULT_BACKEND;
1287 }
1288 else if (backend == IO_OPENCV_BACKEND) {
1289#if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
1290 (void)I;
1291 // OpenCV backend is not available to save file \"" + filename + "\": switch to the default TinyEXR backend
1292 backend = IO_DEFAULT_BACKEND;
1293#endif
1294 }
1295 else if (backend == IO_DEFAULT_BACKEND) {
1296#if !defined(VISP_HAVE_TINYEXR)
1297 // TinyEXR backend is not available to save file \"" + filename + "\": switch to the OpenCV backend
1298 backend = IO_OPENCV_BACKEND;
1299#endif
1300 }
1301
1302 if (backend == IO_OPENCV_BACKEND) {
1303#if defined(VISP_HAVE_OPENCV) && \
1304 (((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || \
1305 ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC)))
1306 writeOpenCV(I, filename);
1307#else
1308 (void)I;
1309 (void)filename;
1310 const std::string message = "Cannot save file \"" + filename + "\": OpenCV backend is not available";
1312#endif
1313 }
1314 else if (backend == IO_DEFAULT_BACKEND) {
1315#if defined(VISP_HAVE_TINYEXR)
1316 writeEXRTiny(I, filename);
1317#else
1318 (void)I;
1319 (void)filename;
1320 const std::string message = "Cannot save file \"" + filename + "\": TinyEXR backend is not available";
1322#endif
1323 }
1324}
1325
1334void vpImageIo::writeEXR(const vpImage<vpRGBf> &I, const std::string &filename, int backend)
1335{
1336 if (backend == IO_SYSTEM_LIB_BACKEND || backend == IO_SIMDLIB_BACKEND || backend == IO_STB_IMAGE_BACKEND) {
1337 // This backend cannot save file \"" + filename + "\": switch to the default TinyEXR backend
1338 backend = IO_DEFAULT_BACKEND;
1339 }
1340 else if (backend == IO_OPENCV_BACKEND) {
1341#if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
1342 // OpenCV backend is not available to save file \"" + filename + "\": switch to the default TinyEXR backend
1343 backend = IO_DEFAULT_BACKEND;
1344#endif
1345 }
1346 else if (backend == IO_DEFAULT_BACKEND) {
1347#if !defined(VISP_HAVE_TINYEXR)
1348 // TinyEXR backend is not available to save file \"" + filename + "\": switch to the OpenCV backend
1349 backend = IO_OPENCV_BACKEND;
1350#endif
1351 }
1352
1353 if (backend == IO_OPENCV_BACKEND) {
1354#if defined(VISP_HAVE_OPENCV) && \
1355 (((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || \
1356 ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC)))
1357 writeOpenCV(I, filename);
1358#else
1359 (void)I;
1360 (void)filename;
1361 const std::string message = "Cannot save file \"" + filename + "\": OpenCV backend is not available";
1363#endif
1364 }
1365 else if (backend == IO_DEFAULT_BACKEND) {
1366#if defined(VISP_HAVE_TINYEXR)
1367 writeEXRTiny(I, filename);
1368#else
1369 (void)I;
1370 (void)filename;
1371 const std::string message = "Cannot save file \"" + filename + "\": TinyEXR backend is not available";
1373#endif
1374 }
1375}
1376
1382void vpImageIo::writePFM(const vpImage<float> &I, const std::string &filename) { vp_writePFM(I, filename); }
1383
1390void vpImageIo::writePFM_HDR(const vpImage<float> &I, const std::string &filename) { vp_writePFM_HDR(I, filename); }
1391
1398void vpImageIo::writePFM_HDR(const vpImage<vpRGBf> &I, const std::string &filename) { vp_writePFM_HDR(I, filename); }
1399
1405void vpImageIo::writePGM(const vpImage<unsigned char> &I, const std::string &filename) { vp_writePGM(I, filename); }
1406
1412void vpImageIo::writePGM(const vpImage<short> &I, const std::string &filename) { vp_writePGM(I, filename); }
1413
1419void vpImageIo::writePGM(const vpImage<vpRGBa> &I, const std::string &filename) { vp_writePGM(I, filename); }
1420
1426void vpImageIo::readPFM(vpImage<float> &I, const std::string &filename) { vp_readPFM(I, filename); }
1427
1433void vpImageIo::readPFM_HDR(vpImage<float> &I, const std::string &filename) { vp_readPFM_HDR(I, filename); }
1434
1440void vpImageIo::readPFM_HDR(vpImage<vpRGBf> &I, const std::string &filename) { vp_readPFM_HDR(I, filename); }
1441
1447void vpImageIo::readPGM(vpImage<unsigned char> &I, const std::string &filename) { vp_readPGM(I, filename); }
1448
1454void vpImageIo::readPGM(vpImage<vpRGBa> &I, const std::string &filename) { vp_readPGM(I, filename); }
1455
1461void vpImageIo::readPPM(vpImage<unsigned char> &I, const std::string &filename) { vp_readPPM(I, filename); }
1462
1468void vpImageIo::readPPM(vpImage<vpRGBa> &I, const std::string &filename) { vp_readPPM(I, filename); }
1469
1475void vpImageIo::writePPM(const vpImage<unsigned char> &I, const std::string &filename) { vp_writePPM(I, filename); }
1476
1482void vpImageIo::writePPM(const vpImage<vpRGBa> &I, const std::string &filename) { vp_writePPM(I, filename); }
1483
1491void vpImageIo::readPNGfromMem(const std::vector<unsigned char> &buffer, vpImage<unsigned char> &I, int backend)
1492{
1493 if (backend == IO_SYSTEM_LIB_BACKEND || backend == IO_SIMDLIB_BACKEND) {
1494 backend = IO_STB_IMAGE_BACKEND;
1495 }
1496 else if (backend == IO_OPENCV_BACKEND) {
1497#if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
1498 backend = IO_STB_IMAGE_BACKEND;
1499#endif
1500 }
1501 else if (backend == IO_DEFAULT_BACKEND) {
1502#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS)
1503 backend = IO_OPENCV_BACKEND;
1504#elif defined(VISP_HAVE_STBIMAGE)
1505 backend = IO_STB_IMAGE_BACKEND;
1506#else
1507 (void)I;
1508 (void)buffer;
1509 const std::string message = "Cannot in-memory png read: OpenCV or std_image backend are not available";
1511#endif
1512 }
1513
1514 if (backend == IO_OPENCV_BACKEND) {
1515#if defined(VISP_HAVE_OPENCV) && \
1516 (((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || \
1517 ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC)))
1518 readPNGfromMemOpenCV(buffer, I);
1519 (void)backend;
1520#else
1521 (void)buffer;
1522 (void)I;
1523 (void)backend;
1524 const std::string message = "Cannot in-memory png read: OpenCV backend is not available";
1526#endif
1527 }
1528 else {
1529#if defined(VISP_HAVE_STBIMAGE)
1530 readPNGfromMemStb(buffer, I);
1531 (void)backend;
1532#else
1533 (void)buffer;
1534 (void)I;
1535 (void)backend;
1536 const std::string message = "Cannot in-memory png read: std_image backend is not available";
1538#endif
1539 }
1540}
1541
1549void vpImageIo::readPNGfromMem(const std::vector<unsigned char> &buffer, vpImage<vpRGBa> &I, int backend)
1550{
1551 if (backend == IO_SYSTEM_LIB_BACKEND || backend == IO_SIMDLIB_BACKEND) {
1552 backend = IO_STB_IMAGE_BACKEND;
1553 }
1554 else if (backend == IO_OPENCV_BACKEND) {
1555#if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
1556 backend = IO_STB_IMAGE_BACKEND;
1557#endif
1558 }
1559 else if (backend == IO_DEFAULT_BACKEND) {
1560#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS)
1561 backend = IO_OPENCV_BACKEND;
1562#elif defined(VISP_HAVE_STBIMAGE)
1563 backend = IO_STB_IMAGE_BACKEND;
1564#else
1565 (void)I;
1566 (void)buffer;
1567 const std::string message = "Cannot in-memory png read: OpenCV or std_image backend are not available";
1569#endif
1570 }
1571
1572 if (backend == IO_OPENCV_BACKEND) {
1573#if defined(VISP_HAVE_OPENCV) && \
1574 (((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || \
1575 ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC)))
1576 readPNGfromMemOpenCV(buffer, I);
1577 (void)backend;
1578#else
1579 (void)buffer;
1580 (void)I;
1581 (void)backend;
1582 const std::string message = "Cannot in-memory png read: OpenCV backend is not available";
1584#endif
1585 }
1586 else {
1587#if defined(VISP_HAVE_STBIMAGE)
1588 readPNGfromMemStb(buffer, I);
1589 (void)backend;
1590#else
1591 (void)buffer;
1592 (void)I;
1593 (void)backend;
1594 const std::string message = "Cannot in-memory png read: std_image backend is not available";
1596#endif
1597 }
1598}
1599
1608void vpImageIo::writePNGtoMem(const vpImage<unsigned char> &I, std::vector<unsigned char> &buffer, int backend)
1609{
1610 if (backend == IO_SYSTEM_LIB_BACKEND || backend == IO_SIMDLIB_BACKEND) {
1611 backend = IO_STB_IMAGE_BACKEND;
1612 }
1613 else if (backend == IO_OPENCV_BACKEND) {
1614#if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
1615 backend = IO_STB_IMAGE_BACKEND;
1616#endif
1617 }
1618 else if (backend == IO_DEFAULT_BACKEND) {
1619#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS)
1620 backend = IO_OPENCV_BACKEND;
1621#elif defined(VISP_HAVE_STBIMAGE)
1622 backend = IO_STB_IMAGE_BACKEND;
1623#else
1624 (void)I;
1625 (void)buffer;
1626 const std::string message = "Cannot in-memory png write: OpenCV or std_image backend are not available";
1628#endif
1629 }
1630
1631 if (backend == IO_OPENCV_BACKEND) {
1632#if defined(VISP_HAVE_OPENCV) && \
1633 (((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || \
1634 ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC)))
1635 writePNGtoMemOpenCV(I, buffer);
1636#else
1637 (void)I;
1638 (void)buffer;
1639 const std::string message = "Cannot in-memory png write: OpenCV backend is not available";
1641#endif
1642 }
1643 else if (backend == IO_STB_IMAGE_BACKEND) {
1644#if defined(VISP_HAVE_STBIMAGE)
1645 writePNGtoMemStb(I, buffer);
1646#else
1647 (void)I;
1648 (void)buffer;
1649 const std::string message = "Cannot in-memory png write: std_image backend is not available";
1651#endif
1652 }
1653 else {
1654#if VISP_CXX_STANDARD > VISP_CXX_STANDARD_98
1655 const std::string message = "The " + std::to_string(backend) + " backend is not available.";
1657#else
1658 throw(vpImageException(vpImageException::ioError, "The %d backend is not available", backend));
1659#endif
1660 }
1661}
1662
1672void vpImageIo::writePNGtoMem(const vpImage<vpRGBa> &I, std::vector<unsigned char> &buffer, int backend, bool saveAlpha)
1673{
1674 if (backend == IO_SYSTEM_LIB_BACKEND || backend == IO_SIMDLIB_BACKEND) {
1675 backend = IO_STB_IMAGE_BACKEND;
1676 }
1677 else if (backend == IO_OPENCV_BACKEND) {
1678#if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
1679 backend = IO_STB_IMAGE_BACKEND;
1680#endif
1681 }
1682 else if (backend == IO_DEFAULT_BACKEND) {
1683#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS)
1684 backend = IO_OPENCV_BACKEND;
1685#elif defined(VISP_HAVE_STBIMAGE)
1686 backend = IO_STB_IMAGE_BACKEND;
1687#else
1688 (void)I;
1689 (void)buffer;
1690 const std::string message = "Cannot in-memory png write: OpenCV or std_image backend are not available";
1692#endif
1693 }
1694
1695 if (backend == IO_OPENCV_BACKEND) {
1696#if defined(VISP_HAVE_OPENCV) && \
1697 (((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || \
1698 ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC)))
1699 writePNGtoMemOpenCV(I, buffer, saveAlpha);
1700#else
1701 (void)I;
1702 (void)buffer;
1703 const std::string message = "Cannot in-memory png write: OpenCV backend is not available";
1705#endif
1706 }
1707 else if (backend == IO_STB_IMAGE_BACKEND) {
1708#if defined(VISP_HAVE_STBIMAGE)
1709 writePNGtoMemStb(I, buffer, saveAlpha);
1710#else
1711 (void)I;
1712 (void)buffer;
1713 const std::string message = "Cannot in-memory png write: std_image backend is not available";
1715#endif
1716 }
1717 else {
1718#if VISP_CXX_STANDARD > VISP_CXX_STANDARD_98
1719 const std::string message = "The " + std::to_string(backend) + " backend is not available.";
1721#else
1722 throw(vpImageException(vpImageException::ioError, "The %d backend is not available", backend));
1723#endif
1724 }
1725}
error that can be emitted by ViSP classes.
Definition vpException.h:60
@ ioError
I/O error.
Definition vpException.h:67
@ badValue
Used to indicate that a value is not in the allowed range.
Definition vpException.h:73
Error that can be emitted by the vpImage class and its derivatives.
@ ioError
Image io error.
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.
Definition vpImageIo.h:134
@ IO_DEFAULT_BACKEND
Default backend.
Definition vpImageIo.h:130
@ IO_SIMDLIB_BACKEND
Use embedded simd library.
Definition vpImageIo.h:133
@ IO_SYSTEM_LIB_BACKEND
Use system libraries like libpng or libjpeg-turbo.
Definition vpImageIo.h:131
@ IO_OPENCV_BACKEND
Use OpenCV imgcodecs module.
Definition vpImageIo.h:132
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 readPNGfromMem(const std::vector< unsigned char > &buffer, vpImage< unsigned char > &I, int backend=IO_DEFAULT_BACKEND)
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 writePNGtoMem(const vpImage< unsigned char > &I, std::vector< unsigned char > &buffer, 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)
Definition of the vpImage class member functions.
Definition vpImage.h:131
static std::string path(const std::string &pathname)
static std::string toLowerCase(const std::string &input)
Return a lower-case version of the string input . Numbers and special characters stay the same.
static bool checkFilename(const std::string &filename)
static std::string getFileExtension(const std::string &pathname, bool checkFile=false)