ViSP  2.6.2
vpImageIo.cpp
1 /****************************************************************************
2  *
3  * $Id: vpImageIo.cpp 3793 2012-06-14 10:13:30Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2012 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 
51 const int vpImageIo::vpMAX_LEN = 100;
52 
61 FILE *
62 vpImageIo::openFileRead(const char *filename)
63 {
64 
65  FILE *fd ;
66 
67  // Lecture du nom du fichier image.
68  if (filename == '\0') {
69  vpERROR_TRACE("filename empty ") ;
71  "filename empty ")) ;
72  }
73 
74  // Ouverture de l'image.
75  if ((fd = fopen(filename, "r")) == NULL)
76  {
77  vpERROR_TRACE("cannot open file") ;
79  "cannot open file")) ;
80  }
81  return fd ;
82 }
83 
96 FILE *
97 vpImageIo::openFileWrite(const char *filename, const char *mode)
98 {
99  FILE *fd ;
100 
101  // Lecture du nom du fichier image.
102  if (filename == '\0')
103  {
104  vpERROR_TRACE("filename empty ") ;
106  "filename empty ")) ;
107  }
108 
109  // Ouverture de l'image.
110  if ((fd = fopen(filename, mode)) == NULL)
111  {
112  vpERROR_TRACE("cannot open file") ;
114  "cannot open file")) ;
115  }
116  return fd ;
117 }
118 
127 FILE *
128 vpImageIo::openFileRead(const std::string filename)
129 {
130 
131  FILE *fd ;
132 
133  // Lecture du nom du fichier image.
134  if (filename.empty()) {
135  vpERROR_TRACE("filename empty ") ;
137  "filename empty ")) ;
138  }
139 
140  // Ouverture de l'image.
141  if ((fd = fopen(filename.c_str(), "r")) == NULL)
142  {
143  vpERROR_TRACE("cannot open file") ;
145  "cannot open file")) ;
146  }
147  return fd ;
148 }
149 
162 FILE *
163 vpImageIo::openFileWrite(const std::string filename,
164  const std::string mode)
165 {
166  FILE *fd ;
167 
168  // Lecture du nom du fichier image.
169  if (filename.empty())
170  {
171  vpERROR_TRACE("filename empty ") ;
173  "filename empty ")) ;
174  }
175 
176  // Ouverture de l'image.
177  if ((fd = fopen(filename.c_str(), mode.c_str())) == NULL)
178  {
179  vpERROR_TRACE("cannot open file") ;
181  "cannot open file")) ;
182  }
183  return fd ;
184 }
185 
186 vpImageIo::vpImageFormatType
187 vpImageIo::getFormat(const char *filename)
188 {
189  std::string sfilename(filename);
190 
191  std::string ext = vpImageIo::getExtension(sfilename);
192 
193  if (ext.compare(".PGM") == 0)
194  return FORMAT_PGM;
195  else if (ext.compare(".pgm") == 0)
196  return FORMAT_PGM;
197  else if (ext.compare(".PPM") == 0)
198  return FORMAT_PPM;
199  else if (ext.compare(".ppm") == 0)
200  return FORMAT_PPM;
201  else if (ext.compare(".JPG") == 0)
202  return FORMAT_JPEG;
203  else if (ext.compare(".jpg") == 0)
204  return FORMAT_JPEG;
205  else if (ext.compare(".JPEG") == 0)
206  return FORMAT_JPEG;
207  else if (ext.compare(".jpeg") == 0)
208  return FORMAT_JPEG;
209  else if (ext.compare(".PNG") == 0)
210  return FORMAT_PNG;
211  else if (ext.compare(".png") == 0)
212  return FORMAT_PNG;
213  else
214  return FORMAT_UNKNOWN;
215 }
216 
217 // return the extension of the file including the dot
218 std::string vpImageIo::getExtension(const std::string &filename)
219 {
220  // extract the extension
221  size_t dot = filename.find_last_of(".");
222  std::string ext = filename.substr(dot, filename.size()-1);
223  return ext;
224 }
225 
226 
238 void
239 vpImageIo::read(vpImage<unsigned char> &I, const char *filename)
240 {
241  switch(getFormat(filename)){
242  case FORMAT_PGM :
243  readPGM(I,filename); break;
244  case FORMAT_PPM :
245  readPPM(I,filename); break;
246  case FORMAT_JPEG :
247 #if (defined(VISP_HAVE_LIBJPEG) || defined(VISP_HAVE_OPENCV))
248  readJPEG(I,filename); break;
249 #else
250  vpCERROR << "You need the libjpeg library to open JPEG files "
251  << std::endl;
252  break;
253 #endif
254  case FORMAT_PNG :
255 #if (defined(VISP_HAVE_LIBPNG) || defined(VISP_HAVE_OPENCV))
256  readPNG(I,filename); break;
257 #else
258  vpCERROR << "You need the libpng library to open PNG files "
259  << std::endl;
260  break;
261 #endif
262  case FORMAT_UNKNOWN :
263  vpCERROR << "Error: Only PNM (PGM P5 and PPM P6), JPEG and PNG " << std::endl
264  << " image format are implemented..." << std::endl;
266  "cannot read file")) ;
267  break;
268  }
269 }
281 void
282 vpImageIo::read(vpImage<unsigned char> &I, const std::string filename)
283 {
284  read(I,filename.c_str());
285 }
297 void
298 vpImageIo::read(vpImage<vpRGBa> &I, const char *filename)
299 {
300  switch(getFormat(filename)){
301  case FORMAT_PGM :
302  readPGM(I,filename); break;
303  case FORMAT_PPM :
304  readPPM(I,filename); break;
305  case FORMAT_JPEG :
306 #if (defined(VISP_HAVE_LIBJPEG) || defined(VISP_HAVE_OPENCV))
307  readJPEG(I,filename); break;
308 #else
309  vpCERROR << "You need the libjpeg library to open JPEG files "
310  << std::endl;
311  break;
312 #endif
313  case FORMAT_PNG :
314 #if (defined(VISP_HAVE_LIBPNG) || defined(VISP_HAVE_OPENCV))
315  readPNG(I,filename); break;
316 #else
317  vpCERROR << "You need the libpng library to open PNG files "
318  << std::endl;
319  break;
320 #endif
321  case FORMAT_UNKNOWN :
322  vpCERROR << "Error: Only PNM (PGM P5 and PPM P6), JPEG and PNG " << std::endl
323  << " image format are implemented..." << std::endl;
325  "cannot read file")) ;
326  break;
327  }
328 }
340 void
341 vpImageIo::read(vpImage<vpRGBa> &I, const std::string filename)
342 {
343  read(I,filename.c_str());
344 }
345 
354 void
355 vpImageIo::write(const vpImage<unsigned char> &I, const char *filename)
356 {
357  switch(getFormat(filename)){
358  case FORMAT_PGM :
359  writePGM(I,filename); break;
360  case FORMAT_PPM :
361  writePPM(I,filename); break;
362  case FORMAT_JPEG :
363 #if (defined(VISP_HAVE_LIBJPEG) || defined(VISP_HAVE_OPENCV))
364  writeJPEG(I,filename); break;
365 #else
366  vpCERROR << "You need the libjpeg library to write JPEG files "
367  << std::endl;
368  break;
369 #endif
370  case FORMAT_PNG :
371 #if (defined(VISP_HAVE_LIBPNG) || defined(VISP_HAVE_OPENCV))
372  writePNG(I,filename); break;
373 #else
374  vpCERROR << "You need the libpng library to write PNG files "
375  << std::endl;
376  break;
377 #endif
378  case FORMAT_UNKNOWN :
379  vpCERROR << "Error: Only PNM (PGM P5 and PPM P6) JPEG and PNG " << std::endl
380  << " image format are implemented..." << std::endl;
382  "cannot write file")) ;
383  break;
384  }
385 }
394 void
395 vpImageIo::write(const vpImage<unsigned char> &I, const std::string filename)
396 {
397  write(I,filename.c_str());
398 }
407 void
408 vpImageIo::write(const vpImage<vpRGBa> &I, const char *filename)
409 {
410  switch(getFormat(filename)){
411  case FORMAT_PGM :
412  writePGM(I,filename); break;
413  case FORMAT_PPM :
414  writePPM(I,filename); break;
415  case FORMAT_JPEG :
416 #if (defined(VISP_HAVE_LIBJPEG) || defined(VISP_HAVE_OPENCV))
417  writeJPEG(I,filename); break;
418 #else
419  vpCERROR << "You need the libjpeg library to write JPEG files "
420  << std::endl;
421  break;
422 #endif
423  case FORMAT_PNG :
424 #if (defined(VISP_HAVE_LIBPNG) || defined(VISP_HAVE_OPENCV))
425  writePNG(I,filename); break;
426 #else
427  vpCERROR << "You need the libpng library to write PNG files "
428  << std::endl;
429  break;
430 #endif
431  case FORMAT_UNKNOWN :
432  vpCERROR << "Error: Only PNM (PGM P5 and PPM P6), JPEG and PNG " << std::endl
433  << " image format are implemented..." << std::endl;
435  "cannot write file")) ;
436  break;
437  }
438 }
447 void
448 vpImageIo::write(const vpImage<vpRGBa> &I, const std::string filename)
449 {
450  write(I,filename.c_str());
451 }
452 //--------------------------------------------------------------------------
453 // PFM
454 //--------------------------------------------------------------------------
455 
465 void
467  const char *filename)
468 {
469 
470  FILE* fd;
471 
472  // Test the filename
473  if (filename == '\0') {
474  vpERROR_TRACE("no filename\n");
476  "no filename")) ;
477  }
478 
479  fd = fopen(filename, "wb");
480 
481  if (fd == NULL) {
482  vpERROR_TRACE("couldn't write to file \"%s\"\n", filename);
484  "cannot write file")) ;
485  }
486 
487  // Write the head
488  fprintf(fd, "P8\n"); // Magic number
489  fprintf(fd, "%d %d\n", I.getWidth(), I.getHeight()); // Image size
490  fprintf(fd, "255\n"); // Max level
491 
492  // Write the bitmap
493  size_t ierr;
494  size_t nbyte = I.getWidth()*I.getHeight();
495 
496  ierr = fwrite(I.bitmap, sizeof(float), nbyte, fd) ;
497  if (ierr != nbyte) {
498  fclose(fd);
499  vpERROR_TRACE("couldn't write %d bytes to file \"%s\"\n",
500  nbyte, filename) ;
502  "cannot write file")) ;
503  }
504 
505  fflush(fd);
506  fclose(fd);
507 
508 }
509 //--------------------------------------------------------------------------
510 // PGM
511 //--------------------------------------------------------------------------
512 
521 void
523  const char *filename)
524 {
525 
526  FILE* fd;
527 
528  // Test the filename
529  if (filename == '\0') {
530  vpERROR_TRACE("no filename\n");
532  "no filename")) ;
533  }
534 
535  fd = fopen(filename, "wb");
536 
537  if (fd == NULL) {
538  vpERROR_TRACE("couldn't write to file \"%s\"\n", filename);
540  "cannot write file")) ;
541  }
542 
543  // Write the head
544  fprintf(fd, "P5\n"); // Magic number
545  fprintf(fd, "%d %d\n", I.getWidth(), I.getHeight()); // Image size
546  fprintf(fd, "255\n"); // Max level
547 
548  // Write the bitmap
549  size_t ierr;
550  size_t nbyte = I.getWidth()*I.getHeight();
551 
552  ierr = fwrite(I.bitmap, sizeof(unsigned char), nbyte, fd) ;
553  if (ierr != nbyte) {
554  fclose(fd);
555  vpERROR_TRACE("couldn't write %d bytes to file \"%s\"\n",
556  nbyte, filename) ;
558  "cannot write file")) ;
559  }
560 
561  fflush(fd);
562  fclose(fd);
563 
564 }
572 void
573 vpImageIo::writePGM(const vpImage<short> &I, const char *filename)
574 {
576  unsigned int nrows = I.getHeight();
577  unsigned int ncols = I.getWidth();
578 
579  Iuc.resize(nrows, ncols);
580 
581  for (unsigned int i=0 ; i < nrows * ncols ; i++)
582  Iuc.bitmap[i] = (unsigned char)I.bitmap[i] ;
583 
584  vpImageIo::writePGM(Iuc, filename) ;
585 
586 
587 }
597 void
598 vpImageIo::writePGM(const vpImage<vpRGBa> &I, const char *filename)
599 {
600 
601  FILE* fd;
602 
603  // Test the filename
604  if (filename == '\0') {
605  vpERROR_TRACE("no filename\n");
607  "no filename")) ;
608  }
609 
610  fd = fopen(filename, "wb");
611 
612  if (fd == NULL) {
613  vpERROR_TRACE("couldn't write to file \"%s\"\n", filename);
615  "cannot write file")) ;
616  }
617 
618  // Write the head
619  fprintf(fd, "P5\n"); // Magic number
620  fprintf(fd, "%d %d\n", I.getWidth(), I.getHeight()); // Image size
621  fprintf(fd, "255\n"); // Max level
622 
623  // Write the bitmap
624  size_t ierr;
625  size_t nbyte = I.getWidth()*I.getHeight();
626 
627 
629  vpImageConvert::convert(I,Itmp) ;
630 
631  ierr = fwrite(Itmp.bitmap, sizeof(unsigned char), nbyte, fd) ;
632  if (ierr != nbyte) {
633  fclose(fd);
634  vpERROR_TRACE("couldn't write %d bytes to file \"%s\"\n",
635  nbyte, filename) ;
637  "cannot write file")) ;
638  }
639 
640  fflush(fd);
641  fclose(fd);
642 
643 }
644 
661 void
662 vpImageIo::readPFM(vpImage<float> &I, const char *filename)
663 {
664  FILE* fd = NULL; // File descriptor
665  int ierr;
666  int line;
667  int is255;
668  char* err ;
669  char str[vpMAX_LEN];
670  unsigned int w, h;
671 
672  // Test the filename
673  if (filename == '\0')
674  {
675  vpERROR_TRACE("no filename") ;
677  " no filename")) ;
678 
679  }
680 
681  // Open the filename
682  fd = fopen(filename, "rb");
683  if (fd == NULL)
684  {
685  vpERROR_TRACE("couldn't read file \"%s\"", filename) ;
687  "couldn't read file")) ;
688  }
689 
690  // Read the first line with magic number P5
691  line = 0;
692 
693  err = fgets(str, vpMAX_LEN - 1, fd);
694  line++;
695  if (err == NULL)
696  {
697  fclose (fd);
698  vpERROR_TRACE("couldn't read line %d of file \"%s\"\n", line, filename) ;
700  "couldn't read file")) ;
701  }
702 
703  if (strlen(str) < 3)
704  {
705  fclose (fd);
706  vpERROR_TRACE("\"%s\" is not a PGM file\n", filename) ;
708  "this is not a pfm file")) ;
709  }
710 
711  str[2] = '\0';
712  if (strcmp(str, "P8") != 0)
713  {
714  fclose (fd);
715  vpERROR_TRACE("\"%s\" is not a PFM file\n", filename) ;
717  "this is not a pgm file")) ;
718  }
719 
720  // Jump the possible comment, or empty line and read the following line
721  do {
722  err = fgets(str, vpMAX_LEN - 1, fd);
723  line++;
724  if (err == NULL) {
725  fprintf(stderr, "couldn't read line %d of file \"%s\"\n", line, filename);
726  fclose (fd);
727  }
728  } while ((str[0] == '#') || (str[0] == '\n'));
729 
730  // Extract image size
731  ierr = sscanf(str, "%d %d", &w, &h);
732  if(ierr == 1){// the norm allows to have the two values on two separated lines.
733  do {
734  err = fgets(str, vpMAX_LEN - 1, fd);
735  line++;
736  if (err == NULL) {
737  fprintf(stderr, "couldn't read line %d of file \"%s\"\n", line, filename);
738  fclose (fd);
739  }
740  } while ((str[0] == '#') || (str[0] == '\n'));
741  ierr = sscanf(str, "%d", &h);
742  }
743  if (ierr == EOF)
744  {
745  fclose (fd);
746  vpERROR_TRACE("couldn't read line %d of file \"%s\"\n",line, filename) ;
748  "couldn't read file")) ;
749  }
750 
751  if ((h != I.getHeight())||( w != I.getWidth()))
752  {
753 
754  try
755  {
756  I.resize(h,w) ;
757  }
758  catch(...)
759  {
760  vpERROR_TRACE(" ") ;
761  throw ;
762  }
763  }
764 
765  // Read 255
766  err = fgets(str, vpMAX_LEN - 1, fd);
767  line++;
768  if (err == NULL) {
769  fclose (fd);
770  vpERROR_TRACE("couldn't read line %d of file \"%s\"\n",line, filename) ;
772  "couldn't read file")) ;
773  }
774 
775  ierr = sscanf(str, "%d", &is255);
776  if (ierr == EOF) {
777  fclose (fd);
778  vpERROR_TRACE("couldn't read line %d of file \"%s\"\n", line, filename) ;
780  "couldn't read file")) ;
781  }
782 
783  if (is255 != 255)
784  {
785  fclose (fd);
786  vpERROR_TRACE("MAX_VAL is not 255 in file \"%s\"\n", filename) ;
788  "error reading pfm file")) ;
789  }
790 
791  unsigned int nbyte = I.getHeight()*I.getWidth();
792  if (fread (I.bitmap, sizeof(float), nbyte, fd ) != nbyte)
793  {
794  fclose (fd);
795  vpERROR_TRACE("couldn't read %d bytes in file \"%s\"\n", nbyte, filename) ;
797  "error reading pfm file")) ;
798  }
799 
800  fclose (fd);
801 
802 
803 }
804 
805 
806 
823 void
825 {
826  FILE* fd = NULL; // File descriptor
827  int ierr;
828  int line;
829  int is255;
830  char* err ;
831  char str[vpMAX_LEN];
832  unsigned int w, h;
833 
834  // Test the filename
835  if (filename == '\0')
836  {
837  vpERROR_TRACE("no filename") ;
839  " no filename")) ;
840 
841  }
842 
843  // Open the filename
844  fd = fopen(filename, "rb");
845  if (fd == NULL)
846  {
847  vpERROR_TRACE("couldn't read file \"%s\"", filename) ;
849  "couldn't read file")) ;
850  }
851 
852  // Read the first line with magic number P5
853  line = 0;
854 
855  err = fgets(str, vpMAX_LEN - 1, fd);
856  line++;
857  if (err == NULL)
858  {
859  fclose (fd);
860  vpERROR_TRACE("couldn't read line %d of file \"%s\"\n", line, filename) ;
862  "couldn't read file")) ;
863  }
864 
865  if (strlen(str) < 3)
866  {
867  fclose (fd);
868  vpERROR_TRACE("\"%s\" is not a PGM file\n", filename) ;
870  "this is not a pgm file")) ;
871  }
872 
873  str[2] = '\0';
874  if (strcmp(str, "P5") != 0)
875  {
876  fclose (fd);
877  vpERROR_TRACE("\"%s\" is not a PGM file\n", filename) ;
879  "this is not a pgm file")) ;
880  }
881 
882  // Jump the possible comment, or empty line and read the following line
883  do {
884  err = fgets(str, vpMAX_LEN - 1, fd);
885  line++;
886  if (err == NULL) {
887  fprintf(stderr, "couldn't read line %d of file \"%s\"\n", line, filename);
888  fclose (fd);
889  }
890  } while ((str[0] == '#') || (str[0] == '\n'));
891 
892  // Extract image size
893  ierr = sscanf(str, "%d %d", &w, &h);
894  if(ierr == 1){// the norm allows to have the two values on two separated lines.
895  do {
896  err = fgets(str, vpMAX_LEN - 1, fd);
897  line++;
898  if (err == NULL) {
899  fprintf(stderr, "couldn't read line %d of file \"%s\"\n", line, filename);
900  fclose (fd);
901  }
902  } while ((str[0] == '#') || (str[0] == '\n'));
903  ierr = sscanf(str, "%d", &h);
904  }
905  if (ierr == EOF)
906  {
907  fclose (fd);
908  vpERROR_TRACE("couldn't read line %d of file \"%s\"\n",line, filename) ;
910  "couldn't read file")) ;
911  }
912 
913  if ((h != I.getHeight())||( w != I.getWidth()))
914  {
915 
916  try
917  {
918  I.resize(h,w) ;
919  }
920  catch(...)
921  {
922  vpERROR_TRACE(" ") ;
923  throw ;
924  }
925  }
926 
927  // Read 255
928  err = fgets(str, vpMAX_LEN - 1, fd);
929  line++;
930  if (err == NULL) {
931  fclose (fd);
932  vpERROR_TRACE("couldn't read line %d of file \"%s\"\n",line, filename) ;
934  "couldn't read file")) ;
935  }
936 
937  ierr = sscanf(str, "%d", &is255);
938  if (ierr == EOF) {
939  fclose (fd);
940  vpERROR_TRACE("couldn't read line %d of file \"%s\"\n", line, filename) ;
942  "couldn't read file")) ;
943  }
944 
945  if (is255 != 255)
946  {
947  fclose (fd);
948  vpERROR_TRACE("MAX_VAL is not 255 in file \"%s\"\n", filename) ;
950  "error reading pgm file")) ;
951  }
952 
953  unsigned int nbyte = I.getHeight()*I.getWidth();
954  if (fread (I.bitmap, sizeof(unsigned char), nbyte, fd ) != nbyte)
955  {
956  fclose (fd);
957  vpERROR_TRACE("couldn't read %d bytes in file \"%s\"\n", nbyte, filename) ;
959  "error reading pgm file")) ;
960  }
961 
962  fclose (fd);
963 
964 
965 }
966 
967 
986 void
987 vpImageIo::readPGM(vpImage<vpRGBa> &I, const char *filename)
988 {
989 
990  try
991  {
993 
994  vpImageIo::readPGM(Itmp, filename) ;
995 
996 
997  vpImageConvert::convert(Itmp, I) ;
998 
999  }
1000  catch(...)
1001  {
1002  vpERROR_TRACE(" ") ;
1003  throw ;
1004  }
1005 }
1006 
1007 
1008 //--------------------------------------------------------------------------
1009 // PPM
1010 //--------------------------------------------------------------------------
1011 
1028 void
1030 {
1031 
1032  try
1033  {
1034  vpImage<vpRGBa> Itmp ;
1035 
1036  vpImageIo::readPPM(Itmp, filename) ;
1037 
1038  vpImageConvert::convert(Itmp, I) ;
1039  }
1040  catch(...)
1041  {
1042  vpERROR_TRACE(" ") ;
1043  throw ;
1044  }
1045 }
1046 
1047 
1059 void
1060 vpImageIo::readPPM(vpImage<vpRGBa> &I, const char *filename)
1061 {
1062 
1063  FILE* fd = NULL; // File descriptor
1064  int ierr;
1065  int line;
1066  int is255;
1067  char* err ;
1068  char str[vpMAX_LEN];
1069  unsigned int w, h;
1070 
1071  // Test the filename
1072  if (filename == '\0')
1073  {
1074  vpERROR_TRACE("no filename") ;
1076  " no filename")) ;
1077 
1078  }
1079 
1080  // Open the filename
1081  fd = fopen(filename, "rb");
1082  if (fd == NULL)
1083  {
1084  vpERROR_TRACE("couldn't read file \"%s\"", filename) ;
1086  "couldn't read file")) ;
1087  }
1088 
1089  // Read the first line with magic number P5
1090  line = 0;
1091 
1092  err = fgets(str, vpMAX_LEN - 1, fd);
1093  line++;
1094  if (err == NULL)
1095  {
1096  fclose (fd);
1097  vpERROR_TRACE("couldn't read line %d of file \"%s\"\n", line, filename) ;
1099  "couldn't read file")) ;
1100  }
1101 
1102  if (strlen(str) < 3)
1103  {
1104  fclose (fd);
1105  vpERROR_TRACE("\"%s\" is not a PPM file\n", filename) ;
1107  "this is not a ppm file")) ;
1108  }
1109 
1110  str[2] = '\0';
1111  if (strcmp(str, "P6") != 0)
1112  {
1113  fclose (fd);
1114  vpERROR_TRACE("\"%s\" is not a PPM file\n", filename) ;
1116  "this is not a ppm file")) ;
1117  }
1118 
1119  // Jump the possible comment, or empty line and read the following line
1120  do {
1121  err = fgets(str, vpMAX_LEN - 1, fd);
1122  line++;
1123  if (err == NULL) {
1124  fprintf(stderr, "couldn't read line %d of file \"%s\"\n", line, filename);
1125  fclose (fd);
1126  }
1127  } while ((str[0] == '#') || (str[0] == '\n'));
1128 
1129  // Extract image size
1130  ierr = sscanf(str, "%d %d", &w, &h);
1131  if(ierr == 1){// the norm allows to have the two values on two separated lines.
1132  do {
1133  err = fgets(str, vpMAX_LEN - 1, fd);
1134  line++;
1135  if (err == NULL) {
1136  fprintf(stderr, "couldn't read line %d of file \"%s\"\n", line, filename);
1137  fclose (fd);
1138  }
1139  } while ((str[0] == '#') || (str[0] == '\n'));
1140  ierr = sscanf(str, "%d", &h);
1141  }
1142  if (ierr == EOF)
1143  {
1144  fclose (fd);
1145  vpERROR_TRACE("couldn't read line %d of file \"%s\"\n",line, filename) ;
1147  "couldn't read file")) ;
1148  }
1149 
1150  if ((h != I.getHeight())||( w != I.getWidth()))
1151  {
1152 
1153  try
1154  {
1155  I.resize(h,w) ;
1156  }
1157  catch(...)
1158  {
1159  vpERROR_TRACE(" ") ;
1160  throw ;
1161  }
1162  }
1163 
1164  // Read 255
1165  err = fgets(str, vpMAX_LEN - 1, fd);
1166  line++;
1167  if (err == NULL) {
1168  fclose (fd);
1169  vpERROR_TRACE("couldn't read line %d of file \"%s\"\n",line, filename) ;
1171  "couldn't read file")) ;
1172  }
1173 
1174  ierr = sscanf(str, "%d", &is255);
1175  if (ierr == EOF) {
1176  fclose (fd);
1177  vpERROR_TRACE("couldn't read line %d of file \"%s\"\n", line, filename) ;
1179  "couldn't read file")) ;
1180  }
1181 
1182  if (is255 != 255)
1183  {
1184  fclose (fd);
1185  vpERROR_TRACE("MAX_VAL is not 255 in file \"%s\"\n", filename) ;
1187  "error reading ppm file")) ;
1188  }
1189 
1190  for(unsigned int i=0;i<I.getHeight();i++)
1191  {
1192  for(unsigned int j=0;j<I.getWidth();j++)
1193  {
1194  vpRGBa v ;
1195  size_t res = fread(&v.R,sizeof(v.R),1,fd) ;
1196  res |= fread(&v.G,sizeof(v.G),1,fd) ;
1197  res |= fread(&v.B,sizeof(v.B),1,fd) ;
1198  if (res==0)
1199  {
1200  fclose (fd);
1201  vpERROR_TRACE("couldn't read bytes in file \"%s\"\n", filename) ;
1203  "error reading ppm file")) ;
1204  }
1205  I[i][j] = v ;
1206  }
1207  }
1208  fclose(fd) ;
1209 
1210 }
1211 
1222 void
1223 vpImageIo::writePPM(const vpImage<unsigned char> &I, const char *filename)
1224 {
1225 
1226  try
1227  {
1228  vpImage<vpRGBa> Itmp ;
1229 
1230  vpImageConvert::convert(I, Itmp) ;
1231 
1232  vpImageIo::writePPM(Itmp, filename) ;
1233  }
1234  catch(...)
1235  {
1236  vpERROR_TRACE(" ") ;
1237  throw ;
1238  }
1239 }
1240 
1241 
1249 void
1250 vpImageIo::writePPM(const vpImage<vpRGBa> &I, const char *filename)
1251 {
1252 
1253  FILE* f;
1254 
1255 
1256  // Test the filename
1257  if (filename == '\0') {
1258  vpERROR_TRACE("no filename\n");
1260  "no filename")) ;
1261  }
1262 
1263  f = fopen(filename, "wb");
1264 
1265  if (f == NULL) {
1266  vpERROR_TRACE("couldn't write to file \"%s\"\n", filename);
1268  "cannot write file")) ;
1269  }
1270 
1271 
1272 
1273  fprintf(f,"P6\n"); // Magic number
1274  fprintf(f,"%d %d\n", I.getWidth(), I.getHeight()); // Image size
1275  fprintf(f,"%d\n",255); // Max level
1276 
1277  for(unsigned int i=0;i<I.getHeight();i++)
1278  {
1279  for(unsigned int j=0;j<I.getWidth();j++)
1280  {
1281  vpRGBa P ;
1282  size_t res ;
1283  P = I[i][j] ;
1284  unsigned char tmp ;
1285  tmp = P.R ;
1286  res = fwrite(&tmp,sizeof(tmp),1,f) ;
1287  if (res==0)
1288  {
1289  fclose(f);
1290  vpERROR_TRACE("couldn't write file") ;
1292  "cannot write file")) ;
1293  }
1294  tmp = P.G;
1295  res = fwrite(&tmp,sizeof(tmp),1,f) ;
1296  if (res==0)
1297  {
1298  fclose(f);
1299  vpERROR_TRACE("couldn't write file") ;
1301  "cannot write file")) ;
1302  }
1303  tmp = P.B ;
1304  res = fwrite(&tmp,sizeof(tmp),1,f) ;
1305  if (res==0)
1306  {
1307  fclose(f);
1308  vpERROR_TRACE("couldn't write file") ;
1310  "cannot write file")) ;
1311  }
1312  }
1313  }
1314 
1315  fflush(f);
1316  fclose(f);
1317 }
1318 
1319 
1335 void
1336 vpImageIo::readPGM(vpImage<unsigned char> &I, const std::string filename)
1337 {
1338  vpImageIo::readPGM(I, filename.c_str());
1339 }
1340 
1356 void
1357 vpImageIo::readPGM(vpImage<vpRGBa> &I, const std::string filename)
1358 {
1359  vpImageIo::readPGM(I, filename.c_str());
1360 }
1361 
1371 void
1373  const std::string filename)
1374 {
1375  vpImageIo::writePGM(I, filename.c_str());
1376 }
1377 
1386 void
1387 vpImageIo::writePGM(const vpImage<short> &I, const std::string filename)
1388 {
1389 
1390  vpImageIo::writePGM(I, filename.c_str());
1391 }
1392 
1403 void
1404 vpImageIo::writePGM(const vpImage<vpRGBa> &I, const std::string filename)
1405 {
1406  vpImageIo::writePGM(I, filename.c_str());
1407 }
1408 
1409 //--------------------------------------------------------------------------
1410 // PPM
1411 //--------------------------------------------------------------------------
1412 
1429 void
1430 vpImageIo::readPPM(vpImage<unsigned char> &I, const std::string filename)
1431 {
1432  vpImageIo::readPPM(I, filename.c_str());
1433 }
1434 
1446 void
1447 vpImageIo::readPPM(vpImage<vpRGBa> &I, const std::string filename)
1448 {
1449  vpImageIo::readPPM(I, filename.c_str());
1450 }
1451 
1462 void
1463 vpImageIo::writePPM(const vpImage<unsigned char> &I, const std::string filename)
1464 {
1465  vpImageIo::writePPM(I, filename.c_str());
1466 }
1467 
1476 void
1477 vpImageIo::writePPM(const vpImage<vpRGBa> &I, const std::string filename)
1478 {
1479  vpImageIo::writePPM(I, filename.c_str());
1480 }
1481 
1482 
1483 //--------------------------------------------------------------------------
1484 // JPEG
1485 //--------------------------------------------------------------------------
1486 
1487 #if defined(VISP_HAVE_LIBJPEG)
1488 
1496 void
1497 vpImageIo::writeJPEG(const vpImage<unsigned char> &I, const char *filename)
1498 {
1499  struct jpeg_compress_struct cinfo;
1500  struct jpeg_error_mgr jerr;
1501  FILE *file;
1502 
1503  cinfo.err = jpeg_std_error(&jerr);
1504  jpeg_create_compress(&cinfo);
1505 
1506  // Test the filename
1507  if (filename == '\0') {
1508  vpERROR_TRACE("no filename\n");
1510  "no filename")) ;
1511  }
1512 
1513  file = fopen(filename, "wb");
1514 
1515  if (file == NULL) {
1516  vpERROR_TRACE("couldn't write file \"%s\"\n", filename);
1518  "cannot write file")) ;
1519  }
1520 
1521  unsigned int width = I.getWidth();
1522  unsigned int height = I.getHeight();
1523 
1524  jpeg_stdio_dest(&cinfo, file);
1525 
1526  cinfo.image_width = width;
1527  cinfo.image_height = height;
1528  cinfo.input_components = 1;
1529  cinfo.in_color_space = JCS_GRAYSCALE;
1530  jpeg_set_defaults(&cinfo);
1531 
1532  jpeg_start_compress(&cinfo,TRUE);
1533 
1534  unsigned char *line;
1535  line = new unsigned char[width];
1536  unsigned char* input = (unsigned char*)I.bitmap;
1537  while (cinfo.next_scanline < cinfo.image_height)
1538  {
1539  for (unsigned int i = 0; i < width; i++)
1540  {
1541  line[i] = *(input);
1542  input++;
1543  }
1544  jpeg_write_scanlines(&cinfo, &line, 1);
1545  }
1546 
1547  jpeg_finish_compress(&cinfo);
1548  jpeg_destroy_compress(&cinfo);
1549  delete [] line;
1550  fclose(file);
1551 }
1552 
1553 
1561 void
1562 vpImageIo::writeJPEG(const vpImage<unsigned char> &I, const std::string filename)
1563 {
1564  vpImageIo::writeJPEG(I, filename.c_str());
1565 }
1566 
1567 
1575 void
1576 vpImageIo::writeJPEG(const vpImage<vpRGBa> &I, const char *filename)
1577 {
1578  struct jpeg_compress_struct cinfo;
1579  struct jpeg_error_mgr jerr;
1580  FILE *file;
1581 
1582  cinfo.err = jpeg_std_error(&jerr);
1583  jpeg_create_compress(&cinfo);
1584 
1585  // Test the filename
1586  if (filename == '\0') {
1587  vpERROR_TRACE("no filename\n");
1589  "no filename")) ;
1590  }
1591 
1592  file = fopen(filename, "wb");
1593 
1594  if (file == NULL) {
1595  vpERROR_TRACE("couldn't write file \"%s\"\n", filename);
1597  "cannot write file")) ;
1598  }
1599 
1600  unsigned int width = I.getWidth();
1601  unsigned int height = I.getHeight();
1602 
1603  jpeg_stdio_dest(&cinfo, file);
1604 
1605  cinfo.image_width = width;
1606  cinfo.image_height = height;
1607  cinfo.input_components = 3;
1608  cinfo.in_color_space = JCS_RGB;
1609  jpeg_set_defaults(&cinfo);
1610 
1611  jpeg_start_compress(&cinfo,TRUE);
1612 
1613  unsigned char *line;
1614  line = new unsigned char[3*width];
1615  unsigned char* input = (unsigned char*)I.bitmap;
1616  while (cinfo.next_scanline < cinfo.image_height)
1617  {
1618  for (unsigned int i = 0; i < width; i++)
1619  {
1620  line[i*3] = *(input); input++;
1621  line[i*3+1] = *(input); input++;
1622  line[i*3+2] = *(input); input++;
1623  input++;
1624  }
1625  jpeg_write_scanlines(&cinfo, &line, 1);
1626  }
1627 
1628  jpeg_finish_compress(&cinfo);
1629  jpeg_destroy_compress(&cinfo);
1630  delete [] line;
1631  fclose(file);
1632 }
1633 
1634 
1642 void
1643 vpImageIo::writeJPEG(const vpImage<vpRGBa> &I, const std::string filename)
1644 {
1645  vpImageIo::writeJPEG(I, filename.c_str());
1646 }
1647 
1648 
1665 void
1667 {
1668  struct jpeg_decompress_struct cinfo;
1669  struct jpeg_error_mgr jerr;
1670  FILE *file;
1671 
1672  cinfo.err = jpeg_std_error(&jerr);
1673  jpeg_create_decompress(&cinfo);
1674 
1675  // Test the filename
1676  if (filename == '\0') {
1677  vpERROR_TRACE("no filename\n");
1679  "no filename")) ;
1680  }
1681 
1682  file = fopen(filename, "rb");
1683 
1684  if (file == NULL) {
1685  vpERROR_TRACE("couldn't read file \"%s\"\n", filename);
1687  "cannot read file")) ;
1688  }
1689 
1690  jpeg_stdio_src(&cinfo, file);
1691  jpeg_read_header(&cinfo, TRUE);
1692 
1693  unsigned int width = cinfo.image_width;
1694  unsigned int height = cinfo.image_height;
1695 
1696  if ( (width != I.getWidth()) || (height != I.getHeight()) )
1697  I.resize(height,width);
1698 
1699  jpeg_start_decompress(&cinfo);
1700 
1701  unsigned int rowbytes = cinfo.output_width * (unsigned int)(cinfo.output_components);
1702  JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray)
1703  ((j_common_ptr) &cinfo, JPOOL_IMAGE, rowbytes, 1);
1704 
1705  if (cinfo.out_color_space == JCS_RGB) {
1706  vpImage<vpRGBa> Ic(height,width);
1707  unsigned char* output = (unsigned char*)Ic.bitmap;
1708  while (cinfo.output_scanline<cinfo.output_height) {
1709  jpeg_read_scanlines(&cinfo,buffer,1);
1710  for (unsigned int i = 0; i < width; i++) {
1711  *(output++) = buffer[0][i*3];
1712  *(output++) = buffer[0][i*3+1];
1713  *(output++) = buffer[0][i*3+2];
1714  *(output++) = 0;
1715  }
1716  }
1717  vpImageConvert::convert(Ic,I) ;
1718  }
1719 
1720  else if (cinfo.out_color_space == JCS_GRAYSCALE)
1721  {
1722  unsigned int row;
1723  while (cinfo.output_scanline<cinfo.output_height)
1724  {
1725  row = cinfo.output_scanline;
1726  jpeg_read_scanlines(&cinfo,buffer,1);
1727  memcpy(I[row], buffer[0], rowbytes);
1728  }
1729  }
1730 
1731  jpeg_finish_decompress(&cinfo);
1732  jpeg_destroy_decompress(&cinfo);
1733  fclose(file);
1734 }
1735 
1736 
1753 void
1754 vpImageIo::readJPEG(vpImage<unsigned char> &I, const std::string filename)
1755 {
1756  vpImageIo::readJPEG(I, filename.c_str());
1757 }
1758 
1759 
1778 void
1779 vpImageIo::readJPEG(vpImage<vpRGBa> &I, const char *filename)
1780 {
1781  struct jpeg_decompress_struct cinfo;
1782  struct jpeg_error_mgr jerr;
1783  FILE *file;
1784 
1785  cinfo.err = jpeg_std_error(&jerr);
1786  jpeg_create_decompress(&cinfo);
1787 
1788  // Test the filename
1789  if (filename == '\0') {
1790  vpERROR_TRACE("no filename\n");
1792  "no filename")) ;
1793  }
1794 
1795  file = fopen(filename, "rb");
1796 
1797  if (file == NULL) {
1798  vpERROR_TRACE("couldn't read file \"%s\"\n", filename);
1800  "cannot read file")) ;
1801  }
1802 
1803  jpeg_stdio_src(&cinfo, file);
1804 
1805  jpeg_read_header(&cinfo, TRUE);
1806 
1807  unsigned int width = cinfo.image_width;
1808  unsigned int height = cinfo.image_height;
1809 
1810  if ( (width != I.getWidth()) || (height != I.getHeight()) )
1811  I.resize(height,width);
1812 
1813  jpeg_start_decompress(&cinfo);
1814 
1815  unsigned int rowbytes = cinfo.output_width * (unsigned int)(cinfo.output_components);
1816  JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray)
1817  ((j_common_ptr) &cinfo, JPOOL_IMAGE, rowbytes, 1);
1818 
1819  if (cinfo.out_color_space == JCS_RGB)
1820  {
1821  unsigned char* output = (unsigned char*)I.bitmap;
1822  while (cinfo.output_scanline<cinfo.output_height)
1823  {
1824  jpeg_read_scanlines(&cinfo,buffer,1);
1825  for (unsigned int i = 0; i < width; i++) {
1826  *(output++) = buffer[0][i*3];
1827  *(output++) = buffer[0][i*3+1];
1828  *(output++) = buffer[0][i*3+2];
1829  *(output++) = 0;
1830  }
1831  }
1832  }
1833 
1834  else if (cinfo.out_color_space == JCS_GRAYSCALE)
1835  {
1836  vpImage<unsigned char> Ig(height,width);
1837 
1838  unsigned int row;
1839  while (cinfo.output_scanline<cinfo.output_height)
1840  {
1841  row = cinfo.output_scanline;
1842  jpeg_read_scanlines(&cinfo,buffer,1);
1843  memcpy(Ig[row], buffer[0], rowbytes);
1844  }
1845 
1846  vpImageConvert::convert(Ig,I) ;
1847  }
1848 
1849  jpeg_finish_decompress(&cinfo);
1850  jpeg_destroy_decompress(&cinfo);
1851  fclose(file);
1852 }
1853 
1854 
1873 void
1874 vpImageIo::readJPEG(vpImage<vpRGBa> &I, const std::string filename)
1875 {
1876  vpImageIo::readJPEG(I, filename.c_str());
1877 }
1878 
1879 #elif defined(VISP_HAVE_OPENCV)
1880 
1888 void
1889 vpImageIo::writeJPEG(const vpImage<unsigned char> &I, const char *filename)
1890 {
1891  IplImage* Ip = NULL;
1892  vpImageConvert::convert(I, Ip);
1893 
1894  cvSaveImage(filename, Ip);
1895 
1896  cvReleaseImage(&Ip);
1897 }
1898 
1899 
1907 void
1908 vpImageIo::writeJPEG(const vpImage<unsigned char> &I, const std::string filename)
1909 {
1910  vpImageIo::writeJPEG(I, filename.c_str());
1911 }
1912 
1913 
1921 void
1922 vpImageIo::writeJPEG(const vpImage<vpRGBa> &I, const char *filename)
1923 {
1924  IplImage* Ip = NULL;
1925  vpImageConvert::convert(I, Ip);
1926 
1927  cvSaveImage(filename, Ip);
1928 
1929  cvReleaseImage(&Ip);
1930 }
1931 
1932 
1940 void
1941 vpImageIo::writeJPEG(const vpImage<vpRGBa> &I, const std::string filename)
1942 {
1943  vpImageIo::writeJPEG(I, filename.c_str());
1944 }
1945 
1946 
1963 void
1964 vpImageIo::readJPEG(vpImage<unsigned char> &I, const char *filename)
1965 {
1966  IplImage* Ip = NULL;
1967  Ip = cvLoadImage(filename, CV_LOAD_IMAGE_GRAYSCALE);
1968  if (Ip != NULL)
1969  vpImageConvert::convert(Ip, I);
1970  else
1972  "Can't read the image")) ;
1973  cvReleaseImage(&Ip);
1974 }
1975 
1976 
1993 void
1994 vpImageIo::readJPEG(vpImage<unsigned char> &I, const std::string filename)
1995 {
1996  vpImageIo::readJPEG(I, filename.c_str());
1997 }
1998 
1999 
2018 void
2019 vpImageIo::readJPEG(vpImage<vpRGBa> &I, const char *filename)
2020 {
2021  IplImage* Ip = NULL;
2022  Ip = cvLoadImage(filename, CV_LOAD_IMAGE_COLOR);
2023  if (Ip != NULL)
2024  vpImageConvert::convert(Ip, I);
2025  else
2027  "Can't read the image")) ;
2028  cvReleaseImage(&Ip);
2029 }
2030 
2031 
2050 void
2051 vpImageIo::readJPEG(vpImage<vpRGBa> &I, const std::string filename)
2052 {
2053  vpImageIo::readJPEG(I, filename.c_str());
2054 }
2055 
2056 #endif
2057 
2058 
2059 
2060 
2061 
2062 
2063 //--------------------------------------------------------------------------
2064 // PNG
2065 //--------------------------------------------------------------------------
2066 
2067 #if defined(VISP_HAVE_LIBPNG)
2068 
2076 void
2077 vpImageIo::writePNG(const vpImage<unsigned char> &I, const char *filename)
2078 {
2079  FILE *file;
2080 
2081  // Test the filename
2082  if (filename == '\0') {
2083  vpERROR_TRACE("no filename\n");
2085  "no filename")) ;
2086  }
2087 
2088  file = fopen(filename, "wb");
2089 
2090  if (file == NULL) {
2091  vpERROR_TRACE("couldn't write file \"%s\"\n", filename);
2093  "cannot write file")) ;
2094  }
2095 
2096  /* create a png info struct */
2097  png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL, NULL, NULL);
2098  if (!png_ptr)
2099  {
2100  fclose (file);
2101  }
2102 
2103  png_infop info_ptr = png_create_info_struct(png_ptr);
2104  if (!info_ptr)
2105  {
2106  fclose (file);
2107  png_destroy_write_struct (&png_ptr, &info_ptr);
2108  }
2109 
2110  /* initialize the setjmp for returning properly after a libpng error occured */
2111  if (setjmp (png_jmpbuf (png_ptr)))
2112  {
2113  fclose (file);
2114  png_destroy_write_struct (&png_ptr, &info_ptr);
2115  vpERROR_TRACE("Error during init_io\n");
2117  "PNG write error")) ;
2118  }
2119 
2120  /* setup libpng for using standard C fwrite() function with our FILE pointer */
2121  png_init_io (png_ptr, file);
2122 
2123  unsigned int width = I.getWidth();
2124  unsigned int height = I.getHeight();
2125  int bit_depth = 8;
2126  int color_type = PNG_COLOR_TYPE_GRAY;
2127  /* set some useful information from header */
2128 
2129  if (setjmp (png_jmpbuf (png_ptr)))
2130  {
2131  fclose (file);
2132  png_destroy_write_struct (&png_ptr, &info_ptr);
2133  vpERROR_TRACE("Error during write header\n");
2135  "PNG write error")) ;
2136  }
2137 
2138  png_set_IHDR(png_ptr, info_ptr, width, height,
2139  bit_depth, color_type, PNG_INTERLACE_NONE,
2140  PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
2141 
2142  png_write_info(png_ptr, info_ptr);
2143 
2144  png_bytep* row_ptrs = new png_bytep[height];
2145  for (unsigned int i = 0; i < height; i++)
2146  row_ptrs[i] = new png_byte[width];
2147 
2148  unsigned char* input = (unsigned char*)I.bitmap;
2149 
2150  for (unsigned int i = 0; i < height; i++)
2151  {
2152  png_byte* row = row_ptrs[i];
2153  for(unsigned int j = 0; j < width; j++)
2154  {
2155  row[j] = *(input);
2156  input++;
2157  }
2158  }
2159 
2160  if (setjmp (png_jmpbuf (png_ptr)))
2161  {
2162  fclose (file);
2163  png_destroy_write_struct (&png_ptr, &info_ptr);
2164  vpERROR_TRACE("Error during write image\n");
2166  "PNG write error")) ;
2167  }
2168 
2169  png_write_image(png_ptr, row_ptrs);
2170 
2171  if (setjmp (png_jmpbuf (png_ptr)))
2172  {
2173  fclose (file);
2174  png_destroy_write_struct (&png_ptr, &info_ptr);
2175  vpERROR_TRACE("Error during write end\n");
2177  "PNG write error")) ;
2178  }
2179 
2180  png_write_end(png_ptr, NULL);
2181 
2182  for(unsigned int j = 0; j < height; j++)
2183  delete[] /*(png_byte)*/row_ptrs[j];
2184 
2185  delete[] (png_bytep)row_ptrs;
2186 
2187  png_destroy_write_struct (&png_ptr, &info_ptr);
2188 
2189  fclose(file);
2190 }
2191 
2192 
2200 void
2201 vpImageIo::writePNG(const vpImage<unsigned char> &I, const std::string filename)
2202 {
2203  vpImageIo::writePNG(I, filename.c_str());
2204 }
2205 
2206 
2214 void
2215 vpImageIo::writePNG(const vpImage<vpRGBa> &I, const char *filename)
2216 {
2217  FILE *file;
2218 
2219  // Test the filename
2220  if (filename == '\0') {
2221  vpERROR_TRACE("no filename\n");
2223  "no filename")) ;
2224  }
2225 
2226  file = fopen(filename, "wb");
2227 
2228  if (file == NULL) {
2229  vpERROR_TRACE("couldn't write file \"%s\"\n", filename);
2231  "cannot write file")) ;
2232  }
2233 
2234  /* create a png info struct */
2235  png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL, NULL, NULL);
2236  if (!png_ptr)
2237  {
2238  fclose (file);
2239  }
2240 
2241  png_infop info_ptr = png_create_info_struct(png_ptr);
2242  if (!info_ptr)
2243  {
2244  fclose (file);
2245  png_destroy_write_struct (&png_ptr, &info_ptr);
2246  }
2247 
2248  /* initialize the setjmp for returning properly after a libpng error occured */
2249  if (setjmp (png_jmpbuf (png_ptr)))
2250  {
2251  fclose (file);
2252  png_destroy_write_struct (&png_ptr, &info_ptr);
2253  vpERROR_TRACE("Error during init_io\n");
2255  "PNG write error")) ;
2256  }
2257 
2258  /* setup libpng for using standard C fwrite() function with our FILE pointer */
2259  png_init_io (png_ptr, file);
2260 
2261  unsigned int width = I.getWidth();
2262  unsigned int height = I.getHeight();
2263  int bit_depth = 8;
2264  int color_type = PNG_COLOR_TYPE_RGB;
2265  /* set some useful information from header */
2266 
2267  if (setjmp (png_jmpbuf (png_ptr)))
2268  {
2269  fclose (file);
2270  png_destroy_write_struct (&png_ptr, &info_ptr);
2271  vpERROR_TRACE("Error during write header\n");
2273  "PNG write error")) ;
2274  }
2275 
2276  png_set_IHDR(png_ptr, info_ptr, width, height,
2277  bit_depth, color_type, PNG_INTERLACE_NONE,
2278  PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
2279 
2280  png_write_info(png_ptr, info_ptr);
2281 
2282  png_bytep* row_ptrs = new png_bytep[height];
2283  for (unsigned int i = 0; i < height; i++)
2284  row_ptrs[i] = new png_byte[3*width];
2285 
2286  unsigned char* input = (unsigned char*)I.bitmap;;
2287 
2288  for (unsigned int i = 0; i < height; i++)
2289  {
2290  png_byte* row = row_ptrs[i];
2291  for(unsigned int j = 0; j < width; j++)
2292  {
2293  row[3*j] = *(input);input++;
2294  row[3*j+1] = *(input);input++;
2295  row[3*j+2] = *(input);input++;
2296  input++;
2297  }
2298  }
2299 
2300  if (setjmp (png_jmpbuf (png_ptr)))
2301  {
2302  fclose (file);
2303  png_destroy_write_struct (&png_ptr, &info_ptr);
2304  vpERROR_TRACE("Error during write image\n");
2306  "PNG write error")) ;
2307  }
2308 
2309  png_write_image(png_ptr, row_ptrs);
2310 
2311  if (setjmp (png_jmpbuf (png_ptr)))
2312  {
2313  fclose (file);
2314  png_destroy_write_struct (&png_ptr, &info_ptr);
2315  vpERROR_TRACE("Error during write end\n");
2317  "PNG write error")) ;
2318  }
2319 
2320  png_write_end(png_ptr, NULL);
2321 
2322  for(unsigned int j = 0; j < height; j++)
2323  delete[] /*(png_byte)*/row_ptrs[j];
2324 
2325  delete[] (png_bytep)row_ptrs;
2326 
2327  png_destroy_write_struct (&png_ptr, &info_ptr);
2328 
2329  fclose(file);
2330 }
2331 
2332 
2340 void
2341 vpImageIo::writePNG(const vpImage<vpRGBa> &I, const std::string filename)
2342 {
2343  vpImageIo::writePNG(I, filename.c_str());
2344 }
2345 
2362 void
2364 {
2365  FILE *file;
2366  png_byte magic[8];
2367 
2368  // Test the filename
2369  if (filename == '\0') {
2370  vpERROR_TRACE("no filename\n");
2372  "no filename")) ;
2373  }
2374 
2375  file = fopen(filename, "rb");
2376 
2377  if (file == NULL) {
2378  vpERROR_TRACE("couldn't read file \"%s\"\n", filename);
2380  "cannot read file")) ;
2381  }
2382 
2383  /* read magic number */
2384  if (fread (magic, 1, sizeof (magic), file) != sizeof (magic))
2385  {
2386  fclose (file);
2387  vpERROR_TRACE("couldn't read magic number in file \"%s\"\n", filename) ;
2389  "error reading png file")) ;
2390  }
2391 
2392  /* check for valid magic number */
2393  if (png_sig_cmp (magic,0, sizeof (magic)))
2394  {
2395  fprintf (stderr, "error: \"%s\" is not a valid PNG image!\n",filename);
2396  fclose (file);
2398  "error reading png file")) ;
2399  }
2400 
2401  /* create a png read struct */
2402  //printf("version %s\n", PNG_LIBPNG_VER_STRING);
2403  png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
2404  if (png_ptr == NULL)
2405  {
2406  fprintf (stderr, "error: can't create a png read structure!\n");
2407  fclose (file);
2409  "error reading png file")) ;
2410  }
2411 
2412  /* create a png info struct */
2413  png_infop info_ptr = png_create_info_struct (png_ptr);
2414  if (info_ptr == NULL)
2415  {
2416  fprintf (stderr, "error: can't create a png info structure!\n");
2417  fclose (file);
2418  png_destroy_read_struct (&png_ptr, NULL, NULL);
2420  "error reading png file")) ;
2421  }
2422 
2423  /* initialize the setjmp for returning properly after a libpng error occured */
2424  if (setjmp (png_jmpbuf (png_ptr)))
2425  {
2426  fclose (file);
2427  png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
2428  vpERROR_TRACE("Error during init io\n");
2430  "PNG read error")) ;
2431  }
2432 
2433  /* setup libpng for using standard C fread() function with our FILE pointer */
2434  png_init_io (png_ptr, file);
2435 
2436  /* tell libpng that we have already read the magic number */
2437  png_set_sig_bytes (png_ptr, sizeof (magic));
2438 
2439  /* read png info */
2440  png_read_info (png_ptr, info_ptr);
2441 
2442  unsigned int width = png_get_image_width(png_ptr, info_ptr);
2443  unsigned int height = png_get_image_height(png_ptr, info_ptr);
2444 
2445  unsigned int bit_depth, channels, color_type;
2446  /* get some useful information from header */
2447  bit_depth = png_get_bit_depth (png_ptr, info_ptr);
2448  channels = png_get_channels(png_ptr, info_ptr);
2449  color_type = png_get_color_type (png_ptr, info_ptr);
2450 
2451  /* convert index color images to RGB images */
2452  if (color_type == PNG_COLOR_TYPE_PALETTE)
2453  png_set_palette_to_rgb (png_ptr);
2454 
2455  /* convert 1-2-4 bits grayscale images to 8 bits grayscale. */
2456  if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
2457  png_set_expand (png_ptr);
2458 
2459 // if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS))
2460 // png_set_tRNS_to_alpha (png_ptr);
2461 
2462  if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2463  png_set_strip_alpha(png_ptr);
2464 
2465  if (bit_depth == 16)
2466  png_set_strip_16 (png_ptr);
2467  else if (bit_depth < 8)
2468  png_set_packing (png_ptr);
2469 
2470  /* update info structure to apply transformations */
2471  png_read_update_info (png_ptr, info_ptr);
2472 
2473  channels = png_get_channels(png_ptr, info_ptr);
2474 
2475  if ( (width != I.getWidth()) || (height != I.getHeight()) )
2476  I.resize(height,width);
2477 
2478  png_bytep* rowPtrs = new png_bytep[height];
2479 
2480  unsigned int stride = width * bit_depth * channels / 8;
2481  unsigned char* data = new unsigned char[stride * height];
2482 
2483  for (unsigned int i =0; i < height; i++)
2484  rowPtrs[i] = (png_bytep)data + (i * stride);
2485 
2486  png_read_image(png_ptr, rowPtrs);
2487 
2488  vpImage<vpRGBa> Ic(height,width);
2489  unsigned char* output;
2490 
2491  switch (channels)
2492  {
2493  case 1:
2494  output = (unsigned char*)I.bitmap;
2495  for (unsigned int i = 0; i < width*height; i++)
2496  {
2497  *(output++) = data[i];
2498  }
2499  break;
2500  case 2:
2501  output = (unsigned char*)I.bitmap;
2502  for (unsigned int i = 0; i < width*height; i++)
2503  {
2504  *(output++) = data[i*2];
2505  }
2506  break;
2507  case 3:
2508 
2509  output = (unsigned char*)Ic.bitmap;
2510  for (unsigned int i = 0; i < width*height; i++)
2511  {
2512  *(output++) = data[i*3];
2513  *(output++) = data[i*3+1];
2514  *(output++) = data[i*3+2];
2515  *(output++) = 0;
2516  }
2517  vpImageConvert::convert(Ic,I) ;
2518  break;
2519  case 4:
2520  output = (unsigned char*)Ic.bitmap;
2521  for (unsigned int i = 0; i < width*height; i++)
2522  {
2523  *(output++) = data[i*4];
2524  *(output++) = data[i*4+1];
2525  *(output++) = data[i*4+2];
2526  *(output++) = data[i*4+3];
2527  }
2528  vpImageConvert::convert(Ic,I) ;
2529  break;
2530  }
2531 
2532  delete [] (png_bytep)rowPtrs;
2533  delete [] data;
2534  png_read_end (png_ptr, NULL);
2535  png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
2536  fclose(file);
2537 }
2538 
2539 
2556 void
2557 vpImageIo::readPNG(vpImage<unsigned char> &I, const std::string filename)
2558 {
2559  vpImageIo::readPNG(I, filename.c_str());
2560 }
2561 
2562 
2581 void
2582 vpImageIo::readPNG(vpImage<vpRGBa> &I, const char *filename)
2583 {
2584  FILE *file;
2585  png_byte magic[8];
2586 
2587  // Test the filename
2588  if (filename == '\0') {
2589  vpERROR_TRACE("no filename\n");
2591  "no filename")) ;
2592  }
2593 
2594  file = fopen(filename, "rb");
2595 
2596  if (file == NULL) {
2597  vpERROR_TRACE("couldn't read file \"%s\"\n", filename);
2599  "cannot read file")) ;
2600  }
2601 
2602  /* read magic number */
2603  if (fread (magic, 1, sizeof (magic), file) != sizeof (magic))
2604  {
2605  fclose (file);
2606  vpERROR_TRACE("couldn't read magic number in file \"%s\"\n", filename) ;
2608  "error reading pgm file")) ;
2609  }
2610 
2611  /* check for valid magic number */
2612  if (png_sig_cmp (magic,0, sizeof (magic)))
2613  {
2614  fprintf (stderr, "error: \"%s\" is not a valid PNG image!\n",filename);
2615  fclose (file);
2616  }
2617 
2618  /* create a png read struct */
2619  png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
2620  if (!png_ptr)
2621  {
2622  fclose (file);
2623  }
2624 
2625  /* create a png info struct */
2626  png_infop info_ptr = png_create_info_struct (png_ptr);
2627  if (!info_ptr)
2628  {
2629  fclose (file);
2630  png_destroy_read_struct (&png_ptr, NULL, NULL);
2631  }
2632 
2633  /* initialize the setjmp for returning properly after a libpng error occured */
2634  if (setjmp (png_jmpbuf (png_ptr)))
2635  {
2636  fclose (file);
2637  png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
2638  vpERROR_TRACE("Error during init io\n");
2640  "PNG read error")) ;
2641  }
2642 
2643  /* setup libpng for using standard C fread() function with our FILE pointer */
2644  png_init_io (png_ptr, file);
2645 
2646  /* tell libpng that we have already read the magic number */
2647  png_set_sig_bytes (png_ptr, sizeof (magic));
2648 
2649  /* read png info */
2650  png_read_info (png_ptr, info_ptr);
2651 
2652  unsigned int width = png_get_image_width(png_ptr, info_ptr);
2653  unsigned int height = png_get_image_height(png_ptr, info_ptr);
2654 
2655  unsigned int bit_depth, channels, color_type;
2656  /* get some useful information from header */
2657  bit_depth = png_get_bit_depth (png_ptr, info_ptr);
2658  channels = png_get_channels(png_ptr, info_ptr);
2659  color_type = png_get_color_type (png_ptr, info_ptr);
2660 
2661  /* convert index color images to RGB images */
2662  if (color_type == PNG_COLOR_TYPE_PALETTE)
2663  png_set_palette_to_rgb (png_ptr);
2664 
2665  /* convert 1-2-4 bits grayscale images to 8 bits grayscale. */
2666  if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
2667  png_set_expand (png_ptr);
2668 
2669 // if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS))
2670 // png_set_tRNS_to_alpha (png_ptr);
2671 
2672  if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2673  png_set_strip_alpha(png_ptr);
2674 
2675  if (bit_depth == 16)
2676  png_set_strip_16 (png_ptr);
2677  else if (bit_depth < 8)
2678  png_set_packing (png_ptr);
2679 
2680  /* update info structure to apply transformations */
2681  png_read_update_info (png_ptr, info_ptr);
2682 
2683  channels = png_get_channels(png_ptr, info_ptr);
2684 
2685  if ( (width != I.getWidth()) || (height != I.getHeight()) )
2686  I.resize(height,width);
2687 
2688  png_bytep* rowPtrs = new png_bytep[height];
2689 
2690  unsigned int stride = width * bit_depth * channels / 8;
2691  unsigned char* data = new unsigned char[stride * height];
2692 
2693 
2694  for (unsigned int i =0; i < height; i++)
2695  rowPtrs[i] = (png_bytep)data + (i * stride);
2696 
2697  png_read_image(png_ptr, rowPtrs);
2698 
2699  vpImage<unsigned char> Ig(height,width);
2700  unsigned char* output;
2701 
2702  switch (channels)
2703  {
2704  case 1:
2705  output = (unsigned char*)Ig.bitmap;
2706  for (unsigned int i = 0; i < width*height; i++)
2707  {
2708  *(output++) = data[i];
2709  }
2710  vpImageConvert::convert(Ig,I) ;
2711  break;
2712  case 2:
2713  output = (unsigned char*)Ig.bitmap;
2714  for (unsigned int i = 0; i < width*height; i++)
2715  {
2716  *(output++) = data[i*2];
2717  }
2718  vpImageConvert::convert(Ig,I) ;
2719  break;
2720  case 3:
2721 
2722  output = (unsigned char*)I.bitmap;
2723  for (unsigned int i = 0; i < width*height; i++)
2724  {
2725  *(output++) = data[i*3];
2726  *(output++) = data[i*3+1];
2727  *(output++) = data[i*3+2];
2728  *(output++) = 0;
2729  }
2730  break;
2731  case 4:
2732  output = (unsigned char*)I.bitmap;
2733  for (unsigned int i = 0; i < width*height; i++)
2734  {
2735  *(output++) = data[i*4];
2736  *(output++) = data[i*4+1];
2737  *(output++) = data[i*4+2];
2738  *(output++) = data[i*4+3];
2739  }
2740  break;
2741  }
2742 
2743  delete [] (png_bytep)rowPtrs;
2744  delete [] data;
2745  png_read_end (png_ptr, NULL);
2746  png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
2747  fclose(file);
2748 }
2749 
2750 
2769 void
2770 vpImageIo::readPNG(vpImage<vpRGBa> &I, const std::string filename)
2771 {
2772  vpImageIo::readPNG(I, filename.c_str());
2773 }
2774 
2775 #elif defined(VISP_HAVE_OPENCV)
2776 
2784 void
2785 vpImageIo::writePNG(const vpImage<unsigned char> &I, const char *filename)
2786 {
2787  IplImage* Ip = NULL;
2788  vpImageConvert::convert(I, Ip);
2789 
2790  cvSaveImage(filename, Ip);
2791 
2792  cvReleaseImage(&Ip);
2793 }
2794 
2795 
2803 void
2804 vpImageIo::writePNG(const vpImage<unsigned char> &I, const std::string filename)
2805 {
2806  vpImageIo::writeJPEG(I, filename.c_str());
2807 }
2808 
2809 
2817 void
2818 vpImageIo::writePNG(const vpImage<vpRGBa> &I, const char *filename)
2819 {
2820  IplImage* Ip = NULL;
2821  vpImageConvert::convert(I, Ip);
2822 
2823  cvSaveImage(filename, Ip);
2824 
2825  cvReleaseImage(&Ip);
2826 }
2827 
2828 
2836 void
2837 vpImageIo::writePNG(const vpImage<vpRGBa> &I, const std::string filename)
2838 {
2839  vpImageIo::writePNG(I, filename.c_str());
2840 }
2841 
2842 
2859 void
2860 vpImageIo::readPNG(vpImage<unsigned char> &I, const char *filename)
2861 {
2862  IplImage* Ip = NULL;
2863  Ip = cvLoadImage(filename, CV_LOAD_IMAGE_GRAYSCALE);
2864  if (Ip != NULL)
2865  vpImageConvert::convert(Ip, I);
2866  else
2868  "Can't read the image")) ;
2869  cvReleaseImage(&Ip);
2870 }
2871 
2872 
2889 void
2890 vpImageIo::readPNG(vpImage<unsigned char> &I, const std::string filename)
2891 {
2892  vpImageIo::readPNG(I, filename.c_str());
2893 }
2894 
2895 
2914 void
2915 vpImageIo::readPNG(vpImage<vpRGBa> &I, const char *filename)
2916 {
2917  IplImage* Ip = NULL;
2918  Ip = cvLoadImage(filename, CV_LOAD_IMAGE_COLOR);
2919  if (Ip != NULL)
2920  vpImageConvert::convert(Ip, I);
2921  else
2923  "Can't read the image")) ;
2924  cvReleaseImage(&Ip);
2925 }
2926 
2927 
2946 void
2947 vpImageIo::readPNG(vpImage<vpRGBa> &I, const std::string filename)
2948 {
2949  vpImageIo::readPNG(I, filename.c_str());
2950 }
2951 
2952 #endif
2953 
2954 
2955 /*
2956  * Local variables:
2957  * c-basic-offset: 2
2958  * End:
2959  */
2960 
2961 
2962 
2963 /*
2964  * Local variables:
2965  * c-basic-offset: 2
2966  * End:
2967  */
static void write(const vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:355
static void writeJPEG(const vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:1497
static void readPGM(vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:824
unsigned int getWidth() const
Definition: vpImage.h:154
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
#define vpERROR_TRACE
Definition: vpDebug.h:379
unsigned char B
Blue component.
Definition: vpRGBa.h:155
Type * bitmap
points toward the bitmap
Definition: vpImage.h:115
#define vpCERROR
Definition: vpDebug.h:354
void resize(const unsigned int height, const unsigned int width)
set the size of the image
Definition: vpImage.h:530
static void writePGM(const vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:522
static void writePNG(const vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:2077
static void writePFM(const vpImage< float > &I, const char *filename)
Definition: vpImageIo.cpp:466
Error that can be emited by the vpImage class and its derivates.
static void readPFM(vpImage< float > &I, const char *filename)
Definition: vpImageIo.cpp:662
unsigned char G
Green component.
Definition: vpRGBa.h:154
Class that defines a RGB 32 bits structure.
Definition: vpRGBa.h:68
static void readPNG(vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:2363
static void readJPEG(vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:1666
unsigned char R
Red component.
Definition: vpRGBa.h:153
static void readPPM(vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:1029
unsigned int getHeight() const
Definition: vpImage.h:145
static void writePPM(const vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:1223
static void read(vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:239