Visual Servoing Platform  version 3.6.1 under development (2024-07-15)
vpImageIo.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 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 #ifdef ENABLE_VISP_NAMESPACE
45 using namespace VISP_NAMESPACE_NAME;
46 #endif
47 
48 vpImageIo::vpImageFormatType vpImageIo::getFormat(const std::string &filename)
49 {
50  std::string ext = vpImageIo::getExtension(filename);
51 
52  if (ext.compare(".PGM") == 0)
53  return FORMAT_PGM;
54  else if (ext.compare(".pgm") == 0)
55  return FORMAT_PGM;
56  else if (ext.compare(".PPM") == 0)
57  return FORMAT_PPM;
58  else if (ext.compare(".ppm") == 0)
59  return FORMAT_PPM;
60  else if (ext.compare(".JPG") == 0)
61  return FORMAT_JPEG;
62  else if (ext.compare(".jpg") == 0)
63  return FORMAT_JPEG;
64  else if (ext.compare(".JPEG") == 0)
65  return FORMAT_JPEG;
66  else if (ext.compare(".jpeg") == 0)
67  return FORMAT_JPEG;
68  else if (ext.compare(".PNG") == 0)
69  return FORMAT_PNG;
70  else if (ext.compare(".png") == 0)
71  return FORMAT_PNG;
72  // Formats supported by opencv
73  else if (ext.compare(".TIFF") == 0)
74  return FORMAT_TIFF;
75  else if (ext.compare(".tiff") == 0)
76  return FORMAT_TIFF;
77  else if (ext.compare(".TIF") == 0)
78  return FORMAT_TIFF;
79  else if (ext.compare(".tif") == 0)
80  return FORMAT_TIFF;
81  else if (ext.compare(".BMP") == 0)
82  return FORMAT_BMP;
83  else if (ext.compare(".bmp") == 0)
84  return FORMAT_BMP;
85  else if (ext.compare(".DIB") == 0)
86  return FORMAT_DIB;
87  else if (ext.compare(".dib") == 0)
88  return FORMAT_DIB;
89  else if (ext.compare(".PBM") == 0)
90  return FORMAT_PBM;
91  else if (ext.compare(".pbm") == 0)
92  return FORMAT_PBM;
93  else if (ext.compare(".SR") == 0)
94  return FORMAT_RASTER;
95  else if (ext.compare(".sr") == 0)
96  return FORMAT_RASTER;
97  else if (ext.compare(".RAS") == 0)
98  return FORMAT_RASTER;
99  else if (ext.compare(".ras") == 0)
100  return FORMAT_RASTER;
101  else if (ext.compare(".JP2") == 0)
102  return FORMAT_JPEG2000;
103  else if (ext.compare(".jp2") == 0)
104  return FORMAT_JPEG2000;
105  else
106  return FORMAT_UNKNOWN;
107 }
108 
109 // return the extension of the file including the dot
110 std::string vpImageIo::getExtension(const std::string &filename)
111 {
112  // extract the extension
113  size_t dot = filename.find_last_of(".");
114  std::string ext = filename.substr(dot, filename.size() - 1);
115  return ext;
116 }
117 
147 void vpImageIo::read(vpImage<unsigned char> &I, const std::string &filename, int backend)
148 {
149  bool exist = vpIoTools::checkFilename(filename);
150  if (!exist) {
151  const std::string message = "Cannot read file: \"" + std::string(filename) + "\" doesn't exist";
153  }
154 
155  // Allows to use ~ symbol or env variables in path
156  std::string final_filename = vpIoTools::path(filename);
157 
158  bool try_opencv_reader = false;
159 
160  switch (getFormat(final_filename)) {
161  case FORMAT_PGM:
162  readPGM(I, final_filename);
163  break;
164  case FORMAT_PPM:
165  readPPM(I, final_filename);
166  break;
167  case FORMAT_JPEG:
168  readJPEG(I, final_filename, backend);
169  break;
170  case FORMAT_PNG:
171  readPNG(I, final_filename, backend);
172  break;
173  case FORMAT_TIFF:
174  case FORMAT_BMP:
175  case FORMAT_DIB:
176  case FORMAT_PBM:
177  case FORMAT_RASTER:
178  case FORMAT_JPEG2000:
179  case FORMAT_UNKNOWN:
180  try_opencv_reader = true;
181  break;
182  }
183 
184  if (try_opencv_reader) {
185 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
186  readOpenCV(I, filename);
187 #else
188  const std::string message = "Cannot read file \"" + filename + "\": No backend able to support this image format";
190 #endif
191  }
192 }
193 
223 void vpImageIo::read(vpImage<vpRGBa> &I, const std::string &filename, int backend)
224 {
225  bool exist = vpIoTools::checkFilename(filename);
226  if (!exist) {
227  const std::string message = "Cannot read file: \"" + std::string(filename) + "\" doesn't exist";
229  }
230  // Allows to use ~ symbol or env variables in path
231  std::string final_filename = vpIoTools::path(filename);
232 
233  bool try_opencv_reader = false;
234 
235  switch (getFormat(final_filename)) {
236  case FORMAT_PGM:
237  readPGM(I, final_filename);
238  break;
239  case FORMAT_PPM:
240  readPPM(I, final_filename);
241  break;
242  case FORMAT_JPEG:
243  readJPEG(I, final_filename, backend);
244  break;
245  case FORMAT_PNG:
246  readPNG(I, final_filename, backend);
247  break;
248  case FORMAT_TIFF:
249  case FORMAT_BMP:
250  case FORMAT_DIB:
251  case FORMAT_PBM:
252  case FORMAT_RASTER:
253  case FORMAT_JPEG2000:
254  case FORMAT_UNKNOWN:
255  try_opencv_reader = true;
256  break;
257  }
258 
259  if (try_opencv_reader) {
260 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
261  readOpenCV(I, filename);
262 #else
263  const std::string message = "Cannot read file \"" + filename + "\": No backend able to support this image format";
265 #endif
266  }
267 }
268 
291 void vpImageIo::write(const vpImage<unsigned char> &I, const std::string &filename, int backend)
292 {
293  bool try_opencv_writer = false;
294 
295  switch (getFormat(filename)) {
296  case FORMAT_PGM:
297  writePGM(I, filename);
298  break;
299  case FORMAT_PPM:
300  writePPM(I, filename);
301  break;
302  case FORMAT_JPEG:
303  writeJPEG(I, filename, backend);
304  break;
305  case FORMAT_PNG:
306  writePNG(I, filename, backend);
307  break;
308  case FORMAT_TIFF:
309  case FORMAT_BMP:
310  case FORMAT_DIB:
311  case FORMAT_PBM:
312  case FORMAT_RASTER:
313  case FORMAT_JPEG2000:
314  case FORMAT_UNKNOWN:
315  try_opencv_writer = true;
316  break;
317  }
318 
319  if (try_opencv_writer) {
320 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
321  writeOpenCV(I, filename, 90);
322 #else
323  const std::string message = "Cannot write file \"" + filename + "\": No backend able to support this image format";
325 #endif
326  }
327 }
328 
351 void vpImageIo::write(const vpImage<vpRGBa> &I, const std::string &filename, int backend)
352 {
353  bool try_opencv_writer = false;
354 
355  switch (getFormat(filename)) {
356  case FORMAT_PGM:
357  writePGM(I, filename);
358  break;
359  case FORMAT_PPM:
360  writePPM(I, filename);
361  break;
362  case FORMAT_JPEG:
363  writeJPEG(I, filename, backend);
364  break;
365  case FORMAT_PNG:
366  writePNG(I, filename, backend);
367  break;
368  case FORMAT_TIFF:
369  case FORMAT_BMP:
370  case FORMAT_DIB:
371  case FORMAT_PBM:
372  case FORMAT_RASTER:
373  case FORMAT_JPEG2000:
374  case FORMAT_UNKNOWN:
375  try_opencv_writer = true;
376  break;
377  }
378 
379  if (try_opencv_writer) {
380 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
381  writeOpenCV(I, filename, 90);
382 #else
383  const std::string message = "Cannot write file \"" + filename + "\": No backend able to support this image format";
385 #endif
386  }
387 }
388 
397 // Strategy based on benchmark: see https://github.com/lagadic/visp/pull/1004
398 // Default: 1. opencv, 2. system, 3. stb_image
399 void vpImageIo::readJPEG(vpImage<unsigned char> &I, const std::string &filename, int backend)
400 {
401  if (backend == IO_SYSTEM_LIB_BACKEND) {
402 #if !defined(VISP_HAVE_JPEG)
403  // Libjpeg backend is not available to read file \"" + filename + "\": switch to stb_image backend
404  backend = IO_STB_IMAGE_BACKEND;
405 #endif
406  }
407  else if (backend == IO_OPENCV_BACKEND) {
408 #if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
409  // OpenCV backend is not available to read file \"" + filename + "\": switch to stb_image backend
410  backend = IO_STB_IMAGE_BACKEND;
411 #endif
412  }
413  else if (backend == IO_DEFAULT_BACKEND) {
414 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
415  backend = IO_OPENCV_BACKEND;
416 #elif defined(VISP_HAVE_JPEG)
417  backend = IO_SYSTEM_LIB_BACKEND;
418 #elif defined(VISP_HAVE_SIMDLIB)
419  backend = IO_SIMDLIB_BACKEND;
420 #elif defined(VISP_HAVE_STBIMAGE)
421  backend = IO_STB_IMAGE_BACKEND;
422 #else
423  (void)I;
424  (void)filename;
425  const std::string message = "Cannot read file \"" + filename + "\": no backend available";
427 #endif
428  }
429 
430  if (backend == IO_SYSTEM_LIB_BACKEND) {
431 #if defined(VISP_HAVE_JPEG)
432  readJPEGLibjpeg(I, filename);
433 #else
434  (void)I;
435  (void)filename;
436  const std::string message = "Cannot read file \"" + filename + "\": jpeg library backend is not available";
438 #endif
439  }
440  else if (backend == IO_OPENCV_BACKEND) {
441 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
442  readOpenCV(I, filename);
443 #else
444  (void)I;
445  (void)filename;
446  const std::string message = "Cannot read file \"" + filename + "\": OpenCV library backend is not available";
448 #endif
449  }
450  else if (backend == IO_STB_IMAGE_BACKEND) {
451 #if defined(VISP_HAVE_STBIMAGE)
452  readStb(I, filename);
453 #else
454  (void)I;
455  (void)filename;
456  const std::string message = "Cannot read file \"" + filename + "\": stb_image backend is not available";
458 #endif
459  }
460  else if (backend == IO_SIMDLIB_BACKEND) {
461 #if defined(VISP_HAVE_SIMDLIB)
462  readSimdlib(I, filename);
463 #else
464  (void)I;
465  (void)filename;
466  const std::string message = "Cannot read file \"" + filename + "\": Simd library backend is not available";
468 #endif
469  }
470 }
471 
480 // Strategy based on benchmark: see https://github.com/lagadic/visp/pull/1004
481 // Default: 1. opencv, 2. system, 3. stb_image
482 void vpImageIo::readJPEG(vpImage<vpRGBa> &I, const std::string &filename, int backend)
483 {
484  if (backend == IO_SYSTEM_LIB_BACKEND) {
485 #if !defined(VISP_HAVE_JPEG)
486  // Libjpeg backend is not available to read file \"" + filename + "\": switch to stb_image backend";
487  backend = IO_STB_IMAGE_BACKEND;
488 #endif
489  }
490  else if (backend == IO_OPENCV_BACKEND) {
491 #if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
492  // OpenCV backend is not available to read file \"" + filename + "\": switch to stb_image backend";
493  backend = IO_STB_IMAGE_BACKEND;
494 #endif
495  }
496  else if (backend == IO_DEFAULT_BACKEND) {
497 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS)
498  backend = IO_OPENCV_BACKEND;
499 #elif defined(VISP_HAVE_JPEG)
500  backend = IO_SYSTEM_LIB_BACKEND;
501 #elif defined(VISP_HAVE_SIMDLIB)
502  backend = IO_SIMDLIB_BACKEND;
503 #elif defined(VISP_HAVE_STBIMAGE)
504  backend = IO_STB_IMAGE_BACKEND;
505 #else
506  (void)I;
507  (void)filename;
508  const std::string message = "Cannot read file \"" + filename + "\": no backend available";
510 #endif
511  }
512 
513  if (backend == IO_SYSTEM_LIB_BACKEND) {
514 #if defined(VISP_HAVE_JPEG)
515  readJPEGLibjpeg(I, filename);
516 #else
517  (void)I;
518  (void)filename;
519  const std::string message = "Cannot read file \"" + filename + "\": jpeg library backend is not available";
521 #endif
522  }
523  else if (backend == IO_OPENCV_BACKEND) {
524 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
525  readOpenCV(I, filename);
526 #else
527  (void)I;
528  (void)filename;
529  const std::string message = "Cannot read file \"" + filename + "\": OpenCV library backend is not available";
531 #endif
532  }
533  else if (backend == IO_STB_IMAGE_BACKEND) {
534 #if defined(VISP_HAVE_STBIMAGE)
535  readStb(I, filename);
536 #else
537  (void)I;
538  (void)filename;
539  const std::string message = "Cannot read file \"" + filename + "\": stb_image backend is not available";
541 #endif
542  }
543  else if (backend == IO_SIMDLIB_BACKEND) {
544 #if defined(VISP_HAVE_SIMDLIB)
545  readSimdlib(I, filename);
546 #else
547  (void)I;
548  (void)filename;
549  const std::string message = "Cannot read file \"" + filename + "\": Simd library backend is not available";
551 #endif
552  }
553 }
554 
563 // Strategy based on benchmark: see https://github.com/lagadic/visp/pull/1004
564 // Default: 1. system, 2. opencv, 3. stb_image
565 void vpImageIo::readPNG(vpImage<unsigned char> &I, const std::string &filename, int backend)
566 {
567  if (backend == IO_SYSTEM_LIB_BACKEND) {
568 #if !defined(VISP_HAVE_PNG)
569  // Libpng backend is not available to read file \"" + filename + "\": switch to stb_image backend";
570  backend = IO_STB_IMAGE_BACKEND;
571 #endif
572  }
573  else if (backend == IO_OPENCV_BACKEND) {
574 #if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
575  // OpenCV backend is not available to read file \"" + filename + "\": switch to stb_image backend";
576  backend = IO_STB_IMAGE_BACKEND;
577 #endif
578  }
579  else if (backend == IO_DEFAULT_BACKEND) {
580 #if defined(VISP_HAVE_PNG)
581  backend = IO_SYSTEM_LIB_BACKEND;
582 #elif defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS)
583  backend = IO_OPENCV_BACKEND;
584 #elif defined(VISP_HAVE_SIMDLIB)
585  backend = IO_SIMDLIB_BACKEND;
586 #elif defined(VISP_HAVE_STBIMAGE)
587  backend = IO_STB_IMAGE_BACKEND;
588 #else
589  (void)I;
590  (void)filename;
591  const std::string message = "Cannot read file \"" + filename + "\": no backend available";
593 #endif
594  }
595 
596  if (backend == IO_SYSTEM_LIB_BACKEND) {
597 #if defined(VISP_HAVE_PNG)
598  readPNGLibpng(I, filename);
599 #else
600  (void)I;
601  (void)filename;
602  const std::string message = "Cannot read file \"" + filename + "\": png library backend is not available";
604 #endif
605  }
606  else if (backend == IO_OPENCV_BACKEND) {
607 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
608  readOpenCV(I, filename);
609 #else
610  (void)I;
611  (void)filename;
612  const std::string message = "Cannot read file \"" + filename + "\": OpenCV library backend is not available";
614 #endif
615  }
616  else if (backend == IO_STB_IMAGE_BACKEND) {
617 #if defined(VISP_HAVE_STBIMAGE)
618  readStb(I, filename);
619 #else
620  (void)I;
621  (void)filename;
622  const std::string message = "Cannot read file \"" + filename + "\": stb_image backend is not available";
624 #endif
625  }
626  else if (backend == IO_SIMDLIB_BACKEND) {
627 #if defined(VISP_HAVE_SIMDLIB)
628  readSimdlib(I, filename);
629 #else
630  (void)I;
631  (void)filename;
632  const std::string message = "Cannot read file \"" + filename + "\": Simd library backend is not available";
634 #endif
635  }
636 }
637 
646 // Strategy based on benchmark: see https://github.com/lagadic/visp/pull/1004
647 // Default: 1. opencv, 2. stb_image
648 void vpImageIo::readPNG(vpImage<vpRGBa> &I, const std::string &filename, int backend)
649 {
650  if (backend == IO_SYSTEM_LIB_BACKEND) {
651 #if !defined(VISP_HAVE_PNG)
652  // Libpng backend is not available to read file \"" + filename + "\": switch to stb_image backend";
653  backend = IO_STB_IMAGE_BACKEND;
654 #endif
655  }
656  else if (backend == IO_OPENCV_BACKEND) {
657 #if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
658  // OpenCV backend is not available to read file \"" + filename + "\": switch to stb_image backend";
659  backend = IO_STB_IMAGE_BACKEND;
660 #endif
661  }
662  else if (backend == IO_DEFAULT_BACKEND) {
663 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
664  backend = IO_OPENCV_BACKEND;
665 #elif defined(VISP_HAVE_SIMDLIB)
666  backend = IO_SIMDLIB_BACKEND;
667 #elif defined(VISP_HAVE_STBIMAGE)
668  backend = IO_STB_IMAGE_BACKEND;
669 #else
670  (void)I;
671  (void)filename;
672  const std::string message = "Cannot read file \"" + filename + "\": no backend available";
674 #endif
675  }
676 
677  if (backend == IO_SYSTEM_LIB_BACKEND) {
678 #if defined(VISP_HAVE_PNG)
679  readPNGLibpng(I, filename);
680 #else
681  (void)I;
682  (void)filename;
683  const std::string message = "Cannot read file \"" + filename + "\": png library backend is not available";
685 #endif
686  }
687  else if (backend == IO_OPENCV_BACKEND) {
688 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
689  readOpenCV(I, filename);
690 #else
691  (void)I;
692  (void)filename;
693  const std::string message = "Cannot read file \"" + filename + "\": OpenCV library backend is not available";
695 #endif
696  }
697  else if (backend == IO_STB_IMAGE_BACKEND) {
698 #if defined(VISP_HAVE_STBIMAGE)
699  readStb(I, filename);
700 #else
701  (void)I;
702  (void)filename;
703  const std::string message = "Cannot read file \"" + filename + "\": stb_image backend is not available";
705 #endif
706  }
707  else if (backend == IO_SIMDLIB_BACKEND) {
708 #if defined(VISP_HAVE_SIMDLIB)
709  readSimdlib(I, filename);
710 #else
711  (void)I;
712  (void)filename;
713  const std::string message = "Cannot read file \"" + filename + "\": Simd library backend is not available";
715 #endif
716  }
717 }
718 
727 void vpImageIo::readEXR(vpImage<float> &I, const std::string &filename, int backend)
728 {
729  if (backend == IO_SYSTEM_LIB_BACKEND || backend == IO_SIMDLIB_BACKEND || backend == IO_STB_IMAGE_BACKEND) {
730  // This backend cannot read file \"" + filename + "\": switch to the default TinyEXR backend
731  backend = IO_DEFAULT_BACKEND;
732  }
733  else if (backend == IO_OPENCV_BACKEND) {
734 #if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
735  // OpenCV backend is not available to read file \"" + filename + "\": switch to the default TinyEXR backend
736  backend = IO_DEFAULT_BACKEND;
737 #endif
738  }
739  else if (backend == IO_DEFAULT_BACKEND) {
740 #if !defined(VISP_HAVE_TINYEXR)
741  // TinyEXR backend is not available to read file \"" + filename + "\": switch to the OpenCV backend
742  backend = IO_OPENCV_BACKEND;
743 #endif
744  }
745 
746  if (backend == IO_OPENCV_BACKEND) {
747 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
748  readOpenCV(I, filename);
749 #else
750  (void)I;
751  (void)filename;
752  const std::string message = "Cannot read file \"" + filename + "\": OpenCV backend is not available";
754 #endif
755  }
756  else if (backend == IO_DEFAULT_BACKEND) {
757 #if defined(VISP_HAVE_TINYEXR)
758  readEXRTiny(I, filename);
759 #else
760  (void)I;
761  (void)filename;
762  const std::string message = "Cannot read file \"" + filename + "\": Default TinyEXR backend is not available";
764 #endif
765  }
766 }
767 
776 void vpImageIo::readEXR(vpImage<vpRGBf> &I, const std::string &filename, int backend)
777 {
778  if (backend == IO_SYSTEM_LIB_BACKEND || backend == IO_SIMDLIB_BACKEND || backend == IO_STB_IMAGE_BACKEND) {
779  // This backend cannot read file \"" + filename + "\": switch to the default TinyEXR backend
780  backend = IO_DEFAULT_BACKEND;
781  }
782  else if (backend == IO_OPENCV_BACKEND) {
783 #if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
784  // OpenCV backend is not available to read file \"" + filename + "\": switch to the default TinyEXR backend
785  backend = IO_DEFAULT_BACKEND;
786 #endif
787  }
788  else if (backend == IO_DEFAULT_BACKEND) {
789 #if !defined(VISP_HAVE_TINYEXR)
790  // TinyEXR backend is not available to read file \"" + filename + "\": switch to the OpenCV backend
791  backend = IO_OPENCV_BACKEND;
792 #endif
793  }
794 
795  if (backend == IO_OPENCV_BACKEND) {
796 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
797  readOpenCV(I, filename);
798 #else
799  (void)I;
800  (void)filename;
801  const std::string message = "Cannot read file \"" + filename + "\": OpenCV backend is not available";
803 #endif
804  }
805  else if (backend == IO_DEFAULT_BACKEND) {
806 #if defined(VISP_HAVE_TINYEXR)
807  readEXRTiny(I, filename);
808 #else
809  (void)I;
810  (void)filename;
811  const std::string message = "Cannot read file \"" + filename + "\": TinyEXR backend is not available";
813 #endif
814  }
815 }
816 
826 // Strategy based on benchmark: see https://github.com/lagadic/visp/pull/1004
827 // Default: 1. system, 2. opencv, 3. simd
828 void vpImageIo::writeJPEG(const vpImage<unsigned char> &I, const std::string &filename, int backend, int quality)
829 {
830  if (backend == IO_SYSTEM_LIB_BACKEND) {
831 #if !defined(VISP_HAVE_JPEG)
832 #if defined(VISP_HAVE_SIMDLIB)
833  // Libjpeg backend is not available to save file \"" + filename + "\": switch to simd backend
834  backend = IO_SIMDLIB_BACKEND;
835 #else
836  // Libjpeg backend is not available to save file \"" + filename + "\": switch to stb_image backend
837  backend = IO_STB_IMAGE_BACKEND;
838 #endif
839 #endif
840  }
841  else if (backend == IO_OPENCV_BACKEND) {
842 #if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
843 #if defined(VISP_HAVE_SIMDLIB)
844  // OpenCV backend is not available to save file \"" + filename + "\": switch to simd backend
845  backend = IO_SIMDLIB_BACKEND;
846 #else
847  // OpenCV backend is not available to save file \"" + filename + "\": switch to stb_image backend
848  backend = IO_STB_IMAGE_BACKEND;
849 #endif
850 #endif
851  }
852  else if (backend == IO_DEFAULT_BACKEND) {
853 #if defined(VISP_HAVE_JPEG)
854  backend = IO_SYSTEM_LIB_BACKEND;
855 #elif defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS)
856  backend = IO_OPENCV_BACKEND;
857 #elif defined(VISP_HAVE_SIMDLIB)
858  backend = IO_SIMDLIB_BACKEND;
859 #elif defined(VISP_HAVE_STBIMAGE)
860  backend = IO_STB_IMAGE_BACKEND;
861 #else
862  (void)I;
863  (void)filename;
864  const std::string message = "Cannot save file \"" + filename + "\": no available backend";
866 #endif
867  }
868 
869  if (backend == IO_SYSTEM_LIB_BACKEND) {
870 #if defined(VISP_HAVE_JPEG)
871  writeJPEGLibjpeg(I, filename, quality);
872 #else
873  (void)I;
874  (void)filename;
875  (void)quality;
876  const std::string message = "Cannot save file \"" + filename + "\": jpeg backend is not available";
878 #endif
879  }
880  else if (backend == IO_OPENCV_BACKEND) {
881 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) \
882  && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
883  writeOpenCV(I, filename, quality);
884 #else
885  (void)I;
886  (void)filename;
887  (void)quality;
888  const std::string message = "Cannot save file \"" + filename + "\": OpenCV backend is not available";
890 #endif
891  }
892  else if (backend == IO_SIMDLIB_BACKEND) {
893 #if defined(VISP_HAVE_SIMDLIB)
894  writeJPEGSimdlib(I, filename, quality);
895 #else
896  (void)I;
897  (void)filename;
898  (void)quality;
899  const std::string message = "Cannot save file \"" + filename + "\": Simd library backend is not available";
901 #endif
902  }
903  else if (backend == IO_STB_IMAGE_BACKEND) {
904 #if defined(VISP_HAVE_STBIMAGE)
905  writeJPEGStb(I, filename, quality);
906 #else
907  (void)I;
908  (void)filename;
909  (void)quality;
910  const std::string message = "Cannot save file \"" + filename + "\": stb_image backend is not available";
912 #endif
913  }
914 }
915 
925 // Strategy based on benchmark: see https://github.com/lagadic/visp/pull/1004
926 // Default: 1. system, 2. opencv, , 3. simd
927 void vpImageIo::writeJPEG(const vpImage<vpRGBa> &I, const std::string &filename, int backend, int quality)
928 {
929  if (backend == IO_SYSTEM_LIB_BACKEND) {
930 #if !defined(VISP_HAVE_JPEG)
931 #if defined(VISP_HAVE_SIMDLIB)
932  // Libjpeg backend is not available to save file \"" + filename + "\": switch to simd backend
933  backend = IO_SIMDLIB_BACKEND;
934 #else
935  // Libjpeg backend is not available to save file \"" + filename + "\": switch to stb_image backend
936  backend = IO_STB_IMAGE_BACKEND;
937 #endif
938 #endif
939  }
940  else if (backend == IO_OPENCV_BACKEND) {
941 #if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
942 #if defined(VISP_HAVE_SIMDLIB)
943  // OpenCV backend is not available to save file \"" + filename + "\": switch to simd backend
944  backend = IO_SIMDLIB_BACKEND;
945 #else
946  // OpenCV backend is not available to save file \"" + filename + "\": switch to stb_image backend
947  backend = IO_STB_IMAGE_BACKEND;
948 #endif
949 #endif
950  }
951  else if (backend == IO_DEFAULT_BACKEND) {
952 #if defined(VISP_HAVE_JPEG)
953  backend = IO_SYSTEM_LIB_BACKEND;
954 #elif defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS)
955  backend = IO_OPENCV_BACKEND;
956 #elif defined(VISP_HAVE_SIMDLIB)
957  backend = IO_SIMDLIB_BACKEND;
958 #elif defined(VISP_HAVE_STBIMAGE)
959  backend = IO_STB_IMAGE_BACKEND;
960 #else
961  (void)I;
962  (void)filename;
963  const std::string message = "Cannot save file \"" + filename + "\": no backend available";
965 #endif
966  }
967 
968  if (backend == IO_SYSTEM_LIB_BACKEND) {
969 #if defined(VISP_HAVE_JPEG)
970  writeJPEGLibjpeg(I, filename, quality);
971 #else
972  (void)I;
973  (void)filename;
974  (void)quality;
975  const std::string message = "Cannot save file \"" + filename + "\": jpeg library backend is not available";
977 #endif
978  }
979  else if (backend == IO_OPENCV_BACKEND) {
980 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
981  writeOpenCV(I, filename, quality);
982 #else
983  (void)I;
984  (void)filename;
985  (void)quality;
986  const std::string message = "Cannot save file \"" + filename + "\": OpenCV library backend is not available";
988 #endif
989  }
990  else if (backend == IO_SIMDLIB_BACKEND) {
991 #if defined(VISP_HAVE_SIMDLIB)
992  writeJPEGSimdlib(I, filename, quality);
993 #else
994  (void)I;
995  (void)filename;
996  (void)quality;
997  const std::string message = "Cannot save file \"" + filename + "\": Simd library backend is not available";
999 #endif
1000  }
1001  else if (backend == IO_STB_IMAGE_BACKEND) {
1002 #if defined(VISP_HAVE_STBIMAGE)
1003  writeJPEGStb(I, filename, quality);
1004 #else
1005  (void)I;
1006  (void)filename;
1007  (void)quality;
1008  const std::string message = "Cannot save file \"" + filename + "\": stb_image backend is not available";
1010 #endif
1011  }
1012 }
1013 
1022 // Strategy based on benchmark: see https://github.com/lagadic/visp/pull/1004
1023 // Default: 1. opencv, 2. simd
1024 void vpImageIo::writePNG(const vpImage<unsigned char> &I, const std::string &filename, int backend)
1025 {
1026  if (backend == IO_SYSTEM_LIB_BACKEND) {
1027 #if !defined(VISP_HAVE_PNG)
1028 #if defined(VISP_HAVE_SIMDLIB)
1029  // Libpng backend is not available to save file \"" + filename + "\": switch to simd backend
1030  backend = IO_SIMDLIB_BACKEND;
1031 #else
1032  // Libpng backend is not available to save file \"" + filename + "\": switch to stb_image backend
1033  backend = IO_STB_IMAGE_BACKEND;
1034 #endif
1035 #endif
1036  }
1037  else if (backend == IO_OPENCV_BACKEND) {
1038 #if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
1039 #if defined(VISP_HAVE_SIMDLIB)
1040  // OpenCV backend is not available to save file \"" + filename + "\": switch to simd backend
1041  backend = IO_SIMDLIB_BACKEND;
1042 #else
1043  // OpenCV backend is not available to save file \"" + filename + "\": switch to stb_image backend
1044  backend = IO_STB_IMAGE_BACKEND;
1045 #endif
1046 #endif
1047  }
1048  else if (backend == IO_DEFAULT_BACKEND) {
1049 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
1050  backend = IO_OPENCV_BACKEND;
1051 #elif defined(VISP_HAVE_SIMDLIB)
1052  backend = IO_SIMDLIB_BACKEND;
1053 #elif defined(VISP_HAVE_STBIMAGE)
1054  backend = IO_STB_IMAGE_BACKEND;
1055 #else
1056  (void)I;
1057  (void)filename;
1058  const std::string message = "Cannot save file \"" + filename + "\": no backend available";
1060 #endif
1061  }
1062 
1063  if (backend == IO_OPENCV_BACKEND) {
1064 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
1065  writeOpenCV(I, filename, 90);
1066 #else
1067  (void)I;
1068  (void)filename;
1069  const std::string message = "Cannot save file \"" + filename + "\": OpenCV library backend is not available";
1071 #endif
1072  }
1073  else if (backend == IO_SIMDLIB_BACKEND) {
1074 #if defined(VISP_HAVE_SIMDLIB)
1075  writePNGSimdlib(I, filename);
1076 #else
1077  (void)I;
1078  (void)filename;
1079  const std::string message = "Cannot save file \"" + filename + "\": Simd library backend is not available";
1081 #endif
1082  }
1083  else if (backend == IO_STB_IMAGE_BACKEND) {
1084 #if defined(VISP_HAVE_STBIMAGE)
1085  writePNGStb(I, filename);
1086 #else
1087  (void)I;
1088  (void)filename;
1089  const std::string message = "Cannot save file \"" + filename + "\": stb_image backend is not available";
1091 #endif
1092  }
1093  else if (backend == IO_SYSTEM_LIB_BACKEND) {
1094 #if defined(VISP_HAVE_PNG)
1095  writePNGLibpng(I, filename);
1096 #else
1097  (void)I;
1098  (void)filename;
1099  const std::string message = "Cannot save file \"" + filename + "\": png library backend is not available";
1101 #endif
1102  }
1103 }
1104 
1113 // Strategy based on benchmark: see https://github.com/lagadic/visp/pull/1004
1114 // Default: 1. opencv, 2. system, 3. simd
1115 void vpImageIo::writePNG(const vpImage<vpRGBa> &I, const std::string &filename, int backend)
1116 {
1117  if (backend == IO_SYSTEM_LIB_BACKEND) {
1118 #if !defined(VISP_HAVE_PNG)
1119 #if defined(VISP_HAVE_SIMDLIB)
1120  // Libpng backend is not available to save file \"" + filename + "\": switch to simd backend
1121  backend = IO_SIMDLIB_BACKEND;
1122 #else
1123  // Libpng backend is not available to save file \"" + filename + "\": switch to stb_image backend
1124  backend = IO_STB_IMAGE_BACKEND;
1125 #endif
1126 #endif
1127  }
1128  else if (backend == IO_OPENCV_BACKEND) {
1129 #if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
1130 #if defined(VISP_HAVE_SIMDLIB)
1131  // OpenCV backend is not available to save file \"" + filename + "\": switch to simd backend
1132  backend = IO_SIMDLIB_BACKEND;
1133 #else
1134  // OpenCV backend is not available to save file \"" + filename + "\": switch to stb_image backend
1135  backend = IO_STB_IMAGE_BACKEND;
1136 #endif
1137 #endif
1138  }
1139  else if (backend == IO_DEFAULT_BACKEND) {
1140 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
1141  backend = IO_OPENCV_BACKEND;
1142 #elif defined(VISP_HAVE_SIMDLIB)
1143  backend = IO_SIMDLIB_BACKEND;
1144 #elif defined(VISP_HAVE_STBIMAGE)
1145  backend = IO_STB_IMAGE_BACKEND;
1146 #else
1147  (void)I;
1148  (void)filename;
1149  const std::string message = "Cannot save file \"" + filename + "\": no backend available";
1151 #endif
1152  }
1153 
1154  if (backend == IO_OPENCV_BACKEND) {
1155 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
1156  writeOpenCV(I, filename, 90);
1157 #else
1158  (void)I;
1159  (void)filename;
1160  const std::string message = "Cannot save file \"" + filename + "\": OpenCV backend is not available";
1162 #endif
1163  }
1164  else if (backend == IO_SIMDLIB_BACKEND) {
1165 #if defined(VISP_HAVE_SIMDLIB)
1166  writePNGSimdlib(I, filename);
1167 #else
1168  (void)I;
1169  (void)filename;
1170  const std::string message = "Cannot save file \"" + filename + "\": Simd library backend is not available";
1172 #endif
1173  }
1174  else if (backend == IO_STB_IMAGE_BACKEND) {
1175 #if defined(VISP_HAVE_STBIMAGE)
1176  writePNGStb(I, filename);
1177 #else
1178  (void)I;
1179  (void)filename;
1180  const std::string message = "Cannot save file \"" + filename + "\": stb_image backend is not available";
1182 #endif
1183  }
1184  else if (backend == IO_SYSTEM_LIB_BACKEND) {
1185 #if defined(VISP_HAVE_PNG)
1186  writePNGLibpng(I, filename);
1187 #else
1188  (void)I;
1189  (void)filename;
1190  const std::string message = "Cannot save file \"" + filename + "\": libpng backend is not available";
1192 #endif
1193  }
1194 }
1195 
1204 void vpImageIo::writeEXR(const vpImage<float> &I, const std::string &filename, int backend)
1205 {
1206  if (backend == IO_SYSTEM_LIB_BACKEND || backend == IO_SIMDLIB_BACKEND || backend == IO_STB_IMAGE_BACKEND) {
1207  // This backend cannot save file \"" + filename + "\": switch to the default TinyEXR backend
1208  backend = IO_DEFAULT_BACKEND;
1209  }
1210  else if (backend == IO_OPENCV_BACKEND) {
1211 #if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
1212  (void)I;
1213  // OpenCV backend is not available to save file \"" + filename + "\": switch to the default TinyEXR backend
1214  backend = IO_DEFAULT_BACKEND;
1215 #endif
1216  }
1217  else if (backend == IO_DEFAULT_BACKEND) {
1218 #if !defined(VISP_HAVE_TINYEXR)
1219  // TinyEXR backend is not available to save file \"" + filename + "\": switch to the OpenCV backend
1220  backend = IO_OPENCV_BACKEND;
1221 #endif
1222  }
1223 
1224  if (backend == IO_OPENCV_BACKEND) {
1225 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
1226  writeOpenCV(I, filename);
1227 #else
1228  (void)I;
1229  (void)filename;
1230  const std::string message = "Cannot save file \"" + filename + "\": OpenCV backend is not available";
1232 #endif
1233  }
1234  else if (backend == IO_DEFAULT_BACKEND) {
1235 #if defined(VISP_HAVE_TINYEXR)
1236  writeEXRTiny(I, filename);
1237 #else
1238  (void)I;
1239  (void)filename;
1240  const std::string message = "Cannot save file \"" + filename + "\": TinyEXR backend is not available";
1242 #endif
1243  }
1244 }
1245 
1254 void vpImageIo::writeEXR(const vpImage<vpRGBf> &I, const std::string &filename, int backend)
1255 {
1256  if (backend == IO_SYSTEM_LIB_BACKEND || backend == IO_SIMDLIB_BACKEND || backend == IO_STB_IMAGE_BACKEND) {
1257  // This backend cannot save file \"" + filename + "\": switch to the default TinyEXR backend
1258  backend = IO_DEFAULT_BACKEND;
1259  }
1260  else if (backend == IO_OPENCV_BACKEND) {
1261 #if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
1262  // OpenCV backend is not available to save file \"" + filename + "\": switch to the default TinyEXR backend
1263  backend = IO_DEFAULT_BACKEND;
1264 #endif
1265  }
1266  else if (backend == IO_DEFAULT_BACKEND) {
1267 #if !defined(VISP_HAVE_TINYEXR)
1268  // TinyEXR backend is not available to save file \"" + filename + "\": switch to the OpenCV backend
1269  backend = IO_OPENCV_BACKEND;
1270 #endif
1271  }
1272 
1273  if (backend == IO_OPENCV_BACKEND) {
1274 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
1275  writeOpenCV(I, filename);
1276 #else
1277  (void)I;
1278  (void)filename;
1279  const std::string message = "Cannot save file \"" + filename + "\": OpenCV backend is not available";
1281 #endif
1282  }
1283  else if (backend == IO_DEFAULT_BACKEND) {
1284 #if defined(VISP_HAVE_TINYEXR)
1285  writeEXRTiny(I, filename);
1286 #else
1287  (void)I;
1288  (void)filename;
1289  const std::string message = "Cannot save file \"" + filename + "\": TinyEXR backend is not available";
1291 #endif
1292  }
1293 }
1294 
1300 void vpImageIo::writePFM(const vpImage<float> &I, const std::string &filename) { vp_writePFM(I, filename); }
1301 
1308 void vpImageIo::writePFM_HDR(const vpImage<float> &I, const std::string &filename) { vp_writePFM_HDR(I, filename); }
1309 
1316 void vpImageIo::writePFM_HDR(const vpImage<vpRGBf> &I, const std::string &filename) { vp_writePFM_HDR(I, filename); }
1317 
1323 void vpImageIo::writePGM(const vpImage<unsigned char> &I, const std::string &filename) { vp_writePGM(I, filename); }
1324 
1330 void vpImageIo::writePGM(const vpImage<short> &I, const std::string &filename) { vp_writePGM(I, filename); }
1331 
1337 void vpImageIo::writePGM(const vpImage<vpRGBa> &I, const std::string &filename) { vp_writePGM(I, filename); }
1338 
1344 void vpImageIo::readPFM(vpImage<float> &I, const std::string &filename) { vp_readPFM(I, filename); }
1345 
1351 void vpImageIo::readPFM_HDR(vpImage<float> &I, const std::string &filename) { vp_readPFM_HDR(I, filename); }
1352 
1358 void vpImageIo::readPFM_HDR(vpImage<vpRGBf> &I, const std::string &filename) { vp_readPFM_HDR(I, filename); }
1359 
1365 void vpImageIo::readPGM(vpImage<unsigned char> &I, const std::string &filename) { vp_readPGM(I, filename); }
1366 
1372 void vpImageIo::readPGM(vpImage<vpRGBa> &I, const std::string &filename) { vp_readPGM(I, filename); }
1373 
1379 void vpImageIo::readPPM(vpImage<unsigned char> &I, const std::string &filename) { vp_readPPM(I, filename); }
1380 
1386 void vpImageIo::readPPM(vpImage<vpRGBa> &I, const std::string &filename) { vp_readPPM(I, filename); }
1387 
1393 void vpImageIo::writePPM(const vpImage<unsigned char> &I, const std::string &filename) { vp_writePPM(I, filename); }
1394 
1400 void vpImageIo::writePPM(const vpImage<vpRGBa> &I, const std::string &filename) { vp_writePPM(I, filename); }
1401 
1409 void vpImageIo::readPNGfromMem(const std::vector<unsigned char> &buffer, vpImage<unsigned char> &I, int backend)
1410 {
1411  if (backend == IO_SYSTEM_LIB_BACKEND || backend == IO_SIMDLIB_BACKEND) {
1412  backend = IO_STB_IMAGE_BACKEND;
1413  }
1414  else if (backend == IO_OPENCV_BACKEND) {
1415 #if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
1416  backend = IO_STB_IMAGE_BACKEND;
1417 #endif
1418  }
1419  else if (backend == IO_DEFAULT_BACKEND) {
1420 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS)
1421  backend = IO_OPENCV_BACKEND;
1422 #elif defined(VISP_HAVE_STBIMAGE)
1423  backend = IO_STB_IMAGE_BACKEND;
1424 #else
1425  (void)I;
1426  (void)buffer;
1427  const std::string message = "Cannot in-memory png read: OpenCV or std_image backend are not available";
1429 #endif
1430  }
1431 
1432  if (backend == IO_OPENCV_BACKEND) {
1433 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) \
1434  && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
1435  readPNGfromMemOpenCV(buffer, I);
1436  (void)backend;
1437 #else
1438  (void)buffer;
1439  (void)I;
1440  (void)backend;
1441  const std::string message = "Cannot in-memory png read: OpenCV backend is not available";
1443 #endif
1444  }
1445  else {
1446 #if defined(VISP_HAVE_STBIMAGE)
1447  readPNGfromMemStb(buffer, I);
1448  (void)backend;
1449 #else
1450  (void)buffer;
1451  (void)I;
1452  (void)backend;
1453  const std::string message = "Cannot in-memory png read: std_image backend is not available";
1455 #endif
1456  }
1457 }
1458 
1466 void vpImageIo::readPNGfromMem(const std::vector<unsigned char> &buffer, vpImage<vpRGBa> &I, int backend)
1467 {
1468  if (backend == IO_SYSTEM_LIB_BACKEND || backend == IO_SIMDLIB_BACKEND) {
1469  backend = IO_STB_IMAGE_BACKEND;
1470  }
1471  else if (backend == IO_OPENCV_BACKEND) {
1472 #if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
1473  backend = IO_STB_IMAGE_BACKEND;
1474 #endif
1475  }
1476  else if (backend == IO_DEFAULT_BACKEND) {
1477 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS)
1478  backend = IO_OPENCV_BACKEND;
1479 #elif defined(VISP_HAVE_STBIMAGE)
1480  backend = IO_STB_IMAGE_BACKEND;
1481 #else
1482  (void)I;
1483  (void)buffer;
1484  const std::string message = "Cannot in-memory png read: OpenCV or std_image backend are not available";
1486 #endif
1487  }
1488 
1489  if (backend == IO_OPENCV_BACKEND) {
1490 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) \
1491  && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
1492  readPNGfromMemOpenCV(buffer, I);
1493  (void)backend;
1494 #else
1495  (void)buffer;
1496  (void)I;
1497  (void)backend;
1498  const std::string message = "Cannot in-memory png read: OpenCV backend is not available";
1500 #endif
1501  }
1502  else {
1503 #if defined(VISP_HAVE_STBIMAGE)
1504  readPNGfromMemStb(buffer, I);
1505  (void)backend;
1506 #else
1507  (void)buffer;
1508  (void)I;
1509  (void)backend;
1510  const std::string message = "Cannot in-memory png read: std_image backend is not available";
1512 #endif
1513  }
1514 }
1515 
1524 void vpImageIo::writePNGtoMem(const vpImage<unsigned char> &I, std::vector<unsigned char> &buffer, int backend)
1525 {
1526  if (backend == IO_SYSTEM_LIB_BACKEND || backend == IO_SIMDLIB_BACKEND) {
1527  backend = IO_STB_IMAGE_BACKEND;
1528  }
1529  else if (backend == IO_OPENCV_BACKEND) {
1530 #if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
1531  backend = IO_STB_IMAGE_BACKEND;
1532 #endif
1533  }
1534  else if (backend == IO_DEFAULT_BACKEND) {
1535 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS)
1536  backend = IO_OPENCV_BACKEND;
1537 #elif defined(VISP_HAVE_STBIMAGE)
1538  backend = IO_STB_IMAGE_BACKEND;
1539 #else
1540  (void)I;
1541  (void)buffer;
1542  const std::string message = "Cannot in-memory png write: OpenCV or std_image backend are not available";
1544 #endif
1545  }
1546 
1547  if (backend == IO_OPENCV_BACKEND) {
1548 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) \
1549  && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
1550  writePNGtoMemOpenCV(I, buffer);
1551 #else
1552  (void)I;
1553  (void)buffer;
1554  const std::string message = "Cannot in-memory png write: OpenCV backend is not available";
1556 #endif
1557  }
1558  else if (backend == IO_STB_IMAGE_BACKEND) {
1559 #if defined(VISP_HAVE_STBIMAGE)
1560  writePNGtoMemStb(I, buffer);
1561 #else
1562  (void)I;
1563  (void)buffer;
1564  const std::string message = "Cannot in-memory png write: std_image backend is not available";
1566 #endif
1567  }
1568  else {
1569 #if VISP_CXX_STANDARD > VISP_CXX_STANDARD_98
1570  const std::string message = "The " + std::to_string(backend) + " backend is not available.";
1572 #else
1573  throw(vpImageException(vpImageException::ioError, "The %d backend is not available", backend));
1574 #endif
1575  }
1576 }
1577 
1587 void vpImageIo::writePNGtoMem(const vpImage<vpRGBa> &I, std::vector<unsigned char> &buffer, int backend, bool saveAlpha)
1588 {
1589  if (backend == IO_SYSTEM_LIB_BACKEND || backend == IO_SIMDLIB_BACKEND) {
1590  backend = IO_STB_IMAGE_BACKEND;
1591  }
1592  else if (backend == IO_OPENCV_BACKEND) {
1593 #if !(defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS))
1594  backend = IO_STB_IMAGE_BACKEND;
1595 #endif
1596  }
1597  else if (backend == IO_DEFAULT_BACKEND) {
1598 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS)
1599  backend = IO_OPENCV_BACKEND;
1600 #elif defined(VISP_HAVE_STBIMAGE)
1601  backend = IO_STB_IMAGE_BACKEND;
1602 #else
1603  (void)I;
1604  (void)buffer;
1605  const std::string message = "Cannot in-memory png write: OpenCV or std_image backend are not available";
1607 #endif
1608  }
1609 
1610  if (backend == IO_OPENCV_BACKEND) {
1611 #if ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) \
1612  && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
1613  writePNGtoMemOpenCV(I, buffer, saveAlpha);
1614 #else
1615  (void)I;
1616  (void)buffer;
1617  const std::string message = "Cannot in-memory png write: OpenCV backend is not available";
1619 #endif
1620  }
1621  else if (backend == IO_STB_IMAGE_BACKEND) {
1622 #if defined(VISP_HAVE_STBIMAGE)
1623  writePNGtoMemStb(I, buffer, saveAlpha);
1624 #else
1625  (void)I;
1626  (void)buffer;
1627  const std::string message = "Cannot in-memory png write: std_image backend is not available";
1629 #endif
1630  }
1631  else {
1632 #if VISP_CXX_STANDARD > VISP_CXX_STANDARD_98
1633  const std::string message = "The " + std::to_string(backend) + " backend is not available.";
1635 #else
1636  throw(vpImageException(vpImageException::ioError, "The %d backend is not available", backend));
1637 #endif
1638  }
1639 }
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:1300
static void readPGM(vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:1365
static void writeJPEG(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND, int quality=90)
Definition: vpImageIo.cpp:828
static void readJPEG(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:399
static void readPFM(vpImage< float > &I, const std::string &filename)
Definition: vpImageIo.cpp:1344
static void writePNG(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:1024
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:147
static void readPFM_HDR(vpImage< float > &I, const std::string &filename)
Definition: vpImageIo.cpp:1351
static void readPPM(vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:1379
static void writePFM_HDR(const vpImage< float > &I, const std::string &filename)
Definition: vpImageIo.cpp:1308
static void readPNGfromMem(const std::vector< unsigned char > &buffer, vpImage< unsigned char > &I, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:1409
static void readEXR(vpImage< float > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:727
static void writePGM(const vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:1323
static void writeEXR(const vpImage< float > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:1204
static void writePNGtoMem(const vpImage< unsigned char > &I, std::vector< unsigned char > &buffer, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:1524
static void readPNG(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:565
static void writePPM(const vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:1393
static void write(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:291
static std::string path(const std::string &pathname)
Definition: vpIoTools.cpp:1005
static bool checkFilename(const std::string &filename)
Definition: vpIoTools.cpp:786