ViSP
 All Classes Functions Variables Enumerations Enumerator Friends Groups Pages
vpImageIo.cpp
1 /****************************************************************************
2  *
3  * $Id: vpImageIo.cpp 4056 2013-01-05 13:04:42Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 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  vpERROR_TRACE("Error during png_create_write_struct()\n");
2103  "PNG write error")) ;
2104  }
2105 
2106  png_infop info_ptr = png_create_info_struct(png_ptr);
2107  if (!info_ptr)
2108  {
2109  fclose (file);
2110  png_destroy_write_struct (&png_ptr, NULL);
2111  vpERROR_TRACE("Error during png_create_info_struct()\n");
2113  "PNG write error")) ;
2114  }
2115 
2116  /* initialize the setjmp for returning properly after a libpng error occured */
2117  if (setjmp (png_jmpbuf (png_ptr)))
2118  {
2119  fclose (file);
2120  png_destroy_write_struct (&png_ptr, &info_ptr);
2121  vpERROR_TRACE("Error during init_io\n");
2123  "PNG write error")) ;
2124  }
2125 
2126  /* setup libpng for using standard C fwrite() function with our FILE pointer */
2127  png_init_io (png_ptr, file);
2128 
2129  unsigned int width = I.getWidth();
2130  unsigned int height = I.getHeight();
2131  int bit_depth = 8;
2132  int color_type = PNG_COLOR_TYPE_GRAY;
2133  /* set some useful information from header */
2134 
2135  if (setjmp (png_jmpbuf (png_ptr)))
2136  {
2137  fclose (file);
2138  png_destroy_write_struct (&png_ptr, &info_ptr);
2139  vpERROR_TRACE("Error during write header\n");
2141  "PNG write error")) ;
2142  }
2143 
2144  png_set_IHDR(png_ptr, info_ptr, width, height,
2145  bit_depth, color_type, PNG_INTERLACE_NONE,
2146  PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
2147 
2148  png_write_info(png_ptr, info_ptr);
2149 
2150  png_bytep* row_ptrs = new png_bytep[height];
2151  for (unsigned int i = 0; i < height; i++)
2152  row_ptrs[i] = new png_byte[width];
2153 
2154  unsigned char* input = (unsigned char*)I.bitmap;
2155 
2156  for (unsigned int i = 0; i < height; i++)
2157  {
2158  png_byte* row = row_ptrs[i];
2159  for(unsigned int j = 0; j < width; j++)
2160  {
2161  row[j] = *(input);
2162  input++;
2163  }
2164  }
2165 
2166  if (setjmp (png_jmpbuf (png_ptr)))
2167  {
2168  fclose (file);
2169  png_destroy_write_struct (&png_ptr, &info_ptr);
2170  for(unsigned int j = 0; j < height; j++)
2171  delete[] row_ptrs[j];
2172 
2173  delete[] row_ptrs;
2174  vpERROR_TRACE("Error during write image\n");
2176  "PNG write error")) ;
2177  }
2178 
2179  png_write_image(png_ptr, row_ptrs);
2180 
2181  if (setjmp (png_jmpbuf (png_ptr)))
2182  {
2183  fclose (file);
2184  png_destroy_write_struct (&png_ptr, &info_ptr);
2185  for(unsigned int j = 0; j < height; j++)
2186  delete[] row_ptrs[j];
2187 
2188  delete[] row_ptrs;
2189  vpERROR_TRACE("Error during write end\n");
2191  "PNG write error")) ;
2192  }
2193 
2194  png_write_end(png_ptr, NULL);
2195 
2196  for(unsigned int j = 0; j < height; j++)
2197  delete[] row_ptrs[j];
2198 
2199  delete[] row_ptrs;
2200 
2201  png_destroy_write_struct (&png_ptr, &info_ptr);
2202 
2203  fclose(file);
2204 }
2205 
2206 
2214 void
2215 vpImageIo::writePNG(const vpImage<unsigned char> &I, const std::string filename)
2216 {
2217  vpImageIo::writePNG(I, filename.c_str());
2218 }
2219 
2220 
2228 void
2229 vpImageIo::writePNG(const vpImage<vpRGBa> &I, const char *filename)
2230 {
2231  FILE *file;
2232 
2233  // Test the filename
2234  if (filename == '\0') {
2235  vpERROR_TRACE("no filename\n");
2237  "no filename")) ;
2238  }
2239 
2240  file = fopen(filename, "wb");
2241 
2242  if (file == NULL) {
2243  vpERROR_TRACE("couldn't write file \"%s\"\n", filename);
2245  "cannot write file")) ;
2246  }
2247 
2248  /* create a png info struct */
2249  png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL, NULL, NULL);
2250  if (!png_ptr)
2251  {
2252  fclose (file);
2253  vpERROR_TRACE("Error during png_create_write_struct()\n");
2255  "PNG write error")) ;
2256  }
2257 
2258  png_infop info_ptr = png_create_info_struct(png_ptr);
2259  if (!info_ptr)
2260  {
2261  fclose (file);
2262  png_destroy_write_struct (&png_ptr, NULL);
2263  vpERROR_TRACE("Error during png_create_info_struct()\n");
2265  "PNG write error")) ;
2266  }
2267 
2268  /* initialize the setjmp for returning properly after a libpng error occured */
2269  if (setjmp (png_jmpbuf (png_ptr)))
2270  {
2271  fclose (file);
2272  png_destroy_write_struct (&png_ptr, &info_ptr);
2273  vpERROR_TRACE("Error during init_io\n");
2275  "PNG write error")) ;
2276  }
2277 
2278  /* setup libpng for using standard C fwrite() function with our FILE pointer */
2279  png_init_io (png_ptr, file);
2280 
2281  unsigned int width = I.getWidth();
2282  unsigned int height = I.getHeight();
2283  int bit_depth = 8;
2284  int color_type = PNG_COLOR_TYPE_RGB;
2285  /* set some useful information from header */
2286 
2287  if (setjmp (png_jmpbuf (png_ptr)))
2288  {
2289  fclose (file);
2290  png_destroy_write_struct (&png_ptr, &info_ptr);
2291  vpERROR_TRACE("Error during write header\n");
2293  "PNG write error")) ;
2294  }
2295 
2296  png_set_IHDR(png_ptr, info_ptr, width, height,
2297  bit_depth, color_type, PNG_INTERLACE_NONE,
2298  PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
2299 
2300  png_write_info(png_ptr, info_ptr);
2301 
2302  png_bytep* row_ptrs = new png_bytep[height];
2303  for (unsigned int i = 0; i < height; i++)
2304  row_ptrs[i] = new png_byte[3*width];
2305 
2306  unsigned char* input = (unsigned char*)I.bitmap;;
2307 
2308  for (unsigned int i = 0; i < height; i++)
2309  {
2310  png_byte* row = row_ptrs[i];
2311  for(unsigned int j = 0; j < width; j++)
2312  {
2313  row[3*j] = *(input);input++;
2314  row[3*j+1] = *(input);input++;
2315  row[3*j+2] = *(input);input++;
2316  input++;
2317  }
2318  }
2319 
2320  if (setjmp (png_jmpbuf (png_ptr)))
2321  {
2322  fclose (file);
2323  png_destroy_write_struct (&png_ptr, &info_ptr);
2324  for(unsigned int j = 0; j < height; j++)
2325  delete[] row_ptrs[j];
2326 
2327  delete[] row_ptrs;
2328  vpERROR_TRACE("Error during write image\n");
2330  "PNG write error")) ;
2331  }
2332 
2333  png_write_image(png_ptr, row_ptrs);
2334 
2335  if (setjmp (png_jmpbuf (png_ptr)))
2336  {
2337  fclose (file);
2338  png_destroy_write_struct (&png_ptr, &info_ptr);
2339  for(unsigned int j = 0; j < height; j++)
2340  delete[] row_ptrs[j];
2341 
2342  delete[] row_ptrs;
2343  vpERROR_TRACE("Error during write end\n");
2345  "PNG write error")) ;
2346  }
2347 
2348  png_write_end(png_ptr, NULL);
2349 
2350  for(unsigned int j = 0; j < height; j++)
2351  delete[] row_ptrs[j];
2352 
2353  delete[] row_ptrs;
2354 
2355  png_destroy_write_struct (&png_ptr, &info_ptr);
2356 
2357  fclose(file);
2358 }
2359 
2360 
2368 void
2369 vpImageIo::writePNG(const vpImage<vpRGBa> &I, const std::string filename)
2370 {
2371  vpImageIo::writePNG(I, filename.c_str());
2372 }
2373 
2390 void
2392 {
2393  FILE *file;
2394  png_byte magic[8];
2395 
2396  // Test the filename
2397  if (filename == '\0') {
2398  vpERROR_TRACE("no filename\n");
2400  "no filename")) ;
2401  }
2402 
2403  file = fopen(filename, "rb");
2404 
2405  if (file == NULL) {
2406  vpERROR_TRACE("couldn't read file \"%s\"\n", filename);
2408  "cannot read file")) ;
2409  }
2410 
2411  /* read magic number */
2412  if (fread (magic, 1, sizeof (magic), file) != sizeof (magic))
2413  {
2414  fclose (file);
2415  vpERROR_TRACE("couldn't read magic number in file \"%s\"\n", filename) ;
2417  "error reading png file")) ;
2418  }
2419 
2420  /* check for valid magic number */
2421  if (png_sig_cmp (magic,0, sizeof (magic)))
2422  {
2423  fprintf (stderr, "error: \"%s\" is not a valid PNG image!\n",filename);
2424  fclose (file);
2426  "error reading png file")) ;
2427  }
2428 
2429  /* create a png read struct */
2430  //printf("version %s\n", PNG_LIBPNG_VER_STRING);
2431  png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
2432  if (png_ptr == NULL)
2433  {
2434  fprintf (stderr, "error: can't create a png read structure!\n");
2435  fclose (file);
2437  "error reading png file")) ;
2438  }
2439 
2440  /* create a png info struct */
2441  png_infop info_ptr = png_create_info_struct (png_ptr);
2442  if (info_ptr == NULL)
2443  {
2444  fprintf (stderr, "error: can't create a png info structure!\n");
2445  fclose (file);
2446  png_destroy_read_struct (&png_ptr, NULL, NULL);
2448  "error reading png file")) ;
2449  }
2450 
2451  /* initialize the setjmp for returning properly after a libpng error occured */
2452  if (setjmp (png_jmpbuf (png_ptr)))
2453  {
2454  fclose (file);
2455  png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
2456  vpERROR_TRACE("Error during init io\n");
2458  "PNG read error")) ;
2459  }
2460 
2461  /* setup libpng for using standard C fread() function with our FILE pointer */
2462  png_init_io (png_ptr, file);
2463 
2464  /* tell libpng that we have already read the magic number */
2465  png_set_sig_bytes (png_ptr, sizeof (magic));
2466 
2467  /* read png info */
2468  png_read_info (png_ptr, info_ptr);
2469 
2470  unsigned int width = png_get_image_width(png_ptr, info_ptr);
2471  unsigned int height = png_get_image_height(png_ptr, info_ptr);
2472 
2473  unsigned int bit_depth, channels, color_type;
2474  /* get some useful information from header */
2475  bit_depth = png_get_bit_depth (png_ptr, info_ptr);
2476  channels = png_get_channels(png_ptr, info_ptr);
2477  color_type = png_get_color_type (png_ptr, info_ptr);
2478 
2479  /* convert index color images to RGB images */
2480  if (color_type == PNG_COLOR_TYPE_PALETTE)
2481  png_set_palette_to_rgb (png_ptr);
2482 
2483  /* convert 1-2-4 bits grayscale images to 8 bits grayscale. */
2484  if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
2485  png_set_expand (png_ptr);
2486 
2487 // if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS))
2488 // png_set_tRNS_to_alpha (png_ptr);
2489 
2490  if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2491  png_set_strip_alpha(png_ptr);
2492 
2493  if (bit_depth == 16)
2494  png_set_strip_16 (png_ptr);
2495  else if (bit_depth < 8)
2496  png_set_packing (png_ptr);
2497 
2498  /* update info structure to apply transformations */
2499  png_read_update_info (png_ptr, info_ptr);
2500 
2501  channels = png_get_channels(png_ptr, info_ptr);
2502 
2503  if ( (width != I.getWidth()) || (height != I.getHeight()) )
2504  I.resize(height,width);
2505 
2506  png_bytep* rowPtrs = new png_bytep[height];
2507 
2508  unsigned int stride = width * bit_depth * channels / 8;
2509  unsigned char* data = new unsigned char[stride * height];
2510 
2511  for (unsigned int i =0; i < height; i++)
2512  rowPtrs[i] = (png_bytep)data + (i * stride);
2513 
2514  png_read_image(png_ptr, rowPtrs);
2515 
2516  vpImage<vpRGBa> Ic(height,width);
2517  unsigned char* output;
2518 
2519  switch (channels)
2520  {
2521  case 1:
2522  output = (unsigned char*)I.bitmap;
2523  for (unsigned int i = 0; i < width*height; i++)
2524  {
2525  *(output++) = data[i];
2526  }
2527  break;
2528  case 2:
2529  output = (unsigned char*)I.bitmap;
2530  for (unsigned int i = 0; i < width*height; i++)
2531  {
2532  *(output++) = data[i*2];
2533  }
2534  break;
2535  case 3:
2536 
2537  output = (unsigned char*)Ic.bitmap;
2538  for (unsigned int i = 0; i < width*height; i++)
2539  {
2540  *(output++) = data[i*3];
2541  *(output++) = data[i*3+1];
2542  *(output++) = data[i*3+2];
2543  *(output++) = 0;
2544  }
2545  vpImageConvert::convert(Ic,I) ;
2546  break;
2547  case 4:
2548  output = (unsigned char*)Ic.bitmap;
2549  for (unsigned int i = 0; i < width*height; i++)
2550  {
2551  *(output++) = data[i*4];
2552  *(output++) = data[i*4+1];
2553  *(output++) = data[i*4+2];
2554  *(output++) = data[i*4+3];
2555  }
2556  vpImageConvert::convert(Ic,I) ;
2557  break;
2558  }
2559 
2560  delete [] (png_bytep)rowPtrs;
2561  delete [] data;
2562  png_read_end (png_ptr, NULL);
2563  png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
2564  fclose(file);
2565 }
2566 
2567 
2584 void
2585 vpImageIo::readPNG(vpImage<unsigned char> &I, const std::string filename)
2586 {
2587  vpImageIo::readPNG(I, filename.c_str());
2588 }
2589 
2590 
2609 void
2610 vpImageIo::readPNG(vpImage<vpRGBa> &I, const char *filename)
2611 {
2612  FILE *file;
2613  png_byte magic[8];
2614 
2615  // Test the filename
2616  if (filename == '\0') {
2617  vpERROR_TRACE("no filename\n");
2619  "no filename")) ;
2620  }
2621 
2622  file = fopen(filename, "rb");
2623 
2624  if (file == NULL) {
2625  vpERROR_TRACE("couldn't read file \"%s\"\n", filename);
2627  "cannot read file")) ;
2628  }
2629 
2630  /* read magic number */
2631  if (fread (magic, 1, sizeof (magic), file) != sizeof (magic))
2632  {
2633  fclose (file);
2634  vpERROR_TRACE("couldn't read magic number in file \"%s\"\n", filename) ;
2636  "error reading pgm file")) ;
2637  }
2638 
2639  /* check for valid magic number */
2640  if (png_sig_cmp (magic,0, sizeof (magic)))
2641  {
2642  fclose (file);
2643  vpERROR_TRACE("error: \"%s\" is not a valid PNG image!\n",filename);
2645  "PNG read error")) ;
2646  }
2647 
2648  /* create a png read struct */
2649  png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
2650  if (!png_ptr)
2651  {
2652  fclose (file);
2653  vpERROR_TRACE("Error during png_create_read_struct()\n");
2655  "PNG read error")) ;
2656  }
2657 
2658  /* create a png info struct */
2659  png_infop info_ptr = png_create_info_struct (png_ptr);
2660  if (!info_ptr)
2661  {
2662  fclose (file);
2663  png_destroy_read_struct (&png_ptr, NULL, NULL);
2664  vpERROR_TRACE("Error during png_create_info_struct()\n");
2666  "PNG read error")) ;
2667  }
2668 
2669  /* initialize the setjmp for returning properly after a libpng error occured */
2670  if (setjmp (png_jmpbuf (png_ptr)))
2671  {
2672  fclose (file);
2673  png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
2674  vpERROR_TRACE("Error during init io\n");
2676  "PNG read error")) ;
2677  }
2678 
2679  /* setup libpng for using standard C fread() function with our FILE pointer */
2680  png_init_io (png_ptr, file);
2681 
2682  /* tell libpng that we have already read the magic number */
2683  png_set_sig_bytes (png_ptr, sizeof (magic));
2684 
2685  /* read png info */
2686  png_read_info (png_ptr, info_ptr);
2687 
2688  unsigned int width = png_get_image_width(png_ptr, info_ptr);
2689  unsigned int height = png_get_image_height(png_ptr, info_ptr);
2690 
2691  unsigned int bit_depth, channels, color_type;
2692  /* get some useful information from header */
2693  bit_depth = png_get_bit_depth (png_ptr, info_ptr);
2694  channels = png_get_channels(png_ptr, info_ptr);
2695  color_type = png_get_color_type (png_ptr, info_ptr);
2696 
2697  /* convert index color images to RGB images */
2698  if (color_type == PNG_COLOR_TYPE_PALETTE)
2699  png_set_palette_to_rgb (png_ptr);
2700 
2701  /* convert 1-2-4 bits grayscale images to 8 bits grayscale. */
2702  if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
2703  png_set_expand (png_ptr);
2704 
2705 // if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS))
2706 // png_set_tRNS_to_alpha (png_ptr);
2707 
2708  if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2709  png_set_strip_alpha(png_ptr);
2710 
2711  if (bit_depth == 16)
2712  png_set_strip_16 (png_ptr);
2713  else if (bit_depth < 8)
2714  png_set_packing (png_ptr);
2715 
2716  /* update info structure to apply transformations */
2717  png_read_update_info (png_ptr, info_ptr);
2718 
2719  channels = png_get_channels(png_ptr, info_ptr);
2720 
2721  if ( (width != I.getWidth()) || (height != I.getHeight()) )
2722  I.resize(height,width);
2723 
2724  png_bytep* rowPtrs = new png_bytep[height];
2725 
2726  unsigned int stride = width * bit_depth * channels / 8;
2727  unsigned char* data = new unsigned char[stride * height];
2728 
2729 
2730  for (unsigned int i =0; i < height; i++)
2731  rowPtrs[i] = (png_bytep)data + (i * stride);
2732 
2733  png_read_image(png_ptr, rowPtrs);
2734 
2735  vpImage<unsigned char> Ig(height,width);
2736  unsigned char* output;
2737 
2738  switch (channels)
2739  {
2740  case 1:
2741  output = (unsigned char*)Ig.bitmap;
2742  for (unsigned int i = 0; i < width*height; i++)
2743  {
2744  *(output++) = data[i];
2745  }
2746  vpImageConvert::convert(Ig,I) ;
2747  break;
2748  case 2:
2749  output = (unsigned char*)Ig.bitmap;
2750  for (unsigned int i = 0; i < width*height; i++)
2751  {
2752  *(output++) = data[i*2];
2753  }
2754  vpImageConvert::convert(Ig,I) ;
2755  break;
2756  case 3:
2757 
2758  output = (unsigned char*)I.bitmap;
2759  for (unsigned int i = 0; i < width*height; i++)
2760  {
2761  *(output++) = data[i*3];
2762  *(output++) = data[i*3+1];
2763  *(output++) = data[i*3+2];
2764  *(output++) = 0;
2765  }
2766  break;
2767  case 4:
2768  output = (unsigned char*)I.bitmap;
2769  for (unsigned int i = 0; i < width*height; i++)
2770  {
2771  *(output++) = data[i*4];
2772  *(output++) = data[i*4+1];
2773  *(output++) = data[i*4+2];
2774  *(output++) = data[i*4+3];
2775  }
2776  break;
2777  }
2778 
2779  delete [] (png_bytep)rowPtrs;
2780  delete [] data;
2781  png_read_end (png_ptr, NULL);
2782  png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
2783  fclose(file);
2784 }
2785 
2786 
2805 void
2806 vpImageIo::readPNG(vpImage<vpRGBa> &I, const std::string filename)
2807 {
2808  vpImageIo::readPNG(I, filename.c_str());
2809 }
2810 
2811 #elif defined(VISP_HAVE_OPENCV)
2812 
2820 void
2821 vpImageIo::writePNG(const vpImage<unsigned char> &I, const char *filename)
2822 {
2823  IplImage* Ip = NULL;
2824  vpImageConvert::convert(I, Ip);
2825 
2826  cvSaveImage(filename, Ip);
2827 
2828  cvReleaseImage(&Ip);
2829 }
2830 
2831 
2839 void
2840 vpImageIo::writePNG(const vpImage<unsigned char> &I, const std::string filename)
2841 {
2842  vpImageIo::writeJPEG(I, filename.c_str());
2843 }
2844 
2845 
2853 void
2854 vpImageIo::writePNG(const vpImage<vpRGBa> &I, const char *filename)
2855 {
2856  IplImage* Ip = NULL;
2857  vpImageConvert::convert(I, Ip);
2858 
2859  cvSaveImage(filename, Ip);
2860 
2861  cvReleaseImage(&Ip);
2862 }
2863 
2864 
2872 void
2873 vpImageIo::writePNG(const vpImage<vpRGBa> &I, const std::string filename)
2874 {
2875  vpImageIo::writePNG(I, filename.c_str());
2876 }
2877 
2878 
2895 void
2896 vpImageIo::readPNG(vpImage<unsigned char> &I, const char *filename)
2897 {
2898  IplImage* Ip = NULL;
2899  Ip = cvLoadImage(filename, CV_LOAD_IMAGE_GRAYSCALE);
2900  if (Ip != NULL)
2901  vpImageConvert::convert(Ip, I);
2902  else
2904  "Can't read the image")) ;
2905  cvReleaseImage(&Ip);
2906 }
2907 
2908 
2925 void
2926 vpImageIo::readPNG(vpImage<unsigned char> &I, const std::string filename)
2927 {
2928  vpImageIo::readPNG(I, filename.c_str());
2929 }
2930 
2931 
2950 void
2951 vpImageIo::readPNG(vpImage<vpRGBa> &I, const char *filename)
2952 {
2953  IplImage* Ip = NULL;
2954  Ip = cvLoadImage(filename, CV_LOAD_IMAGE_COLOR);
2955  if (Ip != NULL)
2956  vpImageConvert::convert(Ip, I);
2957  else
2959  "Can't read the image")) ;
2960  cvReleaseImage(&Ip);
2961 }
2962 
2963 
2982 void
2983 vpImageIo::readPNG(vpImage<vpRGBa> &I, const std::string filename)
2984 {
2985  vpImageIo::readPNG(I, filename.c_str());
2986 }
2987 
2988 #endif
2989 
2990 
2991 /*
2992  * Local variables:
2993  * c-basic-offset: 2
2994  * End:
2995  */
2996 
2997 
2998 
2999 /*
3000  * Local variables:
3001  * c-basic-offset: 2
3002  * End:
3003  */