ViSP  2.10.0
vpImageIo.cpp
1 /****************************************************************************
2  *
3  * $Id: vpImageIo.cpp 5249 2015-02-03 13:04:27Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Read/write images.
36  *
37  * Authors:
38  * Eric Marchand
39  *
40  *****************************************************************************/
41 
47 #include <visp/vpImage.h>
48 #include <visp/vpImageIo.h>
49 #include <visp/vpImageConvert.h> //image conversion
50 #include <visp/vpIoTools.h>
51 
52 const int vpImageIo::vpMAX_LEN = 100;
53 
62 FILE *
63 vpImageIo::openFileRead(const char *filename)
64 {
65 
66  FILE *fd ;
67 
68  // Lecture du nom du fichier image.
69  if (!filename || *filename == '\0') {
70  vpERROR_TRACE("filename empty ") ;
72  "filename empty ")) ;
73  }
74 
75  // Ouverture de l'image.
76  if ((fd = fopen(filename, "r")) == NULL)
77  {
78  vpERROR_TRACE("cannot open file") ;
80  "cannot open file")) ;
81  }
82  return fd ;
83 }
84 
97 FILE *
98 vpImageIo::openFileWrite(const char *filename, const char *mode)
99 {
100  FILE *fd ;
101 
102  // Lecture du nom du fichier image.
103  if (!filename || *filename == '\0')
104  {
105  vpERROR_TRACE("filename empty ") ;
107  "filename empty ")) ;
108  }
109 
110  // Ouverture de l'image.
111  if ((fd = fopen(filename, mode)) == NULL)
112  {
113  vpERROR_TRACE("cannot open file") ;
115  "cannot open file")) ;
116  }
117  return fd ;
118 }
119 
128 FILE *
129 vpImageIo::openFileRead(const std::string filename)
130 {
131 
132  FILE *fd ;
133 
134  // Lecture du nom du fichier image.
135  if (filename.empty()) {
136  vpERROR_TRACE("filename empty ") ;
138  "filename empty ")) ;
139  }
140 
141  // Ouverture de l'image.
142  if ((fd = fopen(filename.c_str(), "r")) == NULL)
143  {
144  vpERROR_TRACE("cannot open file") ;
146  "cannot open file")) ;
147  }
148  return fd ;
149 }
150 
163 FILE *
164 vpImageIo::openFileWrite(const std::string filename,
165  const std::string mode)
166 {
167  FILE *fd ;
168 
169  // Lecture du nom du fichier image.
170  if (filename.empty())
171  {
172  vpERROR_TRACE("filename empty ") ;
174  "filename empty ")) ;
175  }
176 
177  // Ouverture de l'image.
178  if ((fd = fopen(filename.c_str(), mode.c_str())) == NULL)
179  {
180  vpERROR_TRACE("cannot open file") ;
182  "cannot open file")) ;
183  }
184  return fd ;
185 }
186 
187 vpImageIo::vpImageFormatType
188 vpImageIo::getFormat(const char *filename)
189 {
190  std::string sfilename(filename);
191 
192  std::string ext = vpImageIo::getExtension(sfilename);
193 
194  if (ext.compare(".PGM") == 0)
195  return FORMAT_PGM;
196  else if (ext.compare(".pgm") == 0)
197  return FORMAT_PGM;
198  else if (ext.compare(".PPM") == 0)
199  return FORMAT_PPM;
200  else if (ext.compare(".ppm") == 0)
201  return FORMAT_PPM;
202  else if (ext.compare(".JPG") == 0)
203  return FORMAT_JPEG;
204  else if (ext.compare(".jpg") == 0)
205  return FORMAT_JPEG;
206  else if (ext.compare(".JPEG") == 0)
207  return FORMAT_JPEG;
208  else if (ext.compare(".jpeg") == 0)
209  return FORMAT_JPEG;
210  else if (ext.compare(".PNG") == 0)
211  return FORMAT_PNG;
212  else if (ext.compare(".png") == 0)
213  return FORMAT_PNG;
214  // Formats supported by opencv
215  else if (ext.compare(".TIFF") == 0)
216  return FORMAT_TIFF;
217  else if (ext.compare(".tiff") == 0)
218  return FORMAT_TIFF;
219  else if (ext.compare(".TIF") == 0)
220  return FORMAT_TIFF;
221  else if (ext.compare(".tif") == 0)
222  return FORMAT_TIFF;
223  else if (ext.compare(".BMP") == 0)
224  return FORMAT_BMP;
225  else if (ext.compare(".bmp") == 0)
226  return FORMAT_BMP;
227  else if (ext.compare(".DIB") == 0)
228  return FORMAT_DIB;
229  else if (ext.compare(".dib") == 0)
230  return FORMAT_DIB;
231  else if (ext.compare(".PBM") == 0)
232  return FORMAT_PBM;
233  else if (ext.compare(".pbm") == 0)
234  return FORMAT_PBM;
235  else if (ext.compare(".SR") == 0)
236  return FORMAT_RASTER;
237  else if (ext.compare(".sr") == 0)
238  return FORMAT_RASTER;
239  else if (ext.compare(".RAS") == 0)
240  return FORMAT_RASTER;
241  else if (ext.compare(".ras") == 0)
242  return FORMAT_RASTER;
243  else if (ext.compare(".JP2") == 0)
244  return FORMAT_JPEG2000;
245  else if (ext.compare(".jp2") == 0)
246  return FORMAT_JPEG2000;
247  else
248  return FORMAT_UNKNOWN;
249 }
250 
251 // return the extension of the file including the dot
252 std::string vpImageIo::getExtension(const std::string &filename)
253 {
254  // extract the extension
255  size_t dot = filename.find_last_of(".");
256  std::string ext = filename.substr(dot, filename.size()-1);
257  return ext;
258 }
259 
260 
277 void
278 vpImageIo::read(vpImage<unsigned char> &I, const char *filename)
279 {
280  bool exist = vpIoTools::checkFilename(filename);
281  if (!exist) {
282  std::string message = "Cannot read file: \"" + std::string(filename) + "\" doesn't exist";
284  }
285  bool try_opencv_reader = false;
286 
287  switch(getFormat(filename)){
288  case FORMAT_PGM :
289  readPGM(I,filename); break;
290  case FORMAT_PPM :
291  readPPM(I,filename); break;
292  case FORMAT_JPEG :
293 #ifdef VISP_HAVE_LIBJPEG
294  readJPEG(I,filename);
295 #else
296  try_opencv_reader = true;
297 #endif
298  break;
299  case FORMAT_PNG :
300 #if defined(VISP_HAVE_LIBPNG)
301  readPNG(I,filename);
302 #else
303  try_opencv_reader = true;
304 #endif
305  break;
306  case FORMAT_TIFF :
307  case FORMAT_BMP :
308  case FORMAT_DIB :
309  case FORMAT_PBM :
310  case FORMAT_RASTER :
311  case FORMAT_JPEG2000 :
312  case FORMAT_UNKNOWN :
313  try_opencv_reader = true;
314  break;
315  }
316 
317  if (try_opencv_reader) {
318 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
319  //std::cout << "Use opencv to read the image" << std::endl;
320  cv::Mat cvI = cv::imread(filename, cv::IMREAD_GRAYSCALE);
321  if (cvI.cols == 0 && cvI.rows == 0) {
322  std::string message = "Cannot read file \"" + std::string(filename) + "\": Image format not supported";
323  throw (vpImageException(vpImageException::ioError, message)) ;
324  }
325  vpImageConvert::convert(cvI, I);
326 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
327  //std::cout << "Use opencv to read the image" << std::endl;
328  cv::Mat cvI = cv::imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
329  if (cvI.cols == 0 && cvI.rows == 0) {
330  std::string message = "Cannot read file \"" + std::string(filename) + "\": Image format not supported";
331  throw (vpImageException(vpImageException::ioError, message)) ;
332  }
333  vpImageConvert::convert(cvI, I);
334 #else
335  std::string message = "Cannot read file \"" + std::string(filename) + "\": Image format not supported";
336  throw (vpImageException(vpImageException::ioError, message)) ;
337 #endif
338  }
339 }
356 void
357 vpImageIo::read(vpImage<unsigned char> &I, const std::string filename)
358 {
359  read(I,filename.c_str());
360 }
377 void
378 vpImageIo::read(vpImage<vpRGBa> &I, const char *filename)
379 {
380  bool exist = vpIoTools::checkFilename(filename);
381  if (!exist) {
382  std::string message = "Cannot read file: \"" + std::string(filename) + "\" doesn't exist";
384  }
385 
386  bool try_opencv_reader = false;
387 
388  switch(getFormat(filename)){
389  case FORMAT_PGM :
390  readPGM(I,filename); break;
391  case FORMAT_PPM :
392  readPPM(I,filename); break;
393  case FORMAT_JPEG :
394 #ifdef VISP_HAVE_LIBJPEG
395  readJPEG(I,filename);
396 #else
397  try_opencv_reader = true;
398 #endif
399  break;
400  case FORMAT_PNG :
401 #if defined(VISP_HAVE_LIBPNG)
402  readPNG(I,filename);
403 #else
404  try_opencv_reader = true;
405 #endif
406  break;
407  case FORMAT_TIFF :
408  case FORMAT_BMP :
409  case FORMAT_DIB :
410  case FORMAT_PBM :
411  case FORMAT_RASTER :
412  case FORMAT_JPEG2000 :
413  case FORMAT_UNKNOWN :
414  try_opencv_reader = true;
415  break;
416  }
417 
418  if (try_opencv_reader) {
419 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
420  // std::cout << "Use opencv to read the image" << std::endl;
421  cv::Mat cvI = cv::imread(filename, cv::IMREAD_COLOR);
422  if (cvI.cols == 0 && cvI.rows == 0) {
423  std::string message = "Cannot read file \"" + std::string(filename) + "\": Image format not supported";
424  throw (vpImageException(vpImageException::ioError, message)) ;
425  }
426  vpImageConvert::convert(cvI, I);
427 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
428  // std::cout << "Use opencv to read the image" << std::endl;
429  cv::Mat cvI = cv::imread(filename, CV_LOAD_IMAGE_COLOR);
430  if (cvI.cols == 0 && cvI.rows == 0) {
431  std::string message = "Cannot read file \"" + std::string(filename) + "\": Image format not supported";
432  throw (vpImageException(vpImageException::ioError, message)) ;
433  }
434  vpImageConvert::convert(cvI, I);
435 #else
436  std::string message = "Cannot read file \"" + std::string(filename) + "\": Image format not supported";
437  throw (vpImageException(vpImageException::ioError, message)) ;
438 #endif
439  }
440 }
457 void
458 vpImageIo::read(vpImage<vpRGBa> &I, const std::string filename)
459 {
460  read(I,filename.c_str());
461 }
462 
475 void
476 vpImageIo::write(const vpImage<unsigned char> &I, const char *filename)
477 {
478  bool try_opencv_writer = false;
479 
480  switch(getFormat(filename)){
481  case FORMAT_PGM :
482  writePGM(I,filename); break;
483  case FORMAT_PPM :
484  writePPM(I,filename); break;
485  case FORMAT_JPEG :
486 #ifdef VISP_HAVE_LIBJPEG
487  writeJPEG(I,filename);
488 #else
489  try_opencv_writer = true;
490 #endif
491  break;
492  case FORMAT_PNG :
493 #ifdef VISP_HAVE_LIBPNG
494  writePNG(I,filename);
495 #else
496  try_opencv_writer = true;
497 #endif
498  break;
499  case FORMAT_TIFF :
500  case FORMAT_BMP :
501  case FORMAT_DIB :
502  case FORMAT_PBM :
503  case FORMAT_RASTER :
504  case FORMAT_JPEG2000 :
505  case FORMAT_UNKNOWN :
506  try_opencv_writer = true;
507  break;
508  }
509 
510  if (try_opencv_writer) {
511 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
512  // std::cout << "Use opencv to write the image" << std::endl;
513  cv::Mat cvI;
514  vpImageConvert::convert(I, cvI);
515  cv::imwrite(filename, cvI);
516 #else
517  vpCERROR << "Cannot write file: Image format not supported..." << std::endl;
519  "Cannot write file: Image format not supported")) ;
520 #endif
521  }
522 }
535 void
536 vpImageIo::write(const vpImage<unsigned char> &I, const std::string filename)
537 {
538  write(I,filename.c_str());
539 }
552 void
553 vpImageIo::write(const vpImage<vpRGBa> &I, const char *filename)
554 {
555  bool try_opencv_writer = false;
556 
557  switch(getFormat(filename)){
558  case FORMAT_PGM :
559  writePGM(I,filename); break;
560  case FORMAT_PPM :
561  writePPM(I,filename); break;
562  case FORMAT_JPEG :
563 #ifdef VISP_HAVE_LIBJPEG
564  writeJPEG(I,filename);
565 #else
566  try_opencv_writer = true;
567 #endif
568  break;
569  case FORMAT_PNG :
570 #ifdef VISP_HAVE_LIBPNG
571  writePNG(I,filename);
572 #else
573  try_opencv_writer = true;
574 #endif
575  break;
576  case FORMAT_TIFF :
577  case FORMAT_BMP :
578  case FORMAT_DIB :
579  case FORMAT_PBM :
580  case FORMAT_RASTER :
581  case FORMAT_JPEG2000 :
582  case FORMAT_UNKNOWN :
583  try_opencv_writer = true;
584  break;
585  }
586 
587  if (try_opencv_writer) {
588 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
589  // std::cout << "Use opencv to write the image" << std::endl;
590  cv::Mat cvI;
591  vpImageConvert::convert(I, cvI);
592  cv::imwrite(filename, cvI);
593 #else
594  vpCERROR << "Cannot write file: Image format not supported..." << std::endl;
596  "Cannot write file: Image format not supported")) ;
597 #endif
598  }
599 }
612 void
613 vpImageIo::write(const vpImage<vpRGBa> &I, const std::string filename)
614 {
615  write(I,filename.c_str());
616 }
617 //--------------------------------------------------------------------------
618 // PFM
619 //--------------------------------------------------------------------------
620 
630 void
632  const char *filename)
633 {
634 
635  FILE* fd;
636 
637  // Test the filename
638  if (!filename || *filename == '\0') {
639  vpERROR_TRACE("no filename\n");
641  "no filename")) ;
642  }
643 
644  fd = fopen(filename, "wb");
645 
646  if (fd == NULL) {
647  vpERROR_TRACE("couldn't write to file \"%s\"\n", filename);
649  "cannot write file")) ;
650  }
651 
652  // Write the head
653  fprintf(fd, "P8\n"); // Magic number
654  fprintf(fd, "%d %d\n", I.getWidth(), I.getHeight()); // Image size
655  fprintf(fd, "255\n"); // Max level
656 
657  // Write the bitmap
658  size_t ierr;
659  size_t nbyte = I.getWidth()*I.getHeight();
660 
661  ierr = fwrite(I.bitmap, sizeof(float), nbyte, fd) ;
662  if (ierr != nbyte) {
663  fclose(fd);
664  vpERROR_TRACE("couldn't write %d bytes to file \"%s\"\n",
665  nbyte, filename) ;
667  "cannot write file")) ;
668  }
669 
670  fflush(fd);
671  fclose(fd);
672 
673 }
674 //--------------------------------------------------------------------------
675 // PGM
676 //--------------------------------------------------------------------------
677 
686 void
688  const char *filename)
689 {
690 
691  FILE* fd;
692 
693  // Test the filename
694  if (!filename || *filename == '\0') {
695  vpERROR_TRACE("no filename\n");
697  "no filename")) ;
698  }
699 
700  fd = fopen(filename, "wb");
701 
702  if (fd == NULL) {
703  vpERROR_TRACE("couldn't write to file \"%s\"\n", filename);
705  "cannot write file")) ;
706  }
707 
708  // Write the head
709  fprintf(fd, "P5\n"); // Magic number
710  fprintf(fd, "%d %d\n", I.getWidth(), I.getHeight()); // Image size
711  fprintf(fd, "255\n"); // Max level
712 
713  // Write the bitmap
714  size_t ierr;
715  size_t nbyte = I.getWidth()*I.getHeight();
716 
717  ierr = fwrite(I.bitmap, sizeof(unsigned char), nbyte, fd) ;
718  if (ierr != nbyte) {
719  fclose(fd);
720  vpERROR_TRACE("couldn't write %d bytes to file \"%s\"\n",
721  nbyte, filename) ;
723  "cannot write file")) ;
724  }
725 
726  fflush(fd);
727  fclose(fd);
728 
729 }
737 void
738 vpImageIo::writePGM(const vpImage<short> &I, const char *filename)
739 {
741  unsigned int nrows = I.getHeight();
742  unsigned int ncols = I.getWidth();
743 
744  Iuc.resize(nrows, ncols);
745 
746  for (unsigned int i=0 ; i < nrows * ncols ; i++)
747  Iuc.bitmap[i] = (unsigned char)I.bitmap[i] ;
748 
749  vpImageIo::writePGM(Iuc, filename) ;
750 
751 
752 }
762 void
763 vpImageIo::writePGM(const vpImage<vpRGBa> &I, const char *filename)
764 {
765 
766  FILE* fd;
767 
768  // Test the filename
769  if (!filename || *filename == '\0') {
770  vpERROR_TRACE("no filename\n");
772  "no filename")) ;
773  }
774 
775  fd = fopen(filename, "wb");
776 
777  if (fd == NULL) {
778  vpERROR_TRACE("couldn't write to file \"%s\"\n", filename);
780  "cannot write file")) ;
781  }
782 
783  // Write the head
784  fprintf(fd, "P5\n"); // Magic number
785  fprintf(fd, "%d %d\n", I.getWidth(), I.getHeight()); // Image size
786  fprintf(fd, "255\n"); // Max level
787 
788  // Write the bitmap
789  size_t ierr;
790  size_t nbyte = I.getWidth()*I.getHeight();
791 
792 
794  vpImageConvert::convert(I,Itmp) ;
795 
796  ierr = fwrite(Itmp.bitmap, sizeof(unsigned char), nbyte, fd) ;
797  if (ierr != nbyte) {
798  fclose(fd);
799  vpERROR_TRACE("couldn't write %d bytes to file \"%s\"\n",
800  nbyte, filename) ;
802  "cannot write file")) ;
803  }
804 
805  fflush(fd);
806  fclose(fd);
807 
808 }
809 
826 void
827 vpImageIo::readPFM(vpImage<float> &I, const char *filename)
828 {
829  FILE* fd = NULL; // File descriptor
830  int ierr;
831  int line;
832  int is255;
833  char* err ;
834  char str[vpMAX_LEN];
835  unsigned int w, h;
836 
837  // Test the filename
838  if (!filename || *filename == '\0')
839  {
840  vpERROR_TRACE("no filename") ;
842  " no filename")) ;
843 
844  }
845 
846  // Open the filename
847  fd = fopen(filename, "rb");
848  if (fd == NULL)
849  {
850  vpERROR_TRACE("couldn't read file \"%s\"", filename) ;
852  "couldn't read file")) ;
853  }
854 
855  // Read the first line with magic number P8
856  line = 0;
857 
858  err = fgets(str, vpMAX_LEN - 1, fd);
859  line++;
860  if (err == NULL)
861  {
862  fclose (fd);
863  vpERROR_TRACE("couldn't read line %d of file \"%s\"\n", line, filename) ;
865  "couldn't read file")) ;
866  }
867 
868  if (strlen(str) < 3)
869  {
870  fclose (fd);
871  vpERROR_TRACE("\"%s\" is not a PFM file\n", filename) ;
873  "this is not a PFM file")) ;
874  }
875 
876  str[2] = '\0';
877  if (strcmp(str, "P8") != 0)
878  {
879  fclose (fd);
880  vpERROR_TRACE("\"%s\" is not a PFM file\n", filename) ;
882  "this is not a PFM file")) ;
883  }
884 
885  // Jump the possible comment, or empty line and read the following line
886  do {
887  err = fgets(str, vpMAX_LEN - 1, fd);
888  line++;
889  if (err == NULL) {
890  fprintf(stderr, "couldn't read line %d of file \"%s\"\n", line, filename);
891  fclose (fd);
893  "Cannot read content of PFM file")) ;
894  }
895  } while ((str[0] == '#') || (str[0] == '\n'));
896 
897  // Extract image size
898  ierr = sscanf(str, "%d %d", &w, &h);
899  if (w > 100000 || h>100000) {
900  fclose (fd);
901  throw(vpException(vpException::badValue, "Bad image size"));
902  }
903 
904  if(ierr == 1){// the norm allows to have the two values on two separated lines.
905  do {
906  err = fgets(str, vpMAX_LEN - 1, fd);
907  line++;
908  if (err == NULL) {
909  fprintf(stderr, "couldn't read line %d of file \"%s\"\n", line, filename);
910  fclose (fd);
912  "Cannot read content of PFM file")) ;
913  }
914  } while ((str[0] == '#') || (str[0] == '\n'));
915  ierr = sscanf(str, "%d", &h);
916  }
917  if (ierr == EOF)
918  {
919  fclose (fd);
920  vpERROR_TRACE("couldn't read line %d of file \"%s\"\n",line, filename) ;
922  "Cannot read content of PFM file")) ;
923  }
924 
925  if ((h != I.getHeight())||( w != I.getWidth()))
926  {
927  try
928  {
929  I.resize(h,w) ;
930  }
931  catch(...)
932  {
933  fclose (fd);
935  "Cannot read content of PFM file")) ;
936  }
937  }
938 
939  // Read 255
940  err = fgets(str, vpMAX_LEN - 1, fd);
941  line++;
942  if (err == NULL) {
943  fclose (fd);
944  vpERROR_TRACE("couldn't read line %d of file \"%s\"\n",line, filename) ;
946  "Cannot read content of PFM file")) ;
947  }
948 
949  ierr = sscanf(str, "%d", &is255);
950  if (ierr == EOF) {
951  fclose (fd);
952  vpERROR_TRACE("couldn't read line %d of file \"%s\"\n", line, filename) ;
954  "Cannot read content of PFM file")) ;
955  }
956 
957  if (is255 != 255)
958  {
959  fclose (fd);
960  vpERROR_TRACE("MAX_VAL is not 255 in file \"%s\"\n", filename) ;
962  "Cannot read content of PFM file")) ;
963  }
964 
965  unsigned int nbyte = I.getHeight()*I.getWidth();
966  if (fread (I.bitmap, sizeof(float), nbyte, fd ) != nbyte)
967  {
968  fclose (fd);
969  vpERROR_TRACE("couldn't read %d bytes in file \"%s\"\n", nbyte, filename) ;
971  "Cannot read content of PFM file")) ;
972  }
973 
974  fclose (fd);
975 }
976 
977 
978 
995 void
997 {
998  FILE* fd = NULL; // File descriptor
999  int ierr;
1000  char* err ;
1001  char str[vpMAX_LEN];
1002  unsigned int magic=5, w=0, h=0, maxval=255;
1003 
1004  // Test the filename
1005  if (!filename || *filename == '\0') {
1007  "No filename")) ;
1008  }
1009 
1010  // Open the filename
1011  if ((fd = fopen(filename, "rb")) == NULL) {
1013  "Cannot read file \"%s\"", filename)) ;
1014  }
1015 
1016  while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] == '#') || (str[0] == '\n'))) {};
1017  if (err == NULL) {
1018  fclose (fd);
1020  "Cannot read header of file \"%s\"", filename));
1021  }
1022  if ((ierr = sscanf(str, "P%u %u %u %u", &magic, &w, &h, &maxval)) == 0) {
1023  fclose (fd);
1025  "Cannot read header of file \"%s\"", filename));
1026  }
1027 
1028  if (magic != 5) {
1029  fclose (fd);
1031  "\"%s\" is not a PGM P5 file", filename));
1032  }
1033 
1034  // Depending on ierr the line may contain:
1035  // 1 : P5
1036  // 2 : P5 w
1037  // 3 : P5 w h
1038  // 4 : P5 w h maxval
1039 
1040  if (ierr == 1) {
1041 // std::cout << "magic: " << magic << std::endl;
1042  while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] == '#') || (str[0] == '\n'))) {};
1043  if (err == NULL) {
1044  fclose (fd);
1046  "Cannot read header of file \"%s\"", filename));
1047  }
1048  if (((ierr = sscanf(str, "%u %u %u", &w, &h, &maxval)) == 0) || (ierr != 1 && ierr != 2 && ierr != 3)) {
1049  fclose (fd);
1051  "Cannot read header of file \"%s\"", filename));
1052  }
1053  // Depending on ierr the line may contain:
1054  // 1 : w
1055  // 2 : w h
1056  // 3 : w h maxval
1057  if (ierr == 1) {
1058 // std::cout << "w: " << w << std::endl;
1059  while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] == '#') || (str[0] == '\n'))) {};
1060  if (err == NULL) {
1061  fclose (fd);
1063  "Cannot read header of file \"%s\"", filename));
1064  }
1065  if (((ierr = sscanf(str, "%u %u", &h, &maxval)) == 0) || (ierr != 1 && ierr != 2)) {
1066  fclose (fd);
1068  "Cannot read header of file \"%s\"", filename));
1069  }
1070  if (ierr == 1) {
1071 // std::cout << "h: " << h << std::endl;
1072  while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] == '#') || (str[0] == '\n'))) {};
1073  if (err == NULL) {
1074  fclose (fd);
1076  "Cannot read header of file \"%s\"", filename));
1077  }
1078  if ((ierr = sscanf(str, "%u", &maxval)) != 1) {
1079  fclose (fd);
1081  "Cannot read header of file \"%s\"", filename));
1082  }
1083  }
1084 // else {
1085 // std::cout << "h: " << h << " maxval: " << maxval << std::endl;
1086 // }
1087  }
1088  else if (ierr == 2) {
1089 // std::cout << "w: " << w << " h: " << h << std::endl;
1090 
1091  while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] == '#') || (str[0] == '\n'))) {};
1092  if (err == NULL) {
1093  fclose (fd);
1095  "Cannot read header of file \"%s\"", filename));
1096  }
1097  if ((ierr = sscanf(str, "%u", &maxval)) != 1) {
1098  fclose (fd);
1100  "Cannot read header of file \"%s\"", filename));
1101  }
1102 // std::cout << "maxval: " << maxval << std::endl;
1103  }
1104 // else {
1105 // std::cout << "w: " << w << " h: " << h << " maxval: " << maxval << std::endl;
1106 // }
1107  }
1108  else if (ierr == 2) {
1109 // std::cout << "magic: " << magic << " w: " << w << std::endl;
1110  while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] == '#') || (str[0] == '\n'))) {};
1111  if (err == NULL) {
1112  fclose (fd);
1114  "Cannot read header of file \"%s\"", filename));
1115  }
1116  if (((ierr = sscanf(str, "%u %u", &h, &maxval)) == 0) || (ierr != 1 && ierr != 2)) {
1117  fclose (fd);
1119  "Cannot read header of file \"%s\"", filename));
1120  }
1121  if (ierr == 1) {
1122 // std::cout << "h: " << h << std::endl;
1123  while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] == '#') || (str[0] == '\n'))) {};
1124  if (err == NULL) {
1125  fclose (fd);
1127  "Cannot read header of file \"%s\"", filename));
1128  }
1129  if ((ierr = sscanf(str, "%u", &maxval)) != 1) {
1130  fclose (fd);
1132  "Cannot read header of file \"%s\"", filename));
1133  }
1134 // std::cout << "maxval: " << maxval << std::endl;
1135  }
1136 // else {
1137 // std::cout << "h: " << h << " maxval: " << maxval << std::endl;
1138 // }
1139  }
1140  else if (ierr == 3) {
1141 // std::cout << "magic: " << magic << " w: " << w << " h: " << h << std::endl;
1142  while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] == '#') || (str[0] == '\n'))) {};
1143  if (err == NULL) {
1144  fclose (fd);
1146  "Cannot read header of file \"%s\"", filename));
1147  }
1148  if ((ierr = sscanf(str, "%u", &maxval)) != 1) {
1149  fclose (fd);
1151  "Cannot read header of file \"%s\"", filename));
1152  }
1153 // std::cout << "maxval: " << maxval << std::endl;
1154  }
1155 // else if (ierr == 4) {
1156 // std::cout << "magic: " << magic << " w: " << w << " h: " << h << " maxval: " << maxval << std::endl;
1157 // }
1158 
1159  if (w > 100000 || h>100000) {
1160  fclose (fd);
1161  throw(vpException(vpException::badValue, "Bad image size in \"%s\"", filename));
1162  }
1163  if (maxval != 255)
1164  {
1165  fclose (fd);
1167  "Bad maxval in \"%s\"", filename));
1168  }
1169 
1170  if ((h != I.getHeight())||( w != I.getWidth())) {
1171  I.resize(h,w) ;
1172  }
1173 
1174  unsigned int nbyte = I.getHeight()*I.getWidth();
1175  size_t n;
1176  if ((n = fread (I.bitmap, sizeof(unsigned char), nbyte, fd)) != nbyte) {
1177  fclose (fd);
1179  "Read only %d of %d bytes in file \"%s\"", n, nbyte, filename));
1180  }
1181 
1182  fclose (fd);
1183 }
1184 
1185 
1204 void
1205 vpImageIo::readPGM(vpImage<vpRGBa> &I, const char *filename)
1206 {
1207  try
1208  {
1209  vpImage<unsigned char> Itmp ;
1210 
1211  vpImageIo::readPGM(Itmp, filename) ;
1212 
1213  vpImageConvert::convert(Itmp, I) ;
1214 
1215  }
1216  catch(...)
1217  {
1218  vpERROR_TRACE(" ") ;
1219  throw ;
1220  }
1221 }
1222 
1223 
1224 //--------------------------------------------------------------------------
1225 // PPM
1226 //--------------------------------------------------------------------------
1227 
1244 void
1246 {
1247 
1248  try
1249  {
1250  vpImage<vpRGBa> Itmp ;
1251 
1252  vpImageIo::readPPM(Itmp, filename) ;
1253 
1254  vpImageConvert::convert(Itmp, I) ;
1255  }
1256  catch(...)
1257  {
1258  vpERROR_TRACE(" ") ;
1259  throw ;
1260  }
1261 }
1262 
1263 
1275 void
1276 vpImageIo::readPPM(vpImage<vpRGBa> &I, const char *filename)
1277 {
1278  FILE* fd = NULL; // File descriptor
1279  int ierr;
1280  char* err ;
1281  char str[vpMAX_LEN];
1282  unsigned int magic=5, w=0, h=0, maxval=255;
1283 
1284  // Test the filename
1285  if (!filename || *filename == '\0') {
1287  "No filename")) ;
1288  }
1289 
1290  // Open the filename
1291  if ((fd = fopen(filename, "rb")) == NULL) {
1293  "Cannot read file \"%s\"", filename)) ;
1294  }
1295 
1296  while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] == '#') || (str[0] == '\n'))) {};
1297  if (err == NULL) {
1298  fclose (fd);
1300  "Cannot read header of file \"%s\"", filename));
1301  }
1302  if ((ierr = sscanf(str, "P%u %u %u %u", &magic, &w, &h, &maxval)) == 0) {
1303  fclose (fd);
1305  "Cannot read header of file \"%s\"", filename));
1306  }
1307 
1308  if (magic != 6) {
1309  fclose (fd);
1311  "\"%s\" is not a PGM P6 file", filename));
1312  }
1313 
1314  // Depending on ierr the line may contain:
1315  // 1 : P6
1316  // 2 : P6 w
1317  // 3 : P6 w h
1318  // 4 : P6 w h maxval
1319 
1320  if (ierr == 1) {
1321 // std::cout << "magic: " << magic << std::endl;
1322  while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] == '#') || (str[0] == '\n'))) {};
1323  if (err == NULL) {
1324  fclose (fd);
1326  "Cannot read header of file \"%s\"", filename));
1327  }
1328  if (((ierr = sscanf(str, "%u %u %u", &w, &h, &maxval)) == 0) || (ierr != 1 && ierr != 2 && ierr != 3)) {
1329  fclose (fd);
1331  "Cannot read header of file \"%s\"", filename));
1332  }
1333  // Depending on ierr the line may contain:
1334  // 1 : w
1335  // 2 : w h
1336  // 3 : w h maxval
1337  if (ierr == 1) {
1338 // std::cout << "w: " << w << std::endl;
1339  while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] == '#') || (str[0] == '\n'))) {};
1340  if (err == NULL) {
1341  fclose (fd);
1343  "Cannot read header of file \"%s\"", filename));
1344  }
1345  if (((ierr = sscanf(str, "%u %u", &h, &maxval)) == 0) || (ierr != 1 && ierr != 2)) {
1346  fclose (fd);
1348  "Cannot read header of file \"%s\"", filename));
1349  }
1350  if (ierr == 1) {
1351 // std::cout << "h: " << h << std::endl;
1352  while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] == '#') || (str[0] == '\n'))) {};
1353  if (err == NULL) {
1354  fclose (fd);
1356  "Cannot read header of file \"%s\"", filename));
1357  }
1358  if ((ierr = sscanf(str, "%u", &maxval)) != 1) {
1359  fclose (fd);
1361  "Cannot read header of file \"%s\"", filename));
1362  }
1363  }
1364 // else {
1365 // std::cout << "h: " << h << " maxval: " << maxval << std::endl;
1366 // }
1367  }
1368  else if (ierr == 2) {
1369 // std::cout << "w: " << w << " h: " << h << std::endl;
1370 
1371  while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] == '#') || (str[0] == '\n'))) {};
1372  if (err == NULL) {
1373  fclose (fd);
1375  "Cannot read header of file \"%s\"", filename));
1376  }
1377  if ((ierr = sscanf(str, "%u", &maxval)) != 1) {
1378  fclose (fd);
1380  "Cannot read header of file \"%s\"", filename));
1381  }
1382 // std::cout << "maxval: " << maxval << std::endl;
1383  }
1384 // else {
1385 // std::cout << "w: " << w << " h: " << h << " maxval: " << maxval << std::endl;
1386 // }
1387  }
1388  else if (ierr == 2) {
1389 // std::cout << "magic: " << magic << " w: " << w << std::endl;
1390  while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] == '#') || (str[0] == '\n'))) {};
1391  if (err == NULL) {
1392  fclose (fd);
1394  "Cannot read header of file \"%s\"", filename));
1395  }
1396  if (((ierr = sscanf(str, "%u %u", &h, &maxval)) == 0) || (ierr != 1 && ierr != 2)) {
1397  fclose (fd);
1399  "Cannot read header of file \"%s\"", filename));
1400  }
1401  if (ierr == 1) {
1402 // std::cout << "h: " << h << std::endl;
1403  while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] == '#') || (str[0] == '\n'))) {};
1404  if (err == NULL) {
1405  fclose (fd);
1407  "Cannot read header of file \"%s\"", filename));
1408  }
1409  if ((ierr = sscanf(str, "%u", &maxval)) != 1) {
1410  fclose (fd);
1412  "Cannot read header of file \"%s\"", filename));
1413  }
1414 // std::cout << "maxval: " << maxval << std::endl;
1415  }
1416 // else {
1417 // std::cout << "h: " << h << " maxval: " << maxval << std::endl;
1418 // }
1419  }
1420  else if (ierr == 3) {
1421 // std::cout << "magic: " << magic << " w: " << w << " h: " << h << std::endl;
1422  while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] == '#') || (str[0] == '\n'))) {};
1423  if (err == NULL) {
1424  fclose (fd);
1426  "Cannot read header of file \"%s\"", filename));
1427  }
1428  if ((ierr = sscanf(str, "%u", &maxval)) != 1) {
1429  fclose (fd);
1431  "Cannot read header of file \"%s\"", filename));
1432  }
1433 // std::cout << "maxval: " << maxval << std::endl;
1434  }
1435 // else if (ierr == 4) {
1436 // std::cout << "magic: " << magic << " w: " << w << " h: " << h << " maxval: " << maxval << std::endl;
1437 // }
1438 
1439  if (w > 100000 || h>100000) {
1440  fclose (fd);
1441  throw(vpException(vpException::badValue, "Bad image size in \"%s\"", filename));
1442  }
1443  if (maxval != 255)
1444  {
1445  fclose (fd);
1447  "Bad maxval in \"%s\"", filename));
1448  }
1449 
1450  if ((h != I.getHeight())||( w != I.getWidth())) {
1451  I.resize(h,w) ;
1452  }
1453 
1454  for(unsigned int i=0;i<I.getHeight();i++)
1455  {
1456  for(unsigned int j=0;j<I.getWidth();j++)
1457  {
1458  vpRGBa v ;
1459  size_t res = fread(&v.R,sizeof(v.R),1,fd) ;
1460  res |= fread(&v.G,sizeof(v.G),1,fd) ;
1461  res |= fread(&v.B,sizeof(v.B),1,fd) ;
1462  if (res==0)
1463  {
1464  fclose (fd);
1466  "Cannot read bytes in file \"%s\"\n", filename));
1467  }
1468  I[i][j] = v ;
1469  }
1470  }
1471 
1472  fclose (fd);
1473 }
1474 
1485 void
1486 vpImageIo::writePPM(const vpImage<unsigned char> &I, const char *filename)
1487 {
1488 
1489  try
1490  {
1491  vpImage<vpRGBa> Itmp ;
1492 
1493  vpImageConvert::convert(I, Itmp) ;
1494 
1495  vpImageIo::writePPM(Itmp, filename) ;
1496  }
1497  catch(...)
1498  {
1499  vpERROR_TRACE(" ") ;
1500  throw ;
1501  }
1502 }
1503 
1504 
1512 void
1513 vpImageIo::writePPM(const vpImage<vpRGBa> &I, const char *filename)
1514 {
1515 
1516  FILE* f;
1517 
1518 
1519  // Test the filename
1520  if (!filename || *filename == '\0') {
1521  vpERROR_TRACE("no filename\n");
1523  "no filename")) ;
1524  }
1525 
1526  f = fopen(filename, "wb");
1527 
1528  if (f == NULL) {
1529  vpERROR_TRACE("couldn't write to file \"%s\"\n", filename);
1531  "cannot write file")) ;
1532  }
1533 
1534 
1535 
1536  fprintf(f,"P6\n"); // Magic number
1537  fprintf(f,"%d %d\n", I.getWidth(), I.getHeight()); // Image size
1538  fprintf(f,"%d\n",255); // Max level
1539 
1540  for(unsigned int i=0;i<I.getHeight();i++)
1541  {
1542  for(unsigned int j=0;j<I.getWidth();j++)
1543  {
1544  vpRGBa P ;
1545  size_t res ;
1546  P = I[i][j] ;
1547  unsigned char tmp ;
1548  tmp = P.R ;
1549  res = fwrite(&tmp,sizeof(tmp),1,f) ;
1550  if (res==0)
1551  {
1552  fclose(f);
1553  vpERROR_TRACE("couldn't write file") ;
1555  "cannot write file")) ;
1556  }
1557  tmp = P.G;
1558  res = fwrite(&tmp,sizeof(tmp),1,f) ;
1559  if (res==0)
1560  {
1561  fclose(f);
1562  vpERROR_TRACE("couldn't write file") ;
1564  "cannot write file")) ;
1565  }
1566  tmp = P.B ;
1567  res = fwrite(&tmp,sizeof(tmp),1,f) ;
1568  if (res==0)
1569  {
1570  fclose(f);
1571  vpERROR_TRACE("couldn't write file") ;
1573  "cannot write file")) ;
1574  }
1575  }
1576  }
1577 
1578  fflush(f);
1579  fclose(f);
1580 }
1581 
1582 
1598 void
1599 vpImageIo::readPGM(vpImage<unsigned char> &I, const std::string filename)
1600 {
1601  vpImageIo::readPGM(I, filename.c_str());
1602 }
1603 
1619 void
1620 vpImageIo::readPGM(vpImage<vpRGBa> &I, const std::string filename)
1621 {
1622  vpImageIo::readPGM(I, filename.c_str());
1623 }
1624 
1634 void
1636  const std::string filename)
1637 {
1638  vpImageIo::writePGM(I, filename.c_str());
1639 }
1640 
1649 void
1650 vpImageIo::writePGM(const vpImage<short> &I, const std::string filename)
1651 {
1652 
1653  vpImageIo::writePGM(I, filename.c_str());
1654 }
1655 
1666 void
1667 vpImageIo::writePGM(const vpImage<vpRGBa> &I, const std::string filename)
1668 {
1669  vpImageIo::writePGM(I, filename.c_str());
1670 }
1671 
1672 //--------------------------------------------------------------------------
1673 // PPM
1674 //--------------------------------------------------------------------------
1675 
1692 void
1693 vpImageIo::readPPM(vpImage<unsigned char> &I, const std::string filename)
1694 {
1695  vpImageIo::readPPM(I, filename.c_str());
1696 }
1697 
1709 void
1710 vpImageIo::readPPM(vpImage<vpRGBa> &I, const std::string filename)
1711 {
1712  vpImageIo::readPPM(I, filename.c_str());
1713 }
1714 
1725 void
1726 vpImageIo::writePPM(const vpImage<unsigned char> &I, const std::string filename)
1727 {
1728  vpImageIo::writePPM(I, filename.c_str());
1729 }
1730 
1739 void
1740 vpImageIo::writePPM(const vpImage<vpRGBa> &I, const std::string filename)
1741 {
1742  vpImageIo::writePPM(I, filename.c_str());
1743 }
1744 
1745 
1746 //--------------------------------------------------------------------------
1747 // JPEG
1748 //--------------------------------------------------------------------------
1749 
1750 #if defined(VISP_HAVE_LIBJPEG)
1751 
1759 void
1760 vpImageIo::writeJPEG(const vpImage<unsigned char> &I, const char *filename)
1761 {
1762  struct jpeg_compress_struct cinfo;
1763  struct jpeg_error_mgr jerr;
1764  FILE *file;
1765 
1766  cinfo.err = jpeg_std_error(&jerr);
1767  jpeg_create_compress(&cinfo);
1768 
1769  // Test the filename
1770  if (!filename || *filename == '\0') {
1771  vpERROR_TRACE("no filename\n");
1773  "no filename")) ;
1774  }
1775 
1776  file = fopen(filename, "wb");
1777 
1778  if (file == NULL) {
1779  vpERROR_TRACE("couldn't write file \"%s\"\n", filename);
1781  "cannot write file")) ;
1782  }
1783 
1784  unsigned int width = I.getWidth();
1785  unsigned int height = I.getHeight();
1786 
1787  jpeg_stdio_dest(&cinfo, file);
1788 
1789  cinfo.image_width = width;
1790  cinfo.image_height = height;
1791  cinfo.input_components = 1;
1792  cinfo.in_color_space = JCS_GRAYSCALE;
1793  jpeg_set_defaults(&cinfo);
1794 
1795  jpeg_start_compress(&cinfo,TRUE);
1796 
1797  unsigned char *line;
1798  line = new unsigned char[width];
1799  unsigned char* input = (unsigned char*)I.bitmap;
1800  while (cinfo.next_scanline < cinfo.image_height)
1801  {
1802  for (unsigned int i = 0; i < width; i++)
1803  {
1804  line[i] = *(input);
1805  input++;
1806  }
1807  jpeg_write_scanlines(&cinfo, &line, 1);
1808  }
1809 
1810  jpeg_finish_compress(&cinfo);
1811  jpeg_destroy_compress(&cinfo);
1812  delete [] line;
1813  fclose(file);
1814 }
1815 
1816 
1824 void
1825 vpImageIo::writeJPEG(const vpImage<unsigned char> &I, const std::string filename)
1826 {
1827  vpImageIo::writeJPEG(I, filename.c_str());
1828 }
1829 
1830 
1838 void
1839 vpImageIo::writeJPEG(const vpImage<vpRGBa> &I, const char *filename)
1840 {
1841  struct jpeg_compress_struct cinfo;
1842  struct jpeg_error_mgr jerr;
1843  FILE *file;
1844 
1845  cinfo.err = jpeg_std_error(&jerr);
1846  jpeg_create_compress(&cinfo);
1847 
1848  // Test the filename
1849  if (!filename || *filename == '\0') {
1850  vpERROR_TRACE("no filename\n");
1852  "no filename")) ;
1853  }
1854 
1855  file = fopen(filename, "wb");
1856 
1857  if (file == NULL) {
1858  vpERROR_TRACE("couldn't write file \"%s\"\n", filename);
1860  "cannot write file")) ;
1861  }
1862 
1863  unsigned int width = I.getWidth();
1864  unsigned int height = I.getHeight();
1865 
1866  jpeg_stdio_dest(&cinfo, file);
1867 
1868  cinfo.image_width = width;
1869  cinfo.image_height = height;
1870  cinfo.input_components = 3;
1871  cinfo.in_color_space = JCS_RGB;
1872  jpeg_set_defaults(&cinfo);
1873 
1874  jpeg_start_compress(&cinfo,TRUE);
1875 
1876  unsigned char *line;
1877  line = new unsigned char[3*width];
1878  unsigned char* input = (unsigned char*)I.bitmap;
1879  while (cinfo.next_scanline < cinfo.image_height)
1880  {
1881  for (unsigned int i = 0; i < width; i++)
1882  {
1883  line[i*3] = *(input); input++;
1884  line[i*3+1] = *(input); input++;
1885  line[i*3+2] = *(input); input++;
1886  input++;
1887  }
1888  jpeg_write_scanlines(&cinfo, &line, 1);
1889  }
1890 
1891  jpeg_finish_compress(&cinfo);
1892  jpeg_destroy_compress(&cinfo);
1893  delete [] line;
1894  fclose(file);
1895 }
1896 
1897 
1905 void
1906 vpImageIo::writeJPEG(const vpImage<vpRGBa> &I, const std::string filename)
1907 {
1908  vpImageIo::writeJPEG(I, filename.c_str());
1909 }
1910 
1911 
1928 void
1930 {
1931  struct jpeg_decompress_struct cinfo;
1932  struct jpeg_error_mgr jerr;
1933  FILE *file;
1934 
1935  cinfo.err = jpeg_std_error(&jerr);
1936  jpeg_create_decompress(&cinfo);
1937 
1938  // Test the filename
1939  if (!filename || *filename == '\0') {
1940  vpERROR_TRACE("no filename\n");
1942  "no filename")) ;
1943  }
1944 
1945  file = fopen(filename, "rb");
1946 
1947  if (file == NULL) {
1948  vpERROR_TRACE("couldn't read file \"%s\"\n", filename);
1950  "cannot read file")) ;
1951  }
1952 
1953  jpeg_stdio_src(&cinfo, file);
1954  jpeg_read_header(&cinfo, TRUE);
1955 
1956  unsigned int width = cinfo.image_width;
1957  unsigned int height = cinfo.image_height;
1958 
1959  if ( (width != I.getWidth()) || (height != I.getHeight()) )
1960  I.resize(height,width);
1961 
1962  jpeg_start_decompress(&cinfo);
1963 
1964  unsigned int rowbytes = cinfo.output_width * (unsigned int)(cinfo.output_components);
1965  JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray)
1966  ((j_common_ptr) &cinfo, JPOOL_IMAGE, rowbytes, 1);
1967 
1968  if (cinfo.out_color_space == JCS_RGB) {
1969  vpImage<vpRGBa> Ic(height,width);
1970  unsigned char* output = (unsigned char*)Ic.bitmap;
1971  while (cinfo.output_scanline<cinfo.output_height) {
1972  jpeg_read_scanlines(&cinfo,buffer,1);
1973  for (unsigned int i = 0; i < width; i++) {
1974  *(output++) = buffer[0][i*3];
1975  *(output++) = buffer[0][i*3+1];
1976  *(output++) = buffer[0][i*3+2];
1977  *(output++) = 0;
1978  }
1979  }
1980  vpImageConvert::convert(Ic,I) ;
1981  }
1982 
1983  else if (cinfo.out_color_space == JCS_GRAYSCALE)
1984  {
1985  unsigned int row;
1986  while (cinfo.output_scanline<cinfo.output_height)
1987  {
1988  row = cinfo.output_scanline;
1989  jpeg_read_scanlines(&cinfo,buffer,1);
1990  memcpy(I[row], buffer[0], rowbytes);
1991  }
1992  }
1993 
1994  jpeg_finish_decompress(&cinfo);
1995  jpeg_destroy_decompress(&cinfo);
1996  fclose(file);
1997 }
1998 
1999 
2016 void
2017 vpImageIo::readJPEG(vpImage<unsigned char> &I, const std::string filename)
2018 {
2019  vpImageIo::readJPEG(I, filename.c_str());
2020 }
2021 
2022 
2041 void
2042 vpImageIo::readJPEG(vpImage<vpRGBa> &I, const char *filename)
2043 {
2044  struct jpeg_decompress_struct cinfo;
2045  struct jpeg_error_mgr jerr;
2046  FILE *file;
2047 
2048  cinfo.err = jpeg_std_error(&jerr);
2049  jpeg_create_decompress(&cinfo);
2050 
2051  // Test the filename
2052  if (!filename || *filename == '\0') {
2053  vpERROR_TRACE("no filename\n");
2055  "no filename")) ;
2056  }
2057 
2058  file = fopen(filename, "rb");
2059 
2060  if (file == NULL) {
2061  vpERROR_TRACE("couldn't read file \"%s\"\n", filename);
2063  "cannot read file")) ;
2064  }
2065 
2066  jpeg_stdio_src(&cinfo, file);
2067 
2068  jpeg_read_header(&cinfo, TRUE);
2069 
2070  unsigned int width = cinfo.image_width;
2071  unsigned int height = cinfo.image_height;
2072 
2073  if ( (width != I.getWidth()) || (height != I.getHeight()) )
2074  I.resize(height,width);
2075 
2076  jpeg_start_decompress(&cinfo);
2077 
2078  unsigned int rowbytes = cinfo.output_width * (unsigned int)(cinfo.output_components);
2079  JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray)
2080  ((j_common_ptr) &cinfo, JPOOL_IMAGE, rowbytes, 1);
2081 
2082  if (cinfo.out_color_space == JCS_RGB)
2083  {
2084  unsigned char* output = (unsigned char*)I.bitmap;
2085  while (cinfo.output_scanline<cinfo.output_height)
2086  {
2087  jpeg_read_scanlines(&cinfo,buffer,1);
2088  for (unsigned int i = 0; i < width; i++) {
2089  *(output++) = buffer[0][i*3];
2090  *(output++) = buffer[0][i*3+1];
2091  *(output++) = buffer[0][i*3+2];
2092  *(output++) = 0;
2093  }
2094  }
2095  }
2096 
2097  else if (cinfo.out_color_space == JCS_GRAYSCALE)
2098  {
2099  vpImage<unsigned char> Ig(height,width);
2100 
2101  unsigned int row;
2102  while (cinfo.output_scanline<cinfo.output_height)
2103  {
2104  row = cinfo.output_scanline;
2105  jpeg_read_scanlines(&cinfo,buffer,1);
2106  memcpy(Ig[row], buffer[0], rowbytes);
2107  }
2108 
2109  vpImageConvert::convert(Ig,I) ;
2110  }
2111 
2112  jpeg_finish_decompress(&cinfo);
2113  jpeg_destroy_decompress(&cinfo);
2114  fclose(file);
2115 }
2116 
2117 
2136 void
2137 vpImageIo::readJPEG(vpImage<vpRGBa> &I, const std::string filename)
2138 {
2139  vpImageIo::readJPEG(I, filename.c_str());
2140 }
2141 
2142 #elif defined(VISP_HAVE_OPENCV)
2143 
2151 void
2152 vpImageIo::writeJPEG(const vpImage<unsigned char> &I, const char *filename)
2153 {
2154 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408)
2155  cv::Mat Ip;
2156  vpImageConvert::convert(I, Ip);
2157  cv::imwrite(filename, Ip);
2158 #else
2159  IplImage* Ip = NULL;
2160  vpImageConvert::convert(I, Ip);
2161 
2162  cvSaveImage(filename, Ip);
2163 
2164  cvReleaseImage(&Ip);
2165 #endif
2166 }
2167 
2168 
2176 void
2177 vpImageIo::writeJPEG(const vpImage<unsigned char> &I, const std::string filename)
2178 {
2179  vpImageIo::writeJPEG(I, filename.c_str());
2180 }
2181 
2182 
2190 void
2191 vpImageIo::writeJPEG(const vpImage<vpRGBa> &I, const char *filename)
2192 {
2193 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408)
2194  cv::Mat Ip;
2195  vpImageConvert::convert(I, Ip);
2196  cv::imwrite(filename, Ip);
2197 #else
2198  IplImage* Ip = NULL;
2199  vpImageConvert::convert(I, Ip);
2200 
2201  cvSaveImage(filename, Ip);
2202 
2203  cvReleaseImage(&Ip);
2204 #endif
2205 }
2206 
2207 
2215 void
2216 vpImageIo::writeJPEG(const vpImage<vpRGBa> &I, const std::string filename)
2217 {
2218  vpImageIo::writeJPEG(I, filename.c_str());
2219 }
2220 
2221 
2238 void
2239 vpImageIo::readJPEG(vpImage<unsigned char> &I, const char *filename)
2240 {
2241 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
2242  cv::Mat Ip = cv::imread(filename, cv::IMREAD_GRAYSCALE);
2243  if ( ! Ip.empty())
2244  vpImageConvert::convert(Ip, I);
2245  else
2246  throw (vpImageException(vpImageException::ioError, "Can't read the image")) ;
2247 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020408)
2248  cv::Mat Ip = cv::imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
2249  if ( ! Ip.empty())
2250  vpImageConvert::convert(Ip, I);
2251  else
2252  throw (vpImageException(vpImageException::ioError, "Can't read the image")) ;
2253 #else
2254  IplImage* Ip = NULL;
2255  Ip = cvLoadImage(filename, CV_LOAD_IMAGE_GRAYSCALE);
2256  if (Ip != NULL)
2257  vpImageConvert::convert(Ip, I);
2258  else
2260  "Can't read the image")) ;
2261  cvReleaseImage(&Ip);
2262 #endif
2263 }
2264 
2265 
2282 void
2283 vpImageIo::readJPEG(vpImage<unsigned char> &I, const std::string filename)
2284 {
2285  vpImageIo::readJPEG(I, filename.c_str());
2286 }
2287 
2288 
2307 void
2308 vpImageIo::readJPEG(vpImage<vpRGBa> &I, const char *filename)
2309 {
2310 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
2311  cv::Mat Ip = cv::imread(filename, cv::IMREAD_GRAYSCALE);
2312  if ( ! Ip.empty())
2313  vpImageConvert::convert(Ip, I);
2314  else
2315  throw (vpImageException(vpImageException::ioError, "Can't read the image")) ;
2316 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020408)
2317  cv::Mat Ip = cv::imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
2318  if ( ! Ip.empty())
2319  vpImageConvert::convert(Ip, I);
2320  else
2321  throw (vpImageException(vpImageException::ioError, "Can't read the image")) ;
2322 #else
2323  IplImage* Ip = NULL;
2324  Ip = cvLoadImage(filename, CV_LOAD_IMAGE_COLOR);
2325  if (Ip != NULL)
2326  vpImageConvert::convert(Ip, I);
2327  else
2328  throw (vpImageException(vpImageException::ioError, "Can't read the image")) ;
2329  cvReleaseImage(&Ip);
2330 #endif
2331 }
2332 
2333 
2352 void
2353 vpImageIo::readJPEG(vpImage<vpRGBa> &I, const std::string filename)
2354 {
2355  vpImageIo::readJPEG(I, filename.c_str());
2356 }
2357 
2358 #endif
2359 
2360 
2361 
2362 
2363 
2364 
2365 //--------------------------------------------------------------------------
2366 // PNG
2367 //--------------------------------------------------------------------------
2368 
2369 #if defined(VISP_HAVE_LIBPNG)
2370 
2378 void
2379 vpImageIo::writePNG(const vpImage<unsigned char> &I, const char *filename)
2380 {
2381  FILE *file;
2382 
2383  // Test the filename
2384  if (!filename || *filename == '\0') {
2385  vpERROR_TRACE("no filename\n");
2387  "no filename")) ;
2388  }
2389 
2390  file = fopen(filename, "wb");
2391 
2392  if (file == NULL) {
2393  vpERROR_TRACE("couldn't write file \"%s\"\n", filename);
2395  "cannot write file")) ;
2396  }
2397 
2398  /* create a png info struct */
2399  png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL, NULL, NULL);
2400  if (!png_ptr)
2401  {
2402  fclose (file);
2403  vpERROR_TRACE("Error during png_create_write_struct()\n");
2405  "PNG write error")) ;
2406  }
2407 
2408  png_infop info_ptr = png_create_info_struct(png_ptr);
2409  if (!info_ptr)
2410  {
2411  fclose (file);
2412  png_destroy_write_struct (&png_ptr, NULL);
2413  vpERROR_TRACE("Error during png_create_info_struct()\n");
2415  "PNG write error")) ;
2416  }
2417 
2418  /* initialize the setjmp for returning properly after a libpng error occured */
2419  if (setjmp (png_jmpbuf (png_ptr)))
2420  {
2421  fclose (file);
2422  png_destroy_write_struct (&png_ptr, &info_ptr);
2423  vpERROR_TRACE("Error during init_io\n");
2425  "PNG write error")) ;
2426  }
2427 
2428  /* setup libpng for using standard C fwrite() function with our FILE pointer */
2429  png_init_io (png_ptr, file);
2430 
2431  unsigned int width = I.getWidth();
2432  unsigned int height = I.getHeight();
2433  int bit_depth = 8;
2434  int color_type = PNG_COLOR_TYPE_GRAY;
2435  /* set some useful information from header */
2436 
2437  if (setjmp (png_jmpbuf (png_ptr)))
2438  {
2439  fclose (file);
2440  png_destroy_write_struct (&png_ptr, &info_ptr);
2441  vpERROR_TRACE("Error during write header\n");
2443  "PNG write error")) ;
2444  }
2445 
2446  png_set_IHDR(png_ptr, info_ptr, width, height,
2447  bit_depth, color_type, PNG_INTERLACE_NONE,
2448  PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
2449 
2450  png_write_info(png_ptr, info_ptr);
2451 
2452  png_bytep* row_ptrs = new png_bytep[height];
2453  for (unsigned int i = 0; i < height; i++)
2454  row_ptrs[i] = new png_byte[width];
2455 
2456  unsigned char* input = (unsigned char*)I.bitmap;
2457 
2458  for (unsigned int i = 0; i < height; i++)
2459  {
2460  png_byte* row = row_ptrs[i];
2461  for(unsigned int j = 0; j < width; j++)
2462  {
2463  row[j] = *(input);
2464  input++;
2465  }
2466  }
2467 
2468  if (setjmp (png_jmpbuf (png_ptr)))
2469  {
2470  fclose (file);
2471  png_destroy_write_struct (&png_ptr, &info_ptr);
2472  for(unsigned int j = 0; j < height; j++)
2473  delete[] row_ptrs[j];
2474 
2475  delete[] row_ptrs;
2476  vpERROR_TRACE("Error during write image\n");
2478  "PNG write error")) ;
2479  }
2480 
2481  png_write_image(png_ptr, row_ptrs);
2482 
2483  if (setjmp (png_jmpbuf (png_ptr)))
2484  {
2485  fclose (file);
2486  png_destroy_write_struct (&png_ptr, &info_ptr);
2487  for(unsigned int j = 0; j < height; j++)
2488  delete[] row_ptrs[j];
2489 
2490  delete[] row_ptrs;
2491  vpERROR_TRACE("Error during write end\n");
2493  "PNG write error")) ;
2494  }
2495 
2496  png_write_end(png_ptr, NULL);
2497 
2498  for(unsigned int j = 0; j < height; j++)
2499  delete[] row_ptrs[j];
2500 
2501  delete[] row_ptrs;
2502 
2503  png_destroy_write_struct (&png_ptr, &info_ptr);
2504 
2505  fclose(file);
2506 }
2507 
2508 
2516 void
2517 vpImageIo::writePNG(const vpImage<unsigned char> &I, const std::string filename)
2518 {
2519  vpImageIo::writePNG(I, filename.c_str());
2520 }
2521 
2522 
2530 void
2531 vpImageIo::writePNG(const vpImage<vpRGBa> &I, const char *filename)
2532 {
2533  FILE *file;
2534 
2535  // Test the filename
2536  if (!filename || *filename == '\0') {
2537  vpERROR_TRACE("no filename\n");
2539  "no filename")) ;
2540  }
2541 
2542  file = fopen(filename, "wb");
2543 
2544  if (file == NULL) {
2545  vpERROR_TRACE("couldn't write file \"%s\"\n", filename);
2547  "cannot write file")) ;
2548  }
2549 
2550  /* create a png info struct */
2551  png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL, NULL, NULL);
2552  if (!png_ptr)
2553  {
2554  fclose (file);
2555  vpERROR_TRACE("Error during png_create_write_struct()\n");
2557  "PNG write error")) ;
2558  }
2559 
2560  png_infop info_ptr = png_create_info_struct(png_ptr);
2561  if (!info_ptr)
2562  {
2563  fclose (file);
2564  png_destroy_write_struct (&png_ptr, NULL);
2565  vpERROR_TRACE("Error during png_create_info_struct()\n");
2567  "PNG write error")) ;
2568  }
2569 
2570  /* initialize the setjmp for returning properly after a libpng error occured */
2571  if (setjmp (png_jmpbuf (png_ptr)))
2572  {
2573  fclose (file);
2574  png_destroy_write_struct (&png_ptr, &info_ptr);
2575  vpERROR_TRACE("Error during init_io\n");
2577  "PNG write error")) ;
2578  }
2579 
2580  /* setup libpng for using standard C fwrite() function with our FILE pointer */
2581  png_init_io (png_ptr, file);
2582 
2583  unsigned int width = I.getWidth();
2584  unsigned int height = I.getHeight();
2585  int bit_depth = 8;
2586  int color_type = PNG_COLOR_TYPE_RGB;
2587  /* set some useful information from header */
2588 
2589  if (setjmp (png_jmpbuf (png_ptr)))
2590  {
2591  fclose (file);
2592  png_destroy_write_struct (&png_ptr, &info_ptr);
2593  vpERROR_TRACE("Error during write header\n");
2595  "PNG write error")) ;
2596  }
2597 
2598  png_set_IHDR(png_ptr, info_ptr, width, height,
2599  bit_depth, color_type, PNG_INTERLACE_NONE,
2600  PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
2601 
2602  png_write_info(png_ptr, info_ptr);
2603 
2604  png_bytep* row_ptrs = new png_bytep[height];
2605  for (unsigned int i = 0; i < height; i++)
2606  row_ptrs[i] = new png_byte[3*width];
2607 
2608  unsigned char* input = (unsigned char*)I.bitmap;;
2609 
2610  for (unsigned int i = 0; i < height; i++)
2611  {
2612  png_byte* row = row_ptrs[i];
2613  for(unsigned int j = 0; j < width; j++)
2614  {
2615  row[3*j] = *(input);input++;
2616  row[3*j+1] = *(input);input++;
2617  row[3*j+2] = *(input);input++;
2618  input++;
2619  }
2620  }
2621 
2622  if (setjmp (png_jmpbuf (png_ptr)))
2623  {
2624  fclose (file);
2625  png_destroy_write_struct (&png_ptr, &info_ptr);
2626  for(unsigned int j = 0; j < height; j++)
2627  delete[] row_ptrs[j];
2628 
2629  delete[] row_ptrs;
2630  vpERROR_TRACE("Error during write image\n");
2632  "PNG write error")) ;
2633  }
2634 
2635  png_write_image(png_ptr, row_ptrs);
2636 
2637  if (setjmp (png_jmpbuf (png_ptr)))
2638  {
2639  fclose (file);
2640  png_destroy_write_struct (&png_ptr, &info_ptr);
2641  for(unsigned int j = 0; j < height; j++)
2642  delete[] row_ptrs[j];
2643 
2644  delete[] row_ptrs;
2645  vpERROR_TRACE("Error during write end\n");
2647  "PNG write error")) ;
2648  }
2649 
2650  png_write_end(png_ptr, NULL);
2651 
2652  for(unsigned int j = 0; j < height; j++)
2653  delete[] row_ptrs[j];
2654 
2655  delete[] row_ptrs;
2656 
2657  png_destroy_write_struct (&png_ptr, &info_ptr);
2658 
2659  fclose(file);
2660 }
2661 
2662 
2670 void
2671 vpImageIo::writePNG(const vpImage<vpRGBa> &I, const std::string filename)
2672 {
2673  vpImageIo::writePNG(I, filename.c_str());
2674 }
2675 
2692 void
2694 {
2695  FILE *file;
2696  png_byte magic[8];
2697  // Test the filename
2698  if (!filename || *filename == '\0') {
2699  vpERROR_TRACE("no filename\n");
2701  "no filename")) ;
2702  }
2703 
2704  file = fopen(filename, "rb");
2705 
2706  if (file == NULL) {
2707  vpERROR_TRACE("couldn't read file \"%s\"\n", filename);
2709  "cannot read file")) ;
2710  }
2711 
2712  /* read magic number */
2713  if (fread (magic, 1, sizeof (magic), file) != sizeof (magic))
2714  {
2715  fclose (file);
2716  vpERROR_TRACE("couldn't read magic number in file \"%s\"\n", filename) ;
2718  "error reading png file")) ;
2719  }
2720 
2721  /* check for valid magic number */
2722  if (png_sig_cmp (magic,0, sizeof (magic)))
2723  {
2724  fprintf (stderr, "error: \"%s\" is not a valid PNG image!\n",filename);
2725  fclose (file);
2727  "error reading png file")) ;
2728  }
2729 
2730  /* create a png read struct */
2731  //printf("version %s\n", PNG_LIBPNG_VER_STRING);
2732  png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
2733  if (png_ptr == NULL)
2734  {
2735  fprintf (stderr, "error: can't create a png read structure!\n");
2736  fclose (file);
2738  "error reading png file")) ;
2739  }
2740 
2741  /* create a png info struct */
2742  png_infop info_ptr = png_create_info_struct (png_ptr);
2743  if (info_ptr == NULL)
2744  {
2745  fprintf (stderr, "error: can't create a png info structure!\n");
2746  fclose (file);
2747  png_destroy_read_struct (&png_ptr, NULL, NULL);
2749  "error reading png file")) ;
2750  }
2751 
2752  /* initialize the setjmp for returning properly after a libpng error occured */
2753  if (setjmp (png_jmpbuf (png_ptr)))
2754  {
2755  fclose (file);
2756  png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
2757  vpERROR_TRACE("Error during init io\n");
2759  "PNG read error")) ;
2760  }
2761 
2762  /* setup libpng for using standard C fread() function with our FILE pointer */
2763  png_init_io (png_ptr, file);
2764 
2765  /* tell libpng that we have already read the magic number */
2766  png_set_sig_bytes (png_ptr, sizeof (magic));
2767 
2768  /* read png info */
2769  png_read_info (png_ptr, info_ptr);
2770 
2771  unsigned int width = png_get_image_width(png_ptr, info_ptr);
2772  unsigned int height = png_get_image_height(png_ptr, info_ptr);
2773 
2774  unsigned int bit_depth, channels, color_type;
2775  /* get some useful information from header */
2776  bit_depth = png_get_bit_depth (png_ptr, info_ptr);
2777  channels = png_get_channels(png_ptr, info_ptr);
2778  color_type = png_get_color_type (png_ptr, info_ptr);
2779 
2780  /* convert index color images to RGB images */
2781  if (color_type == PNG_COLOR_TYPE_PALETTE)
2782  png_set_palette_to_rgb (png_ptr);
2783 
2784  /* convert 1-2-4 bits grayscale images to 8 bits grayscale. */
2785  if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
2786  png_set_expand (png_ptr);
2787 
2788 // if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS))
2789 // png_set_tRNS_to_alpha (png_ptr);
2790 
2791  if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2792  png_set_strip_alpha(png_ptr);
2793 
2794  if (bit_depth == 16)
2795  png_set_strip_16 (png_ptr);
2796  else if (bit_depth < 8)
2797  png_set_packing (png_ptr);
2798 
2799  /* update info structure to apply transformations */
2800  png_read_update_info (png_ptr, info_ptr);
2801 
2802  channels = png_get_channels(png_ptr, info_ptr);
2803 
2804  if ( (width != I.getWidth()) || (height != I.getHeight()) )
2805  I.resize(height,width);
2806 
2807  png_bytep* rowPtrs = new png_bytep[height];
2808 
2809  unsigned int stride = width * bit_depth * channels / 8;
2810  unsigned char* data = new unsigned char[stride * height];
2811 
2812  for (unsigned int i =0; i < height; i++)
2813  rowPtrs[i] = (png_bytep)data + (i * stride);
2814 
2815  png_read_image(png_ptr, rowPtrs);
2816 
2817  vpImage<vpRGBa> Ic(height,width);
2818  unsigned char* output;
2819 
2820  switch (channels)
2821  {
2822  case 1:
2823  output = (unsigned char*)I.bitmap;
2824  for (unsigned int i = 0; i < width*height; i++)
2825  {
2826  *(output++) = data[i];
2827  }
2828  break;
2829  case 2:
2830  output = (unsigned char*)I.bitmap;
2831  for (unsigned int i = 0; i < width*height; i++)
2832  {
2833  *(output++) = data[i*2];
2834  }
2835  break;
2836  case 3:
2837 
2838  output = (unsigned char*)Ic.bitmap;
2839  for (unsigned int i = 0; i < width*height; i++)
2840  {
2841  *(output++) = data[i*3];
2842  *(output++) = data[i*3+1];
2843  *(output++) = data[i*3+2];
2844  *(output++) = 0;
2845  }
2846  vpImageConvert::convert(Ic,I) ;
2847  break;
2848  case 4:
2849  output = (unsigned char*)Ic.bitmap;
2850  for (unsigned int i = 0; i < width*height; i++)
2851  {
2852  *(output++) = data[i*4];
2853  *(output++) = data[i*4+1];
2854  *(output++) = data[i*4+2];
2855  *(output++) = data[i*4+3];
2856  }
2857  vpImageConvert::convert(Ic,I) ;
2858  break;
2859  }
2860 
2861  delete [] (png_bytep)rowPtrs;
2862  delete [] data;
2863  png_read_end (png_ptr, NULL);
2864  png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
2865  fclose(file);
2866 }
2867 
2868 
2885 void
2886 vpImageIo::readPNG(vpImage<unsigned char> &I, const std::string filename)
2887 {
2888  vpImageIo::readPNG(I, filename.c_str());
2889 }
2890 
2891 
2910 void
2911 vpImageIo::readPNG(vpImage<vpRGBa> &I, const char *filename)
2912 {
2913  FILE *file;
2914  png_byte magic[8];
2915 
2916  // Test the filename
2917  if (!filename || *filename == '\0') {
2918  vpERROR_TRACE("no filename\n");
2920  "no filename")) ;
2921  }
2922 
2923  file = fopen(filename, "rb");
2924 
2925  if (file == NULL) {
2926  vpERROR_TRACE("couldn't read file \"%s\"\n", filename);
2928  "cannot read file")) ;
2929  }
2930 
2931  /* read magic number */
2932  if (fread (magic, 1, sizeof (magic), file) != sizeof (magic))
2933  {
2934  fclose (file);
2935  vpERROR_TRACE("couldn't read magic number in file \"%s\"\n", filename) ;
2937  "error reading pgm file")) ;
2938  }
2939 
2940  /* check for valid magic number */
2941  if (png_sig_cmp (magic,0, sizeof (magic)))
2942  {
2943  fclose (file);
2944  vpERROR_TRACE("error: \"%s\" is not a valid PNG image!\n",filename);
2946  "PNG read error")) ;
2947  }
2948 
2949  /* create a png read struct */
2950  png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
2951  if (!png_ptr)
2952  {
2953  fclose (file);
2954  vpERROR_TRACE("Error during png_create_read_struct()\n");
2956  "PNG read error")) ;
2957  }
2958 
2959  /* create a png info struct */
2960  png_infop info_ptr = png_create_info_struct (png_ptr);
2961  if (!info_ptr)
2962  {
2963  fclose (file);
2964  png_destroy_read_struct (&png_ptr, NULL, NULL);
2965  vpERROR_TRACE("Error during png_create_info_struct()\n");
2967  "PNG read error")) ;
2968  }
2969 
2970  /* initialize the setjmp for returning properly after a libpng error occured */
2971  if (setjmp (png_jmpbuf (png_ptr)))
2972  {
2973  fclose (file);
2974  png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
2975  vpERROR_TRACE("Error during init io\n");
2977  "PNG read error")) ;
2978  }
2979 
2980  /* setup libpng for using standard C fread() function with our FILE pointer */
2981  png_init_io (png_ptr, file);
2982 
2983  /* tell libpng that we have already read the magic number */
2984  png_set_sig_bytes (png_ptr, sizeof (magic));
2985 
2986  /* read png info */
2987  png_read_info (png_ptr, info_ptr);
2988 
2989  unsigned int width = png_get_image_width(png_ptr, info_ptr);
2990  unsigned int height = png_get_image_height(png_ptr, info_ptr);
2991 
2992  unsigned int bit_depth, channels, color_type;
2993  /* get some useful information from header */
2994  bit_depth = png_get_bit_depth (png_ptr, info_ptr);
2995  channels = png_get_channels(png_ptr, info_ptr);
2996  color_type = png_get_color_type (png_ptr, info_ptr);
2997 
2998  /* convert index color images to RGB images */
2999  if (color_type == PNG_COLOR_TYPE_PALETTE)
3000  png_set_palette_to_rgb (png_ptr);
3001 
3002  /* convert 1-2-4 bits grayscale images to 8 bits grayscale. */
3003  if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
3004  png_set_expand (png_ptr);
3005 
3006 // if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS))
3007 // png_set_tRNS_to_alpha (png_ptr);
3008 
3009  if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
3010  png_set_strip_alpha(png_ptr);
3011 
3012  if (bit_depth == 16)
3013  png_set_strip_16 (png_ptr);
3014  else if (bit_depth < 8)
3015  png_set_packing (png_ptr);
3016 
3017  /* update info structure to apply transformations */
3018  png_read_update_info (png_ptr, info_ptr);
3019 
3020  channels = png_get_channels(png_ptr, info_ptr);
3021 
3022  if ( (width != I.getWidth()) || (height != I.getHeight()) )
3023  I.resize(height,width);
3024 
3025  png_bytep* rowPtrs = new png_bytep[height];
3026 
3027  unsigned int stride = width * bit_depth * channels / 8;
3028  unsigned char* data = new unsigned char[stride * height];
3029 
3030 
3031  for (unsigned int i =0; i < height; i++)
3032  rowPtrs[i] = (png_bytep)data + (i * stride);
3033 
3034  png_read_image(png_ptr, rowPtrs);
3035 
3036  vpImage<unsigned char> Ig(height,width);
3037  unsigned char* output;
3038 
3039  switch (channels)
3040  {
3041  case 1:
3042  output = (unsigned char*)Ig.bitmap;
3043  for (unsigned int i = 0; i < width*height; i++)
3044  {
3045  *(output++) = data[i];
3046  }
3047  vpImageConvert::convert(Ig,I) ;
3048  break;
3049  case 2:
3050  output = (unsigned char*)Ig.bitmap;
3051  for (unsigned int i = 0; i < width*height; i++)
3052  {
3053  *(output++) = data[i*2];
3054  }
3055  vpImageConvert::convert(Ig,I) ;
3056  break;
3057  case 3:
3058 
3059  output = (unsigned char*)I.bitmap;
3060  for (unsigned int i = 0; i < width*height; i++)
3061  {
3062  *(output++) = data[i*3];
3063  *(output++) = data[i*3+1];
3064  *(output++) = data[i*3+2];
3065  *(output++) = 0;
3066  }
3067  break;
3068  case 4:
3069  output = (unsigned char*)I.bitmap;
3070  for (unsigned int i = 0; i < width*height; i++)
3071  {
3072  *(output++) = data[i*4];
3073  *(output++) = data[i*4+1];
3074  *(output++) = data[i*4+2];
3075  *(output++) = data[i*4+3];
3076  }
3077  break;
3078  }
3079 
3080  delete [] (png_bytep)rowPtrs;
3081  delete [] data;
3082  png_read_end (png_ptr, NULL);
3083  png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
3084  fclose(file);
3085 }
3086 
3087 
3106 void
3107 vpImageIo::readPNG(vpImage<vpRGBa> &I, const std::string filename)
3108 {
3109  vpImageIo::readPNG(I, filename.c_str());
3110 }
3111 
3112 #elif defined(VISP_HAVE_OPENCV)
3113 
3121 void
3122 vpImageIo::writePNG(const vpImage<unsigned char> &I, const char *filename)
3123 {
3124 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408)
3125  cv::Mat Ip;
3126  vpImageConvert::convert(I, Ip);
3127  cv::imwrite(filename, Ip);
3128 #else
3129  IplImage* Ip = NULL;
3130  vpImageConvert::convert(I, Ip);
3131 
3132  cvSaveImage(filename, Ip);
3133 
3134  cvReleaseImage(&Ip);
3135 #endif
3136 }
3137 
3138 
3146 void
3147 vpImageIo::writePNG(const vpImage<unsigned char> &I, const std::string filename)
3148 {
3149  vpImageIo::writePNG(I, filename.c_str());
3150 }
3151 
3152 
3160 void
3161 vpImageIo::writePNG(const vpImage<vpRGBa> &I, const char *filename)
3162 {
3163 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408)
3164  cv::Mat Ip;
3165  vpImageConvert::convert(I, Ip);
3166  cv::imwrite(filename, Ip);
3167 #else
3168  IplImage* Ip = NULL;
3169  vpImageConvert::convert(I, Ip);
3170 
3171  cvSaveImage(filename, Ip);
3172 
3173  cvReleaseImage(&Ip);
3174 #endif
3175 }
3176 
3177 
3185 void
3186 vpImageIo::writePNG(const vpImage<vpRGBa> &I, const std::string filename)
3187 {
3188  vpImageIo::writePNG(I, filename.c_str());
3189 }
3190 
3191 
3208 void
3209 vpImageIo::readPNG(vpImage<unsigned char> &I, const char *filename)
3210 {
3211 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
3212  cv::Mat Ip = cv::imread(filename, cv::IMREAD_GRAYSCALE);
3213  if ( ! Ip.empty())
3214  vpImageConvert::convert(Ip, I);
3215  else
3216  throw (vpImageException(vpImageException::ioError, "Can't read the image")) ;
3217 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020408)
3218  cv::Mat Ip = cv::imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
3219  if ( ! Ip.empty())
3220  vpImageConvert::convert(Ip, I);
3221  else
3222  throw (vpImageException(vpImageException::ioError, "Can't read the image")) ;
3223 #else
3224  IplImage* Ip = NULL;
3225  Ip = cvLoadImage(filename, CV_LOAD_IMAGE_GRAYSCALE);
3226  if (Ip != NULL)
3227  vpImageConvert::convert(Ip, I);
3228  else
3230  "Can't read the image")) ;
3231  cvReleaseImage(&Ip);
3232 #endif
3233 }
3234 
3235 
3252 void
3253 vpImageIo::readPNG(vpImage<unsigned char> &I, const std::string filename)
3254 {
3255  vpImageIo::readPNG(I, filename.c_str());
3256 }
3257 
3258 
3277 void
3278 vpImageIo::readPNG(vpImage<vpRGBa> &I, const char *filename)
3279 {
3280 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
3281  cv::Mat Ip = cv::imread(filename, cv::IMREAD_GRAYSCALE);
3282  if ( ! Ip.empty())
3283  vpImageConvert::convert(Ip, I);
3284  else
3285  throw (vpImageException(vpImageException::ioError, "Can't read the image")) ;
3286 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020408)
3287  cv::Mat Ip = cv::imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
3288  if ( ! Ip.empty())
3289  vpImageConvert::convert(Ip, I);
3290  else
3291  throw (vpImageException(vpImageException::ioError, "Can't read the image")) ;
3292 #else
3293  IplImage* Ip = NULL;
3294  Ip = cvLoadImage(filename, CV_LOAD_IMAGE_COLOR);
3295  if (Ip != NULL)
3296  vpImageConvert::convert(Ip, I);
3297  else
3299  "Can't read the image")) ;
3300  cvReleaseImage(&Ip);
3301 #endif
3302 }
3303 
3304 
3323 void
3324 vpImageIo::readPNG(vpImage<vpRGBa> &I, const std::string filename)
3325 {
3326  vpImageIo::readPNG(I, filename.c_str());
3327 }
3328 
3329 #endif
static void write(const vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:476
static void writeJPEG(const vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:1760
static void readPGM(vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:996
unsigned int getWidth() const
Definition: vpImage.h:161
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
#define vpERROR_TRACE
Definition: vpDebug.h:395
unsigned char B
Blue component.
Definition: vpRGBa.h:148
Type * bitmap
points toward the bitmap
Definition: vpImage.h:120
#define vpCERROR
Definition: vpDebug.h:369
static void writePGM(const vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:687
static void writePNG(const vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:2379
error that can be emited by ViSP classes.
Definition: vpException.h:76
static void writePFM(const vpImage< float > &I, const char *filename)
Definition: vpImageIo.cpp:631
Error that can be emited by the vpImage class and its derivates.
static void readPFM(vpImage< float > &I, const char *filename)
Definition: vpImageIo.cpp:827
unsigned char G
Green component.
Definition: vpRGBa.h:147
Class that defines a RGB 32 bits structure.
Definition: vpRGBa.h:68
static bool checkFilename(const char *filename)
Definition: vpIoTools.cpp:465
void resize(const unsigned int h, const unsigned int w)
set the size of the image without initializing it.
Definition: vpImage.h:536
static void readPNG(vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:2693
static void readJPEG(vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:1929
unsigned char R
Red component.
Definition: vpRGBa.h:146
static void readPPM(vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:1245
unsigned int getHeight() const
Definition: vpImage.h:152
static void writePPM(const vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:1486
static void read(vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:278