Visual Servoing Platform  version 3.0.0
vpImageIo.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 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 const int vpImageIo::vpMAX_LEN = 100;
49 
58 FILE *
59 vpImageIo::openFileRead(const char *filename)
60 {
61 
62  FILE *fd ;
63 
64  // Lecture du nom du fichier image.
65  if (!filename || *filename == '\0') {
66  vpERROR_TRACE("filename empty ") ;
68  "filename empty ")) ;
69  }
70 
71  // Ouverture de l'image.
72  if ((fd = fopen(filename, "r")) == NULL)
73  {
74  vpERROR_TRACE("cannot open file") ;
76  "cannot open file")) ;
77  }
78  return fd ;
79 }
80 
93 FILE *
94 vpImageIo::openFileWrite(const char *filename, const char *mode)
95 {
96  FILE *fd ;
97 
98  // Lecture du nom du fichier image.
99  if (!filename || *filename == '\0')
100  {
101  vpERROR_TRACE("filename empty ") ;
103  "filename empty ")) ;
104  }
105 
106  // Ouverture de l'image.
107  if ((fd = fopen(filename, mode)) == NULL)
108  {
109  vpERROR_TRACE("cannot open file") ;
111  "cannot open file")) ;
112  }
113  return fd ;
114 }
115 
124 FILE *
125 vpImageIo::openFileRead(const std::string filename)
126 {
127 
128  FILE *fd ;
129 
130  // Lecture du nom du fichier image.
131  if (filename.empty()) {
132  vpERROR_TRACE("filename empty ") ;
134  "filename empty ")) ;
135  }
136 
137  // Ouverture de l'image.
138  if ((fd = fopen(filename.c_str(), "r")) == NULL)
139  {
140  vpERROR_TRACE("cannot open file") ;
142  "cannot open file")) ;
143  }
144  return fd ;
145 }
146 
159 FILE *
160 vpImageIo::openFileWrite(const std::string filename,
161  const std::string mode)
162 {
163  FILE *fd ;
164 
165  // Lecture du nom du fichier image.
166  if (filename.empty())
167  {
168  vpERROR_TRACE("filename empty ") ;
170  "filename empty ")) ;
171  }
172 
173  // Ouverture de l'image.
174  if ((fd = fopen(filename.c_str(), mode.c_str())) == NULL)
175  {
176  vpERROR_TRACE("cannot open file") ;
178  "cannot open file")) ;
179  }
180  return fd ;
181 }
182 
183 vpImageIo::vpImageFormatType
184 vpImageIo::getFormat(const char *filename)
185 {
186  std::string sfilename(filename);
187 
188  std::string ext = vpImageIo::getExtension(sfilename);
189 
190  if (ext.compare(".PGM") == 0)
191  return FORMAT_PGM;
192  else if (ext.compare(".pgm") == 0)
193  return FORMAT_PGM;
194  else if (ext.compare(".PPM") == 0)
195  return FORMAT_PPM;
196  else if (ext.compare(".ppm") == 0)
197  return FORMAT_PPM;
198  else if (ext.compare(".JPG") == 0)
199  return FORMAT_JPEG;
200  else if (ext.compare(".jpg") == 0)
201  return FORMAT_JPEG;
202  else if (ext.compare(".JPEG") == 0)
203  return FORMAT_JPEG;
204  else if (ext.compare(".jpeg") == 0)
205  return FORMAT_JPEG;
206  else if (ext.compare(".PNG") == 0)
207  return FORMAT_PNG;
208  else if (ext.compare(".png") == 0)
209  return FORMAT_PNG;
210  // Formats supported by opencv
211  else if (ext.compare(".TIFF") == 0)
212  return FORMAT_TIFF;
213  else if (ext.compare(".tiff") == 0)
214  return FORMAT_TIFF;
215  else if (ext.compare(".TIF") == 0)
216  return FORMAT_TIFF;
217  else if (ext.compare(".tif") == 0)
218  return FORMAT_TIFF;
219  else if (ext.compare(".BMP") == 0)
220  return FORMAT_BMP;
221  else if (ext.compare(".bmp") == 0)
222  return FORMAT_BMP;
223  else if (ext.compare(".DIB") == 0)
224  return FORMAT_DIB;
225  else if (ext.compare(".dib") == 0)
226  return FORMAT_DIB;
227  else if (ext.compare(".PBM") == 0)
228  return FORMAT_PBM;
229  else if (ext.compare(".pbm") == 0)
230  return FORMAT_PBM;
231  else if (ext.compare(".SR") == 0)
232  return FORMAT_RASTER;
233  else if (ext.compare(".sr") == 0)
234  return FORMAT_RASTER;
235  else if (ext.compare(".RAS") == 0)
236  return FORMAT_RASTER;
237  else if (ext.compare(".ras") == 0)
238  return FORMAT_RASTER;
239  else if (ext.compare(".JP2") == 0)
240  return FORMAT_JPEG2000;
241  else if (ext.compare(".jp2") == 0)
242  return FORMAT_JPEG2000;
243  else
244  return FORMAT_UNKNOWN;
245 }
246 
247 // return the extension of the file including the dot
248 std::string vpImageIo::getExtension(const std::string &filename)
249 {
250  // extract the extension
251  size_t dot = filename.find_last_of(".");
252  std::string ext = filename.substr(dot, filename.size()-1);
253  return ext;
254 }
255 
256 
273 void
274 vpImageIo::read(vpImage<unsigned char> &I, const char *filename)
275 {
276  bool exist = vpIoTools::checkFilename(filename);
277  if (!exist) {
278  std::string message = "Cannot read file: \"" + std::string(filename) + "\" doesn't exist";
280  }
281  bool try_opencv_reader = false;
282 
283  switch(getFormat(filename)){
284  case FORMAT_PGM :
285  readPGM(I,filename); break;
286  case FORMAT_PPM :
287  readPPM(I,filename); break;
288  case FORMAT_JPEG :
289 #ifdef VISP_HAVE_JPEG
290  readJPEG(I,filename);
291 #else
292  try_opencv_reader = true;
293 #endif
294  break;
295  case FORMAT_PNG :
296 #if defined(VISP_HAVE_PNG)
297  readPNG(I,filename);
298 #else
299  try_opencv_reader = true;
300 #endif
301  break;
302  case FORMAT_TIFF :
303  case FORMAT_BMP :
304  case FORMAT_DIB :
305  case FORMAT_PBM :
306  case FORMAT_RASTER :
307  case FORMAT_JPEG2000 :
308  case FORMAT_UNKNOWN :
309  try_opencv_reader = true;
310  break;
311  }
312 
313  if (try_opencv_reader) {
314 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
315  //std::cout << "Use opencv to read the image" << std::endl;
316  cv::Mat cvI = cv::imread(filename, cv::IMREAD_GRAYSCALE);
317  if (cvI.cols == 0 && cvI.rows == 0) {
318  std::string message = "Cannot read file \"" + std::string(filename) + "\": Image format not supported";
319  throw (vpImageException(vpImageException::ioError, message)) ;
320  }
321  vpImageConvert::convert(cvI, I);
322 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
323  //std::cout << "Use opencv to read the image" << std::endl;
324  cv::Mat cvI = cv::imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
325  if (cvI.cols == 0 && cvI.rows == 0) {
326  std::string message = "Cannot read file \"" + std::string(filename) + "\": Image format not supported";
327  throw (vpImageException(vpImageException::ioError, message)) ;
328  }
329  vpImageConvert::convert(cvI, I);
330 #else
331  std::string message = "Cannot read file \"" + std::string(filename) + "\": Image format not supported";
332  throw (vpImageException(vpImageException::ioError, message)) ;
333 #endif
334  }
335 }
352 void
353 vpImageIo::read(vpImage<unsigned char> &I, const std::string filename)
354 {
355  read(I,filename.c_str());
356 }
373 void
374 vpImageIo::read(vpImage<vpRGBa> &I, const char *filename)
375 {
376  bool exist = vpIoTools::checkFilename(filename);
377  if (!exist) {
378  std::string message = "Cannot read file: \"" + std::string(filename) + "\" doesn't exist";
380  }
381 
382  bool try_opencv_reader = false;
383 
384  switch(getFormat(filename)){
385  case FORMAT_PGM :
386  readPGM(I,filename); break;
387  case FORMAT_PPM :
388  readPPM(I,filename); break;
389  case FORMAT_JPEG :
390 #ifdef VISP_HAVE_JPEG
391  readJPEG(I,filename);
392 #else
393  try_opencv_reader = true;
394 #endif
395  break;
396  case FORMAT_PNG :
397 #if defined(VISP_HAVE_PNG)
398  readPNG(I,filename);
399 #else
400  try_opencv_reader = true;
401 #endif
402  break;
403  case FORMAT_TIFF :
404  case FORMAT_BMP :
405  case FORMAT_DIB :
406  case FORMAT_PBM :
407  case FORMAT_RASTER :
408  case FORMAT_JPEG2000 :
409  case FORMAT_UNKNOWN :
410  try_opencv_reader = true;
411  break;
412  }
413 
414  if (try_opencv_reader) {
415 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
416  // std::cout << "Use opencv to read the image" << std::endl;
417  cv::Mat cvI = cv::imread(filename, cv::IMREAD_COLOR);
418  if (cvI.cols == 0 && cvI.rows == 0) {
419  std::string message = "Cannot read file \"" + std::string(filename) + "\": Image format not supported";
420  throw (vpImageException(vpImageException::ioError, message)) ;
421  }
422  vpImageConvert::convert(cvI, I);
423 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
424  // std::cout << "Use opencv to read the image" << std::endl;
425  cv::Mat cvI = cv::imread(filename, CV_LOAD_IMAGE_COLOR);
426  if (cvI.cols == 0 && cvI.rows == 0) {
427  std::string message = "Cannot read file \"" + std::string(filename) + "\": Image format not supported";
428  throw (vpImageException(vpImageException::ioError, message)) ;
429  }
430  vpImageConvert::convert(cvI, I);
431 #else
432  std::string message = "Cannot read file \"" + std::string(filename) + "\": Image format not supported";
433  throw (vpImageException(vpImageException::ioError, message)) ;
434 #endif
435  }
436 }
453 void
454 vpImageIo::read(vpImage<vpRGBa> &I, const std::string filename)
455 {
456  read(I,filename.c_str());
457 }
458 
471 void
472 vpImageIo::write(const vpImage<unsigned char> &I, const char *filename)
473 {
474  bool try_opencv_writer = false;
475 
476  switch(getFormat(filename)){
477  case FORMAT_PGM :
478  writePGM(I,filename); break;
479  case FORMAT_PPM :
480  writePPM(I,filename); break;
481  case FORMAT_JPEG :
482 #ifdef VISP_HAVE_JPEG
483  writeJPEG(I,filename);
484 #else
485  try_opencv_writer = true;
486 #endif
487  break;
488  case FORMAT_PNG :
489 #ifdef VISP_HAVE_PNG
490  writePNG(I,filename);
491 #else
492  try_opencv_writer = true;
493 #endif
494  break;
495  case FORMAT_TIFF :
496  case FORMAT_BMP :
497  case FORMAT_DIB :
498  case FORMAT_PBM :
499  case FORMAT_RASTER :
500  case FORMAT_JPEG2000 :
501  case FORMAT_UNKNOWN :
502  try_opencv_writer = true;
503  break;
504  }
505 
506  if (try_opencv_writer) {
507 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
508  // std::cout << "Use opencv to write the image" << std::endl;
509  cv::Mat cvI;
510  vpImageConvert::convert(I, cvI);
511  cv::imwrite(filename, cvI);
512 #else
513  vpCERROR << "Cannot write file: Image format not supported..." << std::endl;
515  "Cannot write file: Image format not supported")) ;
516 #endif
517  }
518 }
531 void
532 vpImageIo::write(const vpImage<unsigned char> &I, const std::string filename)
533 {
534  write(I,filename.c_str());
535 }
548 void
549 vpImageIo::write(const vpImage<vpRGBa> &I, const char *filename)
550 {
551  bool try_opencv_writer = false;
552 
553  switch(getFormat(filename)){
554  case FORMAT_PGM :
555  writePGM(I,filename); break;
556  case FORMAT_PPM :
557  writePPM(I,filename); break;
558  case FORMAT_JPEG :
559 #ifdef VISP_HAVE_JPEG
560  writeJPEG(I,filename);
561 #else
562  try_opencv_writer = true;
563 #endif
564  break;
565  case FORMAT_PNG :
566 #ifdef VISP_HAVE_PNG
567  writePNG(I,filename);
568 #else
569  try_opencv_writer = true;
570 #endif
571  break;
572  case FORMAT_TIFF :
573  case FORMAT_BMP :
574  case FORMAT_DIB :
575  case FORMAT_PBM :
576  case FORMAT_RASTER :
577  case FORMAT_JPEG2000 :
578  case FORMAT_UNKNOWN :
579  try_opencv_writer = true;
580  break;
581  }
582 
583  if (try_opencv_writer) {
584 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
585  // std::cout << "Use opencv to write the image" << std::endl;
586  cv::Mat cvI;
587  vpImageConvert::convert(I, cvI);
588  cv::imwrite(filename, cvI);
589 #else
590  vpCERROR << "Cannot write file: Image format not supported..." << std::endl;
592  "Cannot write file: Image format not supported")) ;
593 #endif
594  }
595 }
608 void
609 vpImageIo::write(const vpImage<vpRGBa> &I, const std::string filename)
610 {
611  write(I,filename.c_str());
612 }
613 //--------------------------------------------------------------------------
614 // PFM
615 //--------------------------------------------------------------------------
616 
626 void
628  const char *filename)
629 {
630 
631  FILE* fd;
632 
633  // Test the filename
634  if (!filename || *filename == '\0') {
635  vpERROR_TRACE("no filename\n");
637  "no filename")) ;
638  }
639 
640  fd = fopen(filename, "wb");
641 
642  if (fd == NULL) {
643  vpERROR_TRACE("couldn't write to file \"%s\"\n", filename);
645  "cannot write file")) ;
646  }
647 
648  // Write the head
649  fprintf(fd, "P8\n"); // Magic number
650  fprintf(fd, "%d %d\n", I.getWidth(), I.getHeight()); // Image size
651  fprintf(fd, "255\n"); // Max level
652 
653  // Write the bitmap
654  size_t ierr;
655  size_t nbyte = I.getWidth()*I.getHeight();
656 
657  ierr = fwrite(I.bitmap, sizeof(float), nbyte, fd) ;
658  if (ierr != nbyte) {
659  fclose(fd);
660  vpERROR_TRACE("couldn't write %d bytes to file \"%s\"\n",
661  nbyte, filename) ;
663  "cannot write file")) ;
664  }
665 
666  fflush(fd);
667  fclose(fd);
668 
669 }
670 //--------------------------------------------------------------------------
671 // PGM
672 //--------------------------------------------------------------------------
673 
682 void
684  const char *filename)
685 {
686 
687  FILE* fd;
688 
689  // Test the filename
690  if (!filename || *filename == '\0') {
691  vpERROR_TRACE("no filename\n");
693  "no filename")) ;
694  }
695 
696  fd = fopen(filename, "wb");
697 
698  if (fd == NULL) {
699  vpERROR_TRACE("couldn't write to file \"%s\"\n", filename);
701  "cannot write file")) ;
702  }
703 
704  // Write the head
705  fprintf(fd, "P5\n"); // Magic number
706  fprintf(fd, "%d %d\n", I.getWidth(), I.getHeight()); // Image size
707  fprintf(fd, "255\n"); // Max level
708 
709  // Write the bitmap
710  size_t ierr;
711  size_t nbyte = I.getWidth()*I.getHeight();
712 
713  ierr = fwrite(I.bitmap, sizeof(unsigned char), nbyte, fd) ;
714  if (ierr != nbyte) {
715  fclose(fd);
716  vpERROR_TRACE("couldn't write %d bytes to file \"%s\"\n",
717  nbyte, filename) ;
719  "cannot write file")) ;
720  }
721 
722  fflush(fd);
723  fclose(fd);
724 
725 }
733 void
734 vpImageIo::writePGM(const vpImage<short> &I, const char *filename)
735 {
737  unsigned int nrows = I.getHeight();
738  unsigned int ncols = I.getWidth();
739 
740  Iuc.resize(nrows, ncols);
741 
742  for (unsigned int i=0 ; i < nrows * ncols ; i++)
743  Iuc.bitmap[i] = (unsigned char)I.bitmap[i] ;
744 
745  vpImageIo::writePGM(Iuc, filename) ;
746 
747 
748 }
758 void
759 vpImageIo::writePGM(const vpImage<vpRGBa> &I, const char *filename)
760 {
761 
762  FILE* fd;
763 
764  // Test the filename
765  if (!filename || *filename == '\0') {
766  vpERROR_TRACE("no filename\n");
768  "no filename")) ;
769  }
770 
771  fd = fopen(filename, "wb");
772 
773  if (fd == NULL) {
774  vpERROR_TRACE("couldn't write to file \"%s\"\n", filename);
776  "cannot write file")) ;
777  }
778 
779  // Write the head
780  fprintf(fd, "P5\n"); // Magic number
781  fprintf(fd, "%d %d\n", I.getWidth(), I.getHeight()); // Image size
782  fprintf(fd, "255\n"); // Max level
783 
784  // Write the bitmap
785  size_t ierr;
786  size_t nbyte = I.getWidth()*I.getHeight();
787 
788 
790  vpImageConvert::convert(I,Itmp) ;
791 
792  ierr = fwrite(Itmp.bitmap, sizeof(unsigned char), nbyte, fd) ;
793  if (ierr != nbyte) {
794  fclose(fd);
795  vpERROR_TRACE("couldn't write %d bytes to file \"%s\"\n",
796  nbyte, filename) ;
798  "cannot write file")) ;
799  }
800 
801  fflush(fd);
802  fclose(fd);
803 
804 }
805 
822 void
823 vpImageIo::readPFM(vpImage<float> &I, const char *filename)
824 {
825  FILE* fd = NULL; // File descriptor
826  int ierr;
827  int line;
828  int is255;
829  char* err ;
830  char str[vpMAX_LEN];
831  unsigned int w, h;
832 
833  // Test the filename
834  if (!filename || *filename == '\0')
835  {
836  vpERROR_TRACE("no filename") ;
838  " no filename")) ;
839 
840  }
841 
842  // Open the filename
843  fd = fopen(filename, "rb");
844  if (fd == NULL)
845  {
846  vpERROR_TRACE("couldn't read file \"%s\"", filename) ;
848  "couldn't read file")) ;
849  }
850 
851  // Read the first line with magic number P8
852  line = 0;
853 
854  err = fgets(str, vpMAX_LEN - 1, fd);
855  line++;
856  if (err == NULL)
857  {
858  fclose (fd);
859  vpERROR_TRACE("couldn't read line %d of file \"%s\"\n", line, filename) ;
861  "couldn't read file")) ;
862  }
863 
864  if (strlen(str) < 3)
865  {
866  fclose (fd);
867  vpERROR_TRACE("\"%s\" is not a PFM file\n", filename) ;
869  "this is not a PFM file")) ;
870  }
871 
872  str[2] = '\0';
873  if (strcmp(str, "P8") != 0)
874  {
875  fclose (fd);
876  vpERROR_TRACE("\"%s\" is not a PFM file\n", filename) ;
878  "this is not a PFM file")) ;
879  }
880 
881  // Jump the possible comment, or empty line and read the following line
882  do {
883  err = fgets(str, vpMAX_LEN - 1, fd);
884  line++;
885  if (err == NULL) {
886  fprintf(stderr, "couldn't read line %d of file \"%s\"\n", line, filename);
887  fclose (fd);
889  "Cannot read content of PFM file")) ;
890  }
891  } while ((str[0] == '#') || (str[0] == '\n'));
892 
893  // Extract image size
894  ierr = sscanf(str, "%d %d", &w, &h);
895  if (w > 100000 || h>100000) {
896  fclose (fd);
897  throw(vpException(vpException::badValue, "Bad image size"));
898  }
899 
900  if(ierr == 1){// the norm allows to have the two values on two separated lines.
901  do {
902  err = fgets(str, vpMAX_LEN - 1, fd);
903  line++;
904  if (err == NULL) {
905  fprintf(stderr, "couldn't read line %d of file \"%s\"\n", line, filename);
906  fclose (fd);
908  "Cannot read content of PFM file")) ;
909  }
910  } while ((str[0] == '#') || (str[0] == '\n'));
911  ierr = sscanf(str, "%d", &h);
912  }
913  if (ierr == EOF)
914  {
915  fclose (fd);
916  vpERROR_TRACE("couldn't read line %d of file \"%s\"\n",line, filename) ;
918  "Cannot read content of PFM file")) ;
919  }
920 
921  if ((h != I.getHeight())||( w != I.getWidth()))
922  {
923  try
924  {
925  I.resize(h,w) ;
926  }
927  catch(...)
928  {
929  fclose (fd);
931  "Cannot read content of PFM file")) ;
932  }
933  }
934 
935  // Read 255
936  err = fgets(str, vpMAX_LEN - 1, fd);
937  line++;
938  if (err == NULL) {
939  fclose (fd);
940  vpERROR_TRACE("couldn't read line %d of file \"%s\"\n",line, filename) ;
942  "Cannot read content of PFM file")) ;
943  }
944 
945  ierr = sscanf(str, "%d", &is255);
946  if (ierr == EOF) {
947  fclose (fd);
948  vpERROR_TRACE("couldn't read line %d of file \"%s\"\n", line, filename) ;
950  "Cannot read content of PFM file")) ;
951  }
952 
953  if (is255 != 255)
954  {
955  fclose (fd);
956  vpERROR_TRACE("MAX_VAL is not 255 in file \"%s\"\n", filename) ;
958  "Cannot read content of PFM file")) ;
959  }
960 
961  unsigned int nbyte = I.getHeight()*I.getWidth();
962  if (fread (I.bitmap, sizeof(float), nbyte, fd ) != nbyte)
963  {
964  fclose (fd);
965  vpERROR_TRACE("couldn't read %d bytes in file \"%s\"\n", nbyte, filename) ;
967  "Cannot read content of PFM file")) ;
968  }
969 
970  fclose (fd);
971 }
972 
973 
974 
991 void
993 {
994  FILE* fd = NULL; // File descriptor
995  int ierr;
996  char* err ;
997  char str[vpMAX_LEN];
998  unsigned int magic=5, w=0, h=0, maxval=255;
999 
1000  // Test the filename
1001  if (!filename || *filename == '\0') {
1003  "No filename")) ;
1004  }
1005 
1006  // Open the filename
1007  if ((fd = fopen(filename, "rb")) == NULL) {
1009  "Cannot read file \"%s\"", filename)) ;
1010  }
1011 
1012  while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] == '#') || (str[0] == '\n'))) {};
1013  if (err == NULL) {
1014  fclose (fd);
1016  "Cannot read header of file \"%s\"", filename));
1017  }
1018  if ((ierr = sscanf(str, "P%u %u %u %u", &magic, &w, &h, &maxval)) == 0) {
1019  fclose (fd);
1021  "Cannot read header of file \"%s\"", filename));
1022  }
1023 
1024  if (magic != 5) {
1025  fclose (fd);
1027  "\"%s\" is not a PGM P5 file", filename));
1028  }
1029 
1030  // Depending on ierr the line may contain:
1031  // 1 : P5
1032  // 2 : P5 w
1033  // 3 : P5 w h
1034  // 4 : P5 w h maxval
1035 
1036  if (ierr == 1) {
1037 // std::cout << "magic: " << magic << std::endl;
1038  while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] == '#') || (str[0] == '\n'))) {};
1039  if (err == NULL) {
1040  fclose (fd);
1042  "Cannot read header of file \"%s\"", filename));
1043  }
1044  if (((ierr = sscanf(str, "%u %u %u", &w, &h, &maxval)) == 0) || (ierr != 1 && ierr != 2 && ierr != 3)) {
1045  fclose (fd);
1047  "Cannot read header of file \"%s\"", filename));
1048  }
1049  // Depending on ierr the line may contain:
1050  // 1 : w
1051  // 2 : w h
1052  // 3 : w h maxval
1053  if (ierr == 1) {
1054 // std::cout << "w: " << w << std::endl;
1055  while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] == '#') || (str[0] == '\n'))) {};
1056  if (err == NULL) {
1057  fclose (fd);
1059  "Cannot read header of file \"%s\"", filename));
1060  }
1061  if (((ierr = sscanf(str, "%u %u", &h, &maxval)) == 0) || (ierr != 1 && ierr != 2)) {
1062  fclose (fd);
1064  "Cannot read header of file \"%s\"", filename));
1065  }
1066  if (ierr == 1) {
1067 // std::cout << "h: " << h << std::endl;
1068  while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] == '#') || (str[0] == '\n'))) {};
1069  if (err == NULL) {
1070  fclose (fd);
1072  "Cannot read header of file \"%s\"", filename));
1073  }
1074  if ((ierr = sscanf(str, "%u", &maxval)) != 1) {
1075  fclose (fd);
1077  "Cannot read header of file \"%s\"", filename));
1078  }
1079  }
1080 // else {
1081 // std::cout << "h: " << h << " maxval: " << maxval << std::endl;
1082 // }
1083  }
1084  else if (ierr == 2) {
1085 // std::cout << "w: " << w << " h: " << h << std::endl;
1086 
1087  while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] == '#') || (str[0] == '\n'))) {};
1088  if (err == NULL) {
1089  fclose (fd);
1091  "Cannot read header of file \"%s\"", filename));
1092  }
1093  if ((ierr = sscanf(str, "%u", &maxval)) != 1) {
1094  fclose (fd);
1096  "Cannot read header of file \"%s\"", filename));
1097  }
1098 // std::cout << "maxval: " << maxval << std::endl;
1099  }
1100 // else {
1101 // std::cout << "w: " << w << " h: " << h << " maxval: " << maxval << std::endl;
1102 // }
1103  }
1104  else if (ierr == 2) {
1105 // std::cout << "magic: " << magic << " w: " << w << std::endl;
1106  while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] == '#') || (str[0] == '\n'))) {};
1107  if (err == NULL) {
1108  fclose (fd);
1110  "Cannot read header of file \"%s\"", filename));
1111  }
1112  if (((ierr = sscanf(str, "%u %u", &h, &maxval)) == 0) || (ierr != 1 && ierr != 2)) {
1113  fclose (fd);
1115  "Cannot read header of file \"%s\"", filename));
1116  }
1117  if (ierr == 1) {
1118 // std::cout << "h: " << h << std::endl;
1119  while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] == '#') || (str[0] == '\n'))) {};
1120  if (err == NULL) {
1121  fclose (fd);
1123  "Cannot read header of file \"%s\"", filename));
1124  }
1125  if ((ierr = sscanf(str, "%u", &maxval)) != 1) {
1126  fclose (fd);
1128  "Cannot read header of file \"%s\"", filename));
1129  }
1130 // std::cout << "maxval: " << maxval << std::endl;
1131  }
1132 // else {
1133 // std::cout << "h: " << h << " maxval: " << maxval << std::endl;
1134 // }
1135  }
1136  else if (ierr == 3) {
1137 // std::cout << "magic: " << magic << " w: " << w << " h: " << h << std::endl;
1138  while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] == '#') || (str[0] == '\n'))) {};
1139  if (err == NULL) {
1140  fclose (fd);
1142  "Cannot read header of file \"%s\"", filename));
1143  }
1144  if ((ierr = sscanf(str, "%u", &maxval)) != 1) {
1145  fclose (fd);
1147  "Cannot read header of file \"%s\"", filename));
1148  }
1149 // std::cout << "maxval: " << maxval << std::endl;
1150  }
1151 // else if (ierr == 4) {
1152 // std::cout << "magic: " << magic << " w: " << w << " h: " << h << " maxval: " << maxval << std::endl;
1153 // }
1154 
1155  if (w > 100000 || h>100000) {
1156  fclose (fd);
1157  throw(vpException(vpException::badValue, "Bad image size in \"%s\"", filename));
1158  }
1159  if (maxval != 255)
1160  {
1161  fclose (fd);
1163  "Bad maxval in \"%s\"", filename));
1164  }
1165 
1166  if ((h != I.getHeight())||( w != I.getWidth())) {
1167  I.resize(h,w) ;
1168  }
1169 
1170  unsigned int nbyte = I.getHeight()*I.getWidth();
1171  size_t n;
1172  if ((n = fread (I.bitmap, sizeof(unsigned char), nbyte, fd)) != nbyte) {
1173  fclose (fd);
1175  "Read only %d of %d bytes in file \"%s\"", n, nbyte, filename));
1176  }
1177 
1178  fclose (fd);
1179 }
1180 
1181 
1200 void
1201 vpImageIo::readPGM(vpImage<vpRGBa> &I, const char *filename)
1202 {
1203  try
1204  {
1205  vpImage<unsigned char> Itmp ;
1206 
1207  vpImageIo::readPGM(Itmp, filename) ;
1208 
1209  vpImageConvert::convert(Itmp, I) ;
1210 
1211  }
1212  catch(...)
1213  {
1214  vpERROR_TRACE(" ") ;
1215  throw ;
1216  }
1217 }
1218 
1219 
1220 //--------------------------------------------------------------------------
1221 // PPM
1222 //--------------------------------------------------------------------------
1223 
1240 void
1242 {
1243 
1244  try
1245  {
1246  vpImage<vpRGBa> Itmp ;
1247 
1248  vpImageIo::readPPM(Itmp, filename) ;
1249 
1250  vpImageConvert::convert(Itmp, I) ;
1251  }
1252  catch(...)
1253  {
1254  vpERROR_TRACE(" ") ;
1255  throw ;
1256  }
1257 }
1258 
1259 
1271 void
1272 vpImageIo::readPPM(vpImage<vpRGBa> &I, const char *filename)
1273 {
1274  FILE* fd = NULL; // File descriptor
1275  int ierr;
1276  char* err ;
1277  char str[vpMAX_LEN];
1278  unsigned int magic=5, w=0, h=0, maxval=255;
1279 
1280  // Test the filename
1281  if (!filename || *filename == '\0') {
1283  "No filename")) ;
1284  }
1285 
1286  // Open the filename
1287  if ((fd = fopen(filename, "rb")) == NULL) {
1289  "Cannot read file \"%s\"", filename)) ;
1290  }
1291 
1292  while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] == '#') || (str[0] == '\n'))) {};
1293  if (err == NULL) {
1294  fclose (fd);
1296  "Cannot read header of file \"%s\"", filename));
1297  }
1298  if ((ierr = sscanf(str, "P%u %u %u %u", &magic, &w, &h, &maxval)) == 0) {
1299  fclose (fd);
1301  "Cannot read header of file \"%s\"", filename));
1302  }
1303 
1304  if (magic != 6) {
1305  fclose (fd);
1307  "\"%s\" is not a PGM P6 file", filename));
1308  }
1309 
1310  // Depending on ierr the line may contain:
1311  // 1 : P6
1312  // 2 : P6 w
1313  // 3 : P6 w h
1314  // 4 : P6 w h maxval
1315 
1316  if (ierr == 1) {
1317 // std::cout << "magic: " << magic << std::endl;
1318  while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] == '#') || (str[0] == '\n'))) {};
1319  if (err == NULL) {
1320  fclose (fd);
1322  "Cannot read header of file \"%s\"", filename));
1323  }
1324  if (((ierr = sscanf(str, "%u %u %u", &w, &h, &maxval)) == 0) || (ierr != 1 && ierr != 2 && ierr != 3)) {
1325  fclose (fd);
1327  "Cannot read header of file \"%s\"", filename));
1328  }
1329  // Depending on ierr the line may contain:
1330  // 1 : w
1331  // 2 : w h
1332  // 3 : w h maxval
1333  if (ierr == 1) {
1334 // std::cout << "w: " << w << std::endl;
1335  while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] == '#') || (str[0] == '\n'))) {};
1336  if (err == NULL) {
1337  fclose (fd);
1339  "Cannot read header of file \"%s\"", filename));
1340  }
1341  if (((ierr = sscanf(str, "%u %u", &h, &maxval)) == 0) || (ierr != 1 && ierr != 2)) {
1342  fclose (fd);
1344  "Cannot read header of file \"%s\"", filename));
1345  }
1346  if (ierr == 1) {
1347 // std::cout << "h: " << h << std::endl;
1348  while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] == '#') || (str[0] == '\n'))) {};
1349  if (err == NULL) {
1350  fclose (fd);
1352  "Cannot read header of file \"%s\"", filename));
1353  }
1354  if ((ierr = sscanf(str, "%u", &maxval)) != 1) {
1355  fclose (fd);
1357  "Cannot read header of file \"%s\"", filename));
1358  }
1359  }
1360 // else {
1361 // std::cout << "h: " << h << " maxval: " << maxval << std::endl;
1362 // }
1363  }
1364  else if (ierr == 2) {
1365 // std::cout << "w: " << w << " h: " << h << std::endl;
1366 
1367  while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] == '#') || (str[0] == '\n'))) {};
1368  if (err == NULL) {
1369  fclose (fd);
1371  "Cannot read header of file \"%s\"", filename));
1372  }
1373  if ((ierr = sscanf(str, "%u", &maxval)) != 1) {
1374  fclose (fd);
1376  "Cannot read header of file \"%s\"", filename));
1377  }
1378 // std::cout << "maxval: " << maxval << std::endl;
1379  }
1380 // else {
1381 // std::cout << "w: " << w << " h: " << h << " maxval: " << maxval << std::endl;
1382 // }
1383  }
1384  else if (ierr == 2) {
1385 // std::cout << "magic: " << magic << " w: " << w << std::endl;
1386  while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] == '#') || (str[0] == '\n'))) {};
1387  if (err == NULL) {
1388  fclose (fd);
1390  "Cannot read header of file \"%s\"", filename));
1391  }
1392  if (((ierr = sscanf(str, "%u %u", &h, &maxval)) == 0) || (ierr != 1 && ierr != 2)) {
1393  fclose (fd);
1395  "Cannot read header of file \"%s\"", filename));
1396  }
1397  if (ierr == 1) {
1398 // std::cout << "h: " << h << std::endl;
1399  while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] == '#') || (str[0] == '\n'))) {};
1400  if (err == NULL) {
1401  fclose (fd);
1403  "Cannot read header of file \"%s\"", filename));
1404  }
1405  if ((ierr = sscanf(str, "%u", &maxval)) != 1) {
1406  fclose (fd);
1408  "Cannot read header of file \"%s\"", filename));
1409  }
1410 // std::cout << "maxval: " << maxval << std::endl;
1411  }
1412 // else {
1413 // std::cout << "h: " << h << " maxval: " << maxval << std::endl;
1414 // }
1415  }
1416  else if (ierr == 3) {
1417 // std::cout << "magic: " << magic << " w: " << w << " h: " << h << std::endl;
1418  while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] == '#') || (str[0] == '\n'))) {};
1419  if (err == NULL) {
1420  fclose (fd);
1422  "Cannot read header of file \"%s\"", filename));
1423  }
1424  if ((ierr = sscanf(str, "%u", &maxval)) != 1) {
1425  fclose (fd);
1427  "Cannot read header of file \"%s\"", filename));
1428  }
1429 // std::cout << "maxval: " << maxval << std::endl;
1430  }
1431 // else if (ierr == 4) {
1432 // std::cout << "magic: " << magic << " w: " << w << " h: " << h << " maxval: " << maxval << std::endl;
1433 // }
1434 
1435  if (w > 100000 || h>100000) {
1436  fclose (fd);
1437  throw(vpException(vpException::badValue, "Bad image size in \"%s\"", filename));
1438  }
1439  if (maxval != 255)
1440  {
1441  fclose (fd);
1443  "Bad maxval in \"%s\"", filename));
1444  }
1445 
1446  if ((h != I.getHeight())||( w != I.getWidth())) {
1447  I.resize(h,w) ;
1448  }
1449 
1450  for(unsigned int i=0;i<I.getHeight();i++)
1451  {
1452  for(unsigned int j=0;j<I.getWidth();j++)
1453  {
1454  vpRGBa v ;
1455  size_t res = fread(&v.R,sizeof(v.R),1,fd) ;
1456  res |= fread(&v.G,sizeof(v.G),1,fd) ;
1457  res |= fread(&v.B,sizeof(v.B),1,fd) ;
1458  if (res==0)
1459  {
1460  fclose (fd);
1462  "Cannot read bytes in file \"%s\"\n", filename));
1463  }
1464  I[i][j] = v ;
1465  }
1466  }
1467 
1468  fclose (fd);
1469 }
1470 
1481 void
1482 vpImageIo::writePPM(const vpImage<unsigned char> &I, const char *filename)
1483 {
1484 
1485  try
1486  {
1487  vpImage<vpRGBa> Itmp ;
1488 
1489  vpImageConvert::convert(I, Itmp) ;
1490 
1491  vpImageIo::writePPM(Itmp, filename) ;
1492  }
1493  catch(...)
1494  {
1495  vpERROR_TRACE(" ") ;
1496  throw ;
1497  }
1498 }
1499 
1500 
1508 void
1509 vpImageIo::writePPM(const vpImage<vpRGBa> &I, const char *filename)
1510 {
1511 
1512  FILE* f;
1513 
1514 
1515  // Test the filename
1516  if (!filename || *filename == '\0') {
1517  vpERROR_TRACE("no filename\n");
1519  "no filename")) ;
1520  }
1521 
1522  f = fopen(filename, "wb");
1523 
1524  if (f == NULL) {
1525  vpERROR_TRACE("couldn't write to file \"%s\"\n", filename);
1527  "cannot write file")) ;
1528  }
1529 
1530 
1531 
1532  fprintf(f,"P6\n"); // Magic number
1533  fprintf(f,"%d %d\n", I.getWidth(), I.getHeight()); // Image size
1534  fprintf(f,"%d\n",255); // Max level
1535 
1536  for(unsigned int i=0;i<I.getHeight();i++)
1537  {
1538  for(unsigned int j=0;j<I.getWidth();j++)
1539  {
1540  vpRGBa P ;
1541  size_t res ;
1542  P = I[i][j] ;
1543  unsigned char tmp ;
1544  tmp = P.R ;
1545  res = fwrite(&tmp,sizeof(tmp),1,f) ;
1546  if (res==0)
1547  {
1548  fclose(f);
1549  vpERROR_TRACE("couldn't write file") ;
1551  "cannot write file")) ;
1552  }
1553  tmp = P.G;
1554  res = fwrite(&tmp,sizeof(tmp),1,f) ;
1555  if (res==0)
1556  {
1557  fclose(f);
1558  vpERROR_TRACE("couldn't write file") ;
1560  "cannot write file")) ;
1561  }
1562  tmp = P.B ;
1563  res = fwrite(&tmp,sizeof(tmp),1,f) ;
1564  if (res==0)
1565  {
1566  fclose(f);
1567  vpERROR_TRACE("couldn't write file") ;
1569  "cannot write file")) ;
1570  }
1571  }
1572  }
1573 
1574  fflush(f);
1575  fclose(f);
1576 }
1577 
1578 
1594 void
1595 vpImageIo::readPGM(vpImage<unsigned char> &I, const std::string filename)
1596 {
1597  vpImageIo::readPGM(I, filename.c_str());
1598 }
1599 
1615 void
1616 vpImageIo::readPGM(vpImage<vpRGBa> &I, const std::string filename)
1617 {
1618  vpImageIo::readPGM(I, filename.c_str());
1619 }
1620 
1630 void
1632  const std::string filename)
1633 {
1634  vpImageIo::writePGM(I, filename.c_str());
1635 }
1636 
1645 void
1646 vpImageIo::writePGM(const vpImage<short> &I, const std::string filename)
1647 {
1648 
1649  vpImageIo::writePGM(I, filename.c_str());
1650 }
1651 
1662 void
1663 vpImageIo::writePGM(const vpImage<vpRGBa> &I, const std::string filename)
1664 {
1665  vpImageIo::writePGM(I, filename.c_str());
1666 }
1667 
1668 //--------------------------------------------------------------------------
1669 // PPM
1670 //--------------------------------------------------------------------------
1671 
1688 void
1689 vpImageIo::readPPM(vpImage<unsigned char> &I, const std::string filename)
1690 {
1691  vpImageIo::readPPM(I, filename.c_str());
1692 }
1693 
1705 void
1706 vpImageIo::readPPM(vpImage<vpRGBa> &I, const std::string filename)
1707 {
1708  vpImageIo::readPPM(I, filename.c_str());
1709 }
1710 
1721 void
1722 vpImageIo::writePPM(const vpImage<unsigned char> &I, const std::string filename)
1723 {
1724  vpImageIo::writePPM(I, filename.c_str());
1725 }
1726 
1735 void
1736 vpImageIo::writePPM(const vpImage<vpRGBa> &I, const std::string filename)
1737 {
1738  vpImageIo::writePPM(I, filename.c_str());
1739 }
1740 
1741 
1742 //--------------------------------------------------------------------------
1743 // JPEG
1744 //--------------------------------------------------------------------------
1745 
1746 #if defined(VISP_HAVE_JPEG)
1747 
1755 void
1756 vpImageIo::writeJPEG(const vpImage<unsigned char> &I, const char *filename)
1757 {
1758  struct jpeg_compress_struct cinfo;
1759  struct jpeg_error_mgr jerr;
1760  FILE *file;
1761 
1762  cinfo.err = jpeg_std_error(&jerr);
1763  jpeg_create_compress(&cinfo);
1764 
1765  // Test the filename
1766  if (!filename || *filename == '\0') {
1767  vpERROR_TRACE("no filename\n");
1769  "no filename")) ;
1770  }
1771 
1772  file = fopen(filename, "wb");
1773 
1774  if (file == NULL) {
1775  vpERROR_TRACE("couldn't write file \"%s\"\n", filename);
1777  "cannot write file")) ;
1778  }
1779 
1780  unsigned int width = I.getWidth();
1781  unsigned int height = I.getHeight();
1782 
1783  jpeg_stdio_dest(&cinfo, file);
1784 
1785  cinfo.image_width = width;
1786  cinfo.image_height = height;
1787  cinfo.input_components = 1;
1788  cinfo.in_color_space = JCS_GRAYSCALE;
1789  jpeg_set_defaults(&cinfo);
1790 
1791  jpeg_start_compress(&cinfo,TRUE);
1792 
1793  unsigned char *line;
1794  line = new unsigned char[width];
1795  unsigned char* input = (unsigned char*)I.bitmap;
1796  while (cinfo.next_scanline < cinfo.image_height)
1797  {
1798  for (unsigned int i = 0; i < width; i++)
1799  {
1800  line[i] = *(input);
1801  input++;
1802  }
1803  jpeg_write_scanlines(&cinfo, &line, 1);
1804  }
1805 
1806  jpeg_finish_compress(&cinfo);
1807  jpeg_destroy_compress(&cinfo);
1808  delete [] line;
1809  fclose(file);
1810 }
1811 
1812 
1820 void
1821 vpImageIo::writeJPEG(const vpImage<unsigned char> &I, const std::string filename)
1822 {
1823  vpImageIo::writeJPEG(I, filename.c_str());
1824 }
1825 
1826 
1834 void
1835 vpImageIo::writeJPEG(const vpImage<vpRGBa> &I, const char *filename)
1836 {
1837  struct jpeg_compress_struct cinfo;
1838  struct jpeg_error_mgr jerr;
1839  FILE *file;
1840 
1841  cinfo.err = jpeg_std_error(&jerr);
1842  jpeg_create_compress(&cinfo);
1843 
1844  // Test the filename
1845  if (!filename || *filename == '\0') {
1846  vpERROR_TRACE("no filename\n");
1848  "no filename")) ;
1849  }
1850 
1851  file = fopen(filename, "wb");
1852 
1853  if (file == NULL) {
1854  vpERROR_TRACE("couldn't write file \"%s\"\n", filename);
1856  "cannot write file")) ;
1857  }
1858 
1859  unsigned int width = I.getWidth();
1860  unsigned int height = I.getHeight();
1861 
1862  jpeg_stdio_dest(&cinfo, file);
1863 
1864  cinfo.image_width = width;
1865  cinfo.image_height = height;
1866  cinfo.input_components = 3;
1867  cinfo.in_color_space = JCS_RGB;
1868  jpeg_set_defaults(&cinfo);
1869 
1870  jpeg_start_compress(&cinfo,TRUE);
1871 
1872  unsigned char *line;
1873  line = new unsigned char[3*width];
1874  unsigned char* input = (unsigned char*)I.bitmap;
1875  while (cinfo.next_scanline < cinfo.image_height)
1876  {
1877  for (unsigned int i = 0; i < width; i++)
1878  {
1879  line[i*3] = *(input); input++;
1880  line[i*3+1] = *(input); input++;
1881  line[i*3+2] = *(input); input++;
1882  input++;
1883  }
1884  jpeg_write_scanlines(&cinfo, &line, 1);
1885  }
1886 
1887  jpeg_finish_compress(&cinfo);
1888  jpeg_destroy_compress(&cinfo);
1889  delete [] line;
1890  fclose(file);
1891 }
1892 
1893 
1901 void
1902 vpImageIo::writeJPEG(const vpImage<vpRGBa> &I, const std::string filename)
1903 {
1904  vpImageIo::writeJPEG(I, filename.c_str());
1905 }
1906 
1907 
1924 void
1926 {
1927  struct jpeg_decompress_struct cinfo;
1928  struct jpeg_error_mgr jerr;
1929  FILE *file;
1930 
1931  cinfo.err = jpeg_std_error(&jerr);
1932  jpeg_create_decompress(&cinfo);
1933 
1934  // Test the filename
1935  if (!filename || *filename == '\0') {
1936  vpERROR_TRACE("no filename\n");
1938  "no filename")) ;
1939  }
1940 
1941  file = fopen(filename, "rb");
1942 
1943  if (file == NULL) {
1944  vpERROR_TRACE("couldn't read file \"%s\"\n", filename);
1946  "cannot read file")) ;
1947  }
1948 
1949  jpeg_stdio_src(&cinfo, file);
1950  jpeg_read_header(&cinfo, TRUE);
1951 
1952  unsigned int width = cinfo.image_width;
1953  unsigned int height = cinfo.image_height;
1954 
1955  if ( (width != I.getWidth()) || (height != I.getHeight()) )
1956  I.resize(height,width);
1957 
1958  jpeg_start_decompress(&cinfo);
1959 
1960  unsigned int rowbytes = cinfo.output_width * (unsigned int)(cinfo.output_components);
1961  JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray)
1962  ((j_common_ptr) &cinfo, JPOOL_IMAGE, rowbytes, 1);
1963 
1964  if (cinfo.out_color_space == JCS_RGB) {
1965  vpImage<vpRGBa> Ic(height,width);
1966  unsigned char* output = (unsigned char*)Ic.bitmap;
1967  while (cinfo.output_scanline<cinfo.output_height) {
1968  jpeg_read_scanlines(&cinfo,buffer,1);
1969  for (unsigned int i = 0; i < width; i++) {
1970  *(output++) = buffer[0][i*3];
1971  *(output++) = buffer[0][i*3+1];
1972  *(output++) = buffer[0][i*3+2];
1973  *(output++) = 0;
1974  }
1975  }
1976  vpImageConvert::convert(Ic,I) ;
1977  }
1978 
1979  else if (cinfo.out_color_space == JCS_GRAYSCALE)
1980  {
1981  unsigned int row;
1982  while (cinfo.output_scanline<cinfo.output_height)
1983  {
1984  row = cinfo.output_scanline;
1985  jpeg_read_scanlines(&cinfo,buffer,1);
1986  memcpy(I[row], buffer[0], rowbytes);
1987  }
1988  }
1989 
1990  jpeg_finish_decompress(&cinfo);
1991  jpeg_destroy_decompress(&cinfo);
1992  fclose(file);
1993 }
1994 
1995 
2012 void
2013 vpImageIo::readJPEG(vpImage<unsigned char> &I, const std::string filename)
2014 {
2015  vpImageIo::readJPEG(I, filename.c_str());
2016 }
2017 
2018 
2037 void
2038 vpImageIo::readJPEG(vpImage<vpRGBa> &I, const char *filename)
2039 {
2040  struct jpeg_decompress_struct cinfo;
2041  struct jpeg_error_mgr jerr;
2042  FILE *file;
2043 
2044  cinfo.err = jpeg_std_error(&jerr);
2045  jpeg_create_decompress(&cinfo);
2046 
2047  // Test the filename
2048  if (!filename || *filename == '\0') {
2049  vpERROR_TRACE("no filename\n");
2051  "no filename")) ;
2052  }
2053 
2054  file = fopen(filename, "rb");
2055 
2056  if (file == NULL) {
2057  vpERROR_TRACE("couldn't read file \"%s\"\n", filename);
2059  "cannot read file")) ;
2060  }
2061 
2062  jpeg_stdio_src(&cinfo, file);
2063 
2064  jpeg_read_header(&cinfo, TRUE);
2065 
2066  unsigned int width = cinfo.image_width;
2067  unsigned int height = cinfo.image_height;
2068 
2069  if ( (width != I.getWidth()) || (height != I.getHeight()) )
2070  I.resize(height,width);
2071 
2072  jpeg_start_decompress(&cinfo);
2073 
2074  unsigned int rowbytes = cinfo.output_width * (unsigned int)(cinfo.output_components);
2075  JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray)
2076  ((j_common_ptr) &cinfo, JPOOL_IMAGE, rowbytes, 1);
2077 
2078  if (cinfo.out_color_space == JCS_RGB)
2079  {
2080  unsigned char* output = (unsigned char*)I.bitmap;
2081  while (cinfo.output_scanline<cinfo.output_height)
2082  {
2083  jpeg_read_scanlines(&cinfo,buffer,1);
2084  for (unsigned int i = 0; i < width; i++) {
2085  *(output++) = buffer[0][i*3];
2086  *(output++) = buffer[0][i*3+1];
2087  *(output++) = buffer[0][i*3+2];
2088  *(output++) = 0;
2089  }
2090  }
2091  }
2092 
2093  else if (cinfo.out_color_space == JCS_GRAYSCALE)
2094  {
2095  vpImage<unsigned char> Ig(height,width);
2096 
2097  unsigned int row;
2098  while (cinfo.output_scanline<cinfo.output_height)
2099  {
2100  row = cinfo.output_scanline;
2101  jpeg_read_scanlines(&cinfo,buffer,1);
2102  memcpy(Ig[row], buffer[0], rowbytes);
2103  }
2104 
2105  vpImageConvert::convert(Ig,I) ;
2106  }
2107 
2108  jpeg_finish_decompress(&cinfo);
2109  jpeg_destroy_decompress(&cinfo);
2110  fclose(file);
2111 }
2112 
2113 
2132 void
2133 vpImageIo::readJPEG(vpImage<vpRGBa> &I, const std::string filename)
2134 {
2135  vpImageIo::readJPEG(I, filename.c_str());
2136 }
2137 
2138 #elif defined(VISP_HAVE_OPENCV)
2139 
2147 void
2148 vpImageIo::writeJPEG(const vpImage<unsigned char> &I, const char *filename)
2149 {
2150 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408)
2151  cv::Mat Ip;
2152  vpImageConvert::convert(I, Ip);
2153  cv::imwrite(filename, Ip);
2154 #else
2155  IplImage* Ip = NULL;
2156  vpImageConvert::convert(I, Ip);
2157 
2158  cvSaveImage(filename, Ip);
2159 
2160  cvReleaseImage(&Ip);
2161 #endif
2162 }
2163 
2164 
2172 void
2173 vpImageIo::writeJPEG(const vpImage<unsigned char> &I, const std::string filename)
2174 {
2175  vpImageIo::writeJPEG(I, filename.c_str());
2176 }
2177 
2178 
2186 void
2187 vpImageIo::writeJPEG(const vpImage<vpRGBa> &I, const char *filename)
2188 {
2189 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408)
2190  cv::Mat Ip;
2191  vpImageConvert::convert(I, Ip);
2192  cv::imwrite(filename, Ip);
2193 #else
2194  IplImage* Ip = NULL;
2195  vpImageConvert::convert(I, Ip);
2196 
2197  cvSaveImage(filename, Ip);
2198 
2199  cvReleaseImage(&Ip);
2200 #endif
2201 }
2202 
2203 
2211 void
2212 vpImageIo::writeJPEG(const vpImage<vpRGBa> &I, const std::string filename)
2213 {
2214  vpImageIo::writeJPEG(I, filename.c_str());
2215 }
2216 
2217 
2234 void
2235 vpImageIo::readJPEG(vpImage<unsigned char> &I, const char *filename)
2236 {
2237 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
2238  cv::Mat Ip = cv::imread(filename, cv::IMREAD_GRAYSCALE);
2239  if ( ! Ip.empty())
2240  vpImageConvert::convert(Ip, I);
2241  else
2242  throw (vpImageException(vpImageException::ioError, "Can't read the image")) ;
2243 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020408)
2244  cv::Mat Ip = cv::imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
2245  if ( ! Ip.empty())
2246  vpImageConvert::convert(Ip, I);
2247  else
2248  throw (vpImageException(vpImageException::ioError, "Can't read the image")) ;
2249 #else
2250  IplImage* Ip = NULL;
2251  Ip = cvLoadImage(filename, CV_LOAD_IMAGE_GRAYSCALE);
2252  if (Ip != NULL)
2253  vpImageConvert::convert(Ip, I);
2254  else
2256  "Can't read the image")) ;
2257  cvReleaseImage(&Ip);
2258 #endif
2259 }
2260 
2261 
2278 void
2279 vpImageIo::readJPEG(vpImage<unsigned char> &I, const std::string filename)
2280 {
2281  vpImageIo::readJPEG(I, filename.c_str());
2282 }
2283 
2284 
2303 void
2304 vpImageIo::readJPEG(vpImage<vpRGBa> &I, const char *filename)
2305 {
2306 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
2307  cv::Mat Ip = cv::imread(filename, cv::IMREAD_GRAYSCALE);
2308  if ( ! Ip.empty())
2309  vpImageConvert::convert(Ip, I);
2310  else
2311  throw (vpImageException(vpImageException::ioError, "Can't read the image")) ;
2312 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020408)
2313  cv::Mat Ip = cv::imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
2314  if ( ! Ip.empty())
2315  vpImageConvert::convert(Ip, I);
2316  else
2317  throw (vpImageException(vpImageException::ioError, "Can't read the image")) ;
2318 #else
2319  IplImage* Ip = NULL;
2320  Ip = cvLoadImage(filename, CV_LOAD_IMAGE_COLOR);
2321  if (Ip != NULL)
2322  vpImageConvert::convert(Ip, I);
2323  else
2324  throw (vpImageException(vpImageException::ioError, "Can't read the image")) ;
2325  cvReleaseImage(&Ip);
2326 #endif
2327 }
2328 
2329 
2348 void
2349 vpImageIo::readJPEG(vpImage<vpRGBa> &I, const std::string filename)
2350 {
2351  vpImageIo::readJPEG(I, filename.c_str());
2352 }
2353 
2354 #endif
2355 
2356 
2357 
2358 
2359 
2360 
2361 //--------------------------------------------------------------------------
2362 // PNG
2363 //--------------------------------------------------------------------------
2364 
2365 #if defined(VISP_HAVE_PNG)
2366 
2374 void
2375 vpImageIo::writePNG(const vpImage<unsigned char> &I, const char *filename)
2376 {
2377  FILE *file;
2378 
2379  // Test the filename
2380  if (!filename || *filename == '\0') {
2381  vpERROR_TRACE("no filename\n");
2383  "no filename")) ;
2384  }
2385 
2386  file = fopen(filename, "wb");
2387 
2388  if (file == NULL) {
2389  vpERROR_TRACE("couldn't write file \"%s\"\n", filename);
2391  "cannot write file")) ;
2392  }
2393 
2394  /* create a png info struct */
2395  png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL, NULL, NULL);
2396  if (!png_ptr)
2397  {
2398  fclose (file);
2399  vpERROR_TRACE("Error during png_create_write_struct()\n");
2401  "PNG write error")) ;
2402  }
2403 
2404  png_infop info_ptr = png_create_info_struct(png_ptr);
2405  if (!info_ptr)
2406  {
2407  fclose (file);
2408  png_destroy_write_struct (&png_ptr, NULL);
2409  vpERROR_TRACE("Error during png_create_info_struct()\n");
2411  "PNG write error")) ;
2412  }
2413 
2414  /* initialize the setjmp for returning properly after a libpng error occured */
2415  if (setjmp (png_jmpbuf (png_ptr)))
2416  {
2417  fclose (file);
2418  png_destroy_write_struct (&png_ptr, &info_ptr);
2419  vpERROR_TRACE("Error during init_io\n");
2421  "PNG write error")) ;
2422  }
2423 
2424  /* setup libpng for using standard C fwrite() function with our FILE pointer */
2425  png_init_io (png_ptr, file);
2426 
2427  unsigned int width = I.getWidth();
2428  unsigned int height = I.getHeight();
2429  int bit_depth = 8;
2430  int color_type = PNG_COLOR_TYPE_GRAY;
2431  /* set some useful information from header */
2432 
2433  if (setjmp (png_jmpbuf (png_ptr)))
2434  {
2435  fclose (file);
2436  png_destroy_write_struct (&png_ptr, &info_ptr);
2437  vpERROR_TRACE("Error during write header\n");
2439  "PNG write error")) ;
2440  }
2441 
2442  png_set_IHDR(png_ptr, info_ptr, width, height,
2443  bit_depth, color_type, PNG_INTERLACE_NONE,
2444  PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
2445 
2446  png_write_info(png_ptr, info_ptr);
2447 
2448  png_bytep* row_ptrs = new png_bytep[height];
2449  for (unsigned int i = 0; i < height; i++)
2450  row_ptrs[i] = new png_byte[width];
2451 
2452  unsigned char* input = (unsigned char*)I.bitmap;
2453 
2454  for (unsigned int i = 0; i < height; i++)
2455  {
2456  png_byte* row = row_ptrs[i];
2457  for(unsigned int j = 0; j < width; j++)
2458  {
2459  row[j] = *(input);
2460  input++;
2461  }
2462  }
2463 
2464  png_write_image(png_ptr, row_ptrs);
2465 
2466  png_write_end(png_ptr, NULL);
2467 
2468  for(unsigned int j = 0; j < height; j++)
2469  delete[] row_ptrs[j];
2470 
2471  delete[] row_ptrs;
2472 
2473  png_destroy_write_struct (&png_ptr, &info_ptr);
2474 
2475  fclose(file);
2476 }
2477 
2478 
2486 void
2487 vpImageIo::writePNG(const vpImage<unsigned char> &I, const std::string filename)
2488 {
2489  vpImageIo::writePNG(I, filename.c_str());
2490 }
2491 
2492 
2500 void
2501 vpImageIo::writePNG(const vpImage<vpRGBa> &I, const char *filename)
2502 {
2503  FILE *file;
2504 
2505  // Test the filename
2506  if (!filename || *filename == '\0') {
2507  vpERROR_TRACE("no filename\n");
2509  "no filename")) ;
2510  }
2511 
2512  file = fopen(filename, "wb");
2513 
2514  if (file == NULL) {
2515  vpERROR_TRACE("couldn't write file \"%s\"\n", filename);
2517  "cannot write file")) ;
2518  }
2519 
2520  /* create a png info struct */
2521  png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL, NULL, NULL);
2522  if (!png_ptr)
2523  {
2524  fclose (file);
2525  vpERROR_TRACE("Error during png_create_write_struct()\n");
2527  "PNG write error")) ;
2528  }
2529 
2530  png_infop info_ptr = png_create_info_struct(png_ptr);
2531  if (!info_ptr)
2532  {
2533  fclose (file);
2534  png_destroy_write_struct (&png_ptr, NULL);
2535  vpERROR_TRACE("Error during png_create_info_struct()\n");
2537  "PNG write error")) ;
2538  }
2539 
2540  /* initialize the setjmp for returning properly after a libpng error occured */
2541  if (setjmp (png_jmpbuf (png_ptr)))
2542  {
2543  fclose (file);
2544  png_destroy_write_struct (&png_ptr, &info_ptr);
2545  vpERROR_TRACE("Error during init_io\n");
2547  "PNG write error")) ;
2548  }
2549 
2550  /* setup libpng for using standard C fwrite() function with our FILE pointer */
2551  png_init_io (png_ptr, file);
2552 
2553  unsigned int width = I.getWidth();
2554  unsigned int height = I.getHeight();
2555  int bit_depth = 8;
2556  int color_type = PNG_COLOR_TYPE_RGB;
2557  /* set some useful information from header */
2558 
2559  if (setjmp (png_jmpbuf (png_ptr)))
2560  {
2561  fclose (file);
2562  png_destroy_write_struct (&png_ptr, &info_ptr);
2563  vpERROR_TRACE("Error during write header\n");
2565  "PNG write error")) ;
2566  }
2567 
2568  png_set_IHDR(png_ptr, info_ptr, width, height,
2569  bit_depth, color_type, PNG_INTERLACE_NONE,
2570  PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
2571 
2572  png_write_info(png_ptr, info_ptr);
2573 
2574  png_bytep* row_ptrs = new png_bytep[height];
2575  for (unsigned int i = 0; i < height; i++)
2576  row_ptrs[i] = new png_byte[3*width];
2577 
2578  unsigned char* input = (unsigned char*)I.bitmap;;
2579 
2580  for (unsigned int i = 0; i < height; i++)
2581  {
2582  png_byte* row = row_ptrs[i];
2583  for(unsigned int j = 0; j < width; j++)
2584  {
2585  row[3*j] = *(input);input++;
2586  row[3*j+1] = *(input);input++;
2587  row[3*j+2] = *(input);input++;
2588  input++;
2589  }
2590  }
2591 
2592  png_write_image(png_ptr, row_ptrs);
2593 
2594  png_write_end(png_ptr, NULL);
2595 
2596  for(unsigned int j = 0; j < height; j++)
2597  delete[] row_ptrs[j];
2598 
2599  delete[] row_ptrs;
2600 
2601  png_destroy_write_struct (&png_ptr, &info_ptr);
2602 
2603  fclose(file);
2604 }
2605 
2606 
2614 void
2615 vpImageIo::writePNG(const vpImage<vpRGBa> &I, const std::string filename)
2616 {
2617  vpImageIo::writePNG(I, filename.c_str());
2618 }
2619 
2636 void
2638 {
2639  FILE *file;
2640  png_byte magic[8];
2641  // Test the filename
2642  if (!filename || *filename == '\0') {
2643  vpERROR_TRACE("no filename\n");
2645  "no filename")) ;
2646  }
2647 
2648  file = fopen(filename, "rb");
2649 
2650  if (file == NULL) {
2651  vpERROR_TRACE("couldn't read file \"%s\"\n", filename);
2653  "cannot read file")) ;
2654  }
2655 
2656  /* read magic number */
2657  if (fread (magic, 1, sizeof (magic), file) != sizeof (magic))
2658  {
2659  fclose (file);
2660  vpERROR_TRACE("couldn't read magic number in file \"%s\"\n", filename) ;
2662  "error reading png file")) ;
2663  }
2664 
2665  /* check for valid magic number */
2666  if (png_sig_cmp (magic,0, sizeof (magic)))
2667  {
2668  fprintf (stderr, "error: \"%s\" is not a valid PNG image!\n",filename);
2669  fclose (file);
2671  "error reading png file")) ;
2672  }
2673 
2674  /* create a png read struct */
2675  //printf("version %s\n", PNG_LIBPNG_VER_STRING);
2676  png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
2677  if (png_ptr == NULL)
2678  {
2679  fprintf (stderr, "error: can't create a png read structure!\n");
2680  fclose (file);
2682  "error reading png file")) ;
2683  }
2684 
2685  /* create a png info struct */
2686  png_infop info_ptr = png_create_info_struct (png_ptr);
2687  if (info_ptr == NULL)
2688  {
2689  fprintf (stderr, "error: can't create a png info structure!\n");
2690  fclose (file);
2691  png_destroy_read_struct (&png_ptr, NULL, NULL);
2693  "error reading png file")) ;
2694  }
2695 
2696  /* initialize the setjmp for returning properly after a libpng error occured */
2697  if (setjmp (png_jmpbuf (png_ptr)))
2698  {
2699  fclose (file);
2700  png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
2701  vpERROR_TRACE("Error during init io\n");
2703  "PNG read error")) ;
2704  }
2705 
2706  /* setup libpng for using standard C fread() function with our FILE pointer */
2707  png_init_io (png_ptr, file);
2708 
2709  /* tell libpng that we have already read the magic number */
2710  png_set_sig_bytes (png_ptr, sizeof (magic));
2711 
2712  /* read png info */
2713  png_read_info (png_ptr, info_ptr);
2714 
2715  unsigned int width = png_get_image_width(png_ptr, info_ptr);
2716  unsigned int height = png_get_image_height(png_ptr, info_ptr);
2717 
2718  unsigned int bit_depth, channels, color_type;
2719  /* get some useful information from header */
2720  bit_depth = png_get_bit_depth (png_ptr, info_ptr);
2721  channels = png_get_channels(png_ptr, info_ptr);
2722  color_type = png_get_color_type (png_ptr, info_ptr);
2723 
2724  /* convert index color images to RGB images */
2725  if (color_type == PNG_COLOR_TYPE_PALETTE)
2726  png_set_palette_to_rgb (png_ptr);
2727 
2728  /* convert 1-2-4 bits grayscale images to 8 bits grayscale. */
2729  if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
2730  png_set_expand (png_ptr);
2731 
2732 // if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS))
2733 // png_set_tRNS_to_alpha (png_ptr);
2734 
2735  if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2736  png_set_strip_alpha(png_ptr);
2737 
2738  if (bit_depth == 16)
2739  png_set_strip_16 (png_ptr);
2740  else if (bit_depth < 8)
2741  png_set_packing (png_ptr);
2742 
2743  /* update info structure to apply transformations */
2744  png_read_update_info (png_ptr, info_ptr);
2745 
2746  channels = png_get_channels(png_ptr, info_ptr);
2747 
2748  if ( (width != I.getWidth()) || (height != I.getHeight()) )
2749  I.resize(height,width);
2750 
2751  png_bytep* rowPtrs = new png_bytep[height];
2752 
2753  unsigned int stride = width * bit_depth * channels / 8;
2754  unsigned char* data = new unsigned char[stride * height];
2755 
2756  for (unsigned int i =0; i < height; i++)
2757  rowPtrs[i] = (png_bytep)data + (i * stride);
2758 
2759  png_read_image(png_ptr, rowPtrs);
2760 
2761  vpImage<vpRGBa> Ic(height,width);
2762  unsigned char* output;
2763 
2764  switch (channels)
2765  {
2766  case 1:
2767  output = (unsigned char*)I.bitmap;
2768  for (unsigned int i = 0; i < width*height; i++)
2769  {
2770  *(output++) = data[i];
2771  }
2772  break;
2773  case 2:
2774  output = (unsigned char*)I.bitmap;
2775  for (unsigned int i = 0; i < width*height; i++)
2776  {
2777  *(output++) = data[i*2];
2778  }
2779  break;
2780  case 3:
2781 
2782  output = (unsigned char*)Ic.bitmap;
2783  for (unsigned int i = 0; i < width*height; i++)
2784  {
2785  *(output++) = data[i*3];
2786  *(output++) = data[i*3+1];
2787  *(output++) = data[i*3+2];
2788  *(output++) = 0;
2789  }
2790  vpImageConvert::convert(Ic,I) ;
2791  break;
2792  case 4:
2793  output = (unsigned char*)Ic.bitmap;
2794  for (unsigned int i = 0; i < width*height; i++)
2795  {
2796  *(output++) = data[i*4];
2797  *(output++) = data[i*4+1];
2798  *(output++) = data[i*4+2];
2799  *(output++) = data[i*4+3];
2800  }
2801  vpImageConvert::convert(Ic,I) ;
2802  break;
2803  }
2804 
2805  delete [] (png_bytep)rowPtrs;
2806  delete [] data;
2807  png_read_end (png_ptr, NULL);
2808  png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
2809  fclose(file);
2810 }
2811 
2812 
2829 void
2830 vpImageIo::readPNG(vpImage<unsigned char> &I, const std::string filename)
2831 {
2832  vpImageIo::readPNG(I, filename.c_str());
2833 }
2834 
2835 
2854 void
2855 vpImageIo::readPNG(vpImage<vpRGBa> &I, const char *filename)
2856 {
2857  FILE *file;
2858  png_byte magic[8];
2859 
2860  // Test the filename
2861  if (!filename || *filename == '\0') {
2862  vpERROR_TRACE("no filename\n");
2864  "no filename")) ;
2865  }
2866 
2867  file = fopen(filename, "rb");
2868 
2869  if (file == NULL) {
2870  vpERROR_TRACE("couldn't read file \"%s\"\n", filename);
2872  "cannot read file")) ;
2873  }
2874 
2875  /* read magic number */
2876  if (fread (magic, 1, sizeof (magic), file) != sizeof (magic))
2877  {
2878  fclose (file);
2879  vpERROR_TRACE("couldn't read magic number in file \"%s\"\n", filename) ;
2881  "error reading pgm file")) ;
2882  }
2883 
2884  /* check for valid magic number */
2885  if (png_sig_cmp (magic,0, sizeof (magic)))
2886  {
2887  fclose (file);
2888  vpERROR_TRACE("error: \"%s\" is not a valid PNG image!\n",filename);
2890  "PNG read error")) ;
2891  }
2892 
2893  /* create a png read struct */
2894  png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
2895  if (!png_ptr)
2896  {
2897  fclose (file);
2898  vpERROR_TRACE("Error during png_create_read_struct()\n");
2900  "PNG read error")) ;
2901  }
2902 
2903  /* create a png info struct */
2904  png_infop info_ptr = png_create_info_struct (png_ptr);
2905  if (!info_ptr)
2906  {
2907  fclose (file);
2908  png_destroy_read_struct (&png_ptr, NULL, NULL);
2909  vpERROR_TRACE("Error during png_create_info_struct()\n");
2911  "PNG read error")) ;
2912  }
2913 
2914  /* initialize the setjmp for returning properly after a libpng error occured */
2915  if (setjmp (png_jmpbuf (png_ptr)))
2916  {
2917  fclose (file);
2918  png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
2919  vpERROR_TRACE("Error during init io\n");
2921  "PNG read error")) ;
2922  }
2923 
2924  /* setup libpng for using standard C fread() function with our FILE pointer */
2925  png_init_io (png_ptr, file);
2926 
2927  /* tell libpng that we have already read the magic number */
2928  png_set_sig_bytes (png_ptr, sizeof (magic));
2929 
2930  /* read png info */
2931  png_read_info (png_ptr, info_ptr);
2932 
2933  unsigned int width = png_get_image_width(png_ptr, info_ptr);
2934  unsigned int height = png_get_image_height(png_ptr, info_ptr);
2935 
2936  unsigned int bit_depth, channels, color_type;
2937  /* get some useful information from header */
2938  bit_depth = png_get_bit_depth (png_ptr, info_ptr);
2939  channels = png_get_channels(png_ptr, info_ptr);
2940  color_type = png_get_color_type (png_ptr, info_ptr);
2941 
2942  /* convert index color images to RGB images */
2943  if (color_type == PNG_COLOR_TYPE_PALETTE)
2944  png_set_palette_to_rgb (png_ptr);
2945 
2946  /* convert 1-2-4 bits grayscale images to 8 bits grayscale. */
2947  if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
2948  png_set_expand (png_ptr);
2949 
2950 // if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS))
2951 // png_set_tRNS_to_alpha (png_ptr);
2952 
2953  if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2954  png_set_strip_alpha(png_ptr);
2955 
2956  if (bit_depth == 16)
2957  png_set_strip_16 (png_ptr);
2958  else if (bit_depth < 8)
2959  png_set_packing (png_ptr);
2960 
2961  /* update info structure to apply transformations */
2962  png_read_update_info (png_ptr, info_ptr);
2963 
2964  channels = png_get_channels(png_ptr, info_ptr);
2965 
2966  if ( (width != I.getWidth()) || (height != I.getHeight()) )
2967  I.resize(height,width);
2968 
2969  png_bytep* rowPtrs = new png_bytep[height];
2970 
2971  unsigned int stride = width * bit_depth * channels / 8;
2972  unsigned char* data = new unsigned char[stride * height];
2973 
2974 
2975  for (unsigned int i =0; i < height; i++)
2976  rowPtrs[i] = (png_bytep)data + (i * stride);
2977 
2978  png_read_image(png_ptr, rowPtrs);
2979 
2980  vpImage<unsigned char> Ig(height,width);
2981  unsigned char* output;
2982 
2983  switch (channels)
2984  {
2985  case 1:
2986  output = (unsigned char*)Ig.bitmap;
2987  for (unsigned int i = 0; i < width*height; i++)
2988  {
2989  *(output++) = data[i];
2990  }
2991  vpImageConvert::convert(Ig,I) ;
2992  break;
2993  case 2:
2994  output = (unsigned char*)Ig.bitmap;
2995  for (unsigned int i = 0; i < width*height; i++)
2996  {
2997  *(output++) = data[i*2];
2998  }
2999  vpImageConvert::convert(Ig,I) ;
3000  break;
3001  case 3:
3002 
3003  output = (unsigned char*)I.bitmap;
3004  for (unsigned int i = 0; i < width*height; i++)
3005  {
3006  *(output++) = data[i*3];
3007  *(output++) = data[i*3+1];
3008  *(output++) = data[i*3+2];
3009  *(output++) = 0;
3010  }
3011  break;
3012  case 4:
3013  output = (unsigned char*)I.bitmap;
3014  for (unsigned int i = 0; i < width*height; i++)
3015  {
3016  *(output++) = data[i*4];
3017  *(output++) = data[i*4+1];
3018  *(output++) = data[i*4+2];
3019  *(output++) = data[i*4+3];
3020  }
3021  break;
3022  }
3023 
3024  delete [] (png_bytep)rowPtrs;
3025  delete [] data;
3026  png_read_end (png_ptr, NULL);
3027  png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
3028  fclose(file);
3029 }
3030 
3031 
3050 void
3051 vpImageIo::readPNG(vpImage<vpRGBa> &I, const std::string filename)
3052 {
3053  vpImageIo::readPNG(I, filename.c_str());
3054 }
3055 
3056 #elif defined(VISP_HAVE_OPENCV)
3057 
3065 void
3066 vpImageIo::writePNG(const vpImage<unsigned char> &I, const char *filename)
3067 {
3068 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408)
3069  cv::Mat Ip;
3070  vpImageConvert::convert(I, Ip);
3071  cv::imwrite(filename, Ip);
3072 #else
3073  IplImage* Ip = NULL;
3074  vpImageConvert::convert(I, Ip);
3075 
3076  cvSaveImage(filename, Ip);
3077 
3078  cvReleaseImage(&Ip);
3079 #endif
3080 }
3081 
3082 
3090 void
3091 vpImageIo::writePNG(const vpImage<unsigned char> &I, const std::string filename)
3092 {
3093  vpImageIo::writePNG(I, filename.c_str());
3094 }
3095 
3096 
3104 void
3105 vpImageIo::writePNG(const vpImage<vpRGBa> &I, const char *filename)
3106 {
3107 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408)
3108  cv::Mat Ip;
3109  vpImageConvert::convert(I, Ip);
3110  cv::imwrite(filename, Ip);
3111 #else
3112  IplImage* Ip = NULL;
3113  vpImageConvert::convert(I, Ip);
3114 
3115  cvSaveImage(filename, Ip);
3116 
3117  cvReleaseImage(&Ip);
3118 #endif
3119 }
3120 
3121 
3129 void
3130 vpImageIo::writePNG(const vpImage<vpRGBa> &I, const std::string filename)
3131 {
3132  vpImageIo::writePNG(I, filename.c_str());
3133 }
3134 
3135 
3152 void
3153 vpImageIo::readPNG(vpImage<unsigned char> &I, const char *filename)
3154 {
3155 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
3156  cv::Mat Ip = cv::imread(filename, cv::IMREAD_GRAYSCALE);
3157  if ( ! Ip.empty())
3158  vpImageConvert::convert(Ip, I);
3159  else
3160  throw (vpImageException(vpImageException::ioError, "Can't read the image")) ;
3161 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020408)
3162  cv::Mat Ip = cv::imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
3163  if ( ! Ip.empty())
3164  vpImageConvert::convert(Ip, I);
3165  else
3166  throw (vpImageException(vpImageException::ioError, "Can't read the image")) ;
3167 #else
3168  IplImage* Ip = NULL;
3169  Ip = cvLoadImage(filename, CV_LOAD_IMAGE_GRAYSCALE);
3170  if (Ip != NULL)
3171  vpImageConvert::convert(Ip, I);
3172  else
3174  "Can't read the image")) ;
3175  cvReleaseImage(&Ip);
3176 #endif
3177 }
3178 
3179 
3196 void
3197 vpImageIo::readPNG(vpImage<unsigned char> &I, const std::string filename)
3198 {
3199  vpImageIo::readPNG(I, filename.c_str());
3200 }
3201 
3202 
3221 void
3222 vpImageIo::readPNG(vpImage<vpRGBa> &I, const char *filename)
3223 {
3224 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
3225  cv::Mat Ip = cv::imread(filename, cv::IMREAD_GRAYSCALE);
3226  if ( ! Ip.empty())
3227  vpImageConvert::convert(Ip, I);
3228  else
3229  throw (vpImageException(vpImageException::ioError, "Can't read the image")) ;
3230 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020408)
3231  cv::Mat Ip = cv::imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
3232  if ( ! Ip.empty())
3233  vpImageConvert::convert(Ip, I);
3234  else
3235  throw (vpImageException(vpImageException::ioError, "Can't read the image")) ;
3236 #else
3237  IplImage* Ip = NULL;
3238  Ip = cvLoadImage(filename, CV_LOAD_IMAGE_COLOR);
3239  if (Ip != NULL)
3240  vpImageConvert::convert(Ip, I);
3241  else
3243  "Can't read the image")) ;
3244  cvReleaseImage(&Ip);
3245 #endif
3246 }
3247 
3248 
3267 void
3268 vpImageIo::readPNG(vpImage<vpRGBa> &I, const std::string filename)
3269 {
3270  vpImageIo::readPNG(I, filename.c_str());
3271 }
3272 
3273 #endif
static void write(const vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:472
static void writeJPEG(const vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:1756
static void readPGM(vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:992
unsigned int getWidth() const
Definition: vpImage.h:161
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:144
Type * bitmap
points toward the bitmap
Definition: vpImage.h:116
#define vpERROR_TRACE
Definition: vpDebug.h:391
static void writePGM(const vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:683
static void writePNG(const vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:2375
error that can be emited by ViSP classes.
Definition: vpException.h:73
static void writePFM(const vpImage< float > &I, const char *filename)
Definition: vpImageIo.cpp:627
Error that can be emited by the vpImage class and its derivates.
static void readPFM(vpImage< float > &I, const char *filename)
Definition: vpImageIo.cpp:823
unsigned char G
Green component.
Definition: vpRGBa.h:143
Class that defines a RGB 32 bits structure.
Definition: vpRGBa.h:64
static bool checkFilename(const char *filename)
Definition: vpIoTools.cpp:485
void resize(const unsigned int h, const unsigned int w)
set the size of the image without initializing it.
Definition: vpImage.h:616
static void readPNG(vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:2637
static void readJPEG(vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:1925
unsigned char R
Red component.
Definition: vpRGBa.h:142
static void readPPM(vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:1241
unsigned int getHeight() const
Definition: vpImage.h:152
static void writePPM(const vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:1482
static void read(vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:274