ViSP  2.8.0
vpImageIo.cpp
1 /****************************************************************************
2  *
3  * $Id: vpImageIo.cpp 4315 2013-07-16 20:32:52Z 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  // Formats supported by opencv
214  else if (ext.compare(".TIFF") == 0)
215  return FORMAT_TIFF;
216  else if (ext.compare(".tiff") == 0)
217  return FORMAT_TIFF;
218  else if (ext.compare(".TIF") == 0)
219  return FORMAT_TIFF;
220  else if (ext.compare(".tif") == 0)
221  return FORMAT_TIFF;
222  else if (ext.compare(".BMP") == 0)
223  return FORMAT_BMP;
224  else if (ext.compare(".bmp") == 0)
225  return FORMAT_BMP;
226  else if (ext.compare(".DIB") == 0)
227  return FORMAT_DIB;
228  else if (ext.compare(".dib") == 0)
229  return FORMAT_DIB;
230  else if (ext.compare(".PBM") == 0)
231  return FORMAT_PBM;
232  else if (ext.compare(".pbm") == 0)
233  return FORMAT_PBM;
234  else if (ext.compare(".SR") == 0)
235  return FORMAT_RASTER;
236  else if (ext.compare(".sr") == 0)
237  return FORMAT_RASTER;
238  else if (ext.compare(".RAS") == 0)
239  return FORMAT_RASTER;
240  else if (ext.compare(".ras") == 0)
241  return FORMAT_RASTER;
242  else if (ext.compare(".JP2") == 0)
243  return FORMAT_JPEG2000;
244  else if (ext.compare(".jp2") == 0)
245  return FORMAT_JPEG2000;
246  else
247  return FORMAT_UNKNOWN;
248 }
249 
250 // return the extension of the file including the dot
251 std::string vpImageIo::getExtension(const std::string &filename)
252 {
253  // extract the extension
254  size_t dot = filename.find_last_of(".");
255  std::string ext = filename.substr(dot, filename.size()-1);
256  return ext;
257 }
258 
259 
276 void
277 vpImageIo::read(vpImage<unsigned char> &I, const char *filename)
278 {
279  bool try_opencv_reader = false;
280 
281  switch(getFormat(filename)){
282  case FORMAT_PGM :
283  readPGM(I,filename); break;
284  case FORMAT_PPM :
285  readPPM(I,filename); break;
286  case FORMAT_JPEG :
287 #ifdef VISP_HAVE_LIBJPEG
288  readJPEG(I,filename);
289 #else
290  try_opencv_reader = true;
291 #endif
292  break;
293  case FORMAT_PNG :
294 #if defined(VISP_HAVE_LIBPNG)
295  readPNG(I,filename);
296 #else
297  try_opencv_reader = true;
298 #endif
299  break;
300  case FORMAT_TIFF :
301  case FORMAT_BMP :
302  case FORMAT_DIB :
303  case FORMAT_PBM :
304  case FORMAT_RASTER :
305  case FORMAT_JPEG2000 :
306  case FORMAT_UNKNOWN :
307  try_opencv_reader = true;
308  break;
309  }
310 
311  if (try_opencv_reader) {
312 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
313  //std::cout << "Use opencv to read the image" << std::endl;
314  cv::Mat cvI = cv::imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
315  vpImageConvert::convert(cvI, I);
316 #else
317  vpCERROR << "Cannot read file: Image format not supported..." << std::endl;
319  "Cannot read file: Image format not supported")) ;
320 #endif
321  }
322 }
339 void
340 vpImageIo::read(vpImage<unsigned char> &I, const std::string filename)
341 {
342  read(I,filename.c_str());
343 }
360 void
361 vpImageIo::read(vpImage<vpRGBa> &I, const char *filename)
362 {
363  bool try_opencv_reader = false;
364 
365  switch(getFormat(filename)){
366  case FORMAT_PGM :
367  readPGM(I,filename); break;
368  case FORMAT_PPM :
369  readPPM(I,filename); break;
370  case FORMAT_JPEG :
371 #ifdef VISP_HAVE_LIBJPEG
372  readJPEG(I,filename);
373 #else
374  try_opencv_reader = true;
375 #endif
376  break;
377  case FORMAT_PNG :
378 #if defined(VISP_HAVE_LIBPNG)
379  readPNG(I,filename);
380 #else
381  try_opencv_reader = true;
382 #endif
383  break;
384  case FORMAT_TIFF :
385  case FORMAT_BMP :
386  case FORMAT_DIB :
387  case FORMAT_PBM :
388  case FORMAT_RASTER :
389  case FORMAT_JPEG2000 :
390  case FORMAT_UNKNOWN :
391  try_opencv_reader = true;
392  break;
393  }
394 
395  if (try_opencv_reader) {
396 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
397  // std::cout << "Use opencv to read the image" << std::endl;
398  cv::Mat cvI = cv::imread(filename, CV_LOAD_IMAGE_COLOR);
399  vpImageConvert::convert(cvI, I);
400 #else
401  vpCERROR << "Cannot read file: Image format not supported..." << std::endl;
403  "Cannot read file: Image format not supported")) ;
404 #endif
405  }
406 }
423 void
424 vpImageIo::read(vpImage<vpRGBa> &I, const std::string filename)
425 {
426  read(I,filename.c_str());
427 }
428 
441 void
442 vpImageIo::write(const vpImage<unsigned char> &I, const char *filename)
443 {
444  bool try_opencv_writer = false;
445 
446  switch(getFormat(filename)){
447  case FORMAT_PGM :
448  writePGM(I,filename); break;
449  case FORMAT_PPM :
450  writePPM(I,filename); break;
451  case FORMAT_JPEG :
452 #ifdef VISP_HAVE_LIBJPEG
453  writeJPEG(I,filename);
454 #else
455  try_opencv_writer = true;
456 #endif
457  break;
458  case FORMAT_PNG :
459 #ifdef VISP_HAVE_LIBPNG
460  writePNG(I,filename);
461 #else
462  try_opencv_writer = true;
463 #endif
464  break;
465  case FORMAT_TIFF :
466  case FORMAT_BMP :
467  case FORMAT_DIB :
468  case FORMAT_PBM :
469  case FORMAT_RASTER :
470  case FORMAT_JPEG2000 :
471  case FORMAT_UNKNOWN :
472  try_opencv_writer = true;
473  break;
474  }
475 
476  if (try_opencv_writer) {
477 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
478  // std::cout << "Use opencv to write the image" << std::endl;
479  cv::Mat cvI;
480  vpImageConvert::convert(I, cvI);
481  cv::imwrite(filename, cvI);
482 #else
483  vpCERROR << "Cannot write file: Image format not supported..." << std::endl;
485  "Cannot write file: Image format not supported")) ;
486 #endif
487  }
488 }
501 void
502 vpImageIo::write(const vpImage<unsigned char> &I, const std::string filename)
503 {
504  write(I,filename.c_str());
505 }
518 void
519 vpImageIo::write(const vpImage<vpRGBa> &I, const char *filename)
520 {
521  bool try_opencv_writer = false;
522 
523  switch(getFormat(filename)){
524  case FORMAT_PGM :
525  writePGM(I,filename); break;
526  case FORMAT_PPM :
527  writePPM(I,filename); break;
528  case FORMAT_JPEG :
529 #ifdef VISP_HAVE_LIBJPEG
530  writeJPEG(I,filename);
531 #else
532  try_opencv_writer = true;
533 #endif
534  break;
535  case FORMAT_PNG :
536 #ifdef VISP_HAVE_LIBPNG
537  writePNG(I,filename);
538 #else
539  try_opencv_writer = true;
540 #endif
541  break;
542  case FORMAT_TIFF :
543  case FORMAT_BMP :
544  case FORMAT_DIB :
545  case FORMAT_PBM :
546  case FORMAT_RASTER :
547  case FORMAT_JPEG2000 :
548  case FORMAT_UNKNOWN :
549  try_opencv_writer = true;
550  break;
551  }
552 
553  if (try_opencv_writer) {
554 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
555  // std::cout << "Use opencv to write the image" << std::endl;
556  cv::Mat cvI;
557  vpImageConvert::convert(I, cvI);
558  cv::imwrite(filename, cvI);
559 #else
560  vpCERROR << "Cannot write file: Image format not supported..." << std::endl;
562  "Cannot write file: Image format not supported")) ;
563 #endif
564  }
565 }
578 void
579 vpImageIo::write(const vpImage<vpRGBa> &I, const std::string filename)
580 {
581  write(I,filename.c_str());
582 }
583 //--------------------------------------------------------------------------
584 // PFM
585 //--------------------------------------------------------------------------
586 
596 void
598  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, "P8\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  ierr = fwrite(I.bitmap, sizeof(float), nbyte, fd) ;
628  if (ierr != nbyte) {
629  fclose(fd);
630  vpERROR_TRACE("couldn't write %d bytes to file \"%s\"\n",
631  nbyte, filename) ;
633  "cannot write file")) ;
634  }
635 
636  fflush(fd);
637  fclose(fd);
638 
639 }
640 //--------------------------------------------------------------------------
641 // PGM
642 //--------------------------------------------------------------------------
643 
652 void
654  const char *filename)
655 {
656 
657  FILE* fd;
658 
659  // Test the filename
660  if (filename == '\0') {
661  vpERROR_TRACE("no filename\n");
663  "no filename")) ;
664  }
665 
666  fd = fopen(filename, "wb");
667 
668  if (fd == NULL) {
669  vpERROR_TRACE("couldn't write to file \"%s\"\n", filename);
671  "cannot write file")) ;
672  }
673 
674  // Write the head
675  fprintf(fd, "P5\n"); // Magic number
676  fprintf(fd, "%d %d\n", I.getWidth(), I.getHeight()); // Image size
677  fprintf(fd, "255\n"); // Max level
678 
679  // Write the bitmap
680  size_t ierr;
681  size_t nbyte = I.getWidth()*I.getHeight();
682 
683  ierr = fwrite(I.bitmap, sizeof(unsigned char), nbyte, fd) ;
684  if (ierr != nbyte) {
685  fclose(fd);
686  vpERROR_TRACE("couldn't write %d bytes to file \"%s\"\n",
687  nbyte, filename) ;
689  "cannot write file")) ;
690  }
691 
692  fflush(fd);
693  fclose(fd);
694 
695 }
703 void
704 vpImageIo::writePGM(const vpImage<short> &I, const char *filename)
705 {
707  unsigned int nrows = I.getHeight();
708  unsigned int ncols = I.getWidth();
709 
710  Iuc.resize(nrows, ncols);
711 
712  for (unsigned int i=0 ; i < nrows * ncols ; i++)
713  Iuc.bitmap[i] = (unsigned char)I.bitmap[i] ;
714 
715  vpImageIo::writePGM(Iuc, filename) ;
716 
717 
718 }
728 void
729 vpImageIo::writePGM(const vpImage<vpRGBa> &I, const char *filename)
730 {
731 
732  FILE* fd;
733 
734  // Test the filename
735  if (filename == '\0') {
736  vpERROR_TRACE("no filename\n");
738  "no filename")) ;
739  }
740 
741  fd = fopen(filename, "wb");
742 
743  if (fd == NULL) {
744  vpERROR_TRACE("couldn't write to file \"%s\"\n", filename);
746  "cannot write file")) ;
747  }
748 
749  // Write the head
750  fprintf(fd, "P5\n"); // Magic number
751  fprintf(fd, "%d %d\n", I.getWidth(), I.getHeight()); // Image size
752  fprintf(fd, "255\n"); // Max level
753 
754  // Write the bitmap
755  size_t ierr;
756  size_t nbyte = I.getWidth()*I.getHeight();
757 
758 
760  vpImageConvert::convert(I,Itmp) ;
761 
762  ierr = fwrite(Itmp.bitmap, sizeof(unsigned char), nbyte, fd) ;
763  if (ierr != nbyte) {
764  fclose(fd);
765  vpERROR_TRACE("couldn't write %d bytes to file \"%s\"\n",
766  nbyte, filename) ;
768  "cannot write file")) ;
769  }
770 
771  fflush(fd);
772  fclose(fd);
773 
774 }
775 
792 void
793 vpImageIo::readPFM(vpImage<float> &I, const char *filename)
794 {
795  FILE* fd = NULL; // File descriptor
796  int ierr;
797  int line;
798  int is255;
799  char* err ;
800  char str[vpMAX_LEN];
801  unsigned int w, h;
802 
803  // Test the filename
804  if (filename == '\0')
805  {
806  vpERROR_TRACE("no filename") ;
808  " no filename")) ;
809 
810  }
811 
812  // Open the filename
813  fd = fopen(filename, "rb");
814  if (fd == NULL)
815  {
816  vpERROR_TRACE("couldn't read file \"%s\"", filename) ;
818  "couldn't read file")) ;
819  }
820 
821  // Read the first line with magic number P5
822  line = 0;
823 
824  err = fgets(str, vpMAX_LEN - 1, fd);
825  line++;
826  if (err == NULL)
827  {
828  fclose (fd);
829  vpERROR_TRACE("couldn't read line %d of file \"%s\"\n", line, filename) ;
831  "couldn't read file")) ;
832  }
833 
834  if (strlen(str) < 3)
835  {
836  fclose (fd);
837  vpERROR_TRACE("\"%s\" is not a PGM file\n", filename) ;
839  "this is not a pfm file")) ;
840  }
841 
842  str[2] = '\0';
843  if (strcmp(str, "P8") != 0)
844  {
845  fclose (fd);
846  vpERROR_TRACE("\"%s\" is not a PFM file\n", filename) ;
848  "this is not a pgm file")) ;
849  }
850 
851  // Jump the possible comment, or empty line and read the following line
852  do {
853  err = fgets(str, vpMAX_LEN - 1, fd);
854  line++;
855  if (err == NULL) {
856  fprintf(stderr, "couldn't read line %d of file \"%s\"\n", line, filename);
857  fclose (fd);
858  }
859  } while ((str[0] == '#') || (str[0] == '\n'));
860 
861  // Extract image size
862  ierr = sscanf(str, "%d %d", &w, &h);
863  if(ierr == 1){// the norm allows to have the two values on two separated lines.
864  do {
865  err = fgets(str, vpMAX_LEN - 1, fd);
866  line++;
867  if (err == NULL) {
868  fprintf(stderr, "couldn't read line %d of file \"%s\"\n", line, filename);
869  fclose (fd);
870  }
871  } while ((str[0] == '#') || (str[0] == '\n'));
872  ierr = sscanf(str, "%d", &h);
873  }
874  if (ierr == EOF)
875  {
876  fclose (fd);
877  vpERROR_TRACE("couldn't read line %d of file \"%s\"\n",line, filename) ;
879  "couldn't read file")) ;
880  }
881 
882  if ((h != I.getHeight())||( w != I.getWidth()))
883  {
884 
885  try
886  {
887  I.resize(h,w) ;
888  }
889  catch(...)
890  {
891  vpERROR_TRACE(" ") ;
892  throw ;
893  }
894  }
895 
896  // Read 255
897  err = fgets(str, vpMAX_LEN - 1, fd);
898  line++;
899  if (err == NULL) {
900  fclose (fd);
901  vpERROR_TRACE("couldn't read line %d of file \"%s\"\n",line, filename) ;
903  "couldn't read file")) ;
904  }
905 
906  ierr = sscanf(str, "%d", &is255);
907  if (ierr == EOF) {
908  fclose (fd);
909  vpERROR_TRACE("couldn't read line %d of file \"%s\"\n", line, filename) ;
911  "couldn't read file")) ;
912  }
913 
914  if (is255 != 255)
915  {
916  fclose (fd);
917  vpERROR_TRACE("MAX_VAL is not 255 in file \"%s\"\n", filename) ;
919  "error reading pfm file")) ;
920  }
921 
922  unsigned int nbyte = I.getHeight()*I.getWidth();
923  if (fread (I.bitmap, sizeof(float), nbyte, fd ) != nbyte)
924  {
925  fclose (fd);
926  vpERROR_TRACE("couldn't read %d bytes in file \"%s\"\n", nbyte, filename) ;
928  "error reading pfm file")) ;
929  }
930 
931  fclose (fd);
932 
933 
934 }
935 
936 
937 
954 void
956 {
957  FILE* fd = NULL; // File descriptor
958  int ierr;
959  int line;
960  int is255;
961  char* err ;
962  char str[vpMAX_LEN];
963  unsigned int w, h;
964 
965  // Test the filename
966  if (filename == '\0')
967  {
968  vpERROR_TRACE("no filename") ;
970  " no filename")) ;
971 
972  }
973 
974  // Open the filename
975  fd = fopen(filename, "rb");
976  if (fd == NULL)
977  {
978  vpERROR_TRACE("couldn't read file \"%s\"", filename) ;
980  "couldn't read file")) ;
981  }
982 
983  // Read the first line with magic number P5
984  line = 0;
985 
986  err = fgets(str, vpMAX_LEN - 1, fd);
987  line++;
988  if (err == NULL)
989  {
990  fclose (fd);
991  vpERROR_TRACE("couldn't read line %d of file \"%s\"\n", line, filename) ;
993  "couldn't read file")) ;
994  }
995 
996  if (strlen(str) < 3)
997  {
998  fclose (fd);
999  vpERROR_TRACE("\"%s\" is not a PGM file\n", filename) ;
1001  "this is not a pgm file")) ;
1002  }
1003 
1004  str[2] = '\0';
1005  if (strcmp(str, "P5") != 0)
1006  {
1007  fclose (fd);
1008  vpERROR_TRACE("\"%s\" is not a PGM file\n", filename) ;
1010  "this is not a pgm file")) ;
1011  }
1012 
1013  // Jump the possible comment, or empty line and read the following line
1014  do {
1015  err = fgets(str, vpMAX_LEN - 1, fd);
1016  line++;
1017  if (err == NULL) {
1018  fprintf(stderr, "couldn't read line %d of file \"%s\"\n", line, filename);
1019  fclose (fd);
1020  }
1021  } while ((str[0] == '#') || (str[0] == '\n'));
1022 
1023  // Extract image size
1024  ierr = sscanf(str, "%d %d", &w, &h);
1025  if(ierr == 1){// the norm allows to have the two values on two separated lines.
1026  do {
1027  err = fgets(str, vpMAX_LEN - 1, fd);
1028  line++;
1029  if (err == NULL) {
1030  fprintf(stderr, "couldn't read line %d of file \"%s\"\n", line, filename);
1031  fclose (fd);
1032  }
1033  } while ((str[0] == '#') || (str[0] == '\n'));
1034  ierr = sscanf(str, "%d", &h);
1035  }
1036  if (ierr == EOF)
1037  {
1038  fclose (fd);
1039  vpERROR_TRACE("couldn't read line %d of file \"%s\"\n",line, filename) ;
1041  "couldn't read file")) ;
1042  }
1043 
1044  if ((h != I.getHeight())||( w != I.getWidth()))
1045  {
1046 
1047  try
1048  {
1049  I.resize(h,w) ;
1050  }
1051  catch(...)
1052  {
1053  vpERROR_TRACE(" ") ;
1054  throw ;
1055  }
1056  }
1057 
1058  // Read 255
1059  err = fgets(str, vpMAX_LEN - 1, fd);
1060  line++;
1061  if (err == NULL) {
1062  fclose (fd);
1063  vpERROR_TRACE("couldn't read line %d of file \"%s\"\n",line, filename) ;
1065  "couldn't read file")) ;
1066  }
1067 
1068  ierr = sscanf(str, "%d", &is255);
1069  if (ierr == EOF) {
1070  fclose (fd);
1071  vpERROR_TRACE("couldn't read line %d of file \"%s\"\n", line, filename) ;
1073  "couldn't read file")) ;
1074  }
1075 
1076  if (is255 != 255)
1077  {
1078  fclose (fd);
1079  vpERROR_TRACE("MAX_VAL is not 255 in file \"%s\"\n", filename) ;
1081  "error reading pgm file")) ;
1082  }
1083 
1084  unsigned int nbyte = I.getHeight()*I.getWidth();
1085  if (fread (I.bitmap, sizeof(unsigned char), nbyte, fd ) != nbyte)
1086  {
1087  fclose (fd);
1088  vpERROR_TRACE("couldn't read %d bytes in file \"%s\"\n", nbyte, filename) ;
1090  "error reading pgm file")) ;
1091  }
1092 
1093  fclose (fd);
1094 
1095 
1096 }
1097 
1098 
1117 void
1118 vpImageIo::readPGM(vpImage<vpRGBa> &I, const char *filename)
1119 {
1120 
1121  try
1122  {
1123  vpImage<unsigned char> Itmp ;
1124 
1125  vpImageIo::readPGM(Itmp, filename) ;
1126 
1127 
1128  vpImageConvert::convert(Itmp, I) ;
1129 
1130  }
1131  catch(...)
1132  {
1133  vpERROR_TRACE(" ") ;
1134  throw ;
1135  }
1136 }
1137 
1138 
1139 //--------------------------------------------------------------------------
1140 // PPM
1141 //--------------------------------------------------------------------------
1142 
1159 void
1161 {
1162 
1163  try
1164  {
1165  vpImage<vpRGBa> Itmp ;
1166 
1167  vpImageIo::readPPM(Itmp, filename) ;
1168 
1169  vpImageConvert::convert(Itmp, I) ;
1170  }
1171  catch(...)
1172  {
1173  vpERROR_TRACE(" ") ;
1174  throw ;
1175  }
1176 }
1177 
1178 
1190 void
1191 vpImageIo::readPPM(vpImage<vpRGBa> &I, const char *filename)
1192 {
1193 
1194  FILE* fd = NULL; // File descriptor
1195  int ierr;
1196  int line;
1197  int is255;
1198  char* err ;
1199  char str[vpMAX_LEN];
1200  unsigned int w, h;
1201 
1202  // Test the filename
1203  if (filename == '\0')
1204  {
1205  vpERROR_TRACE("no filename") ;
1207  " no filename")) ;
1208 
1209  }
1210 
1211  // Open the filename
1212  fd = fopen(filename, "rb");
1213  if (fd == NULL)
1214  {
1215  vpERROR_TRACE("couldn't read file \"%s\"", filename) ;
1217  "couldn't read file")) ;
1218  }
1219 
1220  // Read the first line with magic number P5
1221  line = 0;
1222 
1223  err = fgets(str, vpMAX_LEN - 1, fd);
1224  line++;
1225  if (err == NULL)
1226  {
1227  fclose (fd);
1228  vpERROR_TRACE("couldn't read line %d of file \"%s\"\n", line, filename) ;
1230  "couldn't read file")) ;
1231  }
1232 
1233  if (strlen(str) < 3)
1234  {
1235  fclose (fd);
1236  vpERROR_TRACE("\"%s\" is not a PPM file\n", filename) ;
1238  "this is not a ppm file")) ;
1239  }
1240 
1241  str[2] = '\0';
1242  if (strcmp(str, "P6") != 0)
1243  {
1244  fclose (fd);
1245  vpERROR_TRACE("\"%s\" is not a PPM file\n", filename) ;
1247  "this is not a ppm file")) ;
1248  }
1249 
1250  // Jump the possible comment, or empty line and read the following line
1251  do {
1252  err = fgets(str, vpMAX_LEN - 1, fd);
1253  line++;
1254  if (err == NULL) {
1255  fprintf(stderr, "couldn't read line %d of file \"%s\"\n", line, filename);
1256  fclose (fd);
1257  }
1258  } while ((str[0] == '#') || (str[0] == '\n'));
1259 
1260  // Extract image size
1261  ierr = sscanf(str, "%d %d", &w, &h);
1262  if(ierr == 1){// the norm allows to have the two values on two separated lines.
1263  do {
1264  err = fgets(str, vpMAX_LEN - 1, fd);
1265  line++;
1266  if (err == NULL) {
1267  fprintf(stderr, "couldn't read line %d of file \"%s\"\n", line, filename);
1268  fclose (fd);
1269  }
1270  } while ((str[0] == '#') || (str[0] == '\n'));
1271  ierr = sscanf(str, "%d", &h);
1272  }
1273  if (ierr == EOF)
1274  {
1275  fclose (fd);
1276  vpERROR_TRACE("couldn't read line %d of file \"%s\"\n",line, filename) ;
1278  "couldn't read file")) ;
1279  }
1280 
1281  if ((h != I.getHeight())||( w != I.getWidth()))
1282  {
1283 
1284  try
1285  {
1286  I.resize(h,w) ;
1287  }
1288  catch(...)
1289  {
1290  vpERROR_TRACE(" ") ;
1291  throw ;
1292  }
1293  }
1294 
1295  // Read 255
1296  err = fgets(str, vpMAX_LEN - 1, fd);
1297  line++;
1298  if (err == NULL) {
1299  fclose (fd);
1300  vpERROR_TRACE("couldn't read line %d of file \"%s\"\n",line, filename) ;
1302  "couldn't read file")) ;
1303  }
1304 
1305  ierr = sscanf(str, "%d", &is255);
1306  if (ierr == EOF) {
1307  fclose (fd);
1308  vpERROR_TRACE("couldn't read line %d of file \"%s\"\n", line, filename) ;
1310  "couldn't read file")) ;
1311  }
1312 
1313  if (is255 != 255)
1314  {
1315  fclose (fd);
1316  vpERROR_TRACE("MAX_VAL is not 255 in file \"%s\"\n", filename) ;
1318  "error reading ppm file")) ;
1319  }
1320 
1321  for(unsigned int i=0;i<I.getHeight();i++)
1322  {
1323  for(unsigned int j=0;j<I.getWidth();j++)
1324  {
1325  vpRGBa v ;
1326  size_t res = fread(&v.R,sizeof(v.R),1,fd) ;
1327  res |= fread(&v.G,sizeof(v.G),1,fd) ;
1328  res |= fread(&v.B,sizeof(v.B),1,fd) ;
1329  if (res==0)
1330  {
1331  fclose (fd);
1332  vpERROR_TRACE("couldn't read bytes in file \"%s\"\n", filename) ;
1334  "error reading ppm file")) ;
1335  }
1336  I[i][j] = v ;
1337  }
1338  }
1339  fclose(fd) ;
1340 
1341 }
1342 
1353 void
1354 vpImageIo::writePPM(const vpImage<unsigned char> &I, const char *filename)
1355 {
1356 
1357  try
1358  {
1359  vpImage<vpRGBa> Itmp ;
1360 
1361  vpImageConvert::convert(I, Itmp) ;
1362 
1363  vpImageIo::writePPM(Itmp, filename) ;
1364  }
1365  catch(...)
1366  {
1367  vpERROR_TRACE(" ") ;
1368  throw ;
1369  }
1370 }
1371 
1372 
1380 void
1381 vpImageIo::writePPM(const vpImage<vpRGBa> &I, const char *filename)
1382 {
1383 
1384  FILE* f;
1385 
1386 
1387  // Test the filename
1388  if (filename == '\0') {
1389  vpERROR_TRACE("no filename\n");
1391  "no filename")) ;
1392  }
1393 
1394  f = fopen(filename, "wb");
1395 
1396  if (f == NULL) {
1397  vpERROR_TRACE("couldn't write to file \"%s\"\n", filename);
1399  "cannot write file")) ;
1400  }
1401 
1402 
1403 
1404  fprintf(f,"P6\n"); // Magic number
1405  fprintf(f,"%d %d\n", I.getWidth(), I.getHeight()); // Image size
1406  fprintf(f,"%d\n",255); // Max level
1407 
1408  for(unsigned int i=0;i<I.getHeight();i++)
1409  {
1410  for(unsigned int j=0;j<I.getWidth();j++)
1411  {
1412  vpRGBa P ;
1413  size_t res ;
1414  P = I[i][j] ;
1415  unsigned char tmp ;
1416  tmp = P.R ;
1417  res = fwrite(&tmp,sizeof(tmp),1,f) ;
1418  if (res==0)
1419  {
1420  fclose(f);
1421  vpERROR_TRACE("couldn't write file") ;
1423  "cannot write file")) ;
1424  }
1425  tmp = P.G;
1426  res = fwrite(&tmp,sizeof(tmp),1,f) ;
1427  if (res==0)
1428  {
1429  fclose(f);
1430  vpERROR_TRACE("couldn't write file") ;
1432  "cannot write file")) ;
1433  }
1434  tmp = P.B ;
1435  res = fwrite(&tmp,sizeof(tmp),1,f) ;
1436  if (res==0)
1437  {
1438  fclose(f);
1439  vpERROR_TRACE("couldn't write file") ;
1441  "cannot write file")) ;
1442  }
1443  }
1444  }
1445 
1446  fflush(f);
1447  fclose(f);
1448 }
1449 
1450 
1466 void
1467 vpImageIo::readPGM(vpImage<unsigned char> &I, const std::string filename)
1468 {
1469  vpImageIo::readPGM(I, filename.c_str());
1470 }
1471 
1487 void
1488 vpImageIo::readPGM(vpImage<vpRGBa> &I, const std::string filename)
1489 {
1490  vpImageIo::readPGM(I, filename.c_str());
1491 }
1492 
1502 void
1504  const std::string filename)
1505 {
1506  vpImageIo::writePGM(I, filename.c_str());
1507 }
1508 
1517 void
1518 vpImageIo::writePGM(const vpImage<short> &I, const std::string filename)
1519 {
1520 
1521  vpImageIo::writePGM(I, filename.c_str());
1522 }
1523 
1534 void
1535 vpImageIo::writePGM(const vpImage<vpRGBa> &I, const std::string filename)
1536 {
1537  vpImageIo::writePGM(I, filename.c_str());
1538 }
1539 
1540 //--------------------------------------------------------------------------
1541 // PPM
1542 //--------------------------------------------------------------------------
1543 
1560 void
1561 vpImageIo::readPPM(vpImage<unsigned char> &I, const std::string filename)
1562 {
1563  vpImageIo::readPPM(I, filename.c_str());
1564 }
1565 
1577 void
1578 vpImageIo::readPPM(vpImage<vpRGBa> &I, const std::string filename)
1579 {
1580  vpImageIo::readPPM(I, filename.c_str());
1581 }
1582 
1593 void
1594 vpImageIo::writePPM(const vpImage<unsigned char> &I, const std::string filename)
1595 {
1596  vpImageIo::writePPM(I, filename.c_str());
1597 }
1598 
1607 void
1608 vpImageIo::writePPM(const vpImage<vpRGBa> &I, const std::string filename)
1609 {
1610  vpImageIo::writePPM(I, filename.c_str());
1611 }
1612 
1613 
1614 //--------------------------------------------------------------------------
1615 // JPEG
1616 //--------------------------------------------------------------------------
1617 
1618 #if defined(VISP_HAVE_LIBJPEG)
1619 
1627 void
1628 vpImageIo::writeJPEG(const vpImage<unsigned char> &I, const char *filename)
1629 {
1630  struct jpeg_compress_struct cinfo;
1631  struct jpeg_error_mgr jerr;
1632  FILE *file;
1633 
1634  cinfo.err = jpeg_std_error(&jerr);
1635  jpeg_create_compress(&cinfo);
1636 
1637  // Test the filename
1638  if (filename == '\0') {
1639  vpERROR_TRACE("no filename\n");
1641  "no filename")) ;
1642  }
1643 
1644  file = fopen(filename, "wb");
1645 
1646  if (file == NULL) {
1647  vpERROR_TRACE("couldn't write file \"%s\"\n", filename);
1649  "cannot write file")) ;
1650  }
1651 
1652  unsigned int width = I.getWidth();
1653  unsigned int height = I.getHeight();
1654 
1655  jpeg_stdio_dest(&cinfo, file);
1656 
1657  cinfo.image_width = width;
1658  cinfo.image_height = height;
1659  cinfo.input_components = 1;
1660  cinfo.in_color_space = JCS_GRAYSCALE;
1661  jpeg_set_defaults(&cinfo);
1662 
1663  jpeg_start_compress(&cinfo,TRUE);
1664 
1665  unsigned char *line;
1666  line = new unsigned char[width];
1667  unsigned char* input = (unsigned char*)I.bitmap;
1668  while (cinfo.next_scanline < cinfo.image_height)
1669  {
1670  for (unsigned int i = 0; i < width; i++)
1671  {
1672  line[i] = *(input);
1673  input++;
1674  }
1675  jpeg_write_scanlines(&cinfo, &line, 1);
1676  }
1677 
1678  jpeg_finish_compress(&cinfo);
1679  jpeg_destroy_compress(&cinfo);
1680  delete [] line;
1681  fclose(file);
1682 }
1683 
1684 
1692 void
1693 vpImageIo::writeJPEG(const vpImage<unsigned char> &I, const std::string filename)
1694 {
1695  vpImageIo::writeJPEG(I, filename.c_str());
1696 }
1697 
1698 
1706 void
1707 vpImageIo::writeJPEG(const vpImage<vpRGBa> &I, const char *filename)
1708 {
1709  struct jpeg_compress_struct cinfo;
1710  struct jpeg_error_mgr jerr;
1711  FILE *file;
1712 
1713  cinfo.err = jpeg_std_error(&jerr);
1714  jpeg_create_compress(&cinfo);
1715 
1716  // Test the filename
1717  if (filename == '\0') {
1718  vpERROR_TRACE("no filename\n");
1720  "no filename")) ;
1721  }
1722 
1723  file = fopen(filename, "wb");
1724 
1725  if (file == NULL) {
1726  vpERROR_TRACE("couldn't write file \"%s\"\n", filename);
1728  "cannot write file")) ;
1729  }
1730 
1731  unsigned int width = I.getWidth();
1732  unsigned int height = I.getHeight();
1733 
1734  jpeg_stdio_dest(&cinfo, file);
1735 
1736  cinfo.image_width = width;
1737  cinfo.image_height = height;
1738  cinfo.input_components = 3;
1739  cinfo.in_color_space = JCS_RGB;
1740  jpeg_set_defaults(&cinfo);
1741 
1742  jpeg_start_compress(&cinfo,TRUE);
1743 
1744  unsigned char *line;
1745  line = new unsigned char[3*width];
1746  unsigned char* input = (unsigned char*)I.bitmap;
1747  while (cinfo.next_scanline < cinfo.image_height)
1748  {
1749  for (unsigned int i = 0; i < width; i++)
1750  {
1751  line[i*3] = *(input); input++;
1752  line[i*3+1] = *(input); input++;
1753  line[i*3+2] = *(input); input++;
1754  input++;
1755  }
1756  jpeg_write_scanlines(&cinfo, &line, 1);
1757  }
1758 
1759  jpeg_finish_compress(&cinfo);
1760  jpeg_destroy_compress(&cinfo);
1761  delete [] line;
1762  fclose(file);
1763 }
1764 
1765 
1773 void
1774 vpImageIo::writeJPEG(const vpImage<vpRGBa> &I, const std::string filename)
1775 {
1776  vpImageIo::writeJPEG(I, filename.c_str());
1777 }
1778 
1779 
1796 void
1798 {
1799  struct jpeg_decompress_struct cinfo;
1800  struct jpeg_error_mgr jerr;
1801  FILE *file;
1802 
1803  cinfo.err = jpeg_std_error(&jerr);
1804  jpeg_create_decompress(&cinfo);
1805 
1806  // Test the filename
1807  if (filename == '\0') {
1808  vpERROR_TRACE("no filename\n");
1810  "no filename")) ;
1811  }
1812 
1813  file = fopen(filename, "rb");
1814 
1815  if (file == NULL) {
1816  vpERROR_TRACE("couldn't read file \"%s\"\n", filename);
1818  "cannot read file")) ;
1819  }
1820 
1821  jpeg_stdio_src(&cinfo, file);
1822  jpeg_read_header(&cinfo, TRUE);
1823 
1824  unsigned int width = cinfo.image_width;
1825  unsigned int height = cinfo.image_height;
1826 
1827  if ( (width != I.getWidth()) || (height != I.getHeight()) )
1828  I.resize(height,width);
1829 
1830  jpeg_start_decompress(&cinfo);
1831 
1832  unsigned int rowbytes = cinfo.output_width * (unsigned int)(cinfo.output_components);
1833  JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray)
1834  ((j_common_ptr) &cinfo, JPOOL_IMAGE, rowbytes, 1);
1835 
1836  if (cinfo.out_color_space == JCS_RGB) {
1837  vpImage<vpRGBa> Ic(height,width);
1838  unsigned char* output = (unsigned char*)Ic.bitmap;
1839  while (cinfo.output_scanline<cinfo.output_height) {
1840  jpeg_read_scanlines(&cinfo,buffer,1);
1841  for (unsigned int i = 0; i < width; i++) {
1842  *(output++) = buffer[0][i*3];
1843  *(output++) = buffer[0][i*3+1];
1844  *(output++) = buffer[0][i*3+2];
1845  *(output++) = 0;
1846  }
1847  }
1848  vpImageConvert::convert(Ic,I) ;
1849  }
1850 
1851  else if (cinfo.out_color_space == JCS_GRAYSCALE)
1852  {
1853  unsigned int row;
1854  while (cinfo.output_scanline<cinfo.output_height)
1855  {
1856  row = cinfo.output_scanline;
1857  jpeg_read_scanlines(&cinfo,buffer,1);
1858  memcpy(I[row], buffer[0], rowbytes);
1859  }
1860  }
1861 
1862  jpeg_finish_decompress(&cinfo);
1863  jpeg_destroy_decompress(&cinfo);
1864  fclose(file);
1865 }
1866 
1867 
1884 void
1885 vpImageIo::readJPEG(vpImage<unsigned char> &I, const std::string filename)
1886 {
1887  vpImageIo::readJPEG(I, filename.c_str());
1888 }
1889 
1890 
1909 void
1910 vpImageIo::readJPEG(vpImage<vpRGBa> &I, const char *filename)
1911 {
1912  struct jpeg_decompress_struct cinfo;
1913  struct jpeg_error_mgr jerr;
1914  FILE *file;
1915 
1916  cinfo.err = jpeg_std_error(&jerr);
1917  jpeg_create_decompress(&cinfo);
1918 
1919  // Test the filename
1920  if (filename == '\0') {
1921  vpERROR_TRACE("no filename\n");
1923  "no filename")) ;
1924  }
1925 
1926  file = fopen(filename, "rb");
1927 
1928  if (file == NULL) {
1929  vpERROR_TRACE("couldn't read file \"%s\"\n", filename);
1931  "cannot read file")) ;
1932  }
1933 
1934  jpeg_stdio_src(&cinfo, file);
1935 
1936  jpeg_read_header(&cinfo, TRUE);
1937 
1938  unsigned int width = cinfo.image_width;
1939  unsigned int height = cinfo.image_height;
1940 
1941  if ( (width != I.getWidth()) || (height != I.getHeight()) )
1942  I.resize(height,width);
1943 
1944  jpeg_start_decompress(&cinfo);
1945 
1946  unsigned int rowbytes = cinfo.output_width * (unsigned int)(cinfo.output_components);
1947  JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray)
1948  ((j_common_ptr) &cinfo, JPOOL_IMAGE, rowbytes, 1);
1949 
1950  if (cinfo.out_color_space == JCS_RGB)
1951  {
1952  unsigned char* output = (unsigned char*)I.bitmap;
1953  while (cinfo.output_scanline<cinfo.output_height)
1954  {
1955  jpeg_read_scanlines(&cinfo,buffer,1);
1956  for (unsigned int i = 0; i < width; i++) {
1957  *(output++) = buffer[0][i*3];
1958  *(output++) = buffer[0][i*3+1];
1959  *(output++) = buffer[0][i*3+2];
1960  *(output++) = 0;
1961  }
1962  }
1963  }
1964 
1965  else if (cinfo.out_color_space == JCS_GRAYSCALE)
1966  {
1967  vpImage<unsigned char> Ig(height,width);
1968 
1969  unsigned int row;
1970  while (cinfo.output_scanline<cinfo.output_height)
1971  {
1972  row = cinfo.output_scanline;
1973  jpeg_read_scanlines(&cinfo,buffer,1);
1974  memcpy(Ig[row], buffer[0], rowbytes);
1975  }
1976 
1977  vpImageConvert::convert(Ig,I) ;
1978  }
1979 
1980  jpeg_finish_decompress(&cinfo);
1981  jpeg_destroy_decompress(&cinfo);
1982  fclose(file);
1983 }
1984 
1985 
2004 void
2005 vpImageIo::readJPEG(vpImage<vpRGBa> &I, const std::string filename)
2006 {
2007  vpImageIo::readJPEG(I, filename.c_str());
2008 }
2009 
2010 #elif defined(VISP_HAVE_OPENCV)
2011 
2019 void
2020 vpImageIo::writeJPEG(const vpImage<unsigned char> &I, const char *filename)
2021 {
2022  IplImage* Ip = NULL;
2023  vpImageConvert::convert(I, Ip);
2024 
2025  cvSaveImage(filename, Ip);
2026 
2027  cvReleaseImage(&Ip);
2028 }
2029 
2030 
2038 void
2039 vpImageIo::writeJPEG(const vpImage<unsigned char> &I, const std::string filename)
2040 {
2041  vpImageIo::writeJPEG(I, filename.c_str());
2042 }
2043 
2044 
2052 void
2053 vpImageIo::writeJPEG(const vpImage<vpRGBa> &I, const char *filename)
2054 {
2055  IplImage* Ip = NULL;
2056  vpImageConvert::convert(I, Ip);
2057 
2058  cvSaveImage(filename, Ip);
2059 
2060  cvReleaseImage(&Ip);
2061 }
2062 
2063 
2071 void
2072 vpImageIo::writeJPEG(const vpImage<vpRGBa> &I, const std::string filename)
2073 {
2074  vpImageIo::writeJPEG(I, filename.c_str());
2075 }
2076 
2077 
2094 void
2095 vpImageIo::readJPEG(vpImage<unsigned char> &I, const char *filename)
2096 {
2097  IplImage* Ip = NULL;
2098  Ip = cvLoadImage(filename, CV_LOAD_IMAGE_GRAYSCALE);
2099  if (Ip != NULL)
2100  vpImageConvert::convert(Ip, I);
2101  else
2103  "Can't read the image")) ;
2104  cvReleaseImage(&Ip);
2105 }
2106 
2107 
2124 void
2125 vpImageIo::readJPEG(vpImage<unsigned char> &I, const std::string filename)
2126 {
2127  vpImageIo::readJPEG(I, filename.c_str());
2128 }
2129 
2130 
2149 void
2150 vpImageIo::readJPEG(vpImage<vpRGBa> &I, const char *filename)
2151 {
2152  IplImage* Ip = NULL;
2153  Ip = cvLoadImage(filename, CV_LOAD_IMAGE_COLOR);
2154  if (Ip != NULL)
2155  vpImageConvert::convert(Ip, I);
2156  else
2158  "Can't read the image")) ;
2159  cvReleaseImage(&Ip);
2160 }
2161 
2162 
2181 void
2182 vpImageIo::readJPEG(vpImage<vpRGBa> &I, const std::string filename)
2183 {
2184  vpImageIo::readJPEG(I, filename.c_str());
2185 }
2186 
2187 #endif
2188 
2189 
2190 
2191 
2192 
2193 
2194 //--------------------------------------------------------------------------
2195 // PNG
2196 //--------------------------------------------------------------------------
2197 
2198 #if defined(VISP_HAVE_LIBPNG)
2199 
2207 void
2208 vpImageIo::writePNG(const vpImage<unsigned char> &I, const char *filename)
2209 {
2210  FILE *file;
2211 
2212  // Test the filename
2213  if (filename == '\0') {
2214  vpERROR_TRACE("no filename\n");
2216  "no filename")) ;
2217  }
2218 
2219  file = fopen(filename, "wb");
2220 
2221  if (file == NULL) {
2222  vpERROR_TRACE("couldn't write file \"%s\"\n", filename);
2224  "cannot write file")) ;
2225  }
2226 
2227  /* create a png info struct */
2228  png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL, NULL, NULL);
2229  if (!png_ptr)
2230  {
2231  fclose (file);
2232  vpERROR_TRACE("Error during png_create_write_struct()\n");
2234  "PNG write error")) ;
2235  }
2236 
2237  png_infop info_ptr = png_create_info_struct(png_ptr);
2238  if (!info_ptr)
2239  {
2240  fclose (file);
2241  png_destroy_write_struct (&png_ptr, NULL);
2242  vpERROR_TRACE("Error during png_create_info_struct()\n");
2244  "PNG write error")) ;
2245  }
2246 
2247  /* initialize the setjmp for returning properly after a libpng error occured */
2248  if (setjmp (png_jmpbuf (png_ptr)))
2249  {
2250  fclose (file);
2251  png_destroy_write_struct (&png_ptr, &info_ptr);
2252  vpERROR_TRACE("Error during init_io\n");
2254  "PNG write error")) ;
2255  }
2256 
2257  /* setup libpng for using standard C fwrite() function with our FILE pointer */
2258  png_init_io (png_ptr, file);
2259 
2260  unsigned int width = I.getWidth();
2261  unsigned int height = I.getHeight();
2262  int bit_depth = 8;
2263  int color_type = PNG_COLOR_TYPE_GRAY;
2264  /* set some useful information from header */
2265 
2266  if (setjmp (png_jmpbuf (png_ptr)))
2267  {
2268  fclose (file);
2269  png_destroy_write_struct (&png_ptr, &info_ptr);
2270  vpERROR_TRACE("Error during write header\n");
2272  "PNG write error")) ;
2273  }
2274 
2275  png_set_IHDR(png_ptr, info_ptr, width, height,
2276  bit_depth, color_type, PNG_INTERLACE_NONE,
2277  PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
2278 
2279  png_write_info(png_ptr, info_ptr);
2280 
2281  png_bytep* row_ptrs = new png_bytep[height];
2282  for (unsigned int i = 0; i < height; i++)
2283  row_ptrs[i] = new png_byte[width];
2284 
2285  unsigned char* input = (unsigned char*)I.bitmap;
2286 
2287  for (unsigned int i = 0; i < height; i++)
2288  {
2289  png_byte* row = row_ptrs[i];
2290  for(unsigned int j = 0; j < width; j++)
2291  {
2292  row[j] = *(input);
2293  input++;
2294  }
2295  }
2296 
2297  if (setjmp (png_jmpbuf (png_ptr)))
2298  {
2299  fclose (file);
2300  png_destroy_write_struct (&png_ptr, &info_ptr);
2301  for(unsigned int j = 0; j < height; j++)
2302  delete[] row_ptrs[j];
2303 
2304  delete[] row_ptrs;
2305  vpERROR_TRACE("Error during write image\n");
2307  "PNG write error")) ;
2308  }
2309 
2310  png_write_image(png_ptr, row_ptrs);
2311 
2312  if (setjmp (png_jmpbuf (png_ptr)))
2313  {
2314  fclose (file);
2315  png_destroy_write_struct (&png_ptr, &info_ptr);
2316  for(unsigned int j = 0; j < height; j++)
2317  delete[] row_ptrs[j];
2318 
2319  delete[] row_ptrs;
2320  vpERROR_TRACE("Error during write end\n");
2322  "PNG write error")) ;
2323  }
2324 
2325  png_write_end(png_ptr, NULL);
2326 
2327  for(unsigned int j = 0; j < height; j++)
2328  delete[] row_ptrs[j];
2329 
2330  delete[] row_ptrs;
2331 
2332  png_destroy_write_struct (&png_ptr, &info_ptr);
2333 
2334  fclose(file);
2335 }
2336 
2337 
2345 void
2346 vpImageIo::writePNG(const vpImage<unsigned char> &I, const std::string filename)
2347 {
2348  vpImageIo::writePNG(I, filename.c_str());
2349 }
2350 
2351 
2359 void
2360 vpImageIo::writePNG(const vpImage<vpRGBa> &I, const char *filename)
2361 {
2362  FILE *file;
2363 
2364  // Test the filename
2365  if (filename == '\0') {
2366  vpERROR_TRACE("no filename\n");
2368  "no filename")) ;
2369  }
2370 
2371  file = fopen(filename, "wb");
2372 
2373  if (file == NULL) {
2374  vpERROR_TRACE("couldn't write file \"%s\"\n", filename);
2376  "cannot write file")) ;
2377  }
2378 
2379  /* create a png info struct */
2380  png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL, NULL, NULL);
2381  if (!png_ptr)
2382  {
2383  fclose (file);
2384  vpERROR_TRACE("Error during png_create_write_struct()\n");
2386  "PNG write error")) ;
2387  }
2388 
2389  png_infop info_ptr = png_create_info_struct(png_ptr);
2390  if (!info_ptr)
2391  {
2392  fclose (file);
2393  png_destroy_write_struct (&png_ptr, NULL);
2394  vpERROR_TRACE("Error during png_create_info_struct()\n");
2396  "PNG write error")) ;
2397  }
2398 
2399  /* initialize the setjmp for returning properly after a libpng error occured */
2400  if (setjmp (png_jmpbuf (png_ptr)))
2401  {
2402  fclose (file);
2403  png_destroy_write_struct (&png_ptr, &info_ptr);
2404  vpERROR_TRACE("Error during init_io\n");
2406  "PNG write error")) ;
2407  }
2408 
2409  /* setup libpng for using standard C fwrite() function with our FILE pointer */
2410  png_init_io (png_ptr, file);
2411 
2412  unsigned int width = I.getWidth();
2413  unsigned int height = I.getHeight();
2414  int bit_depth = 8;
2415  int color_type = PNG_COLOR_TYPE_RGB;
2416  /* set some useful information from header */
2417 
2418  if (setjmp (png_jmpbuf (png_ptr)))
2419  {
2420  fclose (file);
2421  png_destroy_write_struct (&png_ptr, &info_ptr);
2422  vpERROR_TRACE("Error during write header\n");
2424  "PNG write error")) ;
2425  }
2426 
2427  png_set_IHDR(png_ptr, info_ptr, width, height,
2428  bit_depth, color_type, PNG_INTERLACE_NONE,
2429  PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
2430 
2431  png_write_info(png_ptr, info_ptr);
2432 
2433  png_bytep* row_ptrs = new png_bytep[height];
2434  for (unsigned int i = 0; i < height; i++)
2435  row_ptrs[i] = new png_byte[3*width];
2436 
2437  unsigned char* input = (unsigned char*)I.bitmap;;
2438 
2439  for (unsigned int i = 0; i < height; i++)
2440  {
2441  png_byte* row = row_ptrs[i];
2442  for(unsigned int j = 0; j < width; j++)
2443  {
2444  row[3*j] = *(input);input++;
2445  row[3*j+1] = *(input);input++;
2446  row[3*j+2] = *(input);input++;
2447  input++;
2448  }
2449  }
2450 
2451  if (setjmp (png_jmpbuf (png_ptr)))
2452  {
2453  fclose (file);
2454  png_destroy_write_struct (&png_ptr, &info_ptr);
2455  for(unsigned int j = 0; j < height; j++)
2456  delete[] row_ptrs[j];
2457 
2458  delete[] row_ptrs;
2459  vpERROR_TRACE("Error during write image\n");
2461  "PNG write error")) ;
2462  }
2463 
2464  png_write_image(png_ptr, row_ptrs);
2465 
2466  if (setjmp (png_jmpbuf (png_ptr)))
2467  {
2468  fclose (file);
2469  png_destroy_write_struct (&png_ptr, &info_ptr);
2470  for(unsigned int j = 0; j < height; j++)
2471  delete[] row_ptrs[j];
2472 
2473  delete[] row_ptrs;
2474  vpERROR_TRACE("Error during write end\n");
2476  "PNG write error")) ;
2477  }
2478 
2479  png_write_end(png_ptr, NULL);
2480 
2481  for(unsigned int j = 0; j < height; j++)
2482  delete[] row_ptrs[j];
2483 
2484  delete[] row_ptrs;
2485 
2486  png_destroy_write_struct (&png_ptr, &info_ptr);
2487 
2488  fclose(file);
2489 }
2490 
2491 
2499 void
2500 vpImageIo::writePNG(const vpImage<vpRGBa> &I, const std::string filename)
2501 {
2502  vpImageIo::writePNG(I, filename.c_str());
2503 }
2504 
2521 void
2523 {
2524  FILE *file;
2525  png_byte magic[8];
2526  // Test the filename
2527  if (filename == '\0') {
2528  vpERROR_TRACE("no filename\n");
2530  "no filename")) ;
2531  }
2532 
2533  file = fopen(filename, "rb");
2534 
2535  if (file == NULL) {
2536  vpERROR_TRACE("couldn't read file \"%s\"\n", filename);
2538  "cannot read file")) ;
2539  }
2540 
2541  /* read magic number */
2542  if (fread (magic, 1, sizeof (magic), file) != sizeof (magic))
2543  {
2544  fclose (file);
2545  vpERROR_TRACE("couldn't read magic number in file \"%s\"\n", filename) ;
2547  "error reading png file")) ;
2548  }
2549 
2550  /* check for valid magic number */
2551  if (png_sig_cmp (magic,0, sizeof (magic)))
2552  {
2553  fprintf (stderr, "error: \"%s\" is not a valid PNG image!\n",filename);
2554  fclose (file);
2556  "error reading png file")) ;
2557  }
2558 
2559  /* create a png read struct */
2560  //printf("version %s\n", PNG_LIBPNG_VER_STRING);
2561  png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
2562  if (png_ptr == NULL)
2563  {
2564  fprintf (stderr, "error: can't create a png read structure!\n");
2565  fclose (file);
2567  "error reading png file")) ;
2568  }
2569 
2570  /* create a png info struct */
2571  png_infop info_ptr = png_create_info_struct (png_ptr);
2572  if (info_ptr == NULL)
2573  {
2574  fprintf (stderr, "error: can't create a png info structure!\n");
2575  fclose (file);
2576  png_destroy_read_struct (&png_ptr, NULL, NULL);
2578  "error reading png file")) ;
2579  }
2580 
2581  /* initialize the setjmp for returning properly after a libpng error occured */
2582  if (setjmp (png_jmpbuf (png_ptr)))
2583  {
2584  fclose (file);
2585  png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
2586  vpERROR_TRACE("Error during init io\n");
2588  "PNG read error")) ;
2589  }
2590 
2591  /* setup libpng for using standard C fread() function with our FILE pointer */
2592  png_init_io (png_ptr, file);
2593 
2594  /* tell libpng that we have already read the magic number */
2595  png_set_sig_bytes (png_ptr, sizeof (magic));
2596 
2597  /* read png info */
2598  png_read_info (png_ptr, info_ptr);
2599 
2600  unsigned int width = png_get_image_width(png_ptr, info_ptr);
2601  unsigned int height = png_get_image_height(png_ptr, info_ptr);
2602 
2603  unsigned int bit_depth, channels, color_type;
2604  /* get some useful information from header */
2605  bit_depth = png_get_bit_depth (png_ptr, info_ptr);
2606  channels = png_get_channels(png_ptr, info_ptr);
2607  color_type = png_get_color_type (png_ptr, info_ptr);
2608 
2609  /* convert index color images to RGB images */
2610  if (color_type == PNG_COLOR_TYPE_PALETTE)
2611  png_set_palette_to_rgb (png_ptr);
2612 
2613  /* convert 1-2-4 bits grayscale images to 8 bits grayscale. */
2614  if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
2615  png_set_expand (png_ptr);
2616 
2617 // if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS))
2618 // png_set_tRNS_to_alpha (png_ptr);
2619 
2620  if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2621  png_set_strip_alpha(png_ptr);
2622 
2623  if (bit_depth == 16)
2624  png_set_strip_16 (png_ptr);
2625  else if (bit_depth < 8)
2626  png_set_packing (png_ptr);
2627 
2628  /* update info structure to apply transformations */
2629  png_read_update_info (png_ptr, info_ptr);
2630 
2631  channels = png_get_channels(png_ptr, info_ptr);
2632 
2633  if ( (width != I.getWidth()) || (height != I.getHeight()) )
2634  I.resize(height,width);
2635 
2636  png_bytep* rowPtrs = new png_bytep[height];
2637 
2638  unsigned int stride = width * bit_depth * channels / 8;
2639  unsigned char* data = new unsigned char[stride * height];
2640 
2641  for (unsigned int i =0; i < height; i++)
2642  rowPtrs[i] = (png_bytep)data + (i * stride);
2643 
2644  png_read_image(png_ptr, rowPtrs);
2645 
2646  vpImage<vpRGBa> Ic(height,width);
2647  unsigned char* output;
2648 
2649  switch (channels)
2650  {
2651  case 1:
2652  output = (unsigned char*)I.bitmap;
2653  for (unsigned int i = 0; i < width*height; i++)
2654  {
2655  *(output++) = data[i];
2656  }
2657  break;
2658  case 2:
2659  output = (unsigned char*)I.bitmap;
2660  for (unsigned int i = 0; i < width*height; i++)
2661  {
2662  *(output++) = data[i*2];
2663  }
2664  break;
2665  case 3:
2666 
2667  output = (unsigned char*)Ic.bitmap;
2668  for (unsigned int i = 0; i < width*height; i++)
2669  {
2670  *(output++) = data[i*3];
2671  *(output++) = data[i*3+1];
2672  *(output++) = data[i*3+2];
2673  *(output++) = 0;
2674  }
2675  vpImageConvert::convert(Ic,I) ;
2676  break;
2677  case 4:
2678  output = (unsigned char*)Ic.bitmap;
2679  for (unsigned int i = 0; i < width*height; i++)
2680  {
2681  *(output++) = data[i*4];
2682  *(output++) = data[i*4+1];
2683  *(output++) = data[i*4+2];
2684  *(output++) = data[i*4+3];
2685  }
2686  vpImageConvert::convert(Ic,I) ;
2687  break;
2688  }
2689 
2690  delete [] (png_bytep)rowPtrs;
2691  delete [] data;
2692  png_read_end (png_ptr, NULL);
2693  png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
2694  fclose(file);
2695 }
2696 
2697 
2714 void
2715 vpImageIo::readPNG(vpImage<unsigned char> &I, const std::string filename)
2716 {
2717  vpImageIo::readPNG(I, filename.c_str());
2718 }
2719 
2720 
2739 void
2740 vpImageIo::readPNG(vpImage<vpRGBa> &I, const char *filename)
2741 {
2742  FILE *file;
2743  png_byte magic[8];
2744 
2745  // Test the filename
2746  if (filename == '\0') {
2747  vpERROR_TRACE("no filename\n");
2749  "no filename")) ;
2750  }
2751 
2752  file = fopen(filename, "rb");
2753 
2754  if (file == NULL) {
2755  vpERROR_TRACE("couldn't read file \"%s\"\n", filename);
2757  "cannot read file")) ;
2758  }
2759 
2760  /* read magic number */
2761  if (fread (magic, 1, sizeof (magic), file) != sizeof (magic))
2762  {
2763  fclose (file);
2764  vpERROR_TRACE("couldn't read magic number in file \"%s\"\n", filename) ;
2766  "error reading pgm file")) ;
2767  }
2768 
2769  /* check for valid magic number */
2770  if (png_sig_cmp (magic,0, sizeof (magic)))
2771  {
2772  fclose (file);
2773  vpERROR_TRACE("error: \"%s\" is not a valid PNG image!\n",filename);
2775  "PNG read error")) ;
2776  }
2777 
2778  /* create a png read struct */
2779  png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
2780  if (!png_ptr)
2781  {
2782  fclose (file);
2783  vpERROR_TRACE("Error during png_create_read_struct()\n");
2785  "PNG read error")) ;
2786  }
2787 
2788  /* create a png info struct */
2789  png_infop info_ptr = png_create_info_struct (png_ptr);
2790  if (!info_ptr)
2791  {
2792  fclose (file);
2793  png_destroy_read_struct (&png_ptr, NULL, NULL);
2794  vpERROR_TRACE("Error during png_create_info_struct()\n");
2796  "PNG read error")) ;
2797  }
2798 
2799  /* initialize the setjmp for returning properly after a libpng error occured */
2800  if (setjmp (png_jmpbuf (png_ptr)))
2801  {
2802  fclose (file);
2803  png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
2804  vpERROR_TRACE("Error during init io\n");
2806  "PNG read error")) ;
2807  }
2808 
2809  /* setup libpng for using standard C fread() function with our FILE pointer */
2810  png_init_io (png_ptr, file);
2811 
2812  /* tell libpng that we have already read the magic number */
2813  png_set_sig_bytes (png_ptr, sizeof (magic));
2814 
2815  /* read png info */
2816  png_read_info (png_ptr, info_ptr);
2817 
2818  unsigned int width = png_get_image_width(png_ptr, info_ptr);
2819  unsigned int height = png_get_image_height(png_ptr, info_ptr);
2820 
2821  unsigned int bit_depth, channels, color_type;
2822  /* get some useful information from header */
2823  bit_depth = png_get_bit_depth (png_ptr, info_ptr);
2824  channels = png_get_channels(png_ptr, info_ptr);
2825  color_type = png_get_color_type (png_ptr, info_ptr);
2826 
2827  /* convert index color images to RGB images */
2828  if (color_type == PNG_COLOR_TYPE_PALETTE)
2829  png_set_palette_to_rgb (png_ptr);
2830 
2831  /* convert 1-2-4 bits grayscale images to 8 bits grayscale. */
2832  if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
2833  png_set_expand (png_ptr);
2834 
2835 // if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS))
2836 // png_set_tRNS_to_alpha (png_ptr);
2837 
2838  if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2839  png_set_strip_alpha(png_ptr);
2840 
2841  if (bit_depth == 16)
2842  png_set_strip_16 (png_ptr);
2843  else if (bit_depth < 8)
2844  png_set_packing (png_ptr);
2845 
2846  /* update info structure to apply transformations */
2847  png_read_update_info (png_ptr, info_ptr);
2848 
2849  channels = png_get_channels(png_ptr, info_ptr);
2850 
2851  if ( (width != I.getWidth()) || (height != I.getHeight()) )
2852  I.resize(height,width);
2853 
2854  png_bytep* rowPtrs = new png_bytep[height];
2855 
2856  unsigned int stride = width * bit_depth * channels / 8;
2857  unsigned char* data = new unsigned char[stride * height];
2858 
2859 
2860  for (unsigned int i =0; i < height; i++)
2861  rowPtrs[i] = (png_bytep)data + (i * stride);
2862 
2863  png_read_image(png_ptr, rowPtrs);
2864 
2865  vpImage<unsigned char> Ig(height,width);
2866  unsigned char* output;
2867 
2868  switch (channels)
2869  {
2870  case 1:
2871  output = (unsigned char*)Ig.bitmap;
2872  for (unsigned int i = 0; i < width*height; i++)
2873  {
2874  *(output++) = data[i];
2875  }
2876  vpImageConvert::convert(Ig,I) ;
2877  break;
2878  case 2:
2879  output = (unsigned char*)Ig.bitmap;
2880  for (unsigned int i = 0; i < width*height; i++)
2881  {
2882  *(output++) = data[i*2];
2883  }
2884  vpImageConvert::convert(Ig,I) ;
2885  break;
2886  case 3:
2887 
2888  output = (unsigned char*)I.bitmap;
2889  for (unsigned int i = 0; i < width*height; i++)
2890  {
2891  *(output++) = data[i*3];
2892  *(output++) = data[i*3+1];
2893  *(output++) = data[i*3+2];
2894  *(output++) = 0;
2895  }
2896  break;
2897  case 4:
2898  output = (unsigned char*)I.bitmap;
2899  for (unsigned int i = 0; i < width*height; i++)
2900  {
2901  *(output++) = data[i*4];
2902  *(output++) = data[i*4+1];
2903  *(output++) = data[i*4+2];
2904  *(output++) = data[i*4+3];
2905  }
2906  break;
2907  }
2908 
2909  delete [] (png_bytep)rowPtrs;
2910  delete [] data;
2911  png_read_end (png_ptr, NULL);
2912  png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
2913  fclose(file);
2914 }
2915 
2916 
2935 void
2936 vpImageIo::readPNG(vpImage<vpRGBa> &I, const std::string filename)
2937 {
2938  vpImageIo::readPNG(I, filename.c_str());
2939 }
2940 
2941 #elif defined(VISP_HAVE_OPENCV)
2942 
2950 void
2951 vpImageIo::writePNG(const vpImage<unsigned char> &I, const char *filename)
2952 {
2953  IplImage* Ip = NULL;
2954  vpImageConvert::convert(I, Ip);
2955 
2956  cvSaveImage(filename, Ip);
2957 
2958  cvReleaseImage(&Ip);
2959 }
2960 
2961 
2969 void
2970 vpImageIo::writePNG(const vpImage<unsigned char> &I, const std::string filename)
2971 {
2972  vpImageIo::writePNG(I, filename.c_str());
2973 }
2974 
2975 
2983 void
2984 vpImageIo::writePNG(const vpImage<vpRGBa> &I, const char *filename)
2985 {
2986  IplImage* Ip = NULL;
2987  vpImageConvert::convert(I, Ip);
2988 
2989  cvSaveImage(filename, Ip);
2990 
2991  cvReleaseImage(&Ip);
2992 }
2993 
2994 
3002 void
3003 vpImageIo::writePNG(const vpImage<vpRGBa> &I, const std::string filename)
3004 {
3005  vpImageIo::writePNG(I, filename.c_str());
3006 }
3007 
3008 
3025 void
3026 vpImageIo::readPNG(vpImage<unsigned char> &I, const char *filename)
3027 {
3028  IplImage* Ip = NULL;
3029  Ip = cvLoadImage(filename, CV_LOAD_IMAGE_GRAYSCALE);
3030  if (Ip != NULL)
3031  vpImageConvert::convert(Ip, I);
3032  else
3034  "Can't read the image")) ;
3035  cvReleaseImage(&Ip);
3036 }
3037 
3038 
3055 void
3056 vpImageIo::readPNG(vpImage<unsigned char> &I, const std::string filename)
3057 {
3058  vpImageIo::readPNG(I, filename.c_str());
3059 }
3060 
3061 
3080 void
3081 vpImageIo::readPNG(vpImage<vpRGBa> &I, const char *filename)
3082 {
3083  IplImage* Ip = NULL;
3084  Ip = cvLoadImage(filename, CV_LOAD_IMAGE_COLOR);
3085  if (Ip != NULL)
3086  vpImageConvert::convert(Ip, I);
3087  else
3089  "Can't read the image")) ;
3090  cvReleaseImage(&Ip);
3091 }
3092 
3093 
3112 void
3113 vpImageIo::readPNG(vpImage<vpRGBa> &I, const std::string filename)
3114 {
3115  vpImageIo::readPNG(I, filename.c_str());
3116 }
3117 
3118 #endif
static void write(const vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:442
static void writeJPEG(const vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:1628
static void readPGM(vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:955
unsigned int getWidth() const
Definition: vpImage.h:159
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
#define vpERROR_TRACE
Definition: vpDebug.h:379
unsigned char B
Blue component.
Definition: vpRGBa.h:155
Type * bitmap
points toward the bitmap
Definition: vpImage.h:120
#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:535
static void writePGM(const vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:653
static void writePNG(const vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:2208
static void writePFM(const vpImage< float > &I, const char *filename)
Definition: vpImageIo.cpp:597
Error that can be emited by the vpImage class and its derivates.
static void readPFM(vpImage< float > &I, const char *filename)
Definition: vpImageIo.cpp:793
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:2522
static void readJPEG(vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:1797
unsigned char R
Red component.
Definition: vpRGBa.h:153
static void readPPM(vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:1160
unsigned int getHeight() const
Definition: vpImage.h:150
static void writePPM(const vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:1354
static void read(vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:277