Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vpImageIo.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
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 http://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  * Authors:
34  * Eric Marchand
35  *
36  *****************************************************************************/
37 
43 #include <visp3/core/vpImage.h>
44 #include <visp3/io/vpImageIo.h>
45 #include <visp3/core/vpImageConvert.h> //image conversion
46 #include <visp3/core/vpIoTools.h>
47 
48 void vp_decodeHeaderPNM(const std::string &filename, std::ifstream &fd, const std::string &magic,
49  unsigned int &w, unsigned int &h, unsigned int &maxval);
50 
51 #ifndef DOXYGEN_SHOULD_SKIP_THIS
52 
61 void vp_decodeHeaderPNM(const std::string &filename, std::ifstream &fd, const std::string &magic, unsigned int &w, unsigned int &h, unsigned int &maxval)
62 {
63  std::string line;
64  unsigned int nb_elt = 4, cpt_elt=0;
65  while(cpt_elt != nb_elt) {
66  // Skip empty lines or lines starting with # (comment)
67  while (std::getline(fd, line) && (line.compare(0, 1, "#") == 0 || line.size() == 0)) {};
68 
69  if (fd.eof()) {
70  fd.close();
72  "Cannot read header of file \"%s\"", filename.c_str()));
73  }
74 
75  std::vector<std::string> header = vpIoTools::splitChain(line, std::string(" "));
76 
77  if(header.size() == 0) {
78  fd.close();
80  "Cannot read header of file \"%s\"", filename.c_str()));
81  }
82 
83  if (cpt_elt == 0) { // decode magic
84  if (header[0].compare(0, magic.size(), magic) != 0) {
85  fd.close();
87  "\"%s\" is not a PNM file with magic number %s", filename.c_str(), magic.c_str()));
88  }
89  cpt_elt ++;
90  header.erase(header.begin(), header.begin()+1); // erase first element that is processed
91  }
92  while(header.size()) {
93  if (cpt_elt == 1) { // decode width
94  std::istringstream ss(header[0]);
95  ss >> w;
96  cpt_elt ++;
97  header.erase(header.begin(), header.begin()+1); // erase first element that is processed
98  }
99  else if (cpt_elt == 2) { // decode height
100  std::istringstream ss(header[0]);
101  ss >> h;
102  cpt_elt ++;
103  header.erase(header.begin(), header.begin()+1); // erase first element that is processed
104  }
105  else if (cpt_elt == 3) { // decode maxval
106  std::istringstream ss(header[0]);
107  ss >> maxval;
108  cpt_elt ++;
109  header.erase(header.begin(), header.begin()+1); // erase first element that is processed
110  }
111  }
112  }
113 }
114 #endif
115 
116 vpImageIo::vpImageFormatType
117 vpImageIo::getFormat(const std::string &filename)
118 {
119  std::string ext = vpImageIo::getExtension(filename);
120 
121  if (ext.compare(".PGM") == 0)
122  return FORMAT_PGM;
123  else if (ext.compare(".pgm") == 0)
124  return FORMAT_PGM;
125  else if (ext.compare(".PPM") == 0)
126  return FORMAT_PPM;
127  else if (ext.compare(".ppm") == 0)
128  return FORMAT_PPM;
129  else if (ext.compare(".JPG") == 0)
130  return FORMAT_JPEG;
131  else if (ext.compare(".jpg") == 0)
132  return FORMAT_JPEG;
133  else if (ext.compare(".JPEG") == 0)
134  return FORMAT_JPEG;
135  else if (ext.compare(".jpeg") == 0)
136  return FORMAT_JPEG;
137  else if (ext.compare(".PNG") == 0)
138  return FORMAT_PNG;
139  else if (ext.compare(".png") == 0)
140  return FORMAT_PNG;
141  // Formats supported by opencv
142  else if (ext.compare(".TIFF") == 0)
143  return FORMAT_TIFF;
144  else if (ext.compare(".tiff") == 0)
145  return FORMAT_TIFF;
146  else if (ext.compare(".TIF") == 0)
147  return FORMAT_TIFF;
148  else if (ext.compare(".tif") == 0)
149  return FORMAT_TIFF;
150  else if (ext.compare(".BMP") == 0)
151  return FORMAT_BMP;
152  else if (ext.compare(".bmp") == 0)
153  return FORMAT_BMP;
154  else if (ext.compare(".DIB") == 0)
155  return FORMAT_DIB;
156  else if (ext.compare(".dib") == 0)
157  return FORMAT_DIB;
158  else if (ext.compare(".PBM") == 0)
159  return FORMAT_PBM;
160  else if (ext.compare(".pbm") == 0)
161  return FORMAT_PBM;
162  else if (ext.compare(".SR") == 0)
163  return FORMAT_RASTER;
164  else if (ext.compare(".sr") == 0)
165  return FORMAT_RASTER;
166  else if (ext.compare(".RAS") == 0)
167  return FORMAT_RASTER;
168  else if (ext.compare(".ras") == 0)
169  return FORMAT_RASTER;
170  else if (ext.compare(".JP2") == 0)
171  return FORMAT_JPEG2000;
172  else if (ext.compare(".jp2") == 0)
173  return FORMAT_JPEG2000;
174  else
175  return FORMAT_UNKNOWN;
176 }
177 
178 // return the extension of the file including the dot
179 std::string vpImageIo::getExtension(const std::string &filename)
180 {
181  // extract the extension
182  size_t dot = filename.find_last_of(".");
183  std::string ext = filename.substr(dot, filename.size()-1);
184  return ext;
185 }
186 
187 
204 void
205 vpImageIo::read(vpImage<unsigned char> &I, const std::string &filename)
206 {
207  bool exist = vpIoTools::checkFilename(filename);
208  if (!exist) {
209  std::string message = "Cannot read file: \"" + std::string(filename) + "\" doesn't exist";
211  }
212 
213  //Allows to use ~ symbol or env variables in path
214  std::string final_filename = vpIoTools::path(filename);
215 
216  bool try_opencv_reader = false;
217 
218  switch(getFormat(final_filename.c_str())){
219  case FORMAT_PGM :
220  readPGM(I,final_filename); break;
221  case FORMAT_PPM :
222  readPPM(I,final_filename); break;
223  case FORMAT_JPEG :
224 #ifdef VISP_HAVE_JPEG
225  readJPEG(I,final_filename);
226 #else
227  try_opencv_reader = true;
228 #endif
229  break;
230  case FORMAT_PNG :
231 #if defined(VISP_HAVE_PNG)
232  readPNG(I,final_filename);
233 #else
234  try_opencv_reader = true;
235 #endif
236  break;
237  case FORMAT_TIFF :
238  case FORMAT_BMP :
239  case FORMAT_DIB :
240  case FORMAT_PBM :
241  case FORMAT_RASTER :
242  case FORMAT_JPEG2000 :
243  case FORMAT_UNKNOWN :
244  try_opencv_reader = true;
245  break;
246  }
247 
248  if (try_opencv_reader) {
249 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
250  //std::cout << "Use opencv to read the image" << std::endl;
251  cv::Mat cvI = cv::imread(final_filename, cv::IMREAD_GRAYSCALE);
252  if (cvI.cols == 0 && cvI.rows == 0) {
253  std::string message = "Cannot read file \"" + std::string(final_filename) + "\": Image format not supported";
254  throw (vpImageException(vpImageException::ioError, message)) ;
255  }
256  vpImageConvert::convert(cvI, I);
257 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
258  //std::cout << "Use opencv to read the image" << std::endl;
259  cv::Mat cvI = cv::imread(final_filename, CV_LOAD_IMAGE_GRAYSCALE);
260  if (cvI.cols == 0 && cvI.rows == 0) {
261  std::string message = "Cannot read file \"" + std::string(final_filename) + "\": Image format not supported";
262  throw (vpImageException(vpImageException::ioError, message)) ;
263  }
264  vpImageConvert::convert(cvI, I);
265 #else
266  std::string message = "Cannot read file \"" + std::string(final_filename) + "\": Image format not supported";
267  throw (vpImageException(vpImageException::ioError, message)) ;
268 #endif
269  }
270 }
271 
288 void
289 vpImageIo::read(vpImage<vpRGBa> &I, const std::string &filename)
290 {
291  bool exist = vpIoTools::checkFilename(filename);
292  if (!exist) {
293  std::string message = "Cannot read file: \"" + std::string(filename) + "\" doesn't exist";
295  }
296  //Allows to use ~ symbol or env variables in path
297  std::string final_filename = vpIoTools::path(filename);
298 
299  bool try_opencv_reader = false;
300 
301  switch(getFormat(final_filename.c_str())){
302  case FORMAT_PGM :
303  readPGM(I,final_filename); break;
304  case FORMAT_PPM :
305  readPPM(I,final_filename); break;
306  case FORMAT_JPEG :
307 #ifdef VISP_HAVE_JPEG
308  readJPEG(I,final_filename);
309 #else
310  try_opencv_reader = true;
311 #endif
312  break;
313  case FORMAT_PNG :
314 #if defined(VISP_HAVE_PNG)
315  readPNG(I,final_filename);
316 #else
317  try_opencv_reader = true;
318 #endif
319  break;
320  case FORMAT_TIFF :
321  case FORMAT_BMP :
322  case FORMAT_DIB :
323  case FORMAT_PBM :
324  case FORMAT_RASTER :
325  case FORMAT_JPEG2000 :
326  case FORMAT_UNKNOWN :
327  try_opencv_reader = true;
328  break;
329  }
330 
331  if (try_opencv_reader) {
332 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
333  // std::cout << "Use opencv to read the image" << std::endl;
334  cv::Mat cvI = cv::imread(final_filename, cv::IMREAD_COLOR);
335  if (cvI.cols == 0 && cvI.rows == 0) {
336  std::string message = "Cannot read file \"" + std::string(final_filename) + "\": Image format not supported";
337  throw (vpImageException(vpImageException::ioError, message)) ;
338  }
339  vpImageConvert::convert(cvI, I);
340 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
341  // std::cout << "Use opencv to read the image" << std::endl;
342  cv::Mat cvI = cv::imread(final_filename, CV_LOAD_IMAGE_COLOR);
343  if (cvI.cols == 0 && cvI.rows == 0) {
344  std::string message = "Cannot read file \"" + std::string(final_filename) + "\": Image format not supported";
345  throw (vpImageException(vpImageException::ioError, message)) ;
346  }
347  vpImageConvert::convert(cvI, I);
348 #else
349  std::string message = "Cannot read file \"" + std::string(final_filename) + "\": Image format not supported";
350  throw (vpImageException(vpImageException::ioError, message)) ;
351 #endif
352  }
353 }
354 
367 void
368 vpImageIo::write(const vpImage<unsigned char> &I, const std::string &filename)
369 {
370  bool try_opencv_writer = false;
371 
372  switch(getFormat(filename)){
373  case FORMAT_PGM :
374  writePGM(I,filename); break;
375  case FORMAT_PPM :
376  writePPM(I,filename); break;
377  case FORMAT_JPEG :
378 #ifdef VISP_HAVE_JPEG
379  writeJPEG(I,filename);
380 #else
381  try_opencv_writer = true;
382 #endif
383  break;
384  case FORMAT_PNG :
385 #ifdef VISP_HAVE_PNG
386  writePNG(I,filename);
387 #else
388  try_opencv_writer = true;
389 #endif
390  break;
391  case FORMAT_TIFF :
392  case FORMAT_BMP :
393  case FORMAT_DIB :
394  case FORMAT_PBM :
395  case FORMAT_RASTER :
396  case FORMAT_JPEG2000 :
397  case FORMAT_UNKNOWN :
398  try_opencv_writer = true;
399  break;
400  }
401 
402  if (try_opencv_writer) {
403 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
404  // std::cout << "Use opencv to write the image" << std::endl;
405  cv::Mat cvI;
406  vpImageConvert::convert(I, cvI);
407  cv::imwrite(filename, cvI);
408 #else
409  vpCERROR << "Cannot write file: Image format not supported..." << std::endl;
411  "Cannot write file: Image format not supported")) ;
412 #endif
413  }
414 }
415 
428 void
429 vpImageIo::write(const vpImage<vpRGBa> &I, const std::string &filename)
430 {
431  bool try_opencv_writer = false;
432 
433  switch(getFormat(filename)){
434  case FORMAT_PGM :
435  writePGM(I,filename); break;
436  case FORMAT_PPM :
437  writePPM(I,filename); break;
438  case FORMAT_JPEG :
439 #ifdef VISP_HAVE_JPEG
440  writeJPEG(I,filename);
441 #else
442  try_opencv_writer = true;
443 #endif
444  break;
445  case FORMAT_PNG :
446 #ifdef VISP_HAVE_PNG
447  writePNG(I,filename);
448 #else
449  try_opencv_writer = true;
450 #endif
451  break;
452  case FORMAT_TIFF :
453  case FORMAT_BMP :
454  case FORMAT_DIB :
455  case FORMAT_PBM :
456  case FORMAT_RASTER :
457  case FORMAT_JPEG2000 :
458  case FORMAT_UNKNOWN :
459  try_opencv_writer = true;
460  break;
461  }
462 
463  if (try_opencv_writer) {
464 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
465  // std::cout << "Use opencv to write the image" << std::endl;
466  cv::Mat cvI;
467  vpImageConvert::convert(I, cvI);
468  cv::imwrite(filename, cvI);
469 #else
470  vpCERROR << "Cannot write file: Image format not supported..." << std::endl;
472  "Cannot write file: Image format not supported")) ;
473 #endif
474  }
475 }
476 
477 //--------------------------------------------------------------------------
478 // PFM
479 //--------------------------------------------------------------------------
480 
490 void
491 vpImageIo::writePFM(const vpImage<float> &I, const std::string &filename)
492 {
493  FILE* fd;
494 
495  // Test the filename
496  if (filename.empty()) {
498  "Cannot write PFM image: filename empty")) ;
499  }
500 
501  fd = fopen(filename.c_str(), "wb");
502 
503  if (fd == NULL) {
505  "Cannot create PFM file \"%s\"", filename.c_str())) ;
506  }
507 
508  // Write the head
509  fprintf(fd, "P8\n"); // Magic number
510  fprintf(fd, "%d %d\n", I.getWidth(), I.getHeight()); // Image size
511  fprintf(fd, "255\n"); // Max level
512 
513  // Write the bitmap
514  size_t ierr;
515  size_t nbyte = I.getWidth()*I.getHeight();
516 
517  ierr = fwrite(I.bitmap, sizeof(float), nbyte, fd) ;
518  if (ierr != nbyte) {
519  fclose(fd);
521  "Cannot save PFM file \"%s\": only %d bytes over %d saved ", filename.c_str(), ierr, nbyte)) ;
522  }
523 
524  fflush(fd);
525  fclose(fd);
526 
527 }
528 //--------------------------------------------------------------------------
529 // PGM
530 //--------------------------------------------------------------------------
531 
540 void
541 vpImageIo::writePGM(const vpImage<unsigned char> &I, const std::string &filename)
542 {
543 
544  FILE* fd;
545 
546  // Test the filename
547  if (filename.empty()) {
549  "Cannot create PGM file: filename empty")) ;
550  }
551 
552  fd = fopen(filename.c_str(), "wb");
553 
554  if (fd == NULL) {
556  "Cannot create PGM file \"%s\"", filename.c_str())) ;
557  }
558 
559  // Write the head
560  fprintf(fd, "P5\n"); // Magic number
561  fprintf(fd, "%d %d\n", I.getWidth(), I.getHeight()); // Image size
562  fprintf(fd, "255\n"); // Max level
563 
564  // Write the bitmap
565  size_t ierr;
566  size_t nbyte = I.getWidth()*I.getHeight();
567 
568  ierr = fwrite(I.bitmap, sizeof(unsigned char), nbyte, fd) ;
569  if (ierr != nbyte) {
570  fclose(fd);
572  "Cannot save PGM file \"%s\": only %d over %d bytes saved", filename.c_str(), ierr, nbyte)) ;
573  }
574 
575  fflush(fd);
576  fclose(fd);
577 }
578 
586 void
587 vpImageIo::writePGM(const vpImage<short> &I, const std::string &filename)
588 {
590  unsigned int nrows = I.getHeight();
591  unsigned int ncols = I.getWidth();
592 
593  Iuc.resize(nrows, ncols);
594 
595  for (unsigned int i=0 ; i < nrows * ncols ; i++)
596  Iuc.bitmap[i] = (unsigned char)I.bitmap[i] ;
597 
598  vpImageIo::writePGM(Iuc, filename) ;
599 }
609 void
610 vpImageIo::writePGM(const vpImage<vpRGBa> &I, const std::string &filename)
611 {
612 
613  FILE* fd;
614 
615  // Test the filename
616  if (filename.empty()) {
618  "Cannot create PGM file: filename empty")) ;
619  }
620 
621  fd = fopen(filename.c_str(), "wb");
622 
623  if (fd == NULL) {
625  "Cannot create PGM file \"%s\"", filename.c_str())) ;
626  }
627 
628  // Write the head
629  fprintf(fd, "P5\n"); // Magic number
630  fprintf(fd, "%d %d\n", I.getWidth(), I.getHeight()); // Image size
631  fprintf(fd, "255\n"); // Max level
632 
633  // Write the bitmap
634  size_t ierr;
635  size_t nbyte = I.getWidth()*I.getHeight();
636 
638  vpImageConvert::convert(I,Itmp) ;
639 
640  ierr = fwrite(Itmp.bitmap, sizeof(unsigned char), nbyte, fd) ;
641  if (ierr != nbyte) {
642  fclose(fd);
644  "Cannot save PGM file \"%s\": only %d over %d bytes saved", filename.c_str(), ierr, nbyte)) ;
645  }
646 
647  fflush(fd);
648  fclose(fd);
649 }
650 
667 void
668 vpImageIo::readPFM(vpImage<float> &I, const std::string &filename)
669 {
670  unsigned int w=0, h=0, maxval=0;
671  unsigned int w_max = 100000, h_max = 100000, maxval_max = 255;
672  std::string magic("P8");
673 
674  std::ifstream fd(filename.c_str(), std::ios::binary);
675 
676  // Open the filename
677  if(! fd.is_open()) {
678  throw (vpImageException(vpImageException::ioError, "Cannot open file \"%s\"", filename.c_str())) ;
679  }
680 
681  vp_decodeHeaderPNM(filename, fd, magic, w, h, maxval);
682 
683  if (w > w_max || h > h_max) {
684  fd.close();
685  throw(vpException(vpException::badValue, "Bad image size in \"%s\"", filename.c_str()));
686  }
687  if (maxval > maxval_max)
688  {
689  fd.close();
691  "Bad maxval in \"%s\"", filename.c_str()));
692  }
693 
694  if ((h != I.getHeight())||( w != I.getWidth())) {
695  I.resize(h,w) ;
696  }
697 
698  unsigned int nbyte = I.getHeight()*I.getWidth();
699  fd.read ((char *)I.bitmap, sizeof(float) * nbyte);
700  if (! fd) {
701  fd.close();
703  "Read only %d of %d bytes in file \"%s\"", fd.gcount(), nbyte, filename.c_str()));
704  }
705 
706  fd.close();
707 }
708 
709 
725 void
726 vpImageIo::readPGM(vpImage<unsigned char> &I, const std::string &filename)
727 {
728  unsigned int w=0, h=0, maxval=0;
729  unsigned int w_max = 100000, h_max = 100000, maxval_max = 255;
730  std::string magic("P5");
731 
732  std::ifstream fd(filename.c_str(), std::ios::binary);
733 
734  // Open the filename
735  if(! fd.is_open()) {
736  throw (vpImageException(vpImageException::ioError, "Cannot open file \"%s\"", filename.c_str())) ;
737  }
738 
739  vp_decodeHeaderPNM(filename, fd, magic, w, h, maxval);
740 
741  if (w > w_max || h > h_max) {
742  fd.close();
743  throw(vpException(vpException::badValue, "Bad image size in \"%s\"", filename.c_str()));
744  }
745  if (maxval > maxval_max)
746  {
747  fd.close();
749  "Bad maxval in \"%s\"", filename.c_str()));
750  }
751 
752  if ((h != I.getHeight())||( w != I.getWidth())) {
753  I.resize(h,w) ;
754  }
755 
756  unsigned int nbyte = I.getHeight()*I.getWidth();
757  fd.read ((char *)I.bitmap, nbyte);
758  if (! fd) {
759  fd.close();
761  "Read only %d of %d bytes in file \"%s\"", fd.gcount(), nbyte, filename.c_str()));
762  }
763 
764  fd.close();
765 }
766 
785 void
786 vpImageIo::readPGM(vpImage<vpRGBa> &I, const std::string &filename)
787 {
789 
790  vpImageIo::readPGM(Itmp, filename) ;
791 
792  vpImageConvert::convert(Itmp, I) ;
793 }
794 
795 
796 //--------------------------------------------------------------------------
797 // PPM
798 //--------------------------------------------------------------------------
799 
816 void
817 vpImageIo::readPPM(vpImage<unsigned char> &I, const std::string &filename)
818 {
819  vpImage<vpRGBa> Itmp ;
820 
821  vpImageIo::readPPM(Itmp, filename) ;
822 
823  vpImageConvert::convert(Itmp, I) ;
824 }
825 
826 
838 void
839 vpImageIo::readPPM(vpImage<vpRGBa> &I, const std::string &filename)
840 {
841  unsigned int w=0, h=0, maxval=0;
842  unsigned int w_max = 100000, h_max = 100000, maxval_max = 255;
843  std::string magic("P6");
844 
845  std::ifstream fd(filename.c_str(), std::ios::binary);
846 
847  // Open the filename
848  if(! fd.is_open()) {
849  throw (vpImageException(vpImageException::ioError, "Cannot open file \"%s\"", filename.c_str())) ;
850  }
851 
852  vp_decodeHeaderPNM(filename, fd, magic, w, h, maxval);
853 
854  if (w > w_max || h > h_max) {
855  fd.close();
856  throw(vpException(vpException::badValue, "Bad image size in \"%s\"", filename.c_str()));
857  }
858  if (maxval > maxval_max)
859  {
860  fd.close();
862  "Bad maxval in \"%s\"", filename.c_str()));
863  }
864 
865  if ((h != I.getHeight())||( w != I.getWidth())) {
866  I.resize(h,w) ;
867  }
868 
869  for(unsigned int i=0;i<I.getHeight();i++) {
870  for(unsigned int j=0;j<I.getWidth();j++) {
871  unsigned char rgb[3];
872  fd.read((char *)&rgb, 3);
873 
874  if (! fd) {
875  fd.close();
877  "Read only %d of %d bytes in file \"%s\"",
878  (i*I.getWidth() + j)*3 + fd.gcount(), I.getSize()*3, filename.c_str()));
879  }
880 
881  I[i][j].R = rgb[0];
882  I[i][j].G = rgb[1];
883  I[i][j].B = rgb[2];
884  I[i][j].A = vpRGBa::alpha_default;
885  }
886  }
887 
888  fd.close();
889 }
890 
901 void
902 vpImageIo::writePPM(const vpImage<unsigned char> &I, const std::string &filename)
903 {
904  vpImage<vpRGBa> Itmp ;
905 
906  vpImageConvert::convert(I, Itmp) ;
907 
908  vpImageIo::writePPM(Itmp, filename) ;
909 }
910 
911 
919 void
920 vpImageIo::writePPM(const vpImage<vpRGBa> &I, const std::string &filename)
921 {
922  FILE* f;
923 
924  // Test the filename
925  if (filename.empty()) {
927  "Cannot create PPM file: filename empty")) ;
928  }
929 
930  f = fopen(filename.c_str(), "wb");
931 
932  if (f == NULL) {
934  "Cannot create PPM file \"%s\"", filename.c_str())) ;
935  }
936 
937  fprintf(f,"P6\n"); // Magic number
938  fprintf(f,"%d %d\n", I.getWidth(), I.getHeight()); // Image size
939  fprintf(f,"%d\n", 255); // Max level
940 
941  for(unsigned int i=0;i<I.getHeight();i++)
942  {
943  for(unsigned int j=0;j<I.getWidth();j++)
944  {
945  vpRGBa v = I[i][j];
946  unsigned char rgb[3];
947  rgb[0] = v.R;
948  rgb[1] = v.G;
949  rgb[2] = v.B;
950 
951  size_t res = fwrite(&rgb, 1, 3, f);
952  if (res != 3) {
953  fclose(f);
955  "cannot write file \"%s\"", filename.c_str())) ;
956  }
957  }
958  }
959 
960  fflush(f);
961  fclose(f);
962 }
963 
964 //--------------------------------------------------------------------------
965 // JPEG
966 //--------------------------------------------------------------------------
967 
968 #if defined(VISP_HAVE_JPEG)
969 
977 void
978 vpImageIo::writeJPEG(const vpImage<unsigned char> &I, const std::string &filename)
979 {
980  struct jpeg_compress_struct cinfo;
981  struct jpeg_error_mgr jerr;
982  FILE *file;
983 
984  cinfo.err = jpeg_std_error(&jerr);
985  jpeg_create_compress(&cinfo);
986 
987  // Test the filename
988  if (filename.empty()) {
990  "Cannot create JPEG file: filename empty")) ;
991  }
992 
993  file = fopen(filename.c_str(), "wb");
994 
995  if (file == NULL) {
997  "Cannot create JPEG file \"%s\"", filename.c_str())) ;
998  }
999 
1000  unsigned int width = I.getWidth();
1001  unsigned int height = I.getHeight();
1002 
1003  jpeg_stdio_dest(&cinfo, file);
1004 
1005  cinfo.image_width = width;
1006  cinfo.image_height = height;
1007  cinfo.input_components = 1;
1008  cinfo.in_color_space = JCS_GRAYSCALE;
1009  jpeg_set_defaults(&cinfo);
1010 
1011  jpeg_start_compress(&cinfo,TRUE);
1012 
1013  unsigned char *line;
1014  line = new unsigned char[width];
1015  unsigned char* input = (unsigned char*)I.bitmap;
1016  while (cinfo.next_scanline < cinfo.image_height)
1017  {
1018  for (unsigned int i = 0; i < width; i++)
1019  {
1020  line[i] = *(input);
1021  input++;
1022  }
1023  jpeg_write_scanlines(&cinfo, &line, 1);
1024  }
1025 
1026  jpeg_finish_compress(&cinfo);
1027  jpeg_destroy_compress(&cinfo);
1028  delete [] line;
1029  fclose(file);
1030 }
1031 
1039 void
1040 vpImageIo::writeJPEG(const vpImage<vpRGBa> &I, const std::string &filename)
1041 {
1042  struct jpeg_compress_struct cinfo;
1043  struct jpeg_error_mgr jerr;
1044  FILE *file;
1045 
1046  cinfo.err = jpeg_std_error(&jerr);
1047  jpeg_create_compress(&cinfo);
1048 
1049  // Test the filename
1050  if (filename.empty()) {
1052  "Cannot create JPEG file: filename empty")) ;
1053  }
1054 
1055  file = fopen(filename.c_str(), "wb");
1056 
1057  if (file == NULL) {
1059  "Cannot create JPEG file \"%s\"", filename.c_str())) ;
1060  }
1061 
1062  unsigned int width = I.getWidth();
1063  unsigned int height = I.getHeight();
1064 
1065  jpeg_stdio_dest(&cinfo, file);
1066 
1067  cinfo.image_width = width;
1068  cinfo.image_height = height;
1069  cinfo.input_components = 3;
1070  cinfo.in_color_space = JCS_RGB;
1071  jpeg_set_defaults(&cinfo);
1072 
1073  jpeg_start_compress(&cinfo,TRUE);
1074 
1075  unsigned char *line;
1076  line = new unsigned char[3*width];
1077  unsigned char* input = (unsigned char*)I.bitmap;
1078  while (cinfo.next_scanline < cinfo.image_height)
1079  {
1080  for (unsigned int i = 0; i < width; i++)
1081  {
1082  line[i*3] = *(input); input++;
1083  line[i*3+1] = *(input); input++;
1084  line[i*3+2] = *(input); input++;
1085  input++;
1086  }
1087  jpeg_write_scanlines(&cinfo, &line, 1);
1088  }
1089 
1090  jpeg_finish_compress(&cinfo);
1091  jpeg_destroy_compress(&cinfo);
1092  delete [] line;
1093  fclose(file);
1094 }
1095 
1112 void
1113 vpImageIo::readJPEG(vpImage<unsigned char> &I, const std::string &filename)
1114 {
1115  struct jpeg_decompress_struct cinfo;
1116  struct jpeg_error_mgr jerr;
1117  FILE *file;
1118 
1119  cinfo.err = jpeg_std_error(&jerr);
1120  jpeg_create_decompress(&cinfo);
1121 
1122  // Test the filename
1123  if (filename.empty()) {
1125  "Cannot read JPEG image: filename empty")) ;
1126  }
1127 
1128  file = fopen(filename.c_str(), "rb");
1129 
1130  if (file == NULL) {
1132  "Cannot read JPEG file \"%s\"", filename.c_str())) ;
1133  }
1134 
1135  jpeg_stdio_src(&cinfo, file);
1136  jpeg_read_header(&cinfo, TRUE);
1137 
1138  unsigned int width = cinfo.image_width;
1139  unsigned int height = cinfo.image_height;
1140 
1141  if ( (width != I.getWidth()) || (height != I.getHeight()) )
1142  I.resize(height,width);
1143 
1144  jpeg_start_decompress(&cinfo);
1145 
1146  unsigned int rowbytes = cinfo.output_width * (unsigned int)(cinfo.output_components);
1147  JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray)
1148  ((j_common_ptr) &cinfo, JPOOL_IMAGE, rowbytes, 1);
1149 
1150  if (cinfo.out_color_space == JCS_RGB) {
1151  vpImage<vpRGBa> Ic(height,width);
1152  unsigned char* output = (unsigned char*)Ic.bitmap;
1153  while (cinfo.output_scanline<cinfo.output_height) {
1154  jpeg_read_scanlines(&cinfo,buffer,1);
1155  for (unsigned int i = 0; i < width; i++) {
1156  *(output++) = buffer[0][i*3];
1157  *(output++) = buffer[0][i*3+1];
1158  *(output++) = buffer[0][i*3+2];
1159  *(output++) = vpRGBa::alpha_default;
1160  }
1161  }
1162  vpImageConvert::convert(Ic,I) ;
1163  }
1164 
1165  else if (cinfo.out_color_space == JCS_GRAYSCALE)
1166  {
1167  while (cinfo.output_scanline<cinfo.output_height)
1168  {
1169  unsigned int row = cinfo.output_scanline;
1170  jpeg_read_scanlines(&cinfo,buffer,1);
1171  memcpy(I[row], buffer[0], rowbytes);
1172  }
1173  }
1174 
1175  jpeg_finish_decompress(&cinfo);
1176  jpeg_destroy_decompress(&cinfo);
1177  fclose(file);
1178 }
1179 
1198 void
1199 vpImageIo::readJPEG(vpImage<vpRGBa> &I, const std::string &filename)
1200 {
1201  struct jpeg_decompress_struct cinfo;
1202  struct jpeg_error_mgr jerr;
1203  FILE *file;
1204 
1205  cinfo.err = jpeg_std_error(&jerr);
1206  jpeg_create_decompress(&cinfo);
1207 
1208  // Test the filename
1209  if (filename.empty()) {
1211  "Cannot read JPEG image: filename empty")) ;
1212  }
1213 
1214  file = fopen(filename.c_str(), "rb");
1215 
1216  if (file == NULL) {
1218  "Cannot read JPEG file \"%s\"", filename.c_str())) ;
1219  }
1220 
1221  jpeg_stdio_src(&cinfo, file);
1222 
1223  jpeg_read_header(&cinfo, TRUE);
1224 
1225  unsigned int width = cinfo.image_width;
1226  unsigned int height = cinfo.image_height;
1227 
1228  if ( (width != I.getWidth()) || (height != I.getHeight()) )
1229  I.resize(height,width);
1230 
1231  jpeg_start_decompress(&cinfo);
1232 
1233  unsigned int rowbytes = cinfo.output_width * (unsigned int)(cinfo.output_components);
1234  JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray)
1235  ((j_common_ptr) &cinfo, JPOOL_IMAGE, rowbytes, 1);
1236 
1237  if (cinfo.out_color_space == JCS_RGB)
1238  {
1239  unsigned char* output = (unsigned char*)I.bitmap;
1240  while (cinfo.output_scanline<cinfo.output_height)
1241  {
1242  jpeg_read_scanlines(&cinfo,buffer,1);
1243  for (unsigned int i = 0; i < width; i++) {
1244  *(output++) = buffer[0][i*3];
1245  *(output++) = buffer[0][i*3+1];
1246  *(output++) = buffer[0][i*3+2];
1247  *(output++) = vpRGBa::alpha_default;
1248  }
1249  }
1250  }
1251 
1252  else if (cinfo.out_color_space == JCS_GRAYSCALE)
1253  {
1254  vpImage<unsigned char> Ig(height,width);
1255 
1256  while (cinfo.output_scanline<cinfo.output_height)
1257  {
1258  unsigned int row = cinfo.output_scanline;
1259  jpeg_read_scanlines(&cinfo,buffer,1);
1260  memcpy(Ig[row], buffer[0], rowbytes);
1261  }
1262 
1263  vpImageConvert::convert(Ig,I) ;
1264  }
1265 
1266  jpeg_finish_decompress(&cinfo);
1267  jpeg_destroy_decompress(&cinfo);
1268  fclose(file);
1269 }
1270 
1271 #elif defined(VISP_HAVE_OPENCV)
1272 
1280 void
1281 vpImageIo::writeJPEG(const vpImage<unsigned char> &I, const std::string &filename)
1282 {
1283 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408)
1284  cv::Mat Ip;
1285  vpImageConvert::convert(I, Ip);
1286  cv::imwrite(filename.c_str(), Ip);
1287 #else
1288  IplImage* Ip = NULL;
1289  vpImageConvert::convert(I, Ip);
1290 
1291  cvSaveImage(filename.c_str(), Ip);
1292 
1293  cvReleaseImage(&Ip);
1294 #endif
1295 }
1296 
1304 void
1305 vpImageIo::writeJPEG(const vpImage<vpRGBa> &I, const std::string &filename)
1306 {
1307 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408)
1308  cv::Mat Ip;
1309  vpImageConvert::convert(I, Ip);
1310  cv::imwrite(filename.c_str(), Ip);
1311 #else
1312  IplImage* Ip = NULL;
1313  vpImageConvert::convert(I, Ip);
1314 
1315  cvSaveImage(filename.c_str(), Ip);
1316 
1317  cvReleaseImage(&Ip);
1318 #endif
1319 }
1320 
1337 void
1338 vpImageIo::readJPEG(vpImage<unsigned char> &I, const std::string &filename)
1339 {
1340 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
1341  cv::Mat Ip = cv::imread(filename.c_str(), cv::IMREAD_GRAYSCALE);
1342  if ( ! Ip.empty())
1343  vpImageConvert::convert(Ip, I);
1344  else
1345  throw (vpImageException(vpImageException::ioError, "Can't read the image")) ;
1346 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020408)
1347  cv::Mat Ip = cv::imread(filename.c_str(), CV_LOAD_IMAGE_GRAYSCALE);
1348  if ( ! Ip.empty())
1349  vpImageConvert::convert(Ip, I);
1350  else
1351  throw (vpImageException(vpImageException::ioError, "Can't read the image")) ;
1352 #else
1353  IplImage* Ip = NULL;
1354  Ip = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_GRAYSCALE);
1355  if (Ip != NULL)
1356  vpImageConvert::convert(Ip, I);
1357  else
1359  "Can't read the image")) ;
1360  cvReleaseImage(&Ip);
1361 #endif
1362 }
1363 
1382 void
1383 vpImageIo::readJPEG(vpImage<vpRGBa> &I, const std::string &filename)
1384 {
1385 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
1386  cv::Mat Ip = cv::imread(filename.c_str(), cv::IMREAD_GRAYSCALE);
1387  if ( ! Ip.empty())
1388  vpImageConvert::convert(Ip, I);
1389  else
1390  throw (vpImageException(vpImageException::ioError, "Can't read the image")) ;
1391 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020408)
1392  cv::Mat Ip = cv::imread(filename.c_str(), CV_LOAD_IMAGE_GRAYSCALE);
1393  if ( ! Ip.empty())
1394  vpImageConvert::convert(Ip, I);
1395  else
1396  throw (vpImageException(vpImageException::ioError, "Can't read the image")) ;
1397 #else
1398  IplImage* Ip = NULL;
1399  Ip = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_COLOR);
1400  if (Ip != NULL)
1401  vpImageConvert::convert(Ip, I);
1402  else
1403  throw (vpImageException(vpImageException::ioError, "Can't read the image")) ;
1404  cvReleaseImage(&Ip);
1405 #endif
1406 }
1407 
1408 #endif
1409 
1410 
1411 
1412 
1413 
1414 
1415 //--------------------------------------------------------------------------
1416 // PNG
1417 //--------------------------------------------------------------------------
1418 
1419 #if defined(VISP_HAVE_PNG)
1420 
1428 void
1429 vpImageIo::writePNG(const vpImage<unsigned char> &I, const std::string &filename)
1430 {
1431  FILE *file;
1432 
1433  // Test the filename
1434  if (filename.empty()) {
1436  "Cannot create PNG file: filename empty")) ;
1437  }
1438 
1439  file = fopen(filename.c_str(), "wb");
1440 
1441  if (file == NULL) {
1443  "Cannot create PNG file \"%s\"", filename.c_str())) ;
1444  }
1445 
1446  /* create a png info struct */
1447  png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL, NULL, NULL);
1448  if (!png_ptr)
1449  {
1450  fclose (file);
1451  vpERROR_TRACE("Error during png_create_write_struct()\n");
1453  "PNG write error")) ;
1454  }
1455 
1456  png_infop info_ptr = png_create_info_struct(png_ptr);
1457  if (!info_ptr)
1458  {
1459  fclose (file);
1460  png_destroy_write_struct (&png_ptr, NULL);
1461  vpERROR_TRACE("Error during png_create_info_struct()\n");
1463  "PNG write error")) ;
1464  }
1465 
1466  /* initialize the setjmp for returning properly after a libpng error occured */
1467  if (setjmp (png_jmpbuf (png_ptr)))
1468  {
1469  fclose (file);
1470  png_destroy_write_struct (&png_ptr, &info_ptr);
1471  vpERROR_TRACE("Error during init_io\n");
1473  "PNG write error")) ;
1474  }
1475 
1476  /* setup libpng for using standard C fwrite() function with our FILE pointer */
1477  png_init_io (png_ptr, file);
1478 
1479  unsigned int width = I.getWidth();
1480  unsigned int height = I.getHeight();
1481  int bit_depth = 8;
1482  int color_type = PNG_COLOR_TYPE_GRAY;
1483  /* set some useful information from header */
1484 
1485  if (setjmp (png_jmpbuf (png_ptr)))
1486  {
1487  fclose (file);
1488  png_destroy_write_struct (&png_ptr, &info_ptr);
1489  vpERROR_TRACE("Error during write header\n");
1491  "PNG write error")) ;
1492  }
1493 
1494  png_set_IHDR(png_ptr, info_ptr, width, height,
1495  bit_depth, color_type, PNG_INTERLACE_NONE,
1496  PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
1497 
1498  png_write_info(png_ptr, info_ptr);
1499 
1500  png_bytep* row_ptrs = new png_bytep[height];
1501  for (unsigned int i = 0; i < height; i++)
1502  row_ptrs[i] = new png_byte[width];
1503 
1504  unsigned char* input = (unsigned char*)I.bitmap;
1505 
1506  for (unsigned int i = 0; i < height; i++)
1507  {
1508  png_byte* row = row_ptrs[i];
1509  for(unsigned int j = 0; j < width; j++)
1510  {
1511  row[j] = *(input);
1512  input++;
1513  }
1514  }
1515 
1516  png_write_image(png_ptr, row_ptrs);
1517 
1518  png_write_end(png_ptr, NULL);
1519 
1520  for(unsigned int j = 0; j < height; j++)
1521  delete[] row_ptrs[j];
1522 
1523  delete[] row_ptrs;
1524 
1525  png_destroy_write_struct (&png_ptr, &info_ptr);
1526 
1527  fclose(file);
1528 }
1529 
1537 void
1538 vpImageIo::writePNG(const vpImage<vpRGBa> &I, const std::string &filename)
1539 {
1540  FILE *file;
1541 
1542  // Test the filename
1543  if (filename.empty()) {
1545  "Cannot create PNG file: filename empty")) ;
1546  }
1547 
1548  file = fopen(filename.c_str(), "wb");
1549 
1550  if (file == NULL) {
1552  "Cannot create PNG file \"%s\"", filename.c_str())) ;
1553  }
1554 
1555  /* create a png info struct */
1556  png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL, NULL, NULL);
1557  if (!png_ptr)
1558  {
1559  fclose (file);
1560  vpERROR_TRACE("Error during png_create_write_struct()\n");
1562  "PNG write error")) ;
1563  }
1564 
1565  png_infop info_ptr = png_create_info_struct(png_ptr);
1566  if (!info_ptr)
1567  {
1568  fclose (file);
1569  png_destroy_write_struct (&png_ptr, NULL);
1570  vpERROR_TRACE("Error during png_create_info_struct()\n");
1572  "PNG write error")) ;
1573  }
1574 
1575  /* initialize the setjmp for returning properly after a libpng error occured */
1576  if (setjmp (png_jmpbuf (png_ptr)))
1577  {
1578  fclose (file);
1579  png_destroy_write_struct (&png_ptr, &info_ptr);
1580  vpERROR_TRACE("Error during init_io\n");
1582  "PNG write error")) ;
1583  }
1584 
1585  /* setup libpng for using standard C fwrite() function with our FILE pointer */
1586  png_init_io (png_ptr, file);
1587 
1588  unsigned int width = I.getWidth();
1589  unsigned int height = I.getHeight();
1590  int bit_depth = 8;
1591  int color_type = PNG_COLOR_TYPE_RGB;
1592  /* set some useful information from header */
1593 
1594  if (setjmp (png_jmpbuf (png_ptr)))
1595  {
1596  fclose (file);
1597  png_destroy_write_struct (&png_ptr, &info_ptr);
1598  vpERROR_TRACE("Error during write header\n");
1600  "PNG write error")) ;
1601  }
1602 
1603  png_set_IHDR(png_ptr, info_ptr, width, height,
1604  bit_depth, color_type, PNG_INTERLACE_NONE,
1605  PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
1606 
1607  png_write_info(png_ptr, info_ptr);
1608 
1609  png_bytep* row_ptrs = new png_bytep[height];
1610  for (unsigned int i = 0; i < height; i++)
1611  row_ptrs[i] = new png_byte[3*width];
1612 
1613  unsigned char* input = (unsigned char*)I.bitmap;;
1614 
1615  for (unsigned int i = 0; i < height; i++)
1616  {
1617  png_byte* row = row_ptrs[i];
1618  for(unsigned int j = 0; j < width; j++)
1619  {
1620  row[3*j] = *(input);input++;
1621  row[3*j+1] = *(input);input++;
1622  row[3*j+2] = *(input);input++;
1623  input++;
1624  }
1625  }
1626 
1627  png_write_image(png_ptr, row_ptrs);
1628 
1629  png_write_end(png_ptr, NULL);
1630 
1631  for(unsigned int j = 0; j < height; j++)
1632  delete[] row_ptrs[j];
1633 
1634  delete[] row_ptrs;
1635 
1636  png_destroy_write_struct (&png_ptr, &info_ptr);
1637 
1638  fclose(file);
1639 }
1640 
1657 void
1658 vpImageIo::readPNG(vpImage<unsigned char> &I, const std::string &filename)
1659 {
1660  FILE *file;
1661  png_byte magic[8];
1662  // Test the filename
1663  if (filename.empty()) {
1665  "Cannot read PNG image: filename empty")) ;
1666  }
1667 
1668  file = fopen(filename.c_str(), "rb");
1669 
1670  if (file == NULL) {
1672  "Cannot read file \"%s\"", filename.c_str())) ;
1673  }
1674 
1675  /* read magic number */
1676  if (fread (magic, 1, sizeof (magic), file) != sizeof (magic))
1677  {
1678  fclose (file);
1680  "Cannot read magic number in file \"%s\"", filename.c_str())) ;
1681  }
1682 
1683  /* check for valid magic number */
1684  if (png_sig_cmp (magic,0, sizeof (magic)))
1685  {
1686  fclose (file);
1688  "Cannot read PNG file: \"%s\" is not a valid PNG image", filename.c_str())) ;
1689  }
1690 
1691  /* create a png read struct */
1692  //printf("version %s\n", PNG_LIBPNG_VER_STRING);
1693  png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
1694  if (png_ptr == NULL)
1695  {
1696  fprintf (stderr, "error: can't create a png read structure!\n");
1697  fclose (file);
1699  "error reading png file")) ;
1700  }
1701 
1702  /* create a png info struct */
1703  png_infop info_ptr = png_create_info_struct (png_ptr);
1704  if (info_ptr == NULL)
1705  {
1706  fprintf (stderr, "error: can't create a png info structure!\n");
1707  fclose (file);
1708  png_destroy_read_struct (&png_ptr, NULL, NULL);
1710  "error reading png file")) ;
1711  }
1712 
1713  /* initialize the setjmp for returning properly after a libpng error occured */
1714  if (setjmp (png_jmpbuf (png_ptr)))
1715  {
1716  fclose (file);
1717  png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
1718  vpERROR_TRACE("Error during init io\n");
1720  "PNG read error")) ;
1721  }
1722 
1723  /* setup libpng for using standard C fread() function with our FILE pointer */
1724  png_init_io (png_ptr, file);
1725 
1726  /* tell libpng that we have already read the magic number */
1727  png_set_sig_bytes (png_ptr, sizeof (magic));
1728 
1729  /* read png info */
1730  png_read_info (png_ptr, info_ptr);
1731 
1732  unsigned int width = png_get_image_width(png_ptr, info_ptr);
1733  unsigned int height = png_get_image_height(png_ptr, info_ptr);
1734 
1735  unsigned int bit_depth, channels, color_type;
1736  /* get some useful information from header */
1737  bit_depth = png_get_bit_depth (png_ptr, info_ptr);
1738  channels = png_get_channels(png_ptr, info_ptr);
1739  color_type = png_get_color_type (png_ptr, info_ptr);
1740 
1741  /* convert index color images to RGB images */
1742  if (color_type == PNG_COLOR_TYPE_PALETTE)
1743  png_set_palette_to_rgb (png_ptr);
1744 
1745  /* convert 1-2-4 bits grayscale images to 8 bits grayscale. */
1746  if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
1747  png_set_expand (png_ptr);
1748 
1749 // if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS))
1750 // png_set_tRNS_to_alpha (png_ptr);
1751 
1752  if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
1753  png_set_strip_alpha(png_ptr);
1754 
1755  if (bit_depth == 16)
1756  png_set_strip_16 (png_ptr);
1757  else if (bit_depth < 8)
1758  png_set_packing (png_ptr);
1759 
1760  /* update info structure to apply transformations */
1761  png_read_update_info (png_ptr, info_ptr);
1762 
1763  channels = png_get_channels(png_ptr, info_ptr);
1764 
1765  if ( (width != I.getWidth()) || (height != I.getHeight()) )
1766  I.resize(height,width);
1767 
1768  png_bytep* rowPtrs = new png_bytep[height];
1769 
1770  unsigned int stride = width * bit_depth * channels / 8;
1771  unsigned char* data = new unsigned char[stride * height];
1772 
1773  for (unsigned int i =0; i < height; i++)
1774  rowPtrs[i] = (png_bytep)data + (i * stride);
1775 
1776  png_read_image(png_ptr, rowPtrs);
1777 
1778  vpImage<vpRGBa> Ic(height,width);
1779  unsigned char* output;
1780 
1781  switch (channels)
1782  {
1783  case 1:
1784  output = (unsigned char*)I.bitmap;
1785  for (unsigned int i = 0; i < width*height; i++)
1786  {
1787  *(output++) = data[i];
1788  }
1789  break;
1790  case 2:
1791  output = (unsigned char*)I.bitmap;
1792  for (unsigned int i = 0; i < width*height; i++)
1793  {
1794  *(output++) = data[i*2];
1795  }
1796  break;
1797  case 3:
1798 
1799  output = (unsigned char*)Ic.bitmap;
1800  for (unsigned int i = 0; i < width*height; i++)
1801  {
1802  *(output++) = data[i*3];
1803  *(output++) = data[i*3+1];
1804  *(output++) = data[i*3+2];
1805  *(output++) = vpRGBa::alpha_default;
1806  }
1807  vpImageConvert::convert(Ic,I) ;
1808  break;
1809  case 4:
1810  output = (unsigned char*)Ic.bitmap;
1811  for (unsigned int i = 0; i < width*height; i++)
1812  {
1813  *(output++) = data[i*4];
1814  *(output++) = data[i*4+1];
1815  *(output++) = data[i*4+2];
1816  *(output++) = data[i*4+3];
1817  }
1818  vpImageConvert::convert(Ic,I) ;
1819  break;
1820  }
1821 
1822  delete [] (png_bytep)rowPtrs;
1823  delete [] data;
1824  png_read_end (png_ptr, NULL);
1825  png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
1826  fclose(file);
1827 }
1828 
1847 void
1848 vpImageIo::readPNG(vpImage<vpRGBa> &I, const std::string &filename)
1849 {
1850  FILE *file;
1851  png_byte magic[8];
1852 
1853  // Test the filename
1854  if (filename.empty()) {
1856  "Cannot read PNG image: filename empty")) ;
1857  }
1858 
1859  file = fopen(filename.c_str(), "rb");
1860 
1861  if (file == NULL) {
1863  "Cannot read file \"%s\"", filename.c_str())) ;
1864  }
1865 
1866  /* read magic number */
1867  if (fread (magic, 1, sizeof (magic), file) != sizeof (magic))
1868  {
1869  fclose (file);
1871  "Cannot read magic number in file \"%s\"", filename.c_str())) ;
1872  }
1873 
1874  /* check for valid magic number */
1875  if (png_sig_cmp (magic,0, sizeof (magic)))
1876  {
1877  fclose (file);
1879  "Cannot read PNG file: \"%s\" is not a valid PNG image", filename.c_str())) ;
1880  }
1881 
1882  /* create a png read struct */
1883  png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
1884  if (!png_ptr)
1885  {
1886  fclose (file);
1887  vpERROR_TRACE("Error during png_create_read_struct()\n");
1889  "PNG read error")) ;
1890  }
1891 
1892  /* create a png info struct */
1893  png_infop info_ptr = png_create_info_struct (png_ptr);
1894  if (!info_ptr)
1895  {
1896  fclose (file);
1897  png_destroy_read_struct (&png_ptr, NULL, NULL);
1898  vpERROR_TRACE("Error during png_create_info_struct()\n");
1900  "PNG read error")) ;
1901  }
1902 
1903  /* initialize the setjmp for returning properly after a libpng error occured */
1904  if (setjmp (png_jmpbuf (png_ptr)))
1905  {
1906  fclose (file);
1907  png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
1908  vpERROR_TRACE("Error during init io\n");
1910  "PNG read error")) ;
1911  }
1912 
1913  /* setup libpng for using standard C fread() function with our FILE pointer */
1914  png_init_io (png_ptr, file);
1915 
1916  /* tell libpng that we have already read the magic number */
1917  png_set_sig_bytes (png_ptr, sizeof (magic));
1918 
1919  /* read png info */
1920  png_read_info (png_ptr, info_ptr);
1921 
1922  unsigned int width = png_get_image_width(png_ptr, info_ptr);
1923  unsigned int height = png_get_image_height(png_ptr, info_ptr);
1924 
1925  unsigned int bit_depth, channels, color_type;
1926  /* get some useful information from header */
1927  bit_depth = png_get_bit_depth (png_ptr, info_ptr);
1928  channels = png_get_channels(png_ptr, info_ptr);
1929  color_type = png_get_color_type (png_ptr, info_ptr);
1930 
1931  /* convert index color images to RGB images */
1932  if (color_type == PNG_COLOR_TYPE_PALETTE)
1933  png_set_palette_to_rgb (png_ptr);
1934 
1935  /* convert 1-2-4 bits grayscale images to 8 bits grayscale. */
1936  if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
1937  png_set_expand (png_ptr);
1938 
1939 // if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS))
1940 // png_set_tRNS_to_alpha (png_ptr);
1941 
1942  if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
1943  png_set_strip_alpha(png_ptr);
1944 
1945  if (bit_depth == 16)
1946  png_set_strip_16 (png_ptr);
1947  else if (bit_depth < 8)
1948  png_set_packing (png_ptr);
1949 
1950  /* update info structure to apply transformations */
1951  png_read_update_info (png_ptr, info_ptr);
1952 
1953  channels = png_get_channels(png_ptr, info_ptr);
1954 
1955  if ( (width != I.getWidth()) || (height != I.getHeight()) )
1956  I.resize(height,width);
1957 
1958  png_bytep* rowPtrs = new png_bytep[height];
1959 
1960  unsigned int stride = width * bit_depth * channels / 8;
1961  unsigned char* data = new unsigned char[stride * height];
1962 
1963 
1964  for (unsigned int i =0; i < height; i++)
1965  rowPtrs[i] = (png_bytep)data + (i * stride);
1966 
1967  png_read_image(png_ptr, rowPtrs);
1968 
1969  vpImage<unsigned char> Ig(height,width);
1970  unsigned char* output;
1971 
1972  switch (channels)
1973  {
1974  case 1:
1975  output = (unsigned char*)Ig.bitmap;
1976  for (unsigned int i = 0; i < width*height; i++)
1977  {
1978  *(output++) = data[i];
1979  }
1980  vpImageConvert::convert(Ig,I) ;
1981  break;
1982  case 2:
1983  output = (unsigned char*)Ig.bitmap;
1984  for (unsigned int i = 0; i < width*height; i++)
1985  {
1986  *(output++) = data[i*2];
1987  }
1988  vpImageConvert::convert(Ig,I) ;
1989  break;
1990  case 3:
1991 
1992  output = (unsigned char*)I.bitmap;
1993  for (unsigned int i = 0; i < width*height; i++)
1994  {
1995  *(output++) = data[i*3];
1996  *(output++) = data[i*3+1];
1997  *(output++) = data[i*3+2];
1998  *(output++) = vpRGBa::alpha_default;
1999  }
2000  break;
2001  case 4:
2002  output = (unsigned char*)I.bitmap;
2003  for (unsigned int i = 0; i < width*height; i++)
2004  {
2005  *(output++) = data[i*4];
2006  *(output++) = data[i*4+1];
2007  *(output++) = data[i*4+2];
2008  *(output++) = data[i*4+3];
2009  }
2010  break;
2011  }
2012 
2013  delete [] (png_bytep)rowPtrs;
2014  delete [] data;
2015  png_read_end (png_ptr, NULL);
2016  png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
2017  fclose(file);
2018 }
2019 
2020 #elif defined(VISP_HAVE_OPENCV)
2021 
2029 void
2030 vpImageIo::writePNG(const vpImage<unsigned char> &I, const std::string &filename)
2031 {
2032 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408)
2033  cv::Mat Ip;
2034  vpImageConvert::convert(I, Ip);
2035  cv::imwrite(filename.c_str(), Ip);
2036 #else
2037  IplImage* Ip = NULL;
2038  vpImageConvert::convert(I, Ip);
2039 
2040  cvSaveImage(filename.c_str(), Ip);
2041 
2042  cvReleaseImage(&Ip);
2043 #endif
2044 }
2045 
2053 void
2054 vpImageIo::writePNG(const vpImage<vpRGBa> &I, const std::string &filename)
2055 {
2056 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408)
2057  cv::Mat Ip;
2058  vpImageConvert::convert(I, Ip);
2059  cv::imwrite(filename.c_str(), Ip);
2060 #else
2061  IplImage* Ip = NULL;
2062  vpImageConvert::convert(I, Ip);
2063 
2064  cvSaveImage(filename.c_str(), Ip);
2065 
2066  cvReleaseImage(&Ip);
2067 #endif
2068 }
2069 
2086 void
2087 vpImageIo::readPNG(vpImage<unsigned char> &I, const std::string &filename)
2088 {
2089 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
2090  cv::Mat Ip = cv::imread(filename.c_str(), cv::IMREAD_GRAYSCALE);
2091  if ( ! Ip.empty())
2092  vpImageConvert::convert(Ip, I);
2093  else
2094  throw (vpImageException(vpImageException::ioError, "Can't read the image")) ;
2095 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020408)
2096  cv::Mat Ip = cv::imread(filename.c_str(), CV_LOAD_IMAGE_GRAYSCALE);
2097  if ( ! Ip.empty())
2098  vpImageConvert::convert(Ip, I);
2099  else
2100  throw (vpImageException(vpImageException::ioError, "Can't read the image")) ;
2101 #else
2102  IplImage* Ip = NULL;
2103  Ip = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_GRAYSCALE);
2104  if (Ip != NULL)
2105  vpImageConvert::convert(Ip, I);
2106  else
2108  "Can't read the image")) ;
2109  cvReleaseImage(&Ip);
2110 #endif
2111 }
2112 
2131 void
2132 vpImageIo::readPNG(vpImage<vpRGBa> &I, const std::string &filename)
2133 {
2134 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
2135  cv::Mat Ip = cv::imread(filename.c_str(), cv::IMREAD_GRAYSCALE);
2136  if ( ! Ip.empty())
2137  vpImageConvert::convert(Ip, I);
2138  else
2139  throw (vpImageException(vpImageException::ioError, "Can't read the image")) ;
2140 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020408)
2141  cv::Mat Ip = cv::imread(filename.c_str(), CV_LOAD_IMAGE_GRAYSCALE);
2142  if ( ! Ip.empty())
2143  vpImageConvert::convert(Ip, I);
2144  else
2145  throw (vpImageException(vpImageException::ioError, "Can't read the image")) ;
2146 #else
2147  IplImage* Ip = NULL;
2148  Ip = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_COLOR);
2149  if (Ip != NULL)
2150  vpImageConvert::convert(Ip, I);
2151  else
2153  "Can't read the image")) ;
2154  cvReleaseImage(&Ip);
2155 #endif
2156 }
2157 
2158 #endif
static void writeJPEG(const vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:978
unsigned int getWidth() const
Definition: vpImage.h:226
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
#define vpCERROR
Definition: vpDebug.h:365
unsigned char B
Blue component.
Definition: vpRGBa.h:155
Type * bitmap
points toward the bitmap
Definition: vpImage.h:134
#define vpERROR_TRACE
Definition: vpDebug.h:391
error that can be emited by ViSP classes.
Definition: vpException.h:73
static std::string path(const char *pathname)
Definition: vpIoTools.cpp:770
Error that can be emited by the vpImage class and its derivates.
unsigned char G
Green component.
Definition: vpRGBa.h:154
static void writePPM(const vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:902
Definition: vpRGBa.h:66
static void write(const vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:368
static bool checkFilename(const char *filename)
Definition: vpIoTools.cpp:508
static void readPPM(vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:817
unsigned int getSize() const
Definition: vpImage.h:212
static void writePFM(const vpImage< float > &I, const std::string &filename)
Definition: vpImageIo.cpp:491
void resize(const unsigned int h, const unsigned int w)
resize the image : Image initialization
Definition: vpImage.h:903
static std::vector< std::string > splitChain(const std::string &chain, const std::string &sep)
Definition: vpIoTools.cpp:1596
static void readPFM(vpImage< float > &I, const std::string &filename)
Definition: vpImageIo.cpp:668
static void read(vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:205
static void readPNG(vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:1658
unsigned char R
Red component.
Definition: vpRGBa.h:153
unsigned int getHeight() const
Definition: vpImage.h:175
static void readPGM(vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:726
static void writePGM(const vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:541
static void readJPEG(vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:1113
static void writePNG(const vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:1429