ViSP  2.9.0
vpImageIo.cpp
1 /****************************************************************************
2  *
3  * $Id: vpImageIo.cpp 4574 2014-01-09 08:48:51Z 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 == '\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 == '\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 >= 0x020100
319  //std::cout << "Use opencv to read the image" << std::endl;
320  cv::Mat cvI = cv::imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
321  vpImageConvert::convert(cvI, I);
322 #else
323  std::string message = "Cannot read file \"" + std::string(filename) + "\": Image format not supported";
324  throw (vpImageException(vpImageException::ioError, message)) ;
325 #endif
326  }
327 }
344 void
345 vpImageIo::read(vpImage<unsigned char> &I, const std::string filename)
346 {
347  read(I,filename.c_str());
348 }
365 void
366 vpImageIo::read(vpImage<vpRGBa> &I, const char *filename)
367 {
368  bool exist = vpIoTools::checkFilename(filename);
369  if (!exist) {
370  std::string message = "Cannot read file: \"" + std::string(filename) + "\" doesn't exist";
372  }
373 
374  bool try_opencv_reader = false;
375 
376  switch(getFormat(filename)){
377  case FORMAT_PGM :
378  readPGM(I,filename); break;
379  case FORMAT_PPM :
380  readPPM(I,filename); break;
381  case FORMAT_JPEG :
382 #ifdef VISP_HAVE_LIBJPEG
383  readJPEG(I,filename);
384 #else
385  try_opencv_reader = true;
386 #endif
387  break;
388  case FORMAT_PNG :
389 #if defined(VISP_HAVE_LIBPNG)
390  readPNG(I,filename);
391 #else
392  try_opencv_reader = true;
393 #endif
394  break;
395  case FORMAT_TIFF :
396  case FORMAT_BMP :
397  case FORMAT_DIB :
398  case FORMAT_PBM :
399  case FORMAT_RASTER :
400  case FORMAT_JPEG2000 :
401  case FORMAT_UNKNOWN :
402  try_opencv_reader = true;
403  break;
404  }
405 
406  if (try_opencv_reader) {
407 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
408  // std::cout << "Use opencv to read the image" << std::endl;
409  cv::Mat cvI = cv::imread(filename, CV_LOAD_IMAGE_COLOR);
410  vpImageConvert::convert(cvI, I);
411 #else
412  std::string message = "Cannot read file \"" + std::string(filename) + "\": Image format not supported";
413  throw (vpImageException(vpImageException::ioError, message)) ;
414 #endif
415  }
416 }
433 void
434 vpImageIo::read(vpImage<vpRGBa> &I, const std::string filename)
435 {
436  read(I,filename.c_str());
437 }
438 
451 void
452 vpImageIo::write(const vpImage<unsigned char> &I, const char *filename)
453 {
454  bool try_opencv_writer = false;
455 
456  switch(getFormat(filename)){
457  case FORMAT_PGM :
458  writePGM(I,filename); break;
459  case FORMAT_PPM :
460  writePPM(I,filename); break;
461  case FORMAT_JPEG :
462 #ifdef VISP_HAVE_LIBJPEG
463  writeJPEG(I,filename);
464 #else
465  try_opencv_writer = true;
466 #endif
467  break;
468  case FORMAT_PNG :
469 #ifdef VISP_HAVE_LIBPNG
470  writePNG(I,filename);
471 #else
472  try_opencv_writer = true;
473 #endif
474  break;
475  case FORMAT_TIFF :
476  case FORMAT_BMP :
477  case FORMAT_DIB :
478  case FORMAT_PBM :
479  case FORMAT_RASTER :
480  case FORMAT_JPEG2000 :
481  case FORMAT_UNKNOWN :
482  try_opencv_writer = true;
483  break;
484  }
485 
486  if (try_opencv_writer) {
487 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
488  // std::cout << "Use opencv to write the image" << std::endl;
489  cv::Mat cvI;
490  vpImageConvert::convert(I, cvI);
491  cv::imwrite(filename, cvI);
492 #else
493  vpCERROR << "Cannot write file: Image format not supported..." << std::endl;
495  "Cannot write file: Image format not supported")) ;
496 #endif
497  }
498 }
511 void
512 vpImageIo::write(const vpImage<unsigned char> &I, const std::string filename)
513 {
514  write(I,filename.c_str());
515 }
528 void
529 vpImageIo::write(const vpImage<vpRGBa> &I, const char *filename)
530 {
531  bool try_opencv_writer = false;
532 
533  switch(getFormat(filename)){
534  case FORMAT_PGM :
535  writePGM(I,filename); break;
536  case FORMAT_PPM :
537  writePPM(I,filename); break;
538  case FORMAT_JPEG :
539 #ifdef VISP_HAVE_LIBJPEG
540  writeJPEG(I,filename);
541 #else
542  try_opencv_writer = true;
543 #endif
544  break;
545  case FORMAT_PNG :
546 #ifdef VISP_HAVE_LIBPNG
547  writePNG(I,filename);
548 #else
549  try_opencv_writer = true;
550 #endif
551  break;
552  case FORMAT_TIFF :
553  case FORMAT_BMP :
554  case FORMAT_DIB :
555  case FORMAT_PBM :
556  case FORMAT_RASTER :
557  case FORMAT_JPEG2000 :
558  case FORMAT_UNKNOWN :
559  try_opencv_writer = true;
560  break;
561  }
562 
563  if (try_opencv_writer) {
564 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
565  // std::cout << "Use opencv to write the image" << std::endl;
566  cv::Mat cvI;
567  vpImageConvert::convert(I, cvI);
568  cv::imwrite(filename, cvI);
569 #else
570  vpCERROR << "Cannot write file: Image format not supported..." << std::endl;
572  "Cannot write file: Image format not supported")) ;
573 #endif
574  }
575 }
588 void
589 vpImageIo::write(const vpImage<vpRGBa> &I, const std::string filename)
590 {
591  write(I,filename.c_str());
592 }
593 //--------------------------------------------------------------------------
594 // PFM
595 //--------------------------------------------------------------------------
596 
606 void
608  const char *filename)
609 {
610 
611  FILE* fd;
612 
613  // Test the filename
614  if (filename == '\0') {
615  vpERROR_TRACE("no filename\n");
617  "no filename")) ;
618  }
619 
620  fd = fopen(filename, "wb");
621 
622  if (fd == NULL) {
623  vpERROR_TRACE("couldn't write to file \"%s\"\n", filename);
625  "cannot write file")) ;
626  }
627 
628  // Write the head
629  fprintf(fd, "P8\n"); // Magic number
630  fprintf(fd, "%d %d\n", I.getWidth(), I.getHeight()); // Image size
631  fprintf(fd, "255\n"); // Max level
632 
633  // Write the bitmap
634  size_t ierr;
635  size_t nbyte = I.getWidth()*I.getHeight();
636 
637  ierr = fwrite(I.bitmap, sizeof(float), nbyte, fd) ;
638  if (ierr != nbyte) {
639  fclose(fd);
640  vpERROR_TRACE("couldn't write %d bytes to file \"%s\"\n",
641  nbyte, filename) ;
643  "cannot write file")) ;
644  }
645 
646  fflush(fd);
647  fclose(fd);
648 
649 }
650 //--------------------------------------------------------------------------
651 // PGM
652 //--------------------------------------------------------------------------
653 
662 void
664  const char *filename)
665 {
666 
667  FILE* fd;
668 
669  // Test the filename
670  if (filename == '\0') {
671  vpERROR_TRACE("no filename\n");
673  "no filename")) ;
674  }
675 
676  fd = fopen(filename, "wb");
677 
678  if (fd == NULL) {
679  vpERROR_TRACE("couldn't write to file \"%s\"\n", filename);
681  "cannot write file")) ;
682  }
683 
684  // Write the head
685  fprintf(fd, "P5\n"); // Magic number
686  fprintf(fd, "%d %d\n", I.getWidth(), I.getHeight()); // Image size
687  fprintf(fd, "255\n"); // Max level
688 
689  // Write the bitmap
690  size_t ierr;
691  size_t nbyte = I.getWidth()*I.getHeight();
692 
693  ierr = fwrite(I.bitmap, sizeof(unsigned char), nbyte, fd) ;
694  if (ierr != nbyte) {
695  fclose(fd);
696  vpERROR_TRACE("couldn't write %d bytes to file \"%s\"\n",
697  nbyte, filename) ;
699  "cannot write file")) ;
700  }
701 
702  fflush(fd);
703  fclose(fd);
704 
705 }
713 void
714 vpImageIo::writePGM(const vpImage<short> &I, const char *filename)
715 {
717  unsigned int nrows = I.getHeight();
718  unsigned int ncols = I.getWidth();
719 
720  Iuc.resize(nrows, ncols);
721 
722  for (unsigned int i=0 ; i < nrows * ncols ; i++)
723  Iuc.bitmap[i] = (unsigned char)I.bitmap[i] ;
724 
725  vpImageIo::writePGM(Iuc, filename) ;
726 
727 
728 }
738 void
739 vpImageIo::writePGM(const vpImage<vpRGBa> &I, const char *filename)
740 {
741 
742  FILE* fd;
743 
744  // Test the filename
745  if (filename == '\0') {
746  vpERROR_TRACE("no filename\n");
748  "no filename")) ;
749  }
750 
751  fd = fopen(filename, "wb");
752 
753  if (fd == NULL) {
754  vpERROR_TRACE("couldn't write to file \"%s\"\n", filename);
756  "cannot write file")) ;
757  }
758 
759  // Write the head
760  fprintf(fd, "P5\n"); // Magic number
761  fprintf(fd, "%d %d\n", I.getWidth(), I.getHeight()); // Image size
762  fprintf(fd, "255\n"); // Max level
763 
764  // Write the bitmap
765  size_t ierr;
766  size_t nbyte = I.getWidth()*I.getHeight();
767 
768 
770  vpImageConvert::convert(I,Itmp) ;
771 
772  ierr = fwrite(Itmp.bitmap, sizeof(unsigned char), nbyte, fd) ;
773  if (ierr != nbyte) {
774  fclose(fd);
775  vpERROR_TRACE("couldn't write %d bytes to file \"%s\"\n",
776  nbyte, filename) ;
778  "cannot write file")) ;
779  }
780 
781  fflush(fd);
782  fclose(fd);
783 
784 }
785 
802 void
803 vpImageIo::readPFM(vpImage<float> &I, const char *filename)
804 {
805  FILE* fd = NULL; // File descriptor
806  int ierr;
807  int line;
808  int is255;
809  char* err ;
810  char str[vpMAX_LEN];
811  unsigned int w, h;
812 
813  // Test the filename
814  if (filename == '\0')
815  {
816  vpERROR_TRACE("no filename") ;
818  " no filename")) ;
819 
820  }
821 
822  // Open the filename
823  fd = fopen(filename, "rb");
824  if (fd == NULL)
825  {
826  vpERROR_TRACE("couldn't read file \"%s\"", filename) ;
828  "couldn't read file")) ;
829  }
830 
831  // Read the first line with magic number P8
832  line = 0;
833 
834  err = fgets(str, vpMAX_LEN - 1, fd);
835  line++;
836  if (err == NULL)
837  {
838  fclose (fd);
839  vpERROR_TRACE("couldn't read line %d of file \"%s\"\n", line, filename) ;
841  "couldn't read file")) ;
842  }
843 
844  if (strlen(str) < 3)
845  {
846  fclose (fd);
847  vpERROR_TRACE("\"%s\" is not a PFM file\n", filename) ;
849  "this is not a PFM file")) ;
850  }
851 
852  str[2] = '\0';
853  if (strcmp(str, "P8") != 0)
854  {
855  fclose (fd);
856  vpERROR_TRACE("\"%s\" is not a PFM file\n", filename) ;
858  "this is not a PFM file")) ;
859  }
860 
861  // Jump the possible comment, or empty line and read the following line
862  do {
863  err = fgets(str, vpMAX_LEN - 1, fd);
864  line++;
865  if (err == NULL) {
866  fprintf(stderr, "couldn't read line %d of file \"%s\"\n", line, filename);
867  fclose (fd);
869  "Cannot read content of PFM file")) ;
870  }
871  } while ((str[0] == '#') || (str[0] == '\n'));
872 
873  // Extract image size
874  ierr = sscanf(str, "%d %d", &w, &h);
875  if (w > 100000 || h>100000) {
876  fclose (fd);
877  throw(vpException(vpException::badValue, "Bad image size"));
878  }
879 
880  if(ierr == 1){// the norm allows to have the two values on two separated lines.
881  do {
882  err = fgets(str, vpMAX_LEN - 1, fd);
883  line++;
884  if (err == NULL) {
885  fprintf(stderr, "couldn't read line %d of file \"%s\"\n", line, filename);
886  fclose (fd);
888  "Cannot read content of PFM file")) ;
889  }
890  } while ((str[0] == '#') || (str[0] == '\n'));
891  ierr = sscanf(str, "%d", &h);
892  }
893  if (ierr == EOF)
894  {
895  fclose (fd);
896  vpERROR_TRACE("couldn't read line %d of file \"%s\"\n",line, filename) ;
898  "Cannot read content of PFM file")) ;
899  }
900 
901  if ((h != I.getHeight())||( w != I.getWidth()))
902  {
903  try
904  {
905  I.resize(h,w) ;
906  }
907  catch(...)
908  {
909  fclose (fd);
911  "Cannot read content of PFM file")) ;
912  }
913  }
914 
915  // Read 255
916  err = fgets(str, vpMAX_LEN - 1, fd);
917  line++;
918  if (err == NULL) {
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  ierr = sscanf(str, "%d", &is255);
926  if (ierr == EOF) {
927  fclose (fd);
928  vpERROR_TRACE("couldn't read line %d of file \"%s\"\n", line, filename) ;
930  "Cannot read content of PFM file")) ;
931  }
932 
933  if (is255 != 255)
934  {
935  fclose (fd);
936  vpERROR_TRACE("MAX_VAL is not 255 in file \"%s\"\n", filename) ;
938  "Cannot read content of PFM file")) ;
939  }
940 
941  unsigned int nbyte = I.getHeight()*I.getWidth();
942  if (fread (I.bitmap, sizeof(float), nbyte, fd ) != nbyte)
943  {
944  fclose (fd);
945  vpERROR_TRACE("couldn't read %d bytes in file \"%s\"\n", nbyte, filename) ;
947  "Cannot read content of PFM file")) ;
948  }
949 
950  fclose (fd);
951 }
952 
953 
954 
971 void
973 {
974  FILE* fd = NULL; // File descriptor
975  int ierr;
976  char* err ;
977  char str[vpMAX_LEN];
978  unsigned int magic=5, w=0, h=0, maxval=255;
979 
980  // Test the filename
981  if (filename == '\0') {
983  "No filename")) ;
984  }
985 
986  // Open the filename
987  if ((fd = fopen(filename, "rb")) == NULL) {
989  "Cannot read file \"%s\"", filename)) ;
990  }
991 
992  while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] == '#') || (str[0] == '\n'))) {};
993  if (err == NULL) {
994  fclose (fd);
996  "Cannot read header of file \"%s\"", filename));
997  }
998  if ((ierr = sscanf(str, "P%u %u %u %u", &magic, &w, &h, &maxval)) == 0) {
999  fclose (fd);
1001  "Cannot read header of file \"%s\"", filename));
1002  }
1003 
1004  if (magic != 5) {
1005  fclose (fd);
1007  "\"%s\" is not a PGM P5 file", filename));
1008  }
1009 
1010  // Depending on ierr the line may contain:
1011  // 1 : P5
1012  // 2 : P5 w
1013  // 3 : P5 w h
1014  // 4 : P5 w h maxval
1015 
1016  if (ierr == 1) {
1017 // std::cout << "magic: " << magic << std::endl;
1018  while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] == '#') || (str[0] == '\n'))) {};
1019  if (err == NULL) {
1020  fclose (fd);
1022  "Cannot read header of file \"%s\"", filename));
1023  }
1024  if (((ierr = sscanf(str, "%u %u %u", &w, &h, &maxval)) == 0) || (ierr != 1 && ierr != 2 && ierr != 3)) {
1025  fclose (fd);
1027  "Cannot read header of file \"%s\"", filename));
1028  }
1029  // Depending on ierr the line may contain:
1030  // 1 : w
1031  // 2 : w h
1032  // 3 : w h maxval
1033  if (ierr == 1) {
1034 // std::cout << "w: " << w << std::endl;
1035  while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] == '#') || (str[0] == '\n'))) {};
1036  if (err == NULL) {
1037  fclose (fd);
1039  "Cannot read header of file \"%s\"", filename));
1040  }
1041  if (((ierr = sscanf(str, "%u %u", &h, &maxval)) == 0) || (ierr != 1 && ierr != 2)) {
1042  fclose (fd);
1044  "Cannot read header of file \"%s\"", filename));
1045  }
1046  if (ierr == 1) {
1047 // std::cout << "h: " << h << std::endl;
1048  while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] == '#') || (str[0] == '\n'))) {};
1049  if (err == NULL) {
1050  fclose (fd);
1052  "Cannot read header of file \"%s\"", filename));
1053  }
1054  if ((ierr = sscanf(str, "%u", &maxval)) != 1) {
1055  fclose (fd);
1057  "Cannot read header of file \"%s\"", filename));
1058  }
1059  }
1060 // else {
1061 // std::cout << "h: " << h << " maxval: " << maxval << std::endl;
1062 // }
1063  }
1064  else if (ierr == 2) {
1065 // std::cout << "w: " << w << " h: " << h << std::endl;
1066 
1067  while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] == '#') || (str[0] == '\n'))) {};
1068  if (err == NULL) {
1069  fclose (fd);
1071  "Cannot read header of file \"%s\"", filename));
1072  }
1073  if ((ierr = sscanf(str, "%u", &maxval)) != 1) {
1074  fclose (fd);
1076  "Cannot read header of file \"%s\"", filename));
1077  }
1078 // std::cout << "maxval: " << maxval << std::endl;
1079  }
1080 // else {
1081 // std::cout << "w: " << w << " h: " << h << " maxval: " << maxval << std::endl;
1082 // }
1083  }
1084  else if (ierr == 2) {
1085 // std::cout << "magic: " << magic << " w: " << w << std::endl;
1086  while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] == '#') || (str[0] == '\n'))) {};
1087  if (err == NULL) {
1088  fclose (fd);
1090  "Cannot read header of file \"%s\"", filename));
1091  }
1092  if (((ierr = sscanf(str, "%u %u", &h, &maxval)) == 0) || (ierr != 1 && ierr != 2)) {
1093  fclose (fd);
1095  "Cannot read header of file \"%s\"", filename));
1096  }
1097  if (ierr == 1) {
1098 // std::cout << "h: " << h << std::endl;
1099  while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] == '#') || (str[0] == '\n'))) {};
1100  if (err == NULL) {
1101  fclose (fd);
1103  "Cannot read header of file \"%s\"", filename));
1104  }
1105  if ((ierr = sscanf(str, "%u", &maxval)) != 1) {
1106  fclose (fd);
1108  "Cannot read header of file \"%s\"", filename));
1109  }
1110 // std::cout << "maxval: " << maxval << std::endl;
1111  }
1112 // else {
1113 // std::cout << "h: " << h << " maxval: " << maxval << std::endl;
1114 // }
1115  }
1116  else if (ierr == 3) {
1117 // std::cout << "magic: " << magic << " w: " << w << " h: " << h << std::endl;
1118  while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] == '#') || (str[0] == '\n'))) {};
1119  if (err == NULL) {
1120  fclose (fd);
1122  "Cannot read header of file \"%s\"", filename));
1123  }
1124  if ((ierr = sscanf(str, "%u", &maxval)) != 1) {
1125  fclose (fd);
1127  "Cannot read header of file \"%s\"", filename));
1128  }
1129 // std::cout << "maxval: " << maxval << std::endl;
1130  }
1131 // else if (ierr == 4) {
1132 // std::cout << "magic: " << magic << " w: " << w << " h: " << h << " maxval: " << maxval << std::endl;
1133 // }
1134 
1135  if (w > 100000 || h>100000) {
1136  fclose (fd);
1137  throw(vpException(vpException::badValue, "Bad image size in \"%s\"", filename));
1138  }
1139  if (maxval != 255)
1140  {
1141  fclose (fd);
1143  "Bad maxval in \"%s\"", filename));
1144  }
1145 
1146  if ((h != I.getHeight())||( w != I.getWidth())) {
1147  I.resize(h,w) ;
1148  }
1149 
1150  unsigned int nbyte = I.getHeight()*I.getWidth();
1151  size_t n;
1152  if ((n = fread (I.bitmap, sizeof(unsigned char), nbyte, fd)) != nbyte) {
1153  fclose (fd);
1155  "Read only %d of %d bytes in file \"%s\"", n, nbyte, filename));
1156  }
1157 
1158  fclose (fd);
1159 }
1160 
1161 
1180 void
1181 vpImageIo::readPGM(vpImage<vpRGBa> &I, const char *filename)
1182 {
1183  try
1184  {
1185  vpImage<unsigned char> Itmp ;
1186 
1187  vpImageIo::readPGM(Itmp, filename) ;
1188 
1189  vpImageConvert::convert(Itmp, I) ;
1190 
1191  }
1192  catch(...)
1193  {
1194  vpERROR_TRACE(" ") ;
1195  throw ;
1196  }
1197 }
1198 
1199 
1200 //--------------------------------------------------------------------------
1201 // PPM
1202 //--------------------------------------------------------------------------
1203 
1220 void
1222 {
1223 
1224  try
1225  {
1226  vpImage<vpRGBa> Itmp ;
1227 
1228  vpImageIo::readPPM(Itmp, filename) ;
1229 
1230  vpImageConvert::convert(Itmp, I) ;
1231  }
1232  catch(...)
1233  {
1234  vpERROR_TRACE(" ") ;
1235  throw ;
1236  }
1237 }
1238 
1239 
1251 void
1252 vpImageIo::readPPM(vpImage<vpRGBa> &I, const char *filename)
1253 {
1254  FILE* fd = NULL; // File descriptor
1255  int ierr;
1256  char* err ;
1257  char str[vpMAX_LEN];
1258  unsigned int magic=5, w=0, h=0, maxval=255;
1259 
1260  // Test the filename
1261  if (filename == '\0') {
1263  "No filename")) ;
1264  }
1265 
1266  // Open the filename
1267  if ((fd = fopen(filename, "rb")) == NULL) {
1269  "Cannot read file \"%s\"", filename)) ;
1270  }
1271 
1272  while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] == '#') || (str[0] == '\n'))) {};
1273  if (err == NULL) {
1274  fclose (fd);
1276  "Cannot read header of file \"%s\"", filename));
1277  }
1278  if ((ierr = sscanf(str, "P%u %u %u %u", &magic, &w, &h, &maxval)) == 0) {
1279  fclose (fd);
1281  "Cannot read header of file \"%s\"", filename));
1282  }
1283 
1284  if (magic != 6) {
1285  fclose (fd);
1287  "\"%s\" is not a PGM P6 file", filename));
1288  }
1289 
1290  // Depending on ierr the line may contain:
1291  // 1 : P6
1292  // 2 : P6 w
1293  // 3 : P6 w h
1294  // 4 : P6 w h maxval
1295 
1296  if (ierr == 1) {
1297 // std::cout << "magic: " << magic << std::endl;
1298  while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] == '#') || (str[0] == '\n'))) {};
1299  if (err == NULL) {
1300  fclose (fd);
1302  "Cannot read header of file \"%s\"", filename));
1303  }
1304  if (((ierr = sscanf(str, "%u %u %u", &w, &h, &maxval)) == 0) || (ierr != 1 && ierr != 2 && ierr != 3)) {
1305  fclose (fd);
1307  "Cannot read header of file \"%s\"", filename));
1308  }
1309  // Depending on ierr the line may contain:
1310  // 1 : w
1311  // 2 : w h
1312  // 3 : w h maxval
1313  if (ierr == 1) {
1314 // std::cout << "w: " << w << std::endl;
1315  while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] == '#') || (str[0] == '\n'))) {};
1316  if (err == NULL) {
1317  fclose (fd);
1319  "Cannot read header of file \"%s\"", filename));
1320  }
1321  if (((ierr = sscanf(str, "%u %u", &h, &maxval)) == 0) || (ierr != 1 && ierr != 2)) {
1322  fclose (fd);
1324  "Cannot read header of file \"%s\"", filename));
1325  }
1326  if (ierr == 1) {
1327 // std::cout << "h: " << h << std::endl;
1328  while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] == '#') || (str[0] == '\n'))) {};
1329  if (err == NULL) {
1330  fclose (fd);
1332  "Cannot read header of file \"%s\"", filename));
1333  }
1334  if ((ierr = sscanf(str, "%u", &maxval)) != 1) {
1335  fclose (fd);
1337  "Cannot read header of file \"%s\"", filename));
1338  }
1339  }
1340 // else {
1341 // std::cout << "h: " << h << " maxval: " << maxval << std::endl;
1342 // }
1343  }
1344  else if (ierr == 2) {
1345 // std::cout << "w: " << w << " h: " << h << std::endl;
1346 
1347  while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] == '#') || (str[0] == '\n'))) {};
1348  if (err == NULL) {
1349  fclose (fd);
1351  "Cannot read header of file \"%s\"", filename));
1352  }
1353  if ((ierr = sscanf(str, "%u", &maxval)) != 1) {
1354  fclose (fd);
1356  "Cannot read header of file \"%s\"", filename));
1357  }
1358 // std::cout << "maxval: " << maxval << std::endl;
1359  }
1360 // else {
1361 // std::cout << "w: " << w << " h: " << h << " maxval: " << maxval << std::endl;
1362 // }
1363  }
1364  else if (ierr == 2) {
1365 // std::cout << "magic: " << magic << " w: " << w << std::endl;
1366  while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] == '#') || (str[0] == '\n'))) {};
1367  if (err == NULL) {
1368  fclose (fd);
1370  "Cannot read header of file \"%s\"", filename));
1371  }
1372  if (((ierr = sscanf(str, "%u %u", &h, &maxval)) == 0) || (ierr != 1 && ierr != 2)) {
1373  fclose (fd);
1375  "Cannot read header of file \"%s\"", filename));
1376  }
1377  if (ierr == 1) {
1378 // std::cout << "h: " << h << std::endl;
1379  while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] == '#') || (str[0] == '\n'))) {};
1380  if (err == NULL) {
1381  fclose (fd);
1383  "Cannot read header of file \"%s\"", filename));
1384  }
1385  if ((ierr = sscanf(str, "%u", &maxval)) != 1) {
1386  fclose (fd);
1388  "Cannot read header of file \"%s\"", filename));
1389  }
1390 // std::cout << "maxval: " << maxval << std::endl;
1391  }
1392 // else {
1393 // std::cout << "h: " << h << " maxval: " << maxval << std::endl;
1394 // }
1395  }
1396  else if (ierr == 3) {
1397 // std::cout << "magic: " << magic << " w: " << w << " h: " << h << std::endl;
1398  while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] == '#') || (str[0] == '\n'))) {};
1399  if (err == NULL) {
1400  fclose (fd);
1402  "Cannot read header of file \"%s\"", filename));
1403  }
1404  if ((ierr = sscanf(str, "%u", &maxval)) != 1) {
1405  fclose (fd);
1407  "Cannot read header of file \"%s\"", filename));
1408  }
1409 // std::cout << "maxval: " << maxval << std::endl;
1410  }
1411 // else if (ierr == 4) {
1412 // std::cout << "magic: " << magic << " w: " << w << " h: " << h << " maxval: " << maxval << std::endl;
1413 // }
1414 
1415  if (w > 100000 || h>100000) {
1416  fclose (fd);
1417  throw(vpException(vpException::badValue, "Bad image size in \"%s\"", filename));
1418  }
1419  if (maxval != 255)
1420  {
1421  fclose (fd);
1423  "Bad maxval in \"%s\"", filename));
1424  }
1425 
1426  if ((h != I.getHeight())||( w != I.getWidth())) {
1427  I.resize(h,w) ;
1428  }
1429 
1430  for(unsigned int i=0;i<I.getHeight();i++)
1431  {
1432  for(unsigned int j=0;j<I.getWidth();j++)
1433  {
1434  vpRGBa v ;
1435  size_t res = fread(&v.R,sizeof(v.R),1,fd) ;
1436  res |= fread(&v.G,sizeof(v.G),1,fd) ;
1437  res |= fread(&v.B,sizeof(v.B),1,fd) ;
1438  if (res==0)
1439  {
1440  fclose (fd);
1442  "Cannot read bytes in file \"%s\"\n", filename));
1443  }
1444  I[i][j] = v ;
1445  }
1446  }
1447 
1448  fclose (fd);
1449 }
1450 
1461 void
1462 vpImageIo::writePPM(const vpImage<unsigned char> &I, const char *filename)
1463 {
1464 
1465  try
1466  {
1467  vpImage<vpRGBa> Itmp ;
1468 
1469  vpImageConvert::convert(I, Itmp) ;
1470 
1471  vpImageIo::writePPM(Itmp, filename) ;
1472  }
1473  catch(...)
1474  {
1475  vpERROR_TRACE(" ") ;
1476  throw ;
1477  }
1478 }
1479 
1480 
1488 void
1489 vpImageIo::writePPM(const vpImage<vpRGBa> &I, const char *filename)
1490 {
1491 
1492  FILE* f;
1493 
1494 
1495  // Test the filename
1496  if (filename == '\0') {
1497  vpERROR_TRACE("no filename\n");
1499  "no filename")) ;
1500  }
1501 
1502  f = fopen(filename, "wb");
1503 
1504  if (f == NULL) {
1505  vpERROR_TRACE("couldn't write to file \"%s\"\n", filename);
1507  "cannot write file")) ;
1508  }
1509 
1510 
1511 
1512  fprintf(f,"P6\n"); // Magic number
1513  fprintf(f,"%d %d\n", I.getWidth(), I.getHeight()); // Image size
1514  fprintf(f,"%d\n",255); // Max level
1515 
1516  for(unsigned int i=0;i<I.getHeight();i++)
1517  {
1518  for(unsigned int j=0;j<I.getWidth();j++)
1519  {
1520  vpRGBa P ;
1521  size_t res ;
1522  P = I[i][j] ;
1523  unsigned char tmp ;
1524  tmp = P.R ;
1525  res = fwrite(&tmp,sizeof(tmp),1,f) ;
1526  if (res==0)
1527  {
1528  fclose(f);
1529  vpERROR_TRACE("couldn't write file") ;
1531  "cannot write file")) ;
1532  }
1533  tmp = P.G;
1534  res = fwrite(&tmp,sizeof(tmp),1,f) ;
1535  if (res==0)
1536  {
1537  fclose(f);
1538  vpERROR_TRACE("couldn't write file") ;
1540  "cannot write file")) ;
1541  }
1542  tmp = P.B ;
1543  res = fwrite(&tmp,sizeof(tmp),1,f) ;
1544  if (res==0)
1545  {
1546  fclose(f);
1547  vpERROR_TRACE("couldn't write file") ;
1549  "cannot write file")) ;
1550  }
1551  }
1552  }
1553 
1554  fflush(f);
1555  fclose(f);
1556 }
1557 
1558 
1574 void
1575 vpImageIo::readPGM(vpImage<unsigned char> &I, const std::string filename)
1576 {
1577  vpImageIo::readPGM(I, filename.c_str());
1578 }
1579 
1595 void
1596 vpImageIo::readPGM(vpImage<vpRGBa> &I, const std::string filename)
1597 {
1598  vpImageIo::readPGM(I, filename.c_str());
1599 }
1600 
1610 void
1612  const std::string filename)
1613 {
1614  vpImageIo::writePGM(I, filename.c_str());
1615 }
1616 
1625 void
1626 vpImageIo::writePGM(const vpImage<short> &I, const std::string filename)
1627 {
1628 
1629  vpImageIo::writePGM(I, filename.c_str());
1630 }
1631 
1642 void
1643 vpImageIo::writePGM(const vpImage<vpRGBa> &I, const std::string filename)
1644 {
1645  vpImageIo::writePGM(I, filename.c_str());
1646 }
1647 
1648 //--------------------------------------------------------------------------
1649 // PPM
1650 //--------------------------------------------------------------------------
1651 
1668 void
1669 vpImageIo::readPPM(vpImage<unsigned char> &I, const std::string filename)
1670 {
1671  vpImageIo::readPPM(I, filename.c_str());
1672 }
1673 
1685 void
1686 vpImageIo::readPPM(vpImage<vpRGBa> &I, const std::string filename)
1687 {
1688  vpImageIo::readPPM(I, filename.c_str());
1689 }
1690 
1701 void
1702 vpImageIo::writePPM(const vpImage<unsigned char> &I, const std::string filename)
1703 {
1704  vpImageIo::writePPM(I, filename.c_str());
1705 }
1706 
1715 void
1716 vpImageIo::writePPM(const vpImage<vpRGBa> &I, const std::string filename)
1717 {
1718  vpImageIo::writePPM(I, filename.c_str());
1719 }
1720 
1721 
1722 //--------------------------------------------------------------------------
1723 // JPEG
1724 //--------------------------------------------------------------------------
1725 
1726 #if defined(VISP_HAVE_LIBJPEG)
1727 
1735 void
1736 vpImageIo::writeJPEG(const vpImage<unsigned char> &I, const char *filename)
1737 {
1738  struct jpeg_compress_struct cinfo;
1739  struct jpeg_error_mgr jerr;
1740  FILE *file;
1741 
1742  cinfo.err = jpeg_std_error(&jerr);
1743  jpeg_create_compress(&cinfo);
1744 
1745  // Test the filename
1746  if (filename == '\0') {
1747  vpERROR_TRACE("no filename\n");
1749  "no filename")) ;
1750  }
1751 
1752  file = fopen(filename, "wb");
1753 
1754  if (file == NULL) {
1755  vpERROR_TRACE("couldn't write file \"%s\"\n", filename);
1757  "cannot write file")) ;
1758  }
1759 
1760  unsigned int width = I.getWidth();
1761  unsigned int height = I.getHeight();
1762 
1763  jpeg_stdio_dest(&cinfo, file);
1764 
1765  cinfo.image_width = width;
1766  cinfo.image_height = height;
1767  cinfo.input_components = 1;
1768  cinfo.in_color_space = JCS_GRAYSCALE;
1769  jpeg_set_defaults(&cinfo);
1770 
1771  jpeg_start_compress(&cinfo,TRUE);
1772 
1773  unsigned char *line;
1774  line = new unsigned char[width];
1775  unsigned char* input = (unsigned char*)I.bitmap;
1776  while (cinfo.next_scanline < cinfo.image_height)
1777  {
1778  for (unsigned int i = 0; i < width; i++)
1779  {
1780  line[i] = *(input);
1781  input++;
1782  }
1783  jpeg_write_scanlines(&cinfo, &line, 1);
1784  }
1785 
1786  jpeg_finish_compress(&cinfo);
1787  jpeg_destroy_compress(&cinfo);
1788  delete [] line;
1789  fclose(file);
1790 }
1791 
1792 
1800 void
1801 vpImageIo::writeJPEG(const vpImage<unsigned char> &I, const std::string filename)
1802 {
1803  vpImageIo::writeJPEG(I, filename.c_str());
1804 }
1805 
1806 
1814 void
1815 vpImageIo::writeJPEG(const vpImage<vpRGBa> &I, const char *filename)
1816 {
1817  struct jpeg_compress_struct cinfo;
1818  struct jpeg_error_mgr jerr;
1819  FILE *file;
1820 
1821  cinfo.err = jpeg_std_error(&jerr);
1822  jpeg_create_compress(&cinfo);
1823 
1824  // Test the filename
1825  if (filename == '\0') {
1826  vpERROR_TRACE("no filename\n");
1828  "no filename")) ;
1829  }
1830 
1831  file = fopen(filename, "wb");
1832 
1833  if (file == NULL) {
1834  vpERROR_TRACE("couldn't write file \"%s\"\n", filename);
1836  "cannot write file")) ;
1837  }
1838 
1839  unsigned int width = I.getWidth();
1840  unsigned int height = I.getHeight();
1841 
1842  jpeg_stdio_dest(&cinfo, file);
1843 
1844  cinfo.image_width = width;
1845  cinfo.image_height = height;
1846  cinfo.input_components = 3;
1847  cinfo.in_color_space = JCS_RGB;
1848  jpeg_set_defaults(&cinfo);
1849 
1850  jpeg_start_compress(&cinfo,TRUE);
1851 
1852  unsigned char *line;
1853  line = new unsigned char[3*width];
1854  unsigned char* input = (unsigned char*)I.bitmap;
1855  while (cinfo.next_scanline < cinfo.image_height)
1856  {
1857  for (unsigned int i = 0; i < width; i++)
1858  {
1859  line[i*3] = *(input); input++;
1860  line[i*3+1] = *(input); input++;
1861  line[i*3+2] = *(input); input++;
1862  input++;
1863  }
1864  jpeg_write_scanlines(&cinfo, &line, 1);
1865  }
1866 
1867  jpeg_finish_compress(&cinfo);
1868  jpeg_destroy_compress(&cinfo);
1869  delete [] line;
1870  fclose(file);
1871 }
1872 
1873 
1881 void
1882 vpImageIo::writeJPEG(const vpImage<vpRGBa> &I, const std::string filename)
1883 {
1884  vpImageIo::writeJPEG(I, filename.c_str());
1885 }
1886 
1887 
1904 void
1906 {
1907  struct jpeg_decompress_struct cinfo;
1908  struct jpeg_error_mgr jerr;
1909  FILE *file;
1910 
1911  cinfo.err = jpeg_std_error(&jerr);
1912  jpeg_create_decompress(&cinfo);
1913 
1914  // Test the filename
1915  if (filename == '\0') {
1916  vpERROR_TRACE("no filename\n");
1918  "no filename")) ;
1919  }
1920 
1921  file = fopen(filename, "rb");
1922 
1923  if (file == NULL) {
1924  vpERROR_TRACE("couldn't read file \"%s\"\n", filename);
1926  "cannot read file")) ;
1927  }
1928 
1929  jpeg_stdio_src(&cinfo, file);
1930  jpeg_read_header(&cinfo, TRUE);
1931 
1932  unsigned int width = cinfo.image_width;
1933  unsigned int height = cinfo.image_height;
1934 
1935  if ( (width != I.getWidth()) || (height != I.getHeight()) )
1936  I.resize(height,width);
1937 
1938  jpeg_start_decompress(&cinfo);
1939 
1940  unsigned int rowbytes = cinfo.output_width * (unsigned int)(cinfo.output_components);
1941  JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray)
1942  ((j_common_ptr) &cinfo, JPOOL_IMAGE, rowbytes, 1);
1943 
1944  if (cinfo.out_color_space == JCS_RGB) {
1945  vpImage<vpRGBa> Ic(height,width);
1946  unsigned char* output = (unsigned char*)Ic.bitmap;
1947  while (cinfo.output_scanline<cinfo.output_height) {
1948  jpeg_read_scanlines(&cinfo,buffer,1);
1949  for (unsigned int i = 0; i < width; i++) {
1950  *(output++) = buffer[0][i*3];
1951  *(output++) = buffer[0][i*3+1];
1952  *(output++) = buffer[0][i*3+2];
1953  *(output++) = 0;
1954  }
1955  }
1956  vpImageConvert::convert(Ic,I) ;
1957  }
1958 
1959  else if (cinfo.out_color_space == JCS_GRAYSCALE)
1960  {
1961  unsigned int row;
1962  while (cinfo.output_scanline<cinfo.output_height)
1963  {
1964  row = cinfo.output_scanline;
1965  jpeg_read_scanlines(&cinfo,buffer,1);
1966  memcpy(I[row], buffer[0], rowbytes);
1967  }
1968  }
1969 
1970  jpeg_finish_decompress(&cinfo);
1971  jpeg_destroy_decompress(&cinfo);
1972  fclose(file);
1973 }
1974 
1975 
1992 void
1993 vpImageIo::readJPEG(vpImage<unsigned char> &I, const std::string filename)
1994 {
1995  vpImageIo::readJPEG(I, filename.c_str());
1996 }
1997 
1998 
2017 void
2018 vpImageIo::readJPEG(vpImage<vpRGBa> &I, const char *filename)
2019 {
2020  struct jpeg_decompress_struct cinfo;
2021  struct jpeg_error_mgr jerr;
2022  FILE *file;
2023 
2024  cinfo.err = jpeg_std_error(&jerr);
2025  jpeg_create_decompress(&cinfo);
2026 
2027  // Test the filename
2028  if (filename == '\0') {
2029  vpERROR_TRACE("no filename\n");
2031  "no filename")) ;
2032  }
2033 
2034  file = fopen(filename, "rb");
2035 
2036  if (file == NULL) {
2037  vpERROR_TRACE("couldn't read file \"%s\"\n", filename);
2039  "cannot read file")) ;
2040  }
2041 
2042  jpeg_stdio_src(&cinfo, file);
2043 
2044  jpeg_read_header(&cinfo, TRUE);
2045 
2046  unsigned int width = cinfo.image_width;
2047  unsigned int height = cinfo.image_height;
2048 
2049  if ( (width != I.getWidth()) || (height != I.getHeight()) )
2050  I.resize(height,width);
2051 
2052  jpeg_start_decompress(&cinfo);
2053 
2054  unsigned int rowbytes = cinfo.output_width * (unsigned int)(cinfo.output_components);
2055  JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray)
2056  ((j_common_ptr) &cinfo, JPOOL_IMAGE, rowbytes, 1);
2057 
2058  if (cinfo.out_color_space == JCS_RGB)
2059  {
2060  unsigned char* output = (unsigned char*)I.bitmap;
2061  while (cinfo.output_scanline<cinfo.output_height)
2062  {
2063  jpeg_read_scanlines(&cinfo,buffer,1);
2064  for (unsigned int i = 0; i < width; i++) {
2065  *(output++) = buffer[0][i*3];
2066  *(output++) = buffer[0][i*3+1];
2067  *(output++) = buffer[0][i*3+2];
2068  *(output++) = 0;
2069  }
2070  }
2071  }
2072 
2073  else if (cinfo.out_color_space == JCS_GRAYSCALE)
2074  {
2075  vpImage<unsigned char> Ig(height,width);
2076 
2077  unsigned int row;
2078  while (cinfo.output_scanline<cinfo.output_height)
2079  {
2080  row = cinfo.output_scanline;
2081  jpeg_read_scanlines(&cinfo,buffer,1);
2082  memcpy(Ig[row], buffer[0], rowbytes);
2083  }
2084 
2085  vpImageConvert::convert(Ig,I) ;
2086  }
2087 
2088  jpeg_finish_decompress(&cinfo);
2089  jpeg_destroy_decompress(&cinfo);
2090  fclose(file);
2091 }
2092 
2093 
2112 void
2113 vpImageIo::readJPEG(vpImage<vpRGBa> &I, const std::string filename)
2114 {
2115  vpImageIo::readJPEG(I, filename.c_str());
2116 }
2117 
2118 #elif defined(VISP_HAVE_OPENCV)
2119 
2127 void
2128 vpImageIo::writeJPEG(const vpImage<unsigned char> &I, const char *filename)
2129 {
2130  IplImage* Ip = NULL;
2131  vpImageConvert::convert(I, Ip);
2132 
2133  cvSaveImage(filename, Ip);
2134 
2135  cvReleaseImage(&Ip);
2136 }
2137 
2138 
2146 void
2147 vpImageIo::writeJPEG(const vpImage<unsigned char> &I, const std::string filename)
2148 {
2149  vpImageIo::writeJPEG(I, filename.c_str());
2150 }
2151 
2152 
2160 void
2161 vpImageIo::writeJPEG(const vpImage<vpRGBa> &I, const char *filename)
2162 {
2163  IplImage* Ip = NULL;
2164  vpImageConvert::convert(I, Ip);
2165 
2166  cvSaveImage(filename, Ip);
2167 
2168  cvReleaseImage(&Ip);
2169 }
2170 
2171 
2179 void
2180 vpImageIo::writeJPEG(const vpImage<vpRGBa> &I, const std::string filename)
2181 {
2182  vpImageIo::writeJPEG(I, filename.c_str());
2183 }
2184 
2185 
2202 void
2203 vpImageIo::readJPEG(vpImage<unsigned char> &I, const char *filename)
2204 {
2205  IplImage* Ip = NULL;
2206  Ip = cvLoadImage(filename, CV_LOAD_IMAGE_GRAYSCALE);
2207  if (Ip != NULL)
2208  vpImageConvert::convert(Ip, I);
2209  else
2211  "Can't read the image")) ;
2212  cvReleaseImage(&Ip);
2213 }
2214 
2215 
2232 void
2233 vpImageIo::readJPEG(vpImage<unsigned char> &I, const std::string filename)
2234 {
2235  vpImageIo::readJPEG(I, filename.c_str());
2236 }
2237 
2238 
2257 void
2258 vpImageIo::readJPEG(vpImage<vpRGBa> &I, const char *filename)
2259 {
2260  IplImage* Ip = NULL;
2261  Ip = cvLoadImage(filename, CV_LOAD_IMAGE_COLOR);
2262  if (Ip != NULL)
2263  vpImageConvert::convert(Ip, I);
2264  else
2266  "Can't read the image")) ;
2267  cvReleaseImage(&Ip);
2268 }
2269 
2270 
2289 void
2290 vpImageIo::readJPEG(vpImage<vpRGBa> &I, const std::string filename)
2291 {
2292  vpImageIo::readJPEG(I, filename.c_str());
2293 }
2294 
2295 #endif
2296 
2297 
2298 
2299 
2300 
2301 
2302 //--------------------------------------------------------------------------
2303 // PNG
2304 //--------------------------------------------------------------------------
2305 
2306 #if defined(VISP_HAVE_LIBPNG)
2307 
2315 void
2316 vpImageIo::writePNG(const vpImage<unsigned char> &I, const char *filename)
2317 {
2318  FILE *file;
2319 
2320  // Test the filename
2321  if (filename == '\0') {
2322  vpERROR_TRACE("no filename\n");
2324  "no filename")) ;
2325  }
2326 
2327  file = fopen(filename, "wb");
2328 
2329  if (file == NULL) {
2330  vpERROR_TRACE("couldn't write file \"%s\"\n", filename);
2332  "cannot write file")) ;
2333  }
2334 
2335  /* create a png info struct */
2336  png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL, NULL, NULL);
2337  if (!png_ptr)
2338  {
2339  fclose (file);
2340  vpERROR_TRACE("Error during png_create_write_struct()\n");
2342  "PNG write error")) ;
2343  }
2344 
2345  png_infop info_ptr = png_create_info_struct(png_ptr);
2346  if (!info_ptr)
2347  {
2348  fclose (file);
2349  png_destroy_write_struct (&png_ptr, NULL);
2350  vpERROR_TRACE("Error during png_create_info_struct()\n");
2352  "PNG write error")) ;
2353  }
2354 
2355  /* initialize the setjmp for returning properly after a libpng error occured */
2356  if (setjmp (png_jmpbuf (png_ptr)))
2357  {
2358  fclose (file);
2359  png_destroy_write_struct (&png_ptr, &info_ptr);
2360  vpERROR_TRACE("Error during init_io\n");
2362  "PNG write error")) ;
2363  }
2364 
2365  /* setup libpng for using standard C fwrite() function with our FILE pointer */
2366  png_init_io (png_ptr, file);
2367 
2368  unsigned int width = I.getWidth();
2369  unsigned int height = I.getHeight();
2370  int bit_depth = 8;
2371  int color_type = PNG_COLOR_TYPE_GRAY;
2372  /* set some useful information from header */
2373 
2374  if (setjmp (png_jmpbuf (png_ptr)))
2375  {
2376  fclose (file);
2377  png_destroy_write_struct (&png_ptr, &info_ptr);
2378  vpERROR_TRACE("Error during write header\n");
2380  "PNG write error")) ;
2381  }
2382 
2383  png_set_IHDR(png_ptr, info_ptr, width, height,
2384  bit_depth, color_type, PNG_INTERLACE_NONE,
2385  PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
2386 
2387  png_write_info(png_ptr, info_ptr);
2388 
2389  png_bytep* row_ptrs = new png_bytep[height];
2390  for (unsigned int i = 0; i < height; i++)
2391  row_ptrs[i] = new png_byte[width];
2392 
2393  unsigned char* input = (unsigned char*)I.bitmap;
2394 
2395  for (unsigned int i = 0; i < height; i++)
2396  {
2397  png_byte* row = row_ptrs[i];
2398  for(unsigned int j = 0; j < width; j++)
2399  {
2400  row[j] = *(input);
2401  input++;
2402  }
2403  }
2404 
2405  if (setjmp (png_jmpbuf (png_ptr)))
2406  {
2407  fclose (file);
2408  png_destroy_write_struct (&png_ptr, &info_ptr);
2409  for(unsigned int j = 0; j < height; j++)
2410  delete[] row_ptrs[j];
2411 
2412  delete[] row_ptrs;
2413  vpERROR_TRACE("Error during write image\n");
2415  "PNG write error")) ;
2416  }
2417 
2418  png_write_image(png_ptr, row_ptrs);
2419 
2420  if (setjmp (png_jmpbuf (png_ptr)))
2421  {
2422  fclose (file);
2423  png_destroy_write_struct (&png_ptr, &info_ptr);
2424  for(unsigned int j = 0; j < height; j++)
2425  delete[] row_ptrs[j];
2426 
2427  delete[] row_ptrs;
2428  vpERROR_TRACE("Error during write end\n");
2430  "PNG write error")) ;
2431  }
2432 
2433  png_write_end(png_ptr, NULL);
2434 
2435  for(unsigned int j = 0; j < height; j++)
2436  delete[] row_ptrs[j];
2437 
2438  delete[] row_ptrs;
2439 
2440  png_destroy_write_struct (&png_ptr, &info_ptr);
2441 
2442  fclose(file);
2443 }
2444 
2445 
2453 void
2454 vpImageIo::writePNG(const vpImage<unsigned char> &I, const std::string filename)
2455 {
2456  vpImageIo::writePNG(I, filename.c_str());
2457 }
2458 
2459 
2467 void
2468 vpImageIo::writePNG(const vpImage<vpRGBa> &I, const char *filename)
2469 {
2470  FILE *file;
2471 
2472  // Test the filename
2473  if (filename == '\0') {
2474  vpERROR_TRACE("no filename\n");
2476  "no filename")) ;
2477  }
2478 
2479  file = fopen(filename, "wb");
2480 
2481  if (file == NULL) {
2482  vpERROR_TRACE("couldn't write file \"%s\"\n", filename);
2484  "cannot write file")) ;
2485  }
2486 
2487  /* create a png info struct */
2488  png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL, NULL, NULL);
2489  if (!png_ptr)
2490  {
2491  fclose (file);
2492  vpERROR_TRACE("Error during png_create_write_struct()\n");
2494  "PNG write error")) ;
2495  }
2496 
2497  png_infop info_ptr = png_create_info_struct(png_ptr);
2498  if (!info_ptr)
2499  {
2500  fclose (file);
2501  png_destroy_write_struct (&png_ptr, NULL);
2502  vpERROR_TRACE("Error during png_create_info_struct()\n");
2504  "PNG write error")) ;
2505  }
2506 
2507  /* initialize the setjmp for returning properly after a libpng error occured */
2508  if (setjmp (png_jmpbuf (png_ptr)))
2509  {
2510  fclose (file);
2511  png_destroy_write_struct (&png_ptr, &info_ptr);
2512  vpERROR_TRACE("Error during init_io\n");
2514  "PNG write error")) ;
2515  }
2516 
2517  /* setup libpng for using standard C fwrite() function with our FILE pointer */
2518  png_init_io (png_ptr, file);
2519 
2520  unsigned int width = I.getWidth();
2521  unsigned int height = I.getHeight();
2522  int bit_depth = 8;
2523  int color_type = PNG_COLOR_TYPE_RGB;
2524  /* set some useful information from header */
2525 
2526  if (setjmp (png_jmpbuf (png_ptr)))
2527  {
2528  fclose (file);
2529  png_destroy_write_struct (&png_ptr, &info_ptr);
2530  vpERROR_TRACE("Error during write header\n");
2532  "PNG write error")) ;
2533  }
2534 
2535  png_set_IHDR(png_ptr, info_ptr, width, height,
2536  bit_depth, color_type, PNG_INTERLACE_NONE,
2537  PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
2538 
2539  png_write_info(png_ptr, info_ptr);
2540 
2541  png_bytep* row_ptrs = new png_bytep[height];
2542  for (unsigned int i = 0; i < height; i++)
2543  row_ptrs[i] = new png_byte[3*width];
2544 
2545  unsigned char* input = (unsigned char*)I.bitmap;;
2546 
2547  for (unsigned int i = 0; i < height; i++)
2548  {
2549  png_byte* row = row_ptrs[i];
2550  for(unsigned int j = 0; j < width; j++)
2551  {
2552  row[3*j] = *(input);input++;
2553  row[3*j+1] = *(input);input++;
2554  row[3*j+2] = *(input);input++;
2555  input++;
2556  }
2557  }
2558 
2559  if (setjmp (png_jmpbuf (png_ptr)))
2560  {
2561  fclose (file);
2562  png_destroy_write_struct (&png_ptr, &info_ptr);
2563  for(unsigned int j = 0; j < height; j++)
2564  delete[] row_ptrs[j];
2565 
2566  delete[] row_ptrs;
2567  vpERROR_TRACE("Error during write image\n");
2569  "PNG write error")) ;
2570  }
2571 
2572  png_write_image(png_ptr, row_ptrs);
2573 
2574  if (setjmp (png_jmpbuf (png_ptr)))
2575  {
2576  fclose (file);
2577  png_destroy_write_struct (&png_ptr, &info_ptr);
2578  for(unsigned int j = 0; j < height; j++)
2579  delete[] row_ptrs[j];
2580 
2581  delete[] row_ptrs;
2582  vpERROR_TRACE("Error during write end\n");
2584  "PNG write error")) ;
2585  }
2586 
2587  png_write_end(png_ptr, NULL);
2588 
2589  for(unsigned int j = 0; j < height; j++)
2590  delete[] row_ptrs[j];
2591 
2592  delete[] row_ptrs;
2593 
2594  png_destroy_write_struct (&png_ptr, &info_ptr);
2595 
2596  fclose(file);
2597 }
2598 
2599 
2607 void
2608 vpImageIo::writePNG(const vpImage<vpRGBa> &I, const std::string filename)
2609 {
2610  vpImageIo::writePNG(I, filename.c_str());
2611 }
2612 
2629 void
2631 {
2632  FILE *file;
2633  png_byte magic[8];
2634  // Test the filename
2635  if (filename == '\0') {
2636  vpERROR_TRACE("no filename\n");
2638  "no filename")) ;
2639  }
2640 
2641  file = fopen(filename, "rb");
2642 
2643  if (file == NULL) {
2644  vpERROR_TRACE("couldn't read file \"%s\"\n", filename);
2646  "cannot read file")) ;
2647  }
2648 
2649  /* read magic number */
2650  if (fread (magic, 1, sizeof (magic), file) != sizeof (magic))
2651  {
2652  fclose (file);
2653  vpERROR_TRACE("couldn't read magic number in file \"%s\"\n", filename) ;
2655  "error reading png file")) ;
2656  }
2657 
2658  /* check for valid magic number */
2659  if (png_sig_cmp (magic,0, sizeof (magic)))
2660  {
2661  fprintf (stderr, "error: \"%s\" is not a valid PNG image!\n",filename);
2662  fclose (file);
2664  "error reading png file")) ;
2665  }
2666 
2667  /* create a png read struct */
2668  //printf("version %s\n", PNG_LIBPNG_VER_STRING);
2669  png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
2670  if (png_ptr == NULL)
2671  {
2672  fprintf (stderr, "error: can't create a png read structure!\n");
2673  fclose (file);
2675  "error reading png file")) ;
2676  }
2677 
2678  /* create a png info struct */
2679  png_infop info_ptr = png_create_info_struct (png_ptr);
2680  if (info_ptr == NULL)
2681  {
2682  fprintf (stderr, "error: can't create a png info structure!\n");
2683  fclose (file);
2684  png_destroy_read_struct (&png_ptr, NULL, NULL);
2686  "error reading png file")) ;
2687  }
2688 
2689  /* initialize the setjmp for returning properly after a libpng error occured */
2690  if (setjmp (png_jmpbuf (png_ptr)))
2691  {
2692  fclose (file);
2693  png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
2694  vpERROR_TRACE("Error during init io\n");
2696  "PNG read error")) ;
2697  }
2698 
2699  /* setup libpng for using standard C fread() function with our FILE pointer */
2700  png_init_io (png_ptr, file);
2701 
2702  /* tell libpng that we have already read the magic number */
2703  png_set_sig_bytes (png_ptr, sizeof (magic));
2704 
2705  /* read png info */
2706  png_read_info (png_ptr, info_ptr);
2707 
2708  unsigned int width = png_get_image_width(png_ptr, info_ptr);
2709  unsigned int height = png_get_image_height(png_ptr, info_ptr);
2710 
2711  unsigned int bit_depth, channels, color_type;
2712  /* get some useful information from header */
2713  bit_depth = png_get_bit_depth (png_ptr, info_ptr);
2714  channels = png_get_channels(png_ptr, info_ptr);
2715  color_type = png_get_color_type (png_ptr, info_ptr);
2716 
2717  /* convert index color images to RGB images */
2718  if (color_type == PNG_COLOR_TYPE_PALETTE)
2719  png_set_palette_to_rgb (png_ptr);
2720 
2721  /* convert 1-2-4 bits grayscale images to 8 bits grayscale. */
2722  if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
2723  png_set_expand (png_ptr);
2724 
2725 // if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS))
2726 // png_set_tRNS_to_alpha (png_ptr);
2727 
2728  if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2729  png_set_strip_alpha(png_ptr);
2730 
2731  if (bit_depth == 16)
2732  png_set_strip_16 (png_ptr);
2733  else if (bit_depth < 8)
2734  png_set_packing (png_ptr);
2735 
2736  /* update info structure to apply transformations */
2737  png_read_update_info (png_ptr, info_ptr);
2738 
2739  channels = png_get_channels(png_ptr, info_ptr);
2740 
2741  if ( (width != I.getWidth()) || (height != I.getHeight()) )
2742  I.resize(height,width);
2743 
2744  png_bytep* rowPtrs = new png_bytep[height];
2745 
2746  unsigned int stride = width * bit_depth * channels / 8;
2747  unsigned char* data = new unsigned char[stride * height];
2748 
2749  for (unsigned int i =0; i < height; i++)
2750  rowPtrs[i] = (png_bytep)data + (i * stride);
2751 
2752  png_read_image(png_ptr, rowPtrs);
2753 
2754  vpImage<vpRGBa> Ic(height,width);
2755  unsigned char* output;
2756 
2757  switch (channels)
2758  {
2759  case 1:
2760  output = (unsigned char*)I.bitmap;
2761  for (unsigned int i = 0; i < width*height; i++)
2762  {
2763  *(output++) = data[i];
2764  }
2765  break;
2766  case 2:
2767  output = (unsigned char*)I.bitmap;
2768  for (unsigned int i = 0; i < width*height; i++)
2769  {
2770  *(output++) = data[i*2];
2771  }
2772  break;
2773  case 3:
2774 
2775  output = (unsigned char*)Ic.bitmap;
2776  for (unsigned int i = 0; i < width*height; i++)
2777  {
2778  *(output++) = data[i*3];
2779  *(output++) = data[i*3+1];
2780  *(output++) = data[i*3+2];
2781  *(output++) = 0;
2782  }
2783  vpImageConvert::convert(Ic,I) ;
2784  break;
2785  case 4:
2786  output = (unsigned char*)Ic.bitmap;
2787  for (unsigned int i = 0; i < width*height; i++)
2788  {
2789  *(output++) = data[i*4];
2790  *(output++) = data[i*4+1];
2791  *(output++) = data[i*4+2];
2792  *(output++) = data[i*4+3];
2793  }
2794  vpImageConvert::convert(Ic,I) ;
2795  break;
2796  }
2797 
2798  delete [] (png_bytep)rowPtrs;
2799  delete [] data;
2800  png_read_end (png_ptr, NULL);
2801  png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
2802  fclose(file);
2803 }
2804 
2805 
2822 void
2823 vpImageIo::readPNG(vpImage<unsigned char> &I, const std::string filename)
2824 {
2825  vpImageIo::readPNG(I, filename.c_str());
2826 }
2827 
2828 
2847 void
2848 vpImageIo::readPNG(vpImage<vpRGBa> &I, const char *filename)
2849 {
2850  FILE *file;
2851  png_byte magic[8];
2852 
2853  // Test the filename
2854  if (filename == '\0') {
2855  vpERROR_TRACE("no filename\n");
2857  "no filename")) ;
2858  }
2859 
2860  file = fopen(filename, "rb");
2861 
2862  if (file == NULL) {
2863  vpERROR_TRACE("couldn't read file \"%s\"\n", filename);
2865  "cannot read file")) ;
2866  }
2867 
2868  /* read magic number */
2869  if (fread (magic, 1, sizeof (magic), file) != sizeof (magic))
2870  {
2871  fclose (file);
2872  vpERROR_TRACE("couldn't read magic number in file \"%s\"\n", filename) ;
2874  "error reading pgm file")) ;
2875  }
2876 
2877  /* check for valid magic number */
2878  if (png_sig_cmp (magic,0, sizeof (magic)))
2879  {
2880  fclose (file);
2881  vpERROR_TRACE("error: \"%s\" is not a valid PNG image!\n",filename);
2883  "PNG read error")) ;
2884  }
2885 
2886  /* create a png read struct */
2887  png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
2888  if (!png_ptr)
2889  {
2890  fclose (file);
2891  vpERROR_TRACE("Error during png_create_read_struct()\n");
2893  "PNG read error")) ;
2894  }
2895 
2896  /* create a png info struct */
2897  png_infop info_ptr = png_create_info_struct (png_ptr);
2898  if (!info_ptr)
2899  {
2900  fclose (file);
2901  png_destroy_read_struct (&png_ptr, NULL, NULL);
2902  vpERROR_TRACE("Error during png_create_info_struct()\n");
2904  "PNG read error")) ;
2905  }
2906 
2907  /* initialize the setjmp for returning properly after a libpng error occured */
2908  if (setjmp (png_jmpbuf (png_ptr)))
2909  {
2910  fclose (file);
2911  png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
2912  vpERROR_TRACE("Error during init io\n");
2914  "PNG read error")) ;
2915  }
2916 
2917  /* setup libpng for using standard C fread() function with our FILE pointer */
2918  png_init_io (png_ptr, file);
2919 
2920  /* tell libpng that we have already read the magic number */
2921  png_set_sig_bytes (png_ptr, sizeof (magic));
2922 
2923  /* read png info */
2924  png_read_info (png_ptr, info_ptr);
2925 
2926  unsigned int width = png_get_image_width(png_ptr, info_ptr);
2927  unsigned int height = png_get_image_height(png_ptr, info_ptr);
2928 
2929  unsigned int bit_depth, channels, color_type;
2930  /* get some useful information from header */
2931  bit_depth = png_get_bit_depth (png_ptr, info_ptr);
2932  channels = png_get_channels(png_ptr, info_ptr);
2933  color_type = png_get_color_type (png_ptr, info_ptr);
2934 
2935  /* convert index color images to RGB images */
2936  if (color_type == PNG_COLOR_TYPE_PALETTE)
2937  png_set_palette_to_rgb (png_ptr);
2938 
2939  /* convert 1-2-4 bits grayscale images to 8 bits grayscale. */
2940  if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
2941  png_set_expand (png_ptr);
2942 
2943 // if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS))
2944 // png_set_tRNS_to_alpha (png_ptr);
2945 
2946  if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2947  png_set_strip_alpha(png_ptr);
2948 
2949  if (bit_depth == 16)
2950  png_set_strip_16 (png_ptr);
2951  else if (bit_depth < 8)
2952  png_set_packing (png_ptr);
2953 
2954  /* update info structure to apply transformations */
2955  png_read_update_info (png_ptr, info_ptr);
2956 
2957  channels = png_get_channels(png_ptr, info_ptr);
2958 
2959  if ( (width != I.getWidth()) || (height != I.getHeight()) )
2960  I.resize(height,width);
2961 
2962  png_bytep* rowPtrs = new png_bytep[height];
2963 
2964  unsigned int stride = width * bit_depth * channels / 8;
2965  unsigned char* data = new unsigned char[stride * height];
2966 
2967 
2968  for (unsigned int i =0; i < height; i++)
2969  rowPtrs[i] = (png_bytep)data + (i * stride);
2970 
2971  png_read_image(png_ptr, rowPtrs);
2972 
2973  vpImage<unsigned char> Ig(height,width);
2974  unsigned char* output;
2975 
2976  switch (channels)
2977  {
2978  case 1:
2979  output = (unsigned char*)Ig.bitmap;
2980  for (unsigned int i = 0; i < width*height; i++)
2981  {
2982  *(output++) = data[i];
2983  }
2984  vpImageConvert::convert(Ig,I) ;
2985  break;
2986  case 2:
2987  output = (unsigned char*)Ig.bitmap;
2988  for (unsigned int i = 0; i < width*height; i++)
2989  {
2990  *(output++) = data[i*2];
2991  }
2992  vpImageConvert::convert(Ig,I) ;
2993  break;
2994  case 3:
2995 
2996  output = (unsigned char*)I.bitmap;
2997  for (unsigned int i = 0; i < width*height; i++)
2998  {
2999  *(output++) = data[i*3];
3000  *(output++) = data[i*3+1];
3001  *(output++) = data[i*3+2];
3002  *(output++) = 0;
3003  }
3004  break;
3005  case 4:
3006  output = (unsigned char*)I.bitmap;
3007  for (unsigned int i = 0; i < width*height; i++)
3008  {
3009  *(output++) = data[i*4];
3010  *(output++) = data[i*4+1];
3011  *(output++) = data[i*4+2];
3012  *(output++) = data[i*4+3];
3013  }
3014  break;
3015  }
3016 
3017  delete [] (png_bytep)rowPtrs;
3018  delete [] data;
3019  png_read_end (png_ptr, NULL);
3020  png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
3021  fclose(file);
3022 }
3023 
3024 
3043 void
3044 vpImageIo::readPNG(vpImage<vpRGBa> &I, const std::string filename)
3045 {
3046  vpImageIo::readPNG(I, filename.c_str());
3047 }
3048 
3049 #elif defined(VISP_HAVE_OPENCV)
3050 
3058 void
3059 vpImageIo::writePNG(const vpImage<unsigned char> &I, const char *filename)
3060 {
3061  IplImage* Ip = NULL;
3062  vpImageConvert::convert(I, Ip);
3063 
3064  cvSaveImage(filename, Ip);
3065 
3066  cvReleaseImage(&Ip);
3067 }
3068 
3069 
3077 void
3078 vpImageIo::writePNG(const vpImage<unsigned char> &I, const std::string filename)
3079 {
3080  vpImageIo::writePNG(I, filename.c_str());
3081 }
3082 
3083 
3091 void
3092 vpImageIo::writePNG(const vpImage<vpRGBa> &I, const char *filename)
3093 {
3094  IplImage* Ip = NULL;
3095  vpImageConvert::convert(I, Ip);
3096 
3097  cvSaveImage(filename, Ip);
3098 
3099  cvReleaseImage(&Ip);
3100 }
3101 
3102 
3110 void
3111 vpImageIo::writePNG(const vpImage<vpRGBa> &I, const std::string filename)
3112 {
3113  vpImageIo::writePNG(I, filename.c_str());
3114 }
3115 
3116 
3133 void
3134 vpImageIo::readPNG(vpImage<unsigned char> &I, const char *filename)
3135 {
3136  IplImage* Ip = NULL;
3137  Ip = cvLoadImage(filename, CV_LOAD_IMAGE_GRAYSCALE);
3138  if (Ip != NULL)
3139  vpImageConvert::convert(Ip, I);
3140  else
3142  "Can't read the image")) ;
3143  cvReleaseImage(&Ip);
3144 }
3145 
3146 
3163 void
3164 vpImageIo::readPNG(vpImage<unsigned char> &I, const std::string filename)
3165 {
3166  vpImageIo::readPNG(I, filename.c_str());
3167 }
3168 
3169 
3188 void
3189 vpImageIo::readPNG(vpImage<vpRGBa> &I, const char *filename)
3190 {
3191  IplImage* Ip = NULL;
3192  Ip = cvLoadImage(filename, CV_LOAD_IMAGE_COLOR);
3193  if (Ip != NULL)
3194  vpImageConvert::convert(Ip, I);
3195  else
3197  "Can't read the image")) ;
3198  cvReleaseImage(&Ip);
3199 }
3200 
3201 
3220 void
3221 vpImageIo::readPNG(vpImage<vpRGBa> &I, const std::string filename)
3222 {
3223  vpImageIo::readPNG(I, filename.c_str());
3224 }
3225 
3226 #endif
static void write(const vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:452
static void writeJPEG(const vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:1736
static void readPGM(vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:972
unsigned int getWidth() const
Definition: vpImage.h:159
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:663
static void writePNG(const vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:2316
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:607
Error that can be emited by the vpImage class and its derivates.
static void readPFM(vpImage< float > &I, const char *filename)
Definition: vpImageIo.cpp:803
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:485
void resize(const unsigned int h, const unsigned int w)
set the size of the image
Definition: vpImage.h:532
static void readPNG(vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:2630
static void readJPEG(vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:1905
unsigned char R
Red component.
Definition: vpRGBa.h:146
static void readPPM(vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:1221
unsigned int getHeight() const
Definition: vpImage.h:150
static void writePPM(const vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:1462
static void read(vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:278