Visual Servoing Platform  version 3.6.1 under development (2024-04-20)
vpImageIo.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2023 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 
39 #include <visp3/core/vpIoTools.h>
40 #include <visp3/io/vpImageIo.h>
41 
42 #include "private/vpImageIoBackend.h"
43 
44 vpImageIo::vpImageFormatType vpImageIo::getFormat(const std::string &filename)
45 {
46  std::string ext = vpImageIo::getExtension(filename);
47 
48  if (ext.compare(".PGM") == 0)
49  return FORMAT_PGM;
50  else if (ext.compare(".pgm") == 0)
51  return FORMAT_PGM;
52  else if (ext.compare(".PPM") == 0)
53  return FORMAT_PPM;
54  else if (ext.compare(".ppm") == 0)
55  return FORMAT_PPM;
56  else if (ext.compare(".JPG") == 0)
57  return FORMAT_JPEG;
58  else if (ext.compare(".jpg") == 0)
59  return FORMAT_JPEG;
60  else if (ext.compare(".JPEG") == 0)
61  return FORMAT_JPEG;
62  else if (ext.compare(".jpeg") == 0)
63  return FORMAT_JPEG;
64  else if (ext.compare(".PNG") == 0)
65  return FORMAT_PNG;
66  else if (ext.compare(".png") == 0)
67  return FORMAT_PNG;
68  // Formats supported by opencv
69  else if (ext.compare(".TIFF") == 0)
70  return FORMAT_TIFF;
71  else if (ext.compare(".tiff") == 0)
72  return FORMAT_TIFF;
73  else if (ext.compare(".TIF") == 0)
74  return FORMAT_TIFF;
75  else if (ext.compare(".tif") == 0)
76  return FORMAT_TIFF;
77  else if (ext.compare(".BMP") == 0)
78  return FORMAT_BMP;
79  else if (ext.compare(".bmp") == 0)
80  return FORMAT_BMP;
81  else if (ext.compare(".DIB") == 0)
82  return FORMAT_DIB;
83  else if (ext.compare(".dib") == 0)
84  return FORMAT_DIB;
85  else if (ext.compare(".PBM") == 0)
86  return FORMAT_PBM;
87  else if (ext.compare(".pbm") == 0)
88  return FORMAT_PBM;
89  else if (ext.compare(".SR") == 0)
90  return FORMAT_RASTER;
91  else if (ext.compare(".sr") == 0)
92  return FORMAT_RASTER;
93  else if (ext.compare(".RAS") == 0)
94  return FORMAT_RASTER;
95  else if (ext.compare(".ras") == 0)
96  return FORMAT_RASTER;
97  else if (ext.compare(".JP2") == 0)
98  return FORMAT_JPEG2000;
99  else if (ext.compare(".jp2") == 0)
100  return FORMAT_JPEG2000;
101  else
102  return FORMAT_UNKNOWN;
103 }
104 
105 // return the extension of the file including the dot
106 std::string vpImageIo::getExtension(const std::string &filename)
107 {
108  // extract the extension
109  size_t dot = filename.find_last_of(".");
110  std::string ext = filename.substr(dot, filename.size() - 1);
111  return ext;
112 }
113 
143 void vpImageIo::read(vpImage<unsigned char> &I, const std::string &filename, int backend)
144 {
145  bool exist = vpIoTools::checkFilename(filename);
146  if (!exist) {
147  const std::string message = "Cannot read file: \"" + std::string(filename) + "\" doesn't exist";
149  }
150 
151  // Allows to use ~ symbol or env variables in path
152  std::string final_filename = vpIoTools::path(filename);
153 
154  bool try_opencv_reader = false;
155 
156  switch (getFormat(final_filename)) {
157  case FORMAT_PGM:
158  readPGM(I, final_filename);
159  break;
160  case FORMAT_PPM:
161  readPPM(I, final_filename);
162  break;
163  case FORMAT_JPEG:
164  readJPEG(I, final_filename, backend);
165  break;
166  case FORMAT_PNG:
167  readPNG(I, final_filename, backend);
168  break;
169  case FORMAT_TIFF:
170  case FORMAT_BMP:
171  case FORMAT_DIB:
172  case FORMAT_PBM:
173  case FORMAT_RASTER:
174  case FORMAT_JPEG2000:
175  case FORMAT_UNKNOWN:
176  try_opencv_reader = true;
177  break;
178  }
179 
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);
183 #else
184  const std::string message = "Cannot read file \"" + filename + "\": No backend able to support this image format";
186 #endif
187  }
188 }
189 
219 void vpImageIo::read(vpImage<vpRGBa> &I, const std::string &filename, int backend)
220 {
221  bool exist = vpIoTools::checkFilename(filename);
222  if (!exist) {
223  const std::string message = "Cannot read file: \"" + std::string(filename) + "\" doesn't exist";
225  }
226  // Allows to use ~ symbol or env variables in path
227  std::string final_filename = vpIoTools::path(filename);
228 
229  bool try_opencv_reader = false;
230 
231  switch (getFormat(final_filename)) {
232  case FORMAT_PGM:
233  readPGM(I, final_filename);
234  break;
235  case FORMAT_PPM:
236  readPPM(I, final_filename);
237  break;
238  case FORMAT_JPEG:
239  readJPEG(I, final_filename, backend);
240  break;
241  case FORMAT_PNG:
242  readPNG(I, final_filename, backend);
243  break;
244  case FORMAT_TIFF:
245  case FORMAT_BMP:
246  case FORMAT_DIB:
247  case FORMAT_PBM:
248  case FORMAT_RASTER:
249  case FORMAT_JPEG2000:
250  case FORMAT_UNKNOWN:
251  try_opencv_reader = true;
252  break;
253  }
254 
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);
258 #else
259  const std::string message = "Cannot read file \"" + filename + "\": No backend able to support this image format";
261 #endif
262  }
263 }
264 
287 void vpImageIo::write(const vpImage<unsigned char> &I, const std::string &filename, int backend)
288 {
289  bool try_opencv_writer = false;
290 
291  switch (getFormat(filename)) {
292  case FORMAT_PGM:
293  writePGM(I, filename);
294  break;
295  case FORMAT_PPM:
296  writePPM(I, filename);
297  break;
298  case FORMAT_JPEG:
299  writeJPEG(I, filename, backend);
300  break;
301  case FORMAT_PNG:
302  writePNG(I, filename, backend);
303  break;
304  case FORMAT_TIFF:
305  case FORMAT_BMP:
306  case FORMAT_DIB:
307  case FORMAT_PBM:
308  case FORMAT_RASTER:
309  case FORMAT_JPEG2000:
310  case FORMAT_UNKNOWN:
311  try_opencv_writer = true;
312  break;
313  }
314 
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);
318 #else
319  const std::string message = "Cannot write file \"" + filename + "\": No backend able to support this image format";
321 #endif
322  }
323 }
324 
347 void vpImageIo::write(const vpImage<vpRGBa> &I, const std::string &filename, int backend)
348 {
349  bool try_opencv_writer = false;
350 
351  switch (getFormat(filename)) {
352  case FORMAT_PGM:
353  writePGM(I, filename);
354  break;
355  case FORMAT_PPM:
356  writePPM(I, filename);
357  break;
358  case FORMAT_JPEG:
359  writeJPEG(I, filename, backend);
360  break;
361  case FORMAT_PNG:
362  writePNG(I, filename, backend);
363  break;
364  case FORMAT_TIFF:
365  case FORMAT_BMP:
366  case FORMAT_DIB:
367  case FORMAT_PBM:
368  case FORMAT_RASTER:
369  case FORMAT_JPEG2000:
370  case FORMAT_UNKNOWN:
371  try_opencv_writer = true;
372  break;
373  }
374 
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);
378 #else
379  const std::string message = "Cannot write file \"" + filename + "\": No backend able to support this image format";
381 #endif
382  }
383 }
384 
393 // Strategy based on benchmark: see https://github.com/lagadic/visp/pull/1004
394 // Default: 1. opencv, 2. system, 3. stb_image
395 void vpImageIo::readJPEG(vpImage<unsigned char> &I, const std::string &filename, int backend)
396 {
397  if (backend == IO_SYSTEM_LIB_BACKEND) {
398 #if !defined(VISP_HAVE_JPEG)
399  // Libjpeg backend is not available to read file \"" + filename + "\": switch to stb_image backend
400  backend = IO_STB_IMAGE_BACKEND;
401 #endif
402  }
403  else if (backend == IO_OPENCV_BACKEND) {
404 #if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
405  // OpenCV backend is not available to read file \"" + filename + "\": switch to stb_image backend
406  backend = IO_STB_IMAGE_BACKEND;
407 #endif
408  }
409  else if (backend == IO_DEFAULT_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))
411  backend = IO_OPENCV_BACKEND;
412 #elif defined(VISP_HAVE_JPEG)
413  backend = IO_SYSTEM_LIB_BACKEND;
414 #elif defined(VISP_HAVE_SIMDLIB)
415  backend = IO_SIMDLIB_BACKEND;
416 #elif defined(VISP_HAVE_STBIMAGE)
417  backend = IO_STB_IMAGE_BACKEND;
418 #else
419  (void)I;
420  (void)filename;
421  const std::string message = "Cannot read file \"" + filename + "\": no backend available";
423 #endif
424  }
425 
426  if (backend == IO_SYSTEM_LIB_BACKEND) {
427 #if defined(VISP_HAVE_JPEG)
428  readJPEGLibjpeg(I, filename);
429 #else
430  (void)I;
431  (void)filename;
432  const std::string message = "Cannot read file \"" + filename + "\": jpeg library backend is not available";
434 #endif
435  }
436  else if (backend == IO_OPENCV_BACKEND) {
437 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
438  readOpenCV(I, filename);
439 #else
440  (void)I;
441  (void)filename;
442  const std::string message = "Cannot read file \"" + filename + "\": OpenCV library backend is not available";
444 #endif
445  }
446  else if (backend == IO_STB_IMAGE_BACKEND) {
447 #if defined(VISP_HAVE_STBIMAGE)
448  readStb(I, filename);
449 #else
450  (void)I;
451  (void)filename;
452  const std::string message = "Cannot read file \"" + filename + "\": stb_image backend is not available";
454 #endif
455  }
456  else if (backend == IO_SIMDLIB_BACKEND) {
457 #if defined(VISP_HAVE_SIMDLIB)
458  readSimdlib(I, filename);
459 #else
460  (void)I;
461  (void)filename;
462  const std::string message = "Cannot read file \"" + filename + "\": Simd library backend is not available";
464 #endif
465  }
466 }
467 
476 // Strategy based on benchmark: see https://github.com/lagadic/visp/pull/1004
477 // Default: 1. opencv, 2. system, 3. stb_image
478 void vpImageIo::readJPEG(vpImage<vpRGBa> &I, const std::string &filename, int backend)
479 {
480  if (backend == IO_SYSTEM_LIB_BACKEND) {
481 #if !defined(VISP_HAVE_JPEG)
482  // Libjpeg backend is not available to read file \"" + filename + "\": switch to stb_image backend";
483  backend = IO_STB_IMAGE_BACKEND;
484 #endif
485  }
486  else if (backend == IO_OPENCV_BACKEND) {
487 #if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
488  // OpenCV backend is not available to read file \"" + filename + "\": switch to stb_image backend";
489  backend = IO_STB_IMAGE_BACKEND;
490 #endif
491  }
492  else if (backend == IO_DEFAULT_BACKEND) {
493 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS)
494  backend = IO_OPENCV_BACKEND;
495 #elif defined(VISP_HAVE_JPEG)
496  backend = IO_SYSTEM_LIB_BACKEND;
497 #elif defined(VISP_HAVE_SIMDLIB)
498  backend = IO_SIMDLIB_BACKEND;
499 #elif defined(VISP_HAVE_STBIMAGE)
500  backend = IO_STB_IMAGE_BACKEND;
501 #else
502  (void)I;
503  (void)filename;
504  const std::string message = "Cannot read file \"" + filename + "\": no backend available";
506 #endif
507  }
508 
509  if (backend == IO_SYSTEM_LIB_BACKEND) {
510 #if defined(VISP_HAVE_JPEG)
511  readJPEGLibjpeg(I, filename);
512 #else
513  (void)I;
514  (void)filename;
515  const std::string message = "Cannot read file \"" + filename + "\": jpeg library backend is not available";
517 #endif
518  }
519  else if (backend == IO_OPENCV_BACKEND) {
520 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
521  readOpenCV(I, filename);
522 #else
523  (void)I;
524  (void)filename;
525  const std::string message = "Cannot read file \"" + filename + "\": OpenCV library backend is not available";
527 #endif
528  }
529  else if (backend == IO_STB_IMAGE_BACKEND) {
530 #if defined(VISP_HAVE_STBIMAGE)
531  readStb(I, filename);
532 #else
533  (void)I;
534  (void)filename;
535  const std::string message = "Cannot read file \"" + filename + "\": stb_image backend is not available";
537 #endif
538  }
539  else if (backend == IO_SIMDLIB_BACKEND) {
540 #if defined(VISP_HAVE_SIMDLIB)
541  readSimdlib(I, filename);
542 #else
543  (void)I;
544  (void)filename;
545  const std::string message = "Cannot read file \"" + filename + "\": Simd library backend is not available";
547 #endif
548  }
549 }
550 
559 // Strategy based on benchmark: see https://github.com/lagadic/visp/pull/1004
560 // Default: 1. system, 2. opencv, 3. stb_image
561 void vpImageIo::readPNG(vpImage<unsigned char> &I, const std::string &filename, int backend)
562 {
563  if (backend == IO_SYSTEM_LIB_BACKEND) {
564 #if !defined(VISP_HAVE_PNG)
565  // Libpng backend is not available to read file \"" + filename + "\": switch to stb_image backend";
566  backend = IO_STB_IMAGE_BACKEND;
567 #endif
568  }
569  else if (backend == IO_OPENCV_BACKEND) {
570 #if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
571  // OpenCV backend is not available to read file \"" + filename + "\": switch to stb_image backend";
572  backend = IO_STB_IMAGE_BACKEND;
573 #endif
574  }
575  else if (backend == IO_DEFAULT_BACKEND) {
576 #if defined(VISP_HAVE_PNG)
577  backend = IO_SYSTEM_LIB_BACKEND;
578 #elif defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS)
579  backend = IO_OPENCV_BACKEND;
580 #elif defined(VISP_HAVE_SIMDLIB)
581  backend = IO_SIMDLIB_BACKEND;
582 #elif defined(VISP_HAVE_STBIMAGE)
583  backend = IO_STB_IMAGE_BACKEND;
584 #else
585  (void)I;
586  (void)filename;
587  const std::string message = "Cannot read file \"" + filename + "\": no backend available";
589 #endif
590  }
591 
592  if (backend == IO_SYSTEM_LIB_BACKEND) {
593 #if defined(VISP_HAVE_PNG)
594  readPNGLibpng(I, filename);
595 #else
596  (void)I;
597  (void)filename;
598  const std::string message = "Cannot read file \"" + filename + "\": png library backend is not available";
600 #endif
601  }
602  else if (backend == IO_OPENCV_BACKEND) {
603 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
604  readOpenCV(I, filename);
605 #else
606  (void)I;
607  (void)filename;
608  const std::string message = "Cannot read file \"" + filename + "\": OpenCV library backend is not available";
610 #endif
611  }
612  else if (backend == IO_STB_IMAGE_BACKEND) {
613 #if defined(VISP_HAVE_STBIMAGE)
614  readStb(I, filename);
615 #else
616  (void)I;
617  (void)filename;
618  const std::string message = "Cannot read file \"" + filename + "\": stb_image backend is not available";
620 #endif
621  }
622  else if (backend == IO_SIMDLIB_BACKEND) {
623 #if defined(VISP_HAVE_SIMDLIB)
624  readSimdlib(I, filename);
625 #else
626  (void)I;
627  (void)filename;
628  const std::string message = "Cannot read file \"" + filename + "\": Simd library backend is not available";
630 #endif
631  }
632 }
633 
642 // Strategy based on benchmark: see https://github.com/lagadic/visp/pull/1004
643 // Default: 1. opencv, 2. stb_image
644 void vpImageIo::readPNG(vpImage<vpRGBa> &I, const std::string &filename, int backend)
645 {
646  if (backend == IO_SYSTEM_LIB_BACKEND) {
647 #if !defined(VISP_HAVE_PNG)
648  // Libpng backend is not available to read file \"" + filename + "\": switch to stb_image backend";
649  backend = IO_STB_IMAGE_BACKEND;
650 #endif
651  }
652  else if (backend == IO_OPENCV_BACKEND) {
653 #if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
654  // OpenCV backend is not available to read file \"" + filename + "\": switch to stb_image backend";
655  backend = IO_STB_IMAGE_BACKEND;
656 #endif
657  }
658  else if (backend == IO_DEFAULT_BACKEND) {
659 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
660  backend = IO_OPENCV_BACKEND;
661 #elif defined(VISP_HAVE_SIMDLIB)
662  backend = IO_SIMDLIB_BACKEND;
663 #elif defined(VISP_HAVE_STBIMAGE)
664  backend = IO_STB_IMAGE_BACKEND;
665 #else
666  (void)I;
667  (void)filename;
668  const std::string message = "Cannot read file \"" + filename + "\": no backend available";
670 #endif
671  }
672 
673  if (backend == IO_SYSTEM_LIB_BACKEND) {
674 #if defined(VISP_HAVE_PNG)
675  readPNGLibpng(I, filename);
676 #else
677  (void)I;
678  (void)filename;
679  const std::string message = "Cannot read file \"" + filename + "\": png library backend is not available";
681 #endif
682  }
683  else if (backend == IO_OPENCV_BACKEND) {
684 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
685  readOpenCV(I, filename);
686 #else
687  (void)I;
688  (void)filename;
689  const std::string message = "Cannot read file \"" + filename + "\": OpenCV library backend is not available";
691 #endif
692  }
693  else if (backend == IO_STB_IMAGE_BACKEND) {
694 #if defined(VISP_HAVE_STBIMAGE)
695  readStb(I, filename);
696 #else
697  (void)I;
698  (void)filename;
699  const std::string message = "Cannot read file \"" + filename + "\": stb_image backend is not available";
701 #endif
702  }
703  else if (backend == IO_SIMDLIB_BACKEND) {
704 #if defined(VISP_HAVE_SIMDLIB)
705  readSimdlib(I, filename);
706 #else
707  (void)I;
708  (void)filename;
709  const std::string message = "Cannot read file \"" + filename + "\": Simd library backend is not available";
711 #endif
712  }
713 }
714 
723 void vpImageIo::readEXR(vpImage<float> &I, const std::string &filename, int backend)
724 {
725  if (backend == IO_SYSTEM_LIB_BACKEND || backend == IO_SIMDLIB_BACKEND || backend == IO_STB_IMAGE_BACKEND) {
726  // This backend cannot read file \"" + filename + "\": switch to the default TinyEXR backend
727  backend = IO_DEFAULT_BACKEND;
728  }
729  else if (backend == IO_OPENCV_BACKEND) {
730 #if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
731  // OpenCV backend is not available to read file \"" + filename + "\": switch to the default TinyEXR backend
732  backend = IO_DEFAULT_BACKEND;
733 #endif
734  }
735  else if (backend == IO_DEFAULT_BACKEND) {
736 #if !defined(VISP_HAVE_TINYEXR)
737  // TinyEXR backend is not available to read file \"" + filename + "\": switch to the OpenCV backend
738  backend = IO_OPENCV_BACKEND;
739 #endif
740  }
741 
742  if (backend == IO_OPENCV_BACKEND) {
743 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
744  readOpenCV(I, filename);
745 #else
746  (void)I;
747  (void)filename;
748  const std::string message = "Cannot read file \"" + filename + "\": OpenCV backend is not available";
750 #endif
751  }
752  else if (backend == IO_DEFAULT_BACKEND) {
753 #if defined(VISP_HAVE_TINYEXR)
754  readEXRTiny(I, filename);
755 #else
756  (void)I;
757  (void)filename;
758  const std::string message = "Cannot read file \"" + filename + "\": Default TinyEXR backend is not available";
760 #endif
761  }
762 }
763 
772 void vpImageIo::readEXR(vpImage<vpRGBf> &I, const std::string &filename, int backend)
773 {
774  if (backend == IO_SYSTEM_LIB_BACKEND || backend == IO_SIMDLIB_BACKEND || backend == IO_STB_IMAGE_BACKEND) {
775  // This backend cannot read file \"" + filename + "\": switch to the default TinyEXR backend
776  backend = IO_DEFAULT_BACKEND;
777  }
778  else if (backend == IO_OPENCV_BACKEND) {
779 #if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
780  // OpenCV backend is not available to read file \"" + filename + "\": switch to the default TinyEXR backend
781  backend = IO_DEFAULT_BACKEND;
782 #endif
783  }
784  else if (backend == IO_DEFAULT_BACKEND) {
785 #if !defined(VISP_HAVE_TINYEXR)
786  // TinyEXR backend is not available to read file \"" + filename + "\": switch to the OpenCV backend
787  backend = IO_OPENCV_BACKEND;
788 #endif
789  }
790 
791  if (backend == IO_OPENCV_BACKEND) {
792 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
793  readOpenCV(I, filename);
794 #else
795  (void)I;
796  (void)filename;
797  const std::string message = "Cannot read file \"" + filename + "\": OpenCV backend is not available";
799 #endif
800  }
801  else if (backend == IO_DEFAULT_BACKEND) {
802 #if defined(VISP_HAVE_TINYEXR)
803  readEXRTiny(I, filename);
804 #else
805  (void)I;
806  (void)filename;
807  const std::string message = "Cannot read file \"" + filename + "\": TinyEXR backend is not available";
809 #endif
810  }
811 }
812 
822 // Strategy based on benchmark: see https://github.com/lagadic/visp/pull/1004
823 // Default: 1. system, 2. opencv, 3. simd
824 void vpImageIo::writeJPEG(const vpImage<unsigned char> &I, const std::string &filename, int backend, int quality)
825 {
826  if (backend == IO_SYSTEM_LIB_BACKEND) {
827 #if !defined(VISP_HAVE_JPEG)
828 #if defined(VISP_HAVE_SIMDLIB)
829  // Libjpeg backend is not available to save file \"" + filename + "\": switch to simd backend
830  backend = IO_SIMDLIB_BACKEND;
831 #else
832  // Libjpeg backend is not available to save file \"" + filename + "\": switch to stb_image backend
833  backend = IO_STB_IMAGE_BACKEND;
834 #endif
835 #endif
836  }
837  else if (backend == IO_OPENCV_BACKEND) {
838 #if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
839 #if defined(VISP_HAVE_SIMDLIB)
840  // OpenCV backend is not available to save file \"" + filename + "\": switch to simd backend
841  backend = IO_SIMDLIB_BACKEND;
842 #else
843  // OpenCV backend is not available to save file \"" + filename + "\": switch to stb_image backend
844  backend = IO_STB_IMAGE_BACKEND;
845 #endif
846 #endif
847  }
848  else if (backend == IO_DEFAULT_BACKEND) {
849 #if defined(VISP_HAVE_JPEG)
850  backend = IO_SYSTEM_LIB_BACKEND;
851 #elif defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS)
852  backend = IO_OPENCV_BACKEND;
853 #elif defined(VISP_HAVE_SIMDLIB)
854  backend = IO_SIMDLIB_BACKEND;
855 #elif defined(VISP_HAVE_STBIMAGE)
856  backend = IO_STB_IMAGE_BACKEND;
857 #else
858  (void)I;
859  (void)filename;
860  const std::string message = "Cannot save file \"" + filename + "\": no available backend";
862 #endif
863  }
864 
865  if (backend == IO_SYSTEM_LIB_BACKEND) {
866 #if defined(VISP_HAVE_JPEG)
867  writeJPEGLibjpeg(I, filename, quality);
868 #else
869  (void)I;
870  (void)filename;
871  (void)quality;
872  const std::string message = "Cannot save file \"" + filename + "\": jpeg backend is not available";
874 #endif
875  }
876  else if (backend == IO_OPENCV_BACKEND) {
877 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) \
878  && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
879  writeOpenCV(I, filename, quality);
880 #else
881  (void)I;
882  (void)filename;
883  (void)quality;
884  const std::string message = "Cannot save file \"" + filename + "\": OpenCV backend is not available";
886 #endif
887  }
888  else if (backend == IO_SIMDLIB_BACKEND) {
889 #if defined(VISP_HAVE_SIMDLIB)
890  writeJPEGSimdlib(I, filename, quality);
891 #else
892  (void)I;
893  (void)filename;
894  (void)quality;
895  const std::string message = "Cannot save file \"" + filename + "\": Simd library backend is not available";
897 #endif
898  }
899  else if (backend == IO_STB_IMAGE_BACKEND) {
900 #if defined(VISP_HAVE_STBIMAGE)
901  writeJPEGStb(I, filename, quality);
902 #else
903  (void)I;
904  (void)filename;
905  (void)quality;
906  const std::string message = "Cannot save file \"" + filename + "\": stb_image backend is not available";
908 #endif
909  }
910 }
911 
921 // Strategy based on benchmark: see https://github.com/lagadic/visp/pull/1004
922 // Default: 1. system, 2. opencv, , 3. simd
923 void vpImageIo::writeJPEG(const vpImage<vpRGBa> &I, const std::string &filename, int backend, int quality)
924 {
925  if (backend == IO_SYSTEM_LIB_BACKEND) {
926 #if !defined(VISP_HAVE_JPEG)
927 #if defined(VISP_HAVE_SIMDLIB)
928  // Libjpeg backend is not available to save file \"" + filename + "\": switch to simd backend
929  backend = IO_SIMDLIB_BACKEND;
930 #else
931  // Libjpeg backend is not available to save file \"" + filename + "\": switch to stb_image backend
932  backend = IO_STB_IMAGE_BACKEND;
933 #endif
934 #endif
935  }
936  else if (backend == IO_OPENCV_BACKEND) {
937 #if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
938 #if defined(VISP_HAVE_SIMDLIB)
939  // OpenCV backend is not available to save file \"" + filename + "\": switch to simd backend
940  backend = IO_SIMDLIB_BACKEND;
941 #else
942  // OpenCV backend is not available to save file \"" + filename + "\": switch to stb_image backend
943  backend = IO_STB_IMAGE_BACKEND;
944 #endif
945 #endif
946  }
947  else if (backend == IO_DEFAULT_BACKEND) {
948 #if defined(VISP_HAVE_JPEG)
949  backend = IO_SYSTEM_LIB_BACKEND;
950 #elif defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS)
951  backend = IO_OPENCV_BACKEND;
952 #elif defined(VISP_HAVE_SIMDLIB)
953  backend = IO_SIMDLIB_BACKEND;
954 #elif defined(VISP_HAVE_STBIMAGE)
955  backend = IO_STB_IMAGE_BACKEND;
956 #else
957  (void)I;
958  (void)filename;
959  const std::string message = "Cannot save file \"" + filename + "\": no backend available";
961 #endif
962  }
963 
964  if (backend == IO_SYSTEM_LIB_BACKEND) {
965 #if defined(VISP_HAVE_JPEG)
966  writeJPEGLibjpeg(I, filename, quality);
967 #else
968  (void)I;
969  (void)filename;
970  (void)quality;
971  const std::string message = "Cannot save file \"" + filename + "\": jpeg library backend is not available";
973 #endif
974  }
975  else if (backend == IO_OPENCV_BACKEND) {
976 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
977  writeOpenCV(I, filename, quality);
978 #else
979  (void)I;
980  (void)filename;
981  (void)quality;
982  const std::string message = "Cannot save file \"" + filename + "\": OpenCV library backend is not available";
984 #endif
985  }
986  else if (backend == IO_SIMDLIB_BACKEND) {
987 #if defined(VISP_HAVE_SIMDLIB)
988  writeJPEGSimdlib(I, filename, quality);
989 #else
990  (void)I;
991  (void)filename;
992  (void)quality;
993  const std::string message = "Cannot save file \"" + filename + "\": Simd library backend is not available";
995 #endif
996  }
997  else if (backend == IO_STB_IMAGE_BACKEND) {
998 #if defined(VISP_HAVE_STBIMAGE)
999  writeJPEGStb(I, filename, quality);
1000 #else
1001  (void)I;
1002  (void)filename;
1003  (void)quality;
1004  const std::string message = "Cannot save file \"" + filename + "\": stb_image backend is not available";
1006 #endif
1007  }
1008 }
1009 
1018 // Strategy based on benchmark: see https://github.com/lagadic/visp/pull/1004
1019 // Default: 1. opencv, 2. simd
1020 void vpImageIo::writePNG(const vpImage<unsigned char> &I, const std::string &filename, int backend)
1021 {
1022  if (backend == IO_SYSTEM_LIB_BACKEND) {
1023 #if !defined(VISP_HAVE_PNG)
1024 #if defined(VISP_HAVE_SIMDLIB)
1025  // Libpng backend is not available to save file \"" + filename + "\": switch to simd backend
1026  backend = IO_SIMDLIB_BACKEND;
1027 #else
1028  // Libpng backend is not available to save file \"" + filename + "\": switch to stb_image backend
1029  backend = IO_STB_IMAGE_BACKEND;
1030 #endif
1031 #endif
1032  }
1033  else if (backend == IO_OPENCV_BACKEND) {
1034 #if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
1035 #if defined(VISP_HAVE_SIMDLIB)
1036  // OpenCV backend is not available to save file \"" + filename + "\": switch to simd backend
1037  backend = IO_SIMDLIB_BACKEND;
1038 #else
1039  // OpenCV backend is not available to save file \"" + filename + "\": switch to stb_image backend
1040  backend = IO_STB_IMAGE_BACKEND;
1041 #endif
1042 #endif
1043  }
1044  else if (backend == IO_DEFAULT_BACKEND) {
1045 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
1046  backend = IO_OPENCV_BACKEND;
1047 #elif defined(VISP_HAVE_SIMDLIB)
1048  backend = IO_SIMDLIB_BACKEND;
1049 #elif defined(VISP_HAVE_STBIMAGE)
1050  backend = IO_STB_IMAGE_BACKEND;
1051 #else
1052  (void)I;
1053  (void)filename;
1054  const std::string message = "Cannot save file \"" + filename + "\": no backend available";
1056 #endif
1057  }
1058 
1059  if (backend == IO_OPENCV_BACKEND) {
1060 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
1061  writeOpenCV(I, filename, 90);
1062 #else
1063  (void)I;
1064  (void)filename;
1065  const std::string message = "Cannot save file \"" + filename + "\": OpenCV library backend is not available";
1067 #endif
1068  }
1069  else if (backend == IO_SIMDLIB_BACKEND) {
1070 #if defined(VISP_HAVE_SIMDLIB)
1071  writePNGSimdlib(I, filename);
1072 #else
1073  (void)I;
1074  (void)filename;
1075  const std::string message = "Cannot save file \"" + filename + "\": Simd library backend is not available";
1077 #endif
1078  }
1079  else if (backend == IO_STB_IMAGE_BACKEND) {
1080 #if defined(VISP_HAVE_STBIMAGE)
1081  writePNGStb(I, filename);
1082 #else
1083  (void)I;
1084  (void)filename;
1085  const std::string message = "Cannot save file \"" + filename + "\": stb_image backend is not available";
1087 #endif
1088  }
1089  else if (backend == IO_SYSTEM_LIB_BACKEND) {
1090 #if defined(VISP_HAVE_PNG)
1091  writePNGLibpng(I, filename);
1092 #else
1093  (void)I;
1094  (void)filename;
1095  const std::string message = "Cannot save file \"" + filename + "\": png library backend is not available";
1097 #endif
1098  }
1099 }
1100 
1109 // Strategy based on benchmark: see https://github.com/lagadic/visp/pull/1004
1110 // Default: 1. opencv, 2. system, 3. simd
1111 void vpImageIo::writePNG(const vpImage<vpRGBa> &I, const std::string &filename, int backend)
1112 {
1113  if (backend == IO_SYSTEM_LIB_BACKEND) {
1114 #if !defined(VISP_HAVE_PNG)
1115 #if defined(VISP_HAVE_SIMDLIB)
1116  // Libpng backend is not available to save file \"" + filename + "\": switch to simd backend
1117  backend = IO_SIMDLIB_BACKEND;
1118 #else
1119  // Libpng backend is not available to save file \"" + filename + "\": switch to stb_image backend
1120  backend = IO_STB_IMAGE_BACKEND;
1121 #endif
1122 #endif
1123  }
1124  else if (backend == IO_OPENCV_BACKEND) {
1125 #if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
1126 #if defined(VISP_HAVE_SIMDLIB)
1127  // OpenCV backend is not available to save file \"" + filename + "\": switch to simd backend
1128  backend = IO_SIMDLIB_BACKEND;
1129 #else
1130  // OpenCV backend is not available to save file \"" + filename + "\": switch to stb_image backend
1131  backend = IO_STB_IMAGE_BACKEND;
1132 #endif
1133 #endif
1134  }
1135  else if (backend == IO_DEFAULT_BACKEND) {
1136 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
1137  backend = IO_OPENCV_BACKEND;
1138 #elif defined(VISP_HAVE_SIMDLIB)
1139  backend = IO_SIMDLIB_BACKEND;
1140 #elif defined(VISP_HAVE_STBIMAGE)
1141  backend = IO_STB_IMAGE_BACKEND;
1142 #else
1143  (void)I;
1144  (void)filename;
1145  const std::string message = "Cannot save file \"" + filename + "\": no backend available";
1147 #endif
1148  }
1149 
1150  if (backend == IO_OPENCV_BACKEND) {
1151 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
1152  writeOpenCV(I, filename, 90);
1153 #else
1154  (void)I;
1155  (void)filename;
1156  const std::string message = "Cannot save file \"" + filename + "\": OpenCV backend is not available";
1158 #endif
1159  }
1160  else if (backend == IO_SIMDLIB_BACKEND) {
1161 #if defined(VISP_HAVE_SIMDLIB)
1162  writePNGSimdlib(I, filename);
1163 #else
1164  (void)I;
1165  (void)filename;
1166  const std::string message = "Cannot save file \"" + filename + "\": Simd library backend is not available";
1168 #endif
1169  }
1170  else if (backend == IO_STB_IMAGE_BACKEND) {
1171 #if defined(VISP_HAVE_STBIMAGE)
1172  writePNGStb(I, filename);
1173 #else
1174  (void)I;
1175  (void)filename;
1176  const std::string message = "Cannot save file \"" + filename + "\": stb_image backend is not available";
1178 #endif
1179  }
1180  else if (backend == IO_SYSTEM_LIB_BACKEND) {
1181 #if defined(VISP_HAVE_PNG)
1182  writePNGLibpng(I, filename);
1183 #else
1184  (void)I;
1185  (void)filename;
1186  const std::string message = "Cannot save file \"" + filename + "\": libpng backend is not available";
1188 #endif
1189  }
1190 }
1191 
1200 void vpImageIo::writeEXR(const vpImage<float> &I, const std::string &filename, int backend)
1201 {
1202  if (backend == IO_SYSTEM_LIB_BACKEND || backend == IO_SIMDLIB_BACKEND || backend == IO_STB_IMAGE_BACKEND) {
1203  // This backend cannot save file \"" + filename + "\": switch to the default TinyEXR backend
1204  backend = IO_DEFAULT_BACKEND;
1205  }
1206  else if (backend == IO_OPENCV_BACKEND) {
1207 #if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
1208  (void)I;
1209  // OpenCV backend is not available to save file \"" + filename + "\": switch to the default TinyEXR backend
1210  backend = IO_DEFAULT_BACKEND;
1211 #endif
1212  }
1213  else if (backend == IO_DEFAULT_BACKEND) {
1214 #if !defined(VISP_HAVE_TINYEXR)
1215  // TinyEXR backend is not available to save file \"" + filename + "\": switch to the OpenCV backend
1216  backend = IO_OPENCV_BACKEND;
1217 #endif
1218  }
1219 
1220  if (backend == IO_OPENCV_BACKEND) {
1221 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
1222  writeOpenCV(I, filename);
1223 #else
1224  (void)I;
1225  (void)filename;
1226  const std::string message = "Cannot save file \"" + filename + "\": OpenCV backend is not available";
1228 #endif
1229  }
1230  else if (backend == IO_DEFAULT_BACKEND) {
1231 #if defined(VISP_HAVE_TINYEXR)
1232  writeEXRTiny(I, filename);
1233 #else
1234  (void)I;
1235  (void)filename;
1236  const std::string message = "Cannot save file \"" + filename + "\": TinyEXR backend is not available";
1238 #endif
1239  }
1240 }
1241 
1250 void vpImageIo::writeEXR(const vpImage<vpRGBf> &I, const std::string &filename, int backend)
1251 {
1252  if (backend == IO_SYSTEM_LIB_BACKEND || backend == IO_SIMDLIB_BACKEND || backend == IO_STB_IMAGE_BACKEND) {
1253  // This backend cannot save file \"" + filename + "\": switch to the default TinyEXR backend
1254  backend = IO_DEFAULT_BACKEND;
1255  }
1256  else if (backend == IO_OPENCV_BACKEND) {
1257 #if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
1258  // OpenCV backend is not available to save file \"" + filename + "\": switch to the default TinyEXR backend
1259  backend = IO_DEFAULT_BACKEND;
1260 #endif
1261  }
1262  else if (backend == IO_DEFAULT_BACKEND) {
1263 #if !defined(VISP_HAVE_TINYEXR)
1264  // TinyEXR backend is not available to save file \"" + filename + "\": switch to the OpenCV backend
1265  backend = IO_OPENCV_BACKEND;
1266 #endif
1267  }
1268 
1269  if (backend == IO_OPENCV_BACKEND) {
1270 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
1271  writeOpenCV(I, filename);
1272 #else
1273  (void)I;
1274  (void)filename;
1275  const std::string message = "Cannot save file \"" + filename + "\": OpenCV backend is not available";
1277 #endif
1278  }
1279  else if (backend == IO_DEFAULT_BACKEND) {
1280 #if defined(VISP_HAVE_TINYEXR)
1281  writeEXRTiny(I, filename);
1282 #else
1283  (void)I;
1284  (void)filename;
1285  const std::string message = "Cannot save file \"" + filename + "\": TinyEXR backend is not available";
1287 #endif
1288  }
1289 }
1290 
1296 void vpImageIo::writePFM(const vpImage<float> &I, const std::string &filename) { vp_writePFM(I, filename); }
1297 
1304 void vpImageIo::writePFM_HDR(const vpImage<float> &I, const std::string &filename) { vp_writePFM_HDR(I, filename); }
1305 
1312 void vpImageIo::writePFM_HDR(const vpImage<vpRGBf> &I, const std::string &filename) { vp_writePFM_HDR(I, filename); }
1313 
1319 void vpImageIo::writePGM(const vpImage<unsigned char> &I, const std::string &filename) { vp_writePGM(I, filename); }
1320 
1326 void vpImageIo::writePGM(const vpImage<short> &I, const std::string &filename) { vp_writePGM(I, filename); }
1327 
1333 void vpImageIo::writePGM(const vpImage<vpRGBa> &I, const std::string &filename) { vp_writePGM(I, filename); }
1334 
1340 void vpImageIo::readPFM(vpImage<float> &I, const std::string &filename) { vp_readPFM(I, filename); }
1341 
1347 void vpImageIo::readPFM_HDR(vpImage<float> &I, const std::string &filename) { vp_readPFM_HDR(I, filename); }
1348 
1354 void vpImageIo::readPFM_HDR(vpImage<vpRGBf> &I, const std::string &filename) { vp_readPFM_HDR(I, filename); }
1355 
1361 void vpImageIo::readPGM(vpImage<unsigned char> &I, const std::string &filename) { vp_readPGM(I, filename); }
1362 
1368 void vpImageIo::readPGM(vpImage<vpRGBa> &I, const std::string &filename) { vp_readPGM(I, filename); }
1369 
1375 void vpImageIo::readPPM(vpImage<unsigned char> &I, const std::string &filename) { vp_readPPM(I, filename); }
1376 
1382 void vpImageIo::readPPM(vpImage<vpRGBa> &I, const std::string &filename) { vp_readPPM(I, filename); }
1383 
1389 void vpImageIo::writePPM(const vpImage<unsigned char> &I, const std::string &filename) { vp_writePPM(I, filename); }
1390 
1396 void vpImageIo::writePPM(const vpImage<vpRGBa> &I, const std::string &filename) { vp_writePPM(I, filename); }
1397 
1405 void vpImageIo::readPNGfromMem(const std::vector<unsigned char> &buffer, vpImage<unsigned char> &I, int backend)
1406 {
1407  if (backend == IO_SYSTEM_LIB_BACKEND || backend == IO_SIMDLIB_BACKEND) {
1408  backend = IO_STB_IMAGE_BACKEND;
1409  }
1410  else if (backend == IO_OPENCV_BACKEND) {
1411 #if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
1412  backend = IO_STB_IMAGE_BACKEND;
1413 #endif
1414  }
1415  else if (backend == IO_DEFAULT_BACKEND) {
1416 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS)
1417  backend = IO_OPENCV_BACKEND;
1418 #elif defined(VISP_HAVE_STBIMAGE)
1419  backend = IO_STB_IMAGE_BACKEND;
1420 #else
1421  (void)I;
1422  (void)buffer;
1423  const std::string message = "Cannot in-memory png read: OpenCV or std_image backend are not available";
1425 #endif
1426  }
1427 
1428  if (backend == IO_OPENCV_BACKEND) {
1429 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) \
1430  && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
1431  readPNGfromMemOpenCV(buffer, I);
1432  (void)backend;
1433 #else
1434  (void)buffer;
1435  (void)I;
1436  (void)backend;
1437  const std::string message = "Cannot in-memory png read: OpenCV backend is not available";
1439 #endif
1440  }
1441  else {
1442 #if defined(VISP_HAVE_STBIMAGE)
1443  readPNGfromMemStb(buffer, I);
1444  (void)backend;
1445 #else
1446  (void)buffer;
1447  (void)I;
1448  (void)backend;
1449  const std::string message = "Cannot in-memory png read: std_image backend is not available";
1451 #endif
1452  }
1453 }
1454 
1462 void vpImageIo::readPNGfromMem(const std::vector<unsigned char> &buffer, vpImage<vpRGBa> &I, int backend)
1463 {
1464  if (backend == IO_SYSTEM_LIB_BACKEND || backend == IO_SIMDLIB_BACKEND) {
1465  backend = IO_STB_IMAGE_BACKEND;
1466  }
1467  else if (backend == IO_OPENCV_BACKEND) {
1468 #if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
1469  backend = IO_STB_IMAGE_BACKEND;
1470 #endif
1471  }
1472  else if (backend == IO_DEFAULT_BACKEND) {
1473 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS)
1474  backend = IO_OPENCV_BACKEND;
1475 #elif defined(VISP_HAVE_STBIMAGE)
1476  backend = IO_STB_IMAGE_BACKEND;
1477 #else
1478  (void)I;
1479  (void)buffer;
1480  const std::string message = "Cannot in-memory png read: OpenCV or std_image backend are not available";
1482 #endif
1483  }
1484 
1485  if (backend == IO_OPENCV_BACKEND) {
1486 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) \
1487  && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
1488  readPNGfromMemOpenCV(buffer, I);
1489  (void)backend;
1490 #else
1491  (void)buffer;
1492  (void)I;
1493  (void)backend;
1494  const std::string message = "Cannot in-memory png read: OpenCV backend is not available";
1496 #endif
1497  }
1498  else {
1499 #if defined(VISP_HAVE_STBIMAGE)
1500  readPNGfromMemStb(buffer, I);
1501  (void)backend;
1502 #else
1503  (void)buffer;
1504  (void)I;
1505  (void)backend;
1506  const std::string message = "Cannot in-memory png read: std_image backend is not available";
1508 #endif
1509  }
1510 }
1511 
1520 void vpImageIo::writePNGtoMem(const vpImage<unsigned char> &I, std::vector<unsigned char> &buffer, int backend)
1521 {
1522  if (backend == IO_SYSTEM_LIB_BACKEND || backend == IO_SIMDLIB_BACKEND) {
1523  backend = IO_STB_IMAGE_BACKEND;
1524  }
1525  else if (backend == IO_OPENCV_BACKEND) {
1526 #if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
1527  backend = IO_STB_IMAGE_BACKEND;
1528 #endif
1529  }
1530  else if (backend == IO_DEFAULT_BACKEND) {
1531 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS)
1532  backend = IO_OPENCV_BACKEND;
1533 #elif defined(VISP_HAVE_STBIMAGE)
1534  backend = IO_STB_IMAGE_BACKEND;
1535 #else
1536  (void)I;
1537  (void)buffer;
1538  const std::string message = "Cannot in-memory png write: OpenCV or std_image backend are not available";
1540 #endif
1541  }
1542 
1543  if (backend == IO_OPENCV_BACKEND) {
1544 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) \
1545  && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
1546  writePNGtoMemOpenCV(I, buffer);
1547 #else
1548  (void)I;
1549  (void)buffer;
1550  const std::string message = "Cannot in-memory png write: OpenCV backend is not available";
1552 #endif
1553  }
1554  else if (backend == IO_STB_IMAGE_BACKEND) {
1555 #if defined(VISP_HAVE_STBIMAGE)
1556  writePNGtoMemStb(I, buffer);
1557 #else
1558  (void)I;
1559  (void)buffer;
1560  const std::string message = "Cannot in-memory png write: std_image backend is not available";
1562 #endif
1563  }
1564  else {
1565 #if VISP_CXX_STANDARD > VISP_CXX_STANDARD_98
1566  const std::string message = "The " + std::to_string(backend) + " backend is not available.";
1568 #else
1569  throw(vpImageException(vpImageException::ioError, "The %d backend is not available", backend));
1570 #endif
1571  }
1572 }
1573 
1583 void vpImageIo::writePNGtoMem(const vpImage<vpRGBa> &I, std::vector<unsigned char> &buffer, int backend, bool saveAlpha)
1584 {
1585  if (backend == IO_SYSTEM_LIB_BACKEND || backend == IO_SIMDLIB_BACKEND) {
1586  backend = IO_STB_IMAGE_BACKEND;
1587  }
1588  else if (backend == IO_OPENCV_BACKEND) {
1589 #if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
1590  backend = IO_STB_IMAGE_BACKEND;
1591 #endif
1592  }
1593  else if (backend == IO_DEFAULT_BACKEND) {
1594 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS)
1595  backend = IO_OPENCV_BACKEND;
1596 #elif defined(VISP_HAVE_STBIMAGE)
1597  backend = IO_STB_IMAGE_BACKEND;
1598 #else
1599  (void)I;
1600  (void)buffer;
1601  const std::string message = "Cannot in-memory png write: OpenCV or std_image backend are not available";
1603 #endif
1604  }
1605 
1606  if (backend == IO_OPENCV_BACKEND) {
1607 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) \
1608  && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
1609  writePNGtoMemOpenCV(I, buffer, saveAlpha);
1610 #else
1611  (void)I;
1612  (void)buffer;
1613  const std::string message = "Cannot in-memory png write: OpenCV backend is not available";
1615 #endif
1616  }
1617  else if (backend == IO_STB_IMAGE_BACKEND) {
1618 #if defined(VISP_HAVE_STBIMAGE)
1619  writePNGtoMemStb(I, buffer, saveAlpha);
1620 #else
1621  (void)I;
1622  (void)buffer;
1623  const std::string message = "Cannot in-memory png write: std_image backend is not available";
1625 #endif
1626  }
1627  else {
1628 #if VISP_CXX_STANDARD > VISP_CXX_STANDARD_98
1629  const std::string message = "The " + std::to_string(backend) + " backend is not available.";
1631 #else
1632  throw(vpImageException(vpImageException::ioError, "The %d backend is not available", backend));
1633 #endif
1634  }
1635 }
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)
Definition: vpImageIo.cpp:1296
static void readPGM(vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:1361
@ IO_STB_IMAGE_BACKEND
Use embedded stb_image library.
Definition: vpImageIo.h:128
@ IO_DEFAULT_BACKEND
Default backend.
Definition: vpImageIo.h:124
@ IO_SIMDLIB_BACKEND
Use embedded simd library.
Definition: vpImageIo.h:127
@ IO_SYSTEM_LIB_BACKEND
Use system libraries like libpng or libjpeg-turbo.
Definition: vpImageIo.h:125
@ IO_OPENCV_BACKEND
Use OpenCV imgcodecs module.
Definition: vpImageIo.h:126
static void writeJPEG(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND, int quality=90)
Definition: vpImageIo.cpp:824
static void readJPEG(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:395
static void readPFM(vpImage< float > &I, const std::string &filename)
Definition: vpImageIo.cpp:1340
static void writePNG(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:1020
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:143
static void readPFM_HDR(vpImage< float > &I, const std::string &filename)
Definition: vpImageIo.cpp:1347
static void readPPM(vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:1375
static void writePFM_HDR(const vpImage< float > &I, const std::string &filename)
Definition: vpImageIo.cpp:1304
static void readPNGfromMem(const std::vector< unsigned char > &buffer, vpImage< unsigned char > &I, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:1405
static void readEXR(vpImage< float > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:723
static void writePGM(const vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:1319
static void writeEXR(const vpImage< float > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:1200
static void writePNGtoMem(const vpImage< unsigned char > &I, std::vector< unsigned char > &buffer, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:1520
static void readPNG(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:561
static void writePPM(const vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:1389
static void write(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:287
static std::string path(const std::string &pathname)
Definition: vpIoTools.cpp:1432
static bool checkFilename(const std::string &filename)
Definition: vpIoTools.cpp:1213