Visual Servoing Platform  version 3.5.1 under development (2023-03-29)
vpImageIo.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Read/write images.
33  *
34  * Authors:
35  * Eric Marchand
36  *
37  *****************************************************************************/
38 
44 #include <visp3/core/vpIoTools.h>
45 #include <visp3/io/vpImageIo.h>
46 
47 #include "private/vpImageIoBackend.h"
48 
49 vpImageIo::vpImageFormatType vpImageIo::getFormat(const std::string &filename)
50 {
51  std::string ext = vpImageIo::getExtension(filename);
52 
53  if (ext.compare(".PGM") == 0)
54  return FORMAT_PGM;
55  else if (ext.compare(".pgm") == 0)
56  return FORMAT_PGM;
57  else if (ext.compare(".PPM") == 0)
58  return FORMAT_PPM;
59  else if (ext.compare(".ppm") == 0)
60  return FORMAT_PPM;
61  else if (ext.compare(".JPG") == 0)
62  return FORMAT_JPEG;
63  else if (ext.compare(".jpg") == 0)
64  return FORMAT_JPEG;
65  else if (ext.compare(".JPEG") == 0)
66  return FORMAT_JPEG;
67  else if (ext.compare(".jpeg") == 0)
68  return FORMAT_JPEG;
69  else if (ext.compare(".PNG") == 0)
70  return FORMAT_PNG;
71  else if (ext.compare(".png") == 0)
72  return FORMAT_PNG;
73  // Formats supported by opencv
74  else if (ext.compare(".TIFF") == 0)
75  return FORMAT_TIFF;
76  else if (ext.compare(".tiff") == 0)
77  return FORMAT_TIFF;
78  else if (ext.compare(".TIF") == 0)
79  return FORMAT_TIFF;
80  else if (ext.compare(".tif") == 0)
81  return FORMAT_TIFF;
82  else if (ext.compare(".BMP") == 0)
83  return FORMAT_BMP;
84  else if (ext.compare(".bmp") == 0)
85  return FORMAT_BMP;
86  else if (ext.compare(".DIB") == 0)
87  return FORMAT_DIB;
88  else if (ext.compare(".dib") == 0)
89  return FORMAT_DIB;
90  else if (ext.compare(".PBM") == 0)
91  return FORMAT_PBM;
92  else if (ext.compare(".pbm") == 0)
93  return FORMAT_PBM;
94  else if (ext.compare(".SR") == 0)
95  return FORMAT_RASTER;
96  else if (ext.compare(".sr") == 0)
97  return FORMAT_RASTER;
98  else if (ext.compare(".RAS") == 0)
99  return FORMAT_RASTER;
100  else if (ext.compare(".ras") == 0)
101  return FORMAT_RASTER;
102  else if (ext.compare(".JP2") == 0)
103  return FORMAT_JPEG2000;
104  else if (ext.compare(".jp2") == 0)
105  return FORMAT_JPEG2000;
106  else
107  return FORMAT_UNKNOWN;
108 }
109 
110 // return the extension of the file including the dot
111 std::string vpImageIo::getExtension(const std::string &filename)
112 {
113  // extract the extension
114  size_t dot = filename.find_last_of(".");
115  std::string ext = filename.substr(dot, filename.size() - 1);
116  return ext;
117 }
118 
148 void vpImageIo::read(vpImage<unsigned char> &I, const std::string &filename, int backend)
149 {
150  bool exist = vpIoTools::checkFilename(filename);
151  if (!exist) {
152  std::string message = "Cannot read file: \"" + std::string(filename) + "\" doesn't exist";
154  }
155 
156  // Allows to use ~ symbol or env variables in path
157  std::string final_filename = vpIoTools::path(filename);
158 
159  bool try_opencv_reader = false;
160 
161  switch (getFormat(final_filename)) {
162  case FORMAT_PGM:
163  readPGM(I, final_filename);
164  break;
165  case FORMAT_PPM:
166  readPPM(I, final_filename);
167  break;
168  case FORMAT_JPEG:
169  readJPEG(I, final_filename, backend);
170  break;
171  case FORMAT_PNG:
172  readPNG(I, final_filename, backend);
173  break;
174  case FORMAT_TIFF:
175  case FORMAT_BMP:
176  case FORMAT_DIB:
177  case FORMAT_PBM:
178  case FORMAT_RASTER:
179  case FORMAT_JPEG2000:
180  case FORMAT_UNKNOWN:
181  try_opencv_reader = true;
182  break;
183  }
184 
185  if (try_opencv_reader) {
186 #if defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100
187  readOpenCV(I, filename);
188 #else
189  std::string message = "Cannot read file \"" + filename + "\": No backend able to support this image format";
191 #endif
192  }
193 }
194 
224 void vpImageIo::read(vpImage<vpRGBa> &I, const std::string &filename, int backend)
225 {
226  bool exist = vpIoTools::checkFilename(filename);
227  if (!exist) {
228  std::string message = "Cannot read file: \"" + std::string(filename) + "\" doesn't exist";
230  }
231  // Allows to use ~ symbol or env variables in path
232  std::string final_filename = vpIoTools::path(filename);
233 
234  bool try_opencv_reader = false;
235 
236  switch (getFormat(final_filename)) {
237  case FORMAT_PGM:
238  readPGM(I, final_filename);
239  break;
240  case FORMAT_PPM:
241  readPPM(I, final_filename);
242  break;
243  case FORMAT_JPEG:
244  readJPEG(I, final_filename, backend);
245  break;
246  case FORMAT_PNG:
247  readPNG(I, final_filename, backend);
248  break;
249  case FORMAT_TIFF:
250  case FORMAT_BMP:
251  case FORMAT_DIB:
252  case FORMAT_PBM:
253  case FORMAT_RASTER:
254  case FORMAT_JPEG2000:
255  case FORMAT_UNKNOWN:
256  try_opencv_reader = true;
257  break;
258  }
259 
260  if (try_opencv_reader) {
261 #if defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100
262  readOpenCV(I, filename);
263 #else
264  std::string message = "Cannot read file \"" + filename + "\": No backend able to support this image format";
266 #endif
267  }
268 }
269 
292 void vpImageIo::write(const vpImage<unsigned char> &I, const std::string &filename, int backend)
293 {
294  bool try_opencv_writer = false;
295 
296  switch (getFormat(filename)) {
297  case FORMAT_PGM:
298  writePGM(I, filename);
299  break;
300  case FORMAT_PPM:
301  writePPM(I, filename);
302  break;
303  case FORMAT_JPEG:
304  writeJPEG(I, filename, backend);
305  break;
306  case FORMAT_PNG:
307  writePNG(I, filename, backend);
308  break;
309  case FORMAT_TIFF:
310  case FORMAT_BMP:
311  case FORMAT_DIB:
312  case FORMAT_PBM:
313  case FORMAT_RASTER:
314  case FORMAT_JPEG2000:
315  case FORMAT_UNKNOWN:
316  try_opencv_writer = true;
317  break;
318  }
319 
320  if (try_opencv_writer) {
321 #if defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100
322  writeOpenCV(I, filename, 90);
323 #else
324  std::string message = "Cannot write file \"" + filename + "\": No backend able to support this image format";
326 #endif
327  }
328 }
329 
352 void vpImageIo::write(const vpImage<vpRGBa> &I, const std::string &filename, int backend)
353 {
354  bool try_opencv_writer = false;
355 
356  switch (getFormat(filename)) {
357  case FORMAT_PGM:
358  writePGM(I, filename);
359  break;
360  case FORMAT_PPM:
361  writePPM(I, filename);
362  break;
363  case FORMAT_JPEG:
364  writeJPEG(I, filename, backend);
365  break;
366  case FORMAT_PNG:
367  writePNG(I, filename, backend);
368  break;
369  case FORMAT_TIFF:
370  case FORMAT_BMP:
371  case FORMAT_DIB:
372  case FORMAT_PBM:
373  case FORMAT_RASTER:
374  case FORMAT_JPEG2000:
375  case FORMAT_UNKNOWN:
376  try_opencv_writer = true;
377  break;
378  }
379 
380  if (try_opencv_writer) {
381 #if defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100
382  writeOpenCV(I, filename, 90);
383 #else
384  std::string message = "Cannot write file \"" + filename + "\": No backend able to support this image format";
386 #endif
387  }
388 }
389 
398 // Strategy based on benchmark: see https://github.com/lagadic/visp/pull/1004
399 // Default: 1. opencv, 2. system, 3. stb_image
400 void vpImageIo::readJPEG(vpImage<unsigned char> &I, const std::string &filename, int backend)
401 {
402  if (backend == IO_SYSTEM_LIB_BACKEND) {
403 #if !defined(VISP_HAVE_JPEG)
404  std::string message =
405  "Libjpeg backend is not available to read file \"" + filename + "\": switch to stb_image backend";
406  backend = IO_STB_IMAGE_BACKEND;
407 #endif
408  } else if (backend == IO_OPENCV_BACKEND) {
409 #if !(defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100)
410  std::string message =
411  "OpenCV backend is not available to read file \"" + filename + "\": switch to stb_image backend";
412  backend = IO_STB_IMAGE_BACKEND;
413 #endif
414  } else if (backend == IO_DEFAULT_BACKEND) {
415 #if defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100
416  backend = IO_OPENCV_BACKEND;
417 #elif defined(VISP_HAVE_JPEG)
418  backend = IO_SYSTEM_LIB_BACKEND;
419 #else
420  backend = IO_STB_IMAGE_BACKEND;
421 #endif
422  }
423 
424  if (backend == IO_SYSTEM_LIB_BACKEND) {
425 #if defined(VISP_HAVE_JPEG)
426  readJPEGLibjpeg(I, filename);
427 #endif
428  } else if (backend == IO_OPENCV_BACKEND) {
429 #if defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100
430  readOpenCV(I, filename);
431 #endif
432  } else if (backend == IO_STB_IMAGE_BACKEND) {
433  readStb(I, filename);
434  } else if (backend == IO_SIMDLIB_BACKEND) {
435  readSimdlib(I, filename);
436  }
437 }
438 
447 // Strategy based on benchmark: see https://github.com/lagadic/visp/pull/1004
448 // Default: 1. opencv, 2. system, 3. stb_image
449 void vpImageIo::readJPEG(vpImage<vpRGBa> &I, const std::string &filename, int backend)
450 {
451  if (backend == IO_SYSTEM_LIB_BACKEND) {
452 #if !defined(VISP_HAVE_JPEG)
453  std::string message =
454  "Libjpeg backend is not available to read file \"" + filename + "\": switch to stb_image backend";
455  backend = IO_STB_IMAGE_BACKEND;
456 #endif
457  } else if (backend == IO_OPENCV_BACKEND) {
458 #if !(defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100)
459  std::string message =
460  "OpenCV backend is not available to read file \"" + filename + "\": switch to stb_image backend";
461  backend = IO_STB_IMAGE_BACKEND;
462 #endif
463  } else if (backend == IO_DEFAULT_BACKEND) {
464 #if defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100
465  backend = IO_OPENCV_BACKEND;
466 #elif defined(VISP_HAVE_JPEG)
467  backend = IO_SYSTEM_LIB_BACKEND;
468 #else
469  backend = IO_STB_IMAGE_BACKEND;
470 #endif
471  }
472 
473  if (backend == IO_SYSTEM_LIB_BACKEND) {
474 #if defined(VISP_HAVE_JPEG)
475  readJPEGLibjpeg(I, filename);
476 #endif
477  } else if (backend == IO_OPENCV_BACKEND) {
478 #if defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100
479  readOpenCV(I, filename);
480 #endif
481  } else if (backend == IO_STB_IMAGE_BACKEND) {
482  readStb(I, filename);
483  } else if (backend == IO_SIMDLIB_BACKEND) {
484  readSimdlib(I, filename);
485  }
486 }
487 
496 // Strategy based on benchmark: see https://github.com/lagadic/visp/pull/1004
497 // Default: 1. system, 2. opencv, 3. stb_image
498 void vpImageIo::readPNG(vpImage<unsigned char> &I, const std::string &filename, int backend)
499 {
500  if (backend == IO_SYSTEM_LIB_BACKEND) {
501 #if !defined(VISP_HAVE_PNG)
502  std::string message =
503  "Libpng backend is not available to read file \"" + filename + "\": switch to stb_image backend";
504  backend = IO_STB_IMAGE_BACKEND;
505 #endif
506  } else if (backend == IO_OPENCV_BACKEND) {
507 #if !(defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100)
508  std::string message =
509  "OpenCV backend is not available to read file \"" + filename + "\": switch to stb_image backend";
510  backend = IO_STB_IMAGE_BACKEND;
511 #endif
512  } else if (backend == IO_DEFAULT_BACKEND) {
513 #if defined(VISP_HAVE_PNG)
514  backend = IO_SYSTEM_LIB_BACKEND;
515 #elif defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100
516  backend = IO_OPENCV_BACKEND;
517 #else
518  backend = IO_STB_IMAGE_BACKEND;
519 #endif
520  }
521 
522  if (backend == IO_SYSTEM_LIB_BACKEND) {
523 #if defined(VISP_HAVE_PNG)
524  readPNGLibpng(I, filename);
525 #endif
526  } else if (backend == IO_OPENCV_BACKEND) {
527 #if defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100
528  readOpenCV(I, filename);
529 #endif
530  } else if (backend == IO_STB_IMAGE_BACKEND) {
531  readStb(I, filename);
532  } else if (backend == IO_SIMDLIB_BACKEND) {
533  readSimdlib(I, filename);
534  }
535 }
536 
545 // Strategy based on benchmark: see https://github.com/lagadic/visp/pull/1004
546 // Default: 1. opencv, 2. stb_image
547 void vpImageIo::readPNG(vpImage<vpRGBa> &I, const std::string &filename, int backend)
548 {
549  if (backend == IO_SYSTEM_LIB_BACKEND) {
550 #if !defined(VISP_HAVE_PNG)
551  std::string message =
552  "Libpng backend is not available to read file \"" + filename + "\": switch to stb_image backend";
553  backend = IO_STB_IMAGE_BACKEND;
554 #endif
555  } else if (backend == IO_OPENCV_BACKEND) {
556 #if !(defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100)
557  std::string message =
558  "OpenCV backend is not available to read file \"" + filename + "\": switch to stb_image backend";
559  backend = IO_STB_IMAGE_BACKEND;
560 #endif
561  } else if (backend == IO_DEFAULT_BACKEND) {
562 #if defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100
563  backend = IO_OPENCV_BACKEND;
564 #else
565  backend = IO_STB_IMAGE_BACKEND;
566 #endif
567  }
568 
569  if (backend == IO_SYSTEM_LIB_BACKEND) {
570 #if defined(VISP_HAVE_PNG)
571  readPNGLibpng(I, filename);
572 #endif
573  } else if (backend == IO_OPENCV_BACKEND) {
574 #if defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100
575  readOpenCV(I, filename);
576 #endif
577  } else if (backend == IO_STB_IMAGE_BACKEND) {
578  readStb(I, filename);
579  } else if (backend == IO_SIMDLIB_BACKEND) {
580  readSimdlib(I, filename);
581  }
582 }
583 
592 void vpImageIo::readEXR(vpImage<float> &I, const std::string &filename, int backend)
593 {
594  if (backend == IO_SYSTEM_LIB_BACKEND || backend == IO_SIMDLIB_BACKEND || backend == IO_STB_IMAGE_BACKEND) {
595  std::string message =
596  "This backend cannot read file \"" + filename + "\": switch to the default TinyEXR backend";
597  backend = IO_DEFAULT_BACKEND;
598  } else if (backend == IO_OPENCV_BACKEND) {
599 #if !(defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100)
600  std::string message =
601  "OpenCV backend is not available to read file \"" + filename + "\": switch to the default TinyEXR backend";
602  backend = IO_DEFAULT_BACKEND;
603 #endif
604  }
605 
606  if (backend == IO_OPENCV_BACKEND) {
607 #if defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100
608  readOpenCV(I, filename);
609 #endif
610  } else if (backend == IO_DEFAULT_BACKEND) {
611 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
612  readEXRTiny(I, filename);
613 #else
614  (void)I;
615  std::string message =
616  "TinyEXR backend is not available to read file \"" + filename + "\": cxx standard should be greater or equal to cxx11";
618 #endif
619  }
620 }
621 
630 void vpImageIo::readEXR(vpImage<vpRGBf> &I, const std::string &filename, int backend)
631 {
632  if (backend == IO_SYSTEM_LIB_BACKEND || backend == IO_SIMDLIB_BACKEND || backend == IO_STB_IMAGE_BACKEND) {
633  std::string message =
634  "This backend cannot read file \"" + filename + "\": switch to the default TinyEXR backend";
635  backend = IO_DEFAULT_BACKEND;
636  } else if (backend == IO_OPENCV_BACKEND) {
637 #if !(defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100)
638  std::string message =
639  "OpenCV backend is not available to read file \"" + filename + "\": switch to the default TinyEXR backend";
640  backend = IO_DEFAULT_BACKEND;
641 #endif
642  }
643 
644  if (backend == IO_OPENCV_BACKEND) {
645 #if defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100
646  readOpenCV(I, filename);
647 #endif
648  } else if (backend == IO_DEFAULT_BACKEND) {
649 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
650  readEXRTiny(I, filename);
651 #else
652  (void)I;
653  std::string message =
654  "TinyEXR backend is not available to read file \"" + filename + "\": cxx standard should be greater or equal to cxx11";
656 #endif
657  }
658 }
659 
669 // Strategy based on benchmark: see https://github.com/lagadic/visp/pull/1004
670 // Default: 1. system, 2. opencv, 3. simd
671 void vpImageIo::writeJPEG(const vpImage<unsigned char> &I, const std::string &filename, int backend, int quality)
672 {
673  if (backend == IO_SYSTEM_LIB_BACKEND) {
674 #if !defined(VISP_HAVE_JPEG)
675  std::string message = "Libjpeg backend is not available to save file \"" + filename + "\": switch to simd backend";
676  backend = IO_SIMDLIB_BACKEND;
677 #endif
678  } else if (backend == IO_OPENCV_BACKEND) {
679 #if !(defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100)
680  std::string message = "OpenCV backend is not available to save file \"" + filename + "\": switch to simd backend";
681  backend = IO_SIMDLIB_BACKEND;
682 #endif
683  } else if (backend == IO_DEFAULT_BACKEND) {
684 #if defined(VISP_HAVE_JPEG)
685  backend = IO_SYSTEM_LIB_BACKEND;
686 #elif defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100
687  backend = IO_OPENCV_BACKEND;
688 #else
689  backend = IO_SIMDLIB_BACKEND;
690 #endif
691  }
692 
693  if (backend == IO_SYSTEM_LIB_BACKEND) {
694 #if defined(VISP_HAVE_JPEG)
695  writeJPEGLibjpeg(I, filename, quality);
696 #endif
697  } else if (backend == IO_OPENCV_BACKEND) {
698 #if defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100
699  writeOpenCV(I, filename, quality);
700 #endif
701  } else if (backend == IO_SIMDLIB_BACKEND) {
702  writeJPEGSimdlib(I, filename, quality);
703  } else if (backend == IO_STB_IMAGE_BACKEND) {
704  writeJPEGStb(I, filename, quality);
705  }
706 }
707 
717 // Strategy based on benchmark: see https://github.com/lagadic/visp/pull/1004
718 // Default: 1. system, 2. opencv, , 3. simd
719 void vpImageIo::writeJPEG(const vpImage<vpRGBa> &I, const std::string &filename, int backend, int quality)
720 {
721  if (backend == IO_SYSTEM_LIB_BACKEND) {
722 #if !defined(VISP_HAVE_JPEG)
723  std::string message = "Libjpeg backend is not available to save file \"" + filename + "\": switch to simd backend";
724  backend = IO_SIMDLIB_BACKEND;
725 #endif
726  } else if (backend == IO_OPENCV_BACKEND) {
727 #if !(defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100)
728  std::string message = "OpenCV backend is not available to save file \"" + filename + "\": switch to simd backend";
729  backend = IO_SIMDLIB_BACKEND;
730 #endif
731  } else if (backend == IO_DEFAULT_BACKEND) {
732 #if defined(VISP_HAVE_JPEG)
733  backend = IO_SYSTEM_LIB_BACKEND;
734 #elif defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100
735  backend = IO_OPENCV_BACKEND;
736 #else
737  backend = IO_SIMDLIB_BACKEND;
738 #endif
739  }
740 
741  if (backend == IO_SYSTEM_LIB_BACKEND) {
742 #if defined(VISP_HAVE_JPEG)
743  writeJPEGLibjpeg(I, filename, quality);
744 #endif
745  } else if (backend == IO_OPENCV_BACKEND) {
746 #if defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100
747  writeOpenCV(I, filename, quality);
748 #endif
749  } else if (backend == IO_SIMDLIB_BACKEND) {
750  writeJPEGSimdlib(I, filename, quality);
751  } else if (backend == IO_STB_IMAGE_BACKEND) {
752  writeJPEGStb(I, filename, quality);
753  }
754 }
755 
764 // Strategy based on benchmark: see https://github.com/lagadic/visp/pull/1004
765 // Default: 1. opencv, 2. simd
766 void vpImageIo::writePNG(const vpImage<unsigned char> &I, const std::string &filename, int backend)
767 {
768  if (backend == IO_SYSTEM_LIB_BACKEND) {
769 #if !defined(VISP_HAVE_PNG)
770  std::string message = "Libpng backend is not available to save file \"" + filename + "\": switch to simd backend";
771  backend = IO_SIMDLIB_BACKEND;
772 #endif
773  } else if (backend == IO_OPENCV_BACKEND) {
774 #if !(defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100)
775  std::string message = "OpenCV backend is not available to save file \"" + filename + "\": switch to simd backend";
776  backend = IO_SIMDLIB_BACKEND;
777 #endif
778  } else if (backend == IO_DEFAULT_BACKEND) {
779 #if defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100
780  backend = IO_OPENCV_BACKEND;
781 #else
782  backend = IO_SIMDLIB_BACKEND;
783 #endif
784  }
785 
786  if (backend == IO_OPENCV_BACKEND) {
787 #if defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100
788  writeOpenCV(I, filename, 90);
789 #endif
790  } else if (backend == IO_SIMDLIB_BACKEND) {
791  writePNGSimdlib(I, filename);
792  } else if (backend == IO_STB_IMAGE_BACKEND) {
793  writePNGStb(I, filename);
794  } else if (backend == IO_SYSTEM_LIB_BACKEND) {
795 #if defined(VISP_HAVE_PNG)
796  writePNGLibpng(I, filename);
797 #endif
798  }
799 }
800 
809 // Strategy based on benchmark: see https://github.com/lagadic/visp/pull/1004
810 // Default: 1. opencv, 2. system, 3. simd
811 void vpImageIo::writePNG(const vpImage<vpRGBa> &I, const std::string &filename, int backend)
812 {
813  if (backend == IO_SYSTEM_LIB_BACKEND) {
814 #if !defined(VISP_HAVE_PNG)
815  std::string message = "Libpng backend is not available to save file \"" + filename + "\": switch to simd backend";
816  backend = IO_SIMDLIB_BACKEND;
817 #endif
818  } else if (backend == IO_OPENCV_BACKEND) {
819 #if !(defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100)
820  std::string message = "OpenCV backend is not available to save file \"" + filename + "\": switch to simd backend";
821  backend = IO_SIMDLIB_BACKEND;
822 #endif
823  } else if (backend == IO_DEFAULT_BACKEND) {
824 #if defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100
825  backend = IO_OPENCV_BACKEND;
826 #else
827  backend = IO_SIMDLIB_BACKEND;
828 #endif
829  }
830 
831  if (backend == IO_OPENCV_BACKEND) {
832 #if defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100
833  writeOpenCV(I, filename, 90);
834 #endif
835  } else if (backend == IO_SIMDLIB_BACKEND) {
836  writePNGSimdlib(I, filename);
837  } else if (backend == IO_STB_IMAGE_BACKEND) {
838  writePNGStb(I, filename);
839  } else if (backend == IO_SYSTEM_LIB_BACKEND) {
840 #if defined(VISP_HAVE_PNG)
841  writePNGLibpng(I, filename);
842 #endif
843  }
844 }
845 
854 void vpImageIo::writeEXR(const vpImage<float> &I, const std::string &filename, int backend)
855 {
856  if (backend == IO_SYSTEM_LIB_BACKEND || backend == IO_SIMDLIB_BACKEND || backend == IO_STB_IMAGE_BACKEND) {
857  std::string message =
858  "This backend cannot save file \"" + filename + "\": switch to the default TinyEXR backend";
859  backend = IO_DEFAULT_BACKEND;
860  } else if (backend == IO_OPENCV_BACKEND) {
861 #if !(defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100)
862  (void)I;
863  std::string message =
864  "OpenCV backend is not available to save file \"" + filename + "\": switch to the default TinyEXR backend";
865  backend = IO_DEFAULT_BACKEND;
866 #endif
867  }
868 
869  if (backend == IO_OPENCV_BACKEND) {
870 #if defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100
871  writeOpenCV(I, filename);
872 #endif
873  } else if (backend == IO_DEFAULT_BACKEND) {
874 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
875  writeEXRTiny(I, filename);
876 #else
877  std::string message =
878  "TinyEXR backend is not available to save file \"" + filename + "\": cxx standard should be greater or equal to cxx11";
880 #endif
881  }
882 }
883 
892 void vpImageIo::writeEXR(const vpImage<vpRGBf> &I, const std::string &filename, int backend)
893 {
894  if (backend == IO_SYSTEM_LIB_BACKEND || backend == IO_SIMDLIB_BACKEND || backend == IO_STB_IMAGE_BACKEND) {
895  std::string message =
896  "This backend cannot save file \"" + filename + "\": switch to the default TinyEXR backend";
897  backend = IO_DEFAULT_BACKEND;
898  } else if (backend == IO_OPENCV_BACKEND) {
899 #if !(defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100)
900  (void)I;
901  std::string message =
902  "OpenCV backend is not available to save file \"" + filename + "\": switch to the default TinyEXR backend";
903  backend = IO_DEFAULT_BACKEND;
904 #endif
905  }
906 
907  if (backend == IO_OPENCV_BACKEND) {
908 #if defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100
909  writeOpenCV(I, filename);
910 #endif
911  } else if (backend == IO_DEFAULT_BACKEND) {
912 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
913  writeEXRTiny(I, filename);
914 #else
915  std::string message =
916  "TinyEXR backend is not available to save file \"" + filename + "\": cxx standard should be greater or equal to cxx11";
918 #endif
919  }
920 }
921 
927 void vpImageIo::writePFM(const vpImage<float> &I, const std::string &filename) { vp_writePFM(I, filename); }
928 
935 void vpImageIo::writePFM_HDR(const vpImage<float> &I, const std::string &filename) { vp_writePFM_HDR(I, filename); }
936 
943 void vpImageIo::writePFM_HDR(const vpImage<vpRGBf> &I, const std::string &filename) { vp_writePFM_HDR(I, filename); }
944 
950 void vpImageIo::writePGM(const vpImage<unsigned char> &I, const std::string &filename) { vp_writePGM(I, filename); }
951 
957 void vpImageIo::writePGM(const vpImage<short> &I, const std::string &filename) { vp_writePGM(I, filename); }
958 
964 void vpImageIo::writePGM(const vpImage<vpRGBa> &I, const std::string &filename) { vp_writePGM(I, filename); }
965 
971 void vpImageIo::readPFM(vpImage<float> &I, const std::string &filename) { vp_readPFM(I, filename); }
972 
978 void vpImageIo::readPFM_HDR(vpImage<float> &I, const std::string &filename) { vp_readPFM_HDR(I, filename); }
979 
985 void vpImageIo::readPFM_HDR(vpImage<vpRGBf> &I, const std::string &filename) { vp_readPFM_HDR(I, filename); }
986 
992 void vpImageIo::readPGM(vpImage<unsigned char> &I, const std::string &filename) { vp_readPGM(I, filename); }
993 
999 void vpImageIo::readPGM(vpImage<vpRGBa> &I, const std::string &filename) { vp_readPGM(I, filename); }
1000 
1006 void vpImageIo::readPPM(vpImage<unsigned char> &I, const std::string &filename) { vp_readPPM(I, filename); }
1007 
1013 void vpImageIo::readPPM(vpImage<vpRGBa> &I, const std::string &filename) { vp_readPPM(I, filename); }
1014 
1020 void vpImageIo::writePPM(const vpImage<unsigned char> &I, const std::string &filename) { vp_writePPM(I, filename); }
1021 
1027 void vpImageIo::writePPM(const vpImage<vpRGBa> &I, const std::string &filename) { vp_writePPM(I, filename); }
Error that can be emited by the vpImage class and its derivates.
static void writePFM(const vpImage< float > &I, const std::string &filename)
Definition: vpImageIo.cpp:927
static void readPGM(vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:992
@ IO_STB_IMAGE_BACKEND
Use embedded stb_image library.
Definition: vpImageIo.h:131
@ IO_DEFAULT_BACKEND
Default backend.
Definition: vpImageIo.h:127
@ IO_SIMDLIB_BACKEND
Use embedded simd library.
Definition: vpImageIo.h:130
@ IO_SYSTEM_LIB_BACKEND
Use system libraries like libpng or libjpeg.
Definition: vpImageIo.h:128
@ IO_OPENCV_BACKEND
Use OpenCV.
Definition: vpImageIo.h:129
static void writeJPEG(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND, int quality=90)
Definition: vpImageIo.cpp:671
static void readJPEG(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:400
static void readPFM(vpImage< float > &I, const std::string &filename)
Definition: vpImageIo.cpp:971
static void writePNG(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:766
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:148
static void readPFM_HDR(vpImage< float > &I, const std::string &filename)
Definition: vpImageIo.cpp:978
static void readPPM(vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:1006
static void writePFM_HDR(const vpImage< float > &I, const std::string &filename)
Definition: vpImageIo.cpp:935
static void readEXR(vpImage< float > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:592
static void writePGM(const vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:950
static void writeEXR(const vpImage< float > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:854
static void readPNG(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:498
static void writePPM(const vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:1020
static void write(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:292
static std::string path(const std::string &pathname)
Definition: vpIoTools.cpp:1020
static bool checkFilename(const std::string &filename)
Definition: vpIoTools.cpp:807