47 #include <visp/vpImage.h>
48 #include <visp/vpImageIo.h>
49 #include <visp/vpImageConvert.h>
50 #include <visp/vpIoTools.h>
52 const int vpImageIo::vpMAX_LEN = 100;
63 vpImageIo::openFileRead(
const char *filename)
69 if (filename ==
'\0') {
76 if ((fd = fopen(filename,
"r")) == NULL)
80 "cannot open file")) ;
98 vpImageIo::openFileWrite(
const char *filename,
const char *mode)
103 if (filename ==
'\0')
107 "filename empty ")) ;
111 if ((fd = fopen(filename, mode)) == NULL)
115 "cannot open file")) ;
129 vpImageIo::openFileRead(
const std::string filename)
135 if (filename.empty()) {
138 "filename empty ")) ;
142 if ((fd = fopen(filename.c_str(),
"r")) == NULL)
146 "cannot open file")) ;
164 vpImageIo::openFileWrite(
const std::string filename,
165 const std::string mode)
170 if (filename.empty())
174 "filename empty ")) ;
178 if ((fd = fopen(filename.c_str(), mode.c_str())) == NULL)
182 "cannot open file")) ;
187 vpImageIo::vpImageFormatType
188 vpImageIo::getFormat(
const char *filename)
190 std::string sfilename(filename);
192 std::string ext = vpImageIo::getExtension(sfilename);
194 if (ext.compare(
".PGM") == 0)
196 else if (ext.compare(
".pgm") == 0)
198 else if (ext.compare(
".PPM") == 0)
200 else if (ext.compare(
".ppm") == 0)
202 else if (ext.compare(
".JPG") == 0)
204 else if (ext.compare(
".jpg") == 0)
206 else if (ext.compare(
".JPEG") == 0)
208 else if (ext.compare(
".jpeg") == 0)
210 else if (ext.compare(
".PNG") == 0)
212 else if (ext.compare(
".png") == 0)
215 else if (ext.compare(
".TIFF") == 0)
217 else if (ext.compare(
".tiff") == 0)
219 else if (ext.compare(
".TIF") == 0)
221 else if (ext.compare(
".tif") == 0)
223 else if (ext.compare(
".BMP") == 0)
225 else if (ext.compare(
".bmp") == 0)
227 else if (ext.compare(
".DIB") == 0)
229 else if (ext.compare(
".dib") == 0)
231 else if (ext.compare(
".PBM") == 0)
233 else if (ext.compare(
".pbm") == 0)
235 else if (ext.compare(
".SR") == 0)
236 return FORMAT_RASTER;
237 else if (ext.compare(
".sr") == 0)
238 return FORMAT_RASTER;
239 else if (ext.compare(
".RAS") == 0)
240 return FORMAT_RASTER;
241 else if (ext.compare(
".ras") == 0)
242 return FORMAT_RASTER;
243 else if (ext.compare(
".JP2") == 0)
244 return FORMAT_JPEG2000;
245 else if (ext.compare(
".jp2") == 0)
246 return FORMAT_JPEG2000;
248 return FORMAT_UNKNOWN;
252 std::string vpImageIo::getExtension(
const std::string &filename)
255 size_t dot = filename.find_last_of(
".");
256 std::string ext = filename.substr(dot, filename.size()-1);
282 std::string message =
"Cannot read file: \"" + std::string(filename) +
"\" doesn't exist";
285 bool try_opencv_reader =
false;
287 switch(getFormat(filename)){
293 #ifdef VISP_HAVE_LIBJPEG
296 try_opencv_reader =
true;
300 #if defined(VISP_HAVE_LIBPNG)
303 try_opencv_reader =
true;
311 case FORMAT_JPEG2000 :
312 case FORMAT_UNKNOWN :
313 try_opencv_reader =
true;
317 if (try_opencv_reader) {
318 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
320 cv::Mat cvI = cv::imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
323 std::string message =
"Cannot read file \"" + std::string(filename) +
"\": Image format not supported";
347 read(I,filename.c_str());
370 std::string message =
"Cannot read file: \"" + std::string(filename) +
"\" doesn't exist";
374 bool try_opencv_reader =
false;
376 switch(getFormat(filename)){
382 #ifdef VISP_HAVE_LIBJPEG
385 try_opencv_reader =
true;
389 #if defined(VISP_HAVE_LIBPNG)
392 try_opencv_reader =
true;
400 case FORMAT_JPEG2000 :
401 case FORMAT_UNKNOWN :
402 try_opencv_reader =
true;
406 if (try_opencv_reader) {
407 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
409 cv::Mat cvI = cv::imread(filename, CV_LOAD_IMAGE_COLOR);
412 std::string message =
"Cannot read file \"" + std::string(filename) +
"\": Image format not supported";
436 read(I,filename.c_str());
454 bool try_opencv_writer =
false;
456 switch(getFormat(filename)){
462 #ifdef VISP_HAVE_LIBJPEG
465 try_opencv_writer =
true;
469 #ifdef VISP_HAVE_LIBPNG
472 try_opencv_writer =
true;
480 case FORMAT_JPEG2000 :
481 case FORMAT_UNKNOWN :
482 try_opencv_writer =
true;
486 if (try_opencv_writer) {
487 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
491 cv::imwrite(filename, cvI);
493 vpCERROR <<
"Cannot write file: Image format not supported..." << std::endl;
495 "Cannot write file: Image format not supported")) ;
514 write(I,filename.c_str());
531 bool try_opencv_writer =
false;
533 switch(getFormat(filename)){
539 #ifdef VISP_HAVE_LIBJPEG
542 try_opencv_writer =
true;
546 #ifdef VISP_HAVE_LIBPNG
549 try_opencv_writer =
true;
557 case FORMAT_JPEG2000 :
558 case FORMAT_UNKNOWN :
559 try_opencv_writer =
true;
563 if (try_opencv_writer) {
564 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
568 cv::imwrite(filename, cvI);
570 vpCERROR <<
"Cannot write file: Image format not supported..." << std::endl;
572 "Cannot write file: Image format not supported")) ;
591 write(I,filename.c_str());
608 const char *filename)
614 if (filename ==
'\0') {
620 fd = fopen(filename,
"wb");
625 "cannot write file")) ;
631 fprintf(fd,
"255\n");
637 ierr = fwrite(I.
bitmap,
sizeof(
float), nbyte, fd) ;
643 "cannot write file")) ;
664 const char *filename)
670 if (filename ==
'\0') {
676 fd = fopen(filename,
"wb");
681 "cannot write file")) ;
687 fprintf(fd,
"255\n");
693 ierr = fwrite(I.
bitmap,
sizeof(
unsigned char), nbyte, fd) ;
699 "cannot write file")) ;
722 for (
unsigned int i=0 ; i < nrows * ncols ; i++)
745 if (filename ==
'\0') {
751 fd = fopen(filename,
"wb");
756 "cannot write file")) ;
762 fprintf(fd,
"255\n");
772 ierr = fwrite(Itmp.
bitmap,
sizeof(
unsigned char), nbyte, fd) ;
778 "cannot write file")) ;
814 if (filename ==
'\0')
823 fd = fopen(filename,
"rb");
828 "couldn't read file")) ;
834 err = fgets(str, vpMAX_LEN - 1, fd);
839 vpERROR_TRACE(
"couldn't read line %d of file \"%s\"\n", line, filename) ;
841 "couldn't read file")) ;
849 "this is not a PFM file")) ;
853 if (strcmp(str,
"P8") != 0)
858 "this is not a PFM file")) ;
863 err = fgets(str, vpMAX_LEN - 1, fd);
866 fprintf(stderr,
"couldn't read line %d of file \"%s\"\n", line, filename);
869 "Cannot read content of PFM file")) ;
871 }
while ((str[0] ==
'#') || (str[0] ==
'\n'));
874 ierr = sscanf(str,
"%d %d", &w, &h);
875 if (w > 100000 || h>100000) {
882 err = fgets(str, vpMAX_LEN - 1, fd);
885 fprintf(stderr,
"couldn't read line %d of file \"%s\"\n", line, filename);
888 "Cannot read content of PFM file")) ;
890 }
while ((str[0] ==
'#') || (str[0] ==
'\n'));
891 ierr = sscanf(str,
"%d", &h);
896 vpERROR_TRACE(
"couldn't read line %d of file \"%s\"\n",line, filename) ;
898 "Cannot read content of PFM file")) ;
911 "Cannot read content of PFM file")) ;
916 err = fgets(str, vpMAX_LEN - 1, fd);
920 vpERROR_TRACE(
"couldn't read line %d of file \"%s\"\n",line, filename) ;
922 "Cannot read content of PFM file")) ;
925 ierr = sscanf(str,
"%d", &is255);
928 vpERROR_TRACE(
"couldn't read line %d of file \"%s\"\n", line, filename) ;
930 "Cannot read content of PFM file")) ;
936 vpERROR_TRACE(
"MAX_VAL is not 255 in file \"%s\"\n", filename) ;
938 "Cannot read content of PFM file")) ;
942 if (fread (I.
bitmap,
sizeof(
float), nbyte, fd ) != nbyte)
945 vpERROR_TRACE(
"couldn't read %d bytes in file \"%s\"\n", nbyte, filename) ;
947 "Cannot read content of PFM file")) ;
978 unsigned int magic=5, w=0, h=0, maxval=255;
981 if (filename ==
'\0') {
987 if ((fd = fopen(filename,
"rb")) == NULL) {
989 "Cannot read file \"%s\"", filename)) ;
992 while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] ==
'#') || (str[0] ==
'\n'))) {};
996 "Cannot read header of file \"%s\"", filename));
998 if ((ierr = sscanf(str,
"P%u %u %u %u", &magic, &w, &h, &maxval)) == 0) {
1001 "Cannot read header of file \"%s\"", filename));
1007 "\"%s\" is not a PGM P5 file", filename));
1018 while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] ==
'#') || (str[0] ==
'\n'))) {};
1022 "Cannot read header of file \"%s\"", filename));
1024 if (((ierr = sscanf(str,
"%u %u %u", &w, &h, &maxval)) == 0) || (ierr != 1 && ierr != 2 && ierr != 3)) {
1027 "Cannot read header of file \"%s\"", filename));
1035 while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] ==
'#') || (str[0] ==
'\n'))) {};
1039 "Cannot read header of file \"%s\"", filename));
1041 if (((ierr = sscanf(str,
"%u %u", &h, &maxval)) == 0) || (ierr != 1 && ierr != 2)) {
1044 "Cannot read header of file \"%s\"", filename));
1048 while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] ==
'#') || (str[0] ==
'\n'))) {};
1052 "Cannot read header of file \"%s\"", filename));
1054 if ((ierr = sscanf(str,
"%u", &maxval)) != 1) {
1057 "Cannot read header of file \"%s\"", filename));
1064 else if (ierr == 2) {
1067 while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] ==
'#') || (str[0] ==
'\n'))) {};
1071 "Cannot read header of file \"%s\"", filename));
1073 if ((ierr = sscanf(str,
"%u", &maxval)) != 1) {
1076 "Cannot read header of file \"%s\"", filename));
1084 else if (ierr == 2) {
1086 while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] ==
'#') || (str[0] ==
'\n'))) {};
1090 "Cannot read header of file \"%s\"", filename));
1092 if (((ierr = sscanf(str,
"%u %u", &h, &maxval)) == 0) || (ierr != 1 && ierr != 2)) {
1095 "Cannot read header of file \"%s\"", filename));
1099 while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] ==
'#') || (str[0] ==
'\n'))) {};
1103 "Cannot read header of file \"%s\"", filename));
1105 if ((ierr = sscanf(str,
"%u", &maxval)) != 1) {
1108 "Cannot read header of file \"%s\"", filename));
1116 else if (ierr == 3) {
1118 while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] ==
'#') || (str[0] ==
'\n'))) {};
1122 "Cannot read header of file \"%s\"", filename));
1124 if ((ierr = sscanf(str,
"%u", &maxval)) != 1) {
1127 "Cannot read header of file \"%s\"", filename));
1135 if (w > 100000 || h>100000) {
1143 "Bad maxval in \"%s\"", filename));
1152 if ((n = fread (I.
bitmap,
sizeof(
unsigned char), nbyte, fd)) != nbyte) {
1155 "Read only %d of %d bytes in file \"%s\"", n, nbyte, filename));
1257 char str[vpMAX_LEN];
1258 unsigned int magic=5, w=0, h=0, maxval=255;
1261 if (filename ==
'\0') {
1267 if ((fd = fopen(filename,
"rb")) == NULL) {
1269 "Cannot read file \"%s\"", filename)) ;
1272 while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] ==
'#') || (str[0] ==
'\n'))) {};
1276 "Cannot read header of file \"%s\"", filename));
1278 if ((ierr = sscanf(str,
"P%u %u %u %u", &magic, &w, &h, &maxval)) == 0) {
1281 "Cannot read header of file \"%s\"", filename));
1287 "\"%s\" is not a PGM P6 file", filename));
1298 while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] ==
'#') || (str[0] ==
'\n'))) {};
1302 "Cannot read header of file \"%s\"", filename));
1304 if (((ierr = sscanf(str,
"%u %u %u", &w, &h, &maxval)) == 0) || (ierr != 1 && ierr != 2 && ierr != 3)) {
1307 "Cannot read header of file \"%s\"", filename));
1315 while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] ==
'#') || (str[0] ==
'\n'))) {};
1319 "Cannot read header of file \"%s\"", filename));
1321 if (((ierr = sscanf(str,
"%u %u", &h, &maxval)) == 0) || (ierr != 1 && ierr != 2)) {
1324 "Cannot read header of file \"%s\"", filename));
1328 while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] ==
'#') || (str[0] ==
'\n'))) {};
1332 "Cannot read header of file \"%s\"", filename));
1334 if ((ierr = sscanf(str,
"%u", &maxval)) != 1) {
1337 "Cannot read header of file \"%s\"", filename));
1344 else if (ierr == 2) {
1347 while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] ==
'#') || (str[0] ==
'\n'))) {};
1351 "Cannot read header of file \"%s\"", filename));
1353 if ((ierr = sscanf(str,
"%u", &maxval)) != 1) {
1356 "Cannot read header of file \"%s\"", filename));
1364 else if (ierr == 2) {
1366 while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] ==
'#') || (str[0] ==
'\n'))) {};
1370 "Cannot read header of file \"%s\"", filename));
1372 if (((ierr = sscanf(str,
"%u %u", &h, &maxval)) == 0) || (ierr != 1 && ierr != 2)) {
1375 "Cannot read header of file \"%s\"", filename));
1379 while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] ==
'#') || (str[0] ==
'\n'))) {};
1383 "Cannot read header of file \"%s\"", filename));
1385 if ((ierr = sscanf(str,
"%u", &maxval)) != 1) {
1388 "Cannot read header of file \"%s\"", filename));
1396 else if (ierr == 3) {
1398 while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] ==
'#') || (str[0] ==
'\n'))) {};
1402 "Cannot read header of file \"%s\"", filename));
1404 if ((ierr = sscanf(str,
"%u", &maxval)) != 1) {
1407 "Cannot read header of file \"%s\"", filename));
1415 if (w > 100000 || h>100000) {
1423 "Bad maxval in \"%s\"", filename));
1430 for(
unsigned int i=0;i<I.
getHeight();i++)
1432 for(
unsigned int j=0;j<I.
getWidth();j++)
1435 size_t res = fread(&v.
R,
sizeof(v.
R),1,fd) ;
1436 res |= fread(&v.
G,
sizeof(v.
G),1,fd) ;
1437 res |= fread(&v.
B,
sizeof(v.
B),1,fd) ;
1442 "Cannot read bytes in file \"%s\"\n", filename));
1496 if (filename ==
'\0') {
1502 f = fopen(filename,
"wb");
1507 "cannot write file")) ;
1514 fprintf(f,
"%d\n",255);
1516 for(
unsigned int i=0;i<I.
getHeight();i++)
1518 for(
unsigned int j=0;j<I.
getWidth();j++)
1525 res = fwrite(&tmp,
sizeof(tmp),1,f) ;
1531 "cannot write file")) ;
1534 res = fwrite(&tmp,
sizeof(tmp),1,f) ;
1540 "cannot write file")) ;
1543 res = fwrite(&tmp,
sizeof(tmp),1,f) ;
1549 "cannot write file")) ;
1612 const std::string filename)
1726 #if defined(VISP_HAVE_LIBJPEG)
1738 struct jpeg_compress_struct cinfo;
1739 struct jpeg_error_mgr jerr;
1742 cinfo.err = jpeg_std_error(&jerr);
1743 jpeg_create_compress(&cinfo);
1746 if (filename ==
'\0') {
1752 file = fopen(filename,
"wb");
1757 "cannot write file")) ;
1763 jpeg_stdio_dest(&cinfo, file);
1765 cinfo.image_width = width;
1766 cinfo.image_height = height;
1767 cinfo.input_components = 1;
1768 cinfo.in_color_space = JCS_GRAYSCALE;
1769 jpeg_set_defaults(&cinfo);
1771 jpeg_start_compress(&cinfo,TRUE);
1773 unsigned char *line;
1774 line =
new unsigned char[width];
1775 unsigned char* input = (
unsigned char*)I.
bitmap;
1776 while (cinfo.next_scanline < cinfo.image_height)
1778 for (
unsigned int i = 0; i < width; i++)
1783 jpeg_write_scanlines(&cinfo, &line, 1);
1786 jpeg_finish_compress(&cinfo);
1787 jpeg_destroy_compress(&cinfo);
1817 struct jpeg_compress_struct cinfo;
1818 struct jpeg_error_mgr jerr;
1821 cinfo.err = jpeg_std_error(&jerr);
1822 jpeg_create_compress(&cinfo);
1825 if (filename ==
'\0') {
1831 file = fopen(filename,
"wb");
1836 "cannot write file")) ;
1842 jpeg_stdio_dest(&cinfo, file);
1844 cinfo.image_width = width;
1845 cinfo.image_height = height;
1846 cinfo.input_components = 3;
1847 cinfo.in_color_space = JCS_RGB;
1848 jpeg_set_defaults(&cinfo);
1850 jpeg_start_compress(&cinfo,TRUE);
1852 unsigned char *line;
1853 line =
new unsigned char[3*width];
1854 unsigned char* input = (
unsigned char*)I.
bitmap;
1855 while (cinfo.next_scanline < cinfo.image_height)
1857 for (
unsigned int i = 0; i < width; i++)
1859 line[i*3] = *(input); input++;
1860 line[i*3+1] = *(input); input++;
1861 line[i*3+2] = *(input); input++;
1864 jpeg_write_scanlines(&cinfo, &line, 1);
1867 jpeg_finish_compress(&cinfo);
1868 jpeg_destroy_compress(&cinfo);
1907 struct jpeg_decompress_struct cinfo;
1908 struct jpeg_error_mgr jerr;
1911 cinfo.err = jpeg_std_error(&jerr);
1912 jpeg_create_decompress(&cinfo);
1915 if (filename ==
'\0') {
1921 file = fopen(filename,
"rb");
1926 "cannot read file")) ;
1929 jpeg_stdio_src(&cinfo, file);
1930 jpeg_read_header(&cinfo, TRUE);
1932 unsigned int width = cinfo.image_width;
1933 unsigned int height = cinfo.image_height;
1938 jpeg_start_decompress(&cinfo);
1940 unsigned int rowbytes = cinfo.output_width * (
unsigned int)(cinfo.output_components);
1941 JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray)
1942 ((j_common_ptr) &cinfo, JPOOL_IMAGE, rowbytes, 1);
1944 if (cinfo.out_color_space == JCS_RGB) {
1946 unsigned char* output = (
unsigned char*)Ic.
bitmap;
1947 while (cinfo.output_scanline<cinfo.output_height) {
1948 jpeg_read_scanlines(&cinfo,buffer,1);
1949 for (
unsigned int i = 0; i < width; i++) {
1950 *(output++) = buffer[0][i*3];
1951 *(output++) = buffer[0][i*3+1];
1952 *(output++) = buffer[0][i*3+2];
1959 else if (cinfo.out_color_space == JCS_GRAYSCALE)
1962 while (cinfo.output_scanline<cinfo.output_height)
1964 row = cinfo.output_scanline;
1965 jpeg_read_scanlines(&cinfo,buffer,1);
1966 memcpy(I[row], buffer[0], rowbytes);
1970 jpeg_finish_decompress(&cinfo);
1971 jpeg_destroy_decompress(&cinfo);
2020 struct jpeg_decompress_struct cinfo;
2021 struct jpeg_error_mgr jerr;
2024 cinfo.err = jpeg_std_error(&jerr);
2025 jpeg_create_decompress(&cinfo);
2028 if (filename ==
'\0') {
2034 file = fopen(filename,
"rb");
2039 "cannot read file")) ;
2042 jpeg_stdio_src(&cinfo, file);
2044 jpeg_read_header(&cinfo, TRUE);
2046 unsigned int width = cinfo.image_width;
2047 unsigned int height = cinfo.image_height;
2052 jpeg_start_decompress(&cinfo);
2054 unsigned int rowbytes = cinfo.output_width * (
unsigned int)(cinfo.output_components);
2055 JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray)
2056 ((j_common_ptr) &cinfo, JPOOL_IMAGE, rowbytes, 1);
2058 if (cinfo.out_color_space == JCS_RGB)
2060 unsigned char* output = (
unsigned char*)I.
bitmap;
2061 while (cinfo.output_scanline<cinfo.output_height)
2063 jpeg_read_scanlines(&cinfo,buffer,1);
2064 for (
unsigned int i = 0; i < width; i++) {
2065 *(output++) = buffer[0][i*3];
2066 *(output++) = buffer[0][i*3+1];
2067 *(output++) = buffer[0][i*3+2];
2073 else if (cinfo.out_color_space == JCS_GRAYSCALE)
2078 while (cinfo.output_scanline<cinfo.output_height)
2080 row = cinfo.output_scanline;
2081 jpeg_read_scanlines(&cinfo,buffer,1);
2082 memcpy(Ig[row], buffer[0], rowbytes);
2088 jpeg_finish_decompress(&cinfo);
2089 jpeg_destroy_decompress(&cinfo);
2118 #elif defined(VISP_HAVE_OPENCV)
2130 IplImage* Ip = NULL;
2133 cvSaveImage(filename, Ip);
2135 cvReleaseImage(&Ip);
2163 IplImage* Ip = NULL;
2166 cvSaveImage(filename, Ip);
2168 cvReleaseImage(&Ip);
2205 IplImage* Ip = NULL;
2206 Ip = cvLoadImage(filename, CV_LOAD_IMAGE_GRAYSCALE);
2211 "Can't read the image")) ;
2212 cvReleaseImage(&Ip);
2260 IplImage* Ip = NULL;
2261 Ip = cvLoadImage(filename, CV_LOAD_IMAGE_COLOR);
2266 "Can't read the image")) ;
2267 cvReleaseImage(&Ip);
2306 #if defined(VISP_HAVE_LIBPNG)
2321 if (filename ==
'\0') {
2327 file = fopen(filename,
"wb");
2332 "cannot write file")) ;
2336 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL, NULL, NULL);
2342 "PNG write error")) ;
2345 png_infop info_ptr = png_create_info_struct(png_ptr);
2349 png_destroy_write_struct (&png_ptr, NULL);
2352 "PNG write error")) ;
2356 if (setjmp (png_jmpbuf (png_ptr)))
2359 png_destroy_write_struct (&png_ptr, &info_ptr);
2362 "PNG write error")) ;
2366 png_init_io (png_ptr, file);
2371 int color_type = PNG_COLOR_TYPE_GRAY;
2374 if (setjmp (png_jmpbuf (png_ptr)))
2377 png_destroy_write_struct (&png_ptr, &info_ptr);
2380 "PNG write error")) ;
2383 png_set_IHDR(png_ptr, info_ptr, width, height,
2384 bit_depth, color_type, PNG_INTERLACE_NONE,
2385 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
2387 png_write_info(png_ptr, info_ptr);
2389 png_bytep* row_ptrs =
new png_bytep[height];
2390 for (
unsigned int i = 0; i < height; i++)
2391 row_ptrs[i] =
new png_byte[width];
2393 unsigned char* input = (
unsigned char*)I.
bitmap;
2395 for (
unsigned int i = 0; i < height; i++)
2397 png_byte* row = row_ptrs[i];
2398 for(
unsigned int j = 0; j < width; j++)
2405 if (setjmp (png_jmpbuf (png_ptr)))
2408 png_destroy_write_struct (&png_ptr, &info_ptr);
2409 for(
unsigned int j = 0; j < height; j++)
2410 delete[] row_ptrs[j];
2415 "PNG write error")) ;
2418 png_write_image(png_ptr, row_ptrs);
2420 if (setjmp (png_jmpbuf (png_ptr)))
2423 png_destroy_write_struct (&png_ptr, &info_ptr);
2424 for(
unsigned int j = 0; j < height; j++)
2425 delete[] row_ptrs[j];
2430 "PNG write error")) ;
2433 png_write_end(png_ptr, NULL);
2435 for(
unsigned int j = 0; j < height; j++)
2436 delete[] row_ptrs[j];
2440 png_destroy_write_struct (&png_ptr, &info_ptr);
2473 if (filename ==
'\0') {
2479 file = fopen(filename,
"wb");
2484 "cannot write file")) ;
2488 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL, NULL, NULL);
2494 "PNG write error")) ;
2497 png_infop info_ptr = png_create_info_struct(png_ptr);
2501 png_destroy_write_struct (&png_ptr, NULL);
2504 "PNG write error")) ;
2508 if (setjmp (png_jmpbuf (png_ptr)))
2511 png_destroy_write_struct (&png_ptr, &info_ptr);
2514 "PNG write error")) ;
2518 png_init_io (png_ptr, file);
2523 int color_type = PNG_COLOR_TYPE_RGB;
2526 if (setjmp (png_jmpbuf (png_ptr)))
2529 png_destroy_write_struct (&png_ptr, &info_ptr);
2532 "PNG write error")) ;
2535 png_set_IHDR(png_ptr, info_ptr, width, height,
2536 bit_depth, color_type, PNG_INTERLACE_NONE,
2537 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
2539 png_write_info(png_ptr, info_ptr);
2541 png_bytep* row_ptrs =
new png_bytep[height];
2542 for (
unsigned int i = 0; i < height; i++)
2543 row_ptrs[i] =
new png_byte[3*width];
2545 unsigned char* input = (
unsigned char*)I.
bitmap;;
2547 for (
unsigned int i = 0; i < height; i++)
2549 png_byte* row = row_ptrs[i];
2550 for(
unsigned int j = 0; j < width; j++)
2552 row[3*j] = *(input);input++;
2553 row[3*j+1] = *(input);input++;
2554 row[3*j+2] = *(input);input++;
2559 if (setjmp (png_jmpbuf (png_ptr)))
2562 png_destroy_write_struct (&png_ptr, &info_ptr);
2563 for(
unsigned int j = 0; j < height; j++)
2564 delete[] row_ptrs[j];
2569 "PNG write error")) ;
2572 png_write_image(png_ptr, row_ptrs);
2574 if (setjmp (png_jmpbuf (png_ptr)))
2577 png_destroy_write_struct (&png_ptr, &info_ptr);
2578 for(
unsigned int j = 0; j < height; j++)
2579 delete[] row_ptrs[j];
2584 "PNG write error")) ;
2587 png_write_end(png_ptr, NULL);
2589 for(
unsigned int j = 0; j < height; j++)
2590 delete[] row_ptrs[j];
2594 png_destroy_write_struct (&png_ptr, &info_ptr);
2635 if (filename ==
'\0') {
2641 file = fopen(filename,
"rb");
2646 "cannot read file")) ;
2650 if (fread (magic, 1,
sizeof (magic), file) !=
sizeof (magic))
2653 vpERROR_TRACE(
"couldn't read magic number in file \"%s\"\n", filename) ;
2655 "error reading png file")) ;
2659 if (png_sig_cmp (magic,0,
sizeof (magic)))
2661 fprintf (stderr,
"error: \"%s\" is not a valid PNG image!\n",filename);
2664 "error reading png file")) ;
2669 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
2670 if (png_ptr == NULL)
2672 fprintf (stderr,
"error: can't create a png read structure!\n");
2675 "error reading png file")) ;
2679 png_infop info_ptr = png_create_info_struct (png_ptr);
2680 if (info_ptr == NULL)
2682 fprintf (stderr,
"error: can't create a png info structure!\n");
2684 png_destroy_read_struct (&png_ptr, NULL, NULL);
2686 "error reading png file")) ;
2690 if (setjmp (png_jmpbuf (png_ptr)))
2693 png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
2696 "PNG read error")) ;
2700 png_init_io (png_ptr, file);
2703 png_set_sig_bytes (png_ptr,
sizeof (magic));
2706 png_read_info (png_ptr, info_ptr);
2708 unsigned int width = png_get_image_width(png_ptr, info_ptr);
2709 unsigned int height = png_get_image_height(png_ptr, info_ptr);
2711 unsigned int bit_depth, channels, color_type;
2713 bit_depth = png_get_bit_depth (png_ptr, info_ptr);
2714 channels = png_get_channels(png_ptr, info_ptr);
2715 color_type = png_get_color_type (png_ptr, info_ptr);
2718 if (color_type == PNG_COLOR_TYPE_PALETTE)
2719 png_set_palette_to_rgb (png_ptr);
2722 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
2723 png_set_expand (png_ptr);
2728 if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2729 png_set_strip_alpha(png_ptr);
2731 if (bit_depth == 16)
2732 png_set_strip_16 (png_ptr);
2733 else if (bit_depth < 8)
2734 png_set_packing (png_ptr);
2737 png_read_update_info (png_ptr, info_ptr);
2739 channels = png_get_channels(png_ptr, info_ptr);
2744 png_bytep* rowPtrs =
new png_bytep[height];
2746 unsigned int stride = width * bit_depth * channels / 8;
2747 unsigned char* data =
new unsigned char[stride * height];
2749 for (
unsigned int i =0; i < height; i++)
2750 rowPtrs[i] = (png_bytep)data + (i * stride);
2752 png_read_image(png_ptr, rowPtrs);
2755 unsigned char* output;
2760 output = (
unsigned char*)I.
bitmap;
2761 for (
unsigned int i = 0; i < width*height; i++)
2763 *(output++) = data[i];
2767 output = (
unsigned char*)I.
bitmap;
2768 for (
unsigned int i = 0; i < width*height; i++)
2770 *(output++) = data[i*2];
2775 output = (
unsigned char*)Ic.
bitmap;
2776 for (
unsigned int i = 0; i < width*height; i++)
2778 *(output++) = data[i*3];
2779 *(output++) = data[i*3+1];
2780 *(output++) = data[i*3+2];
2786 output = (
unsigned char*)Ic.
bitmap;
2787 for (
unsigned int i = 0; i < width*height; i++)
2789 *(output++) = data[i*4];
2790 *(output++) = data[i*4+1];
2791 *(output++) = data[i*4+2];
2792 *(output++) = data[i*4+3];
2798 delete [] (png_bytep)rowPtrs;
2800 png_read_end (png_ptr, NULL);
2801 png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
2854 if (filename ==
'\0') {
2860 file = fopen(filename,
"rb");
2865 "cannot read file")) ;
2869 if (fread (magic, 1,
sizeof (magic), file) !=
sizeof (magic))
2872 vpERROR_TRACE(
"couldn't read magic number in file \"%s\"\n", filename) ;
2874 "error reading pgm file")) ;
2878 if (png_sig_cmp (magic,0,
sizeof (magic)))
2881 vpERROR_TRACE(
"error: \"%s\" is not a valid PNG image!\n",filename);
2883 "PNG read error")) ;
2887 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
2893 "PNG read error")) ;
2897 png_infop info_ptr = png_create_info_struct (png_ptr);
2901 png_destroy_read_struct (&png_ptr, NULL, NULL);
2904 "PNG read error")) ;
2908 if (setjmp (png_jmpbuf (png_ptr)))
2911 png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
2914 "PNG read error")) ;
2918 png_init_io (png_ptr, file);
2921 png_set_sig_bytes (png_ptr,
sizeof (magic));
2924 png_read_info (png_ptr, info_ptr);
2926 unsigned int width = png_get_image_width(png_ptr, info_ptr);
2927 unsigned int height = png_get_image_height(png_ptr, info_ptr);
2929 unsigned int bit_depth, channels, color_type;
2931 bit_depth = png_get_bit_depth (png_ptr, info_ptr);
2932 channels = png_get_channels(png_ptr, info_ptr);
2933 color_type = png_get_color_type (png_ptr, info_ptr);
2936 if (color_type == PNG_COLOR_TYPE_PALETTE)
2937 png_set_palette_to_rgb (png_ptr);
2940 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
2941 png_set_expand (png_ptr);
2946 if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2947 png_set_strip_alpha(png_ptr);
2949 if (bit_depth == 16)
2950 png_set_strip_16 (png_ptr);
2951 else if (bit_depth < 8)
2952 png_set_packing (png_ptr);
2955 png_read_update_info (png_ptr, info_ptr);
2957 channels = png_get_channels(png_ptr, info_ptr);
2962 png_bytep* rowPtrs =
new png_bytep[height];
2964 unsigned int stride = width * bit_depth * channels / 8;
2965 unsigned char* data =
new unsigned char[stride * height];
2968 for (
unsigned int i =0; i < height; i++)
2969 rowPtrs[i] = (png_bytep)data + (i * stride);
2971 png_read_image(png_ptr, rowPtrs);
2974 unsigned char* output;
2979 output = (
unsigned char*)Ig.
bitmap;
2980 for (
unsigned int i = 0; i < width*height; i++)
2982 *(output++) = data[i];
2987 output = (
unsigned char*)Ig.
bitmap;
2988 for (
unsigned int i = 0; i < width*height; i++)
2990 *(output++) = data[i*2];
2996 output = (
unsigned char*)I.
bitmap;
2997 for (
unsigned int i = 0; i < width*height; i++)
2999 *(output++) = data[i*3];
3000 *(output++) = data[i*3+1];
3001 *(output++) = data[i*3+2];
3006 output = (
unsigned char*)I.
bitmap;
3007 for (
unsigned int i = 0; i < width*height; i++)
3009 *(output++) = data[i*4];
3010 *(output++) = data[i*4+1];
3011 *(output++) = data[i*4+2];
3012 *(output++) = data[i*4+3];
3017 delete [] (png_bytep)rowPtrs;
3019 png_read_end (png_ptr, NULL);
3020 png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
3049 #elif defined(VISP_HAVE_OPENCV)
3061 IplImage* Ip = NULL;
3064 cvSaveImage(filename, Ip);
3066 cvReleaseImage(&Ip);
3094 IplImage* Ip = NULL;
3097 cvSaveImage(filename, Ip);
3099 cvReleaseImage(&Ip);
3136 IplImage* Ip = NULL;
3137 Ip = cvLoadImage(filename, CV_LOAD_IMAGE_GRAYSCALE);
3142 "Can't read the image")) ;
3143 cvReleaseImage(&Ip);
3191 IplImage* Ip = NULL;
3192 Ip = cvLoadImage(filename, CV_LOAD_IMAGE_COLOR);
3197 "Can't read the image")) ;
3198 cvReleaseImage(&Ip);
static void write(const vpImage< unsigned char > &I, const char *filename)
static void writeJPEG(const vpImage< unsigned char > &I, const char *filename)
static void readPGM(vpImage< unsigned char > &I, const char *filename)
unsigned int getWidth() const
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
unsigned char B
Blue component.
Type * bitmap
points toward the bitmap
static void writePGM(const vpImage< unsigned char > &I, const char *filename)
static void writePNG(const vpImage< unsigned char > &I, const char *filename)
error that can be emited by ViSP classes.
static void writePFM(const vpImage< float > &I, const char *filename)
Error that can be emited by the vpImage class and its derivates.
static void readPFM(vpImage< float > &I, const char *filename)
unsigned char G
Green component.
Class that defines a RGB 32 bits structure.
void resize(const unsigned int h, const unsigned int w)
set the size of the image
static void readPNG(vpImage< unsigned char > &I, const char *filename)
static void readJPEG(vpImage< unsigned char > &I, const char *filename)
unsigned char R
Red component.
static void readPPM(vpImage< unsigned char > &I, const char *filename)
unsigned int getHeight() const
static void writePPM(const vpImage< unsigned char > &I, const char *filename)
static void read(vpImage< unsigned char > &I, const char *filename)