43 #include <visp3/core/vpImage.h>
44 #include <visp3/io/vpImageIo.h>
45 #include <visp3/core/vpImageConvert.h>
46 #include <visp3/core/vpIoTools.h>
48 void vp_decodeHeaderPNM(
const std::string &filename, std::ifstream &fd,
const std::string &magic,
49 unsigned int &w,
unsigned int &h,
unsigned int &maxval);
51 #ifndef DOXYGEN_SHOULD_SKIP_THIS
61 void vp_decodeHeaderPNM(
const std::string &filename, std::ifstream &fd,
const std::string &magic,
unsigned int &w,
unsigned int &h,
unsigned int &maxval)
64 unsigned int nb_elt = 4, cpt_elt=0;
65 while(cpt_elt != nb_elt) {
67 while (std::getline(fd, line) && (line.compare(0, 1,
"#") == 0 || line.size() == 0)) {};
72 "Cannot read header of file \"%s\"", filename.c_str()));
77 if(header.size() == 0) {
80 "Cannot read header of file \"%s\"", filename.c_str()));
84 if (header[0].compare(0, magic.size(), magic) != 0) {
87 "\"%s\" is not a PNM file with magic number %s", filename.c_str(), magic.c_str()));
90 header.erase(header.begin(), header.begin()+1);
92 while(header.size()) {
94 std::istringstream ss(header[0]);
97 header.erase(header.begin(), header.begin()+1);
99 else if (cpt_elt == 2) {
100 std::istringstream ss(header[0]);
103 header.erase(header.begin(), header.begin()+1);
105 else if (cpt_elt == 3) {
106 std::istringstream ss(header[0]);
109 header.erase(header.begin(), header.begin()+1);
116 vpImageIo::vpImageFormatType
117 vpImageIo::getFormat(
const std::string &filename)
119 std::string ext = vpImageIo::getExtension(filename);
121 if (ext.compare(
".PGM") == 0)
123 else if (ext.compare(
".pgm") == 0)
125 else if (ext.compare(
".PPM") == 0)
127 else if (ext.compare(
".ppm") == 0)
129 else if (ext.compare(
".JPG") == 0)
131 else if (ext.compare(
".jpg") == 0)
133 else if (ext.compare(
".JPEG") == 0)
135 else if (ext.compare(
".jpeg") == 0)
137 else if (ext.compare(
".PNG") == 0)
139 else if (ext.compare(
".png") == 0)
142 else if (ext.compare(
".TIFF") == 0)
144 else if (ext.compare(
".tiff") == 0)
146 else if (ext.compare(
".TIF") == 0)
148 else if (ext.compare(
".tif") == 0)
150 else if (ext.compare(
".BMP") == 0)
152 else if (ext.compare(
".bmp") == 0)
154 else if (ext.compare(
".DIB") == 0)
156 else if (ext.compare(
".dib") == 0)
158 else if (ext.compare(
".PBM") == 0)
160 else if (ext.compare(
".pbm") == 0)
162 else if (ext.compare(
".SR") == 0)
163 return FORMAT_RASTER;
164 else if (ext.compare(
".sr") == 0)
165 return FORMAT_RASTER;
166 else if (ext.compare(
".RAS") == 0)
167 return FORMAT_RASTER;
168 else if (ext.compare(
".ras") == 0)
169 return FORMAT_RASTER;
170 else if (ext.compare(
".JP2") == 0)
171 return FORMAT_JPEG2000;
172 else if (ext.compare(
".jp2") == 0)
173 return FORMAT_JPEG2000;
175 return FORMAT_UNKNOWN;
179 std::string vpImageIo::getExtension(
const std::string &filename)
182 size_t dot = filename.find_last_of(
".");
183 std::string ext = filename.substr(dot, filename.size()-1);
209 std::string message =
"Cannot read file: \"" + std::string(filename) +
"\" doesn't exist";
216 bool try_opencv_reader =
false;
218 switch(getFormat(final_filename.c_str())){
220 readPGM(I,final_filename);
break;
222 readPPM(I,final_filename);
break;
224 #ifdef VISP_HAVE_JPEG
227 try_opencv_reader =
true;
231 #if defined(VISP_HAVE_PNG)
234 try_opencv_reader =
true;
242 case FORMAT_JPEG2000 :
243 case FORMAT_UNKNOWN :
244 try_opencv_reader =
true;
248 if (try_opencv_reader) {
249 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
251 cv::Mat cvI = cv::imread(final_filename, cv::IMREAD_GRAYSCALE);
252 if (cvI.cols == 0 && cvI.rows == 0) {
253 std::string message =
"Cannot read file \"" + std::string(final_filename) +
"\": Image format not supported";
257 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
259 cv::Mat cvI = cv::imread(final_filename, CV_LOAD_IMAGE_GRAYSCALE);
260 if (cvI.cols == 0 && cvI.rows == 0) {
261 std::string message =
"Cannot read file \"" + std::string(final_filename) +
"\": Image format not supported";
266 std::string message =
"Cannot read file \"" + std::string(final_filename) +
"\": Image format not supported";
293 std::string message =
"Cannot read file: \"" + std::string(filename) +
"\" doesn't exist";
299 bool try_opencv_reader =
false;
301 switch(getFormat(final_filename.c_str())){
303 readPGM(I,final_filename);
break;
305 readPPM(I,final_filename);
break;
307 #ifdef VISP_HAVE_JPEG
310 try_opencv_reader =
true;
314 #if defined(VISP_HAVE_PNG)
317 try_opencv_reader =
true;
325 case FORMAT_JPEG2000 :
326 case FORMAT_UNKNOWN :
327 try_opencv_reader =
true;
331 if (try_opencv_reader) {
332 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
334 cv::Mat cvI = cv::imread(final_filename, cv::IMREAD_COLOR);
335 if (cvI.cols == 0 && cvI.rows == 0) {
336 std::string message =
"Cannot read file \"" + std::string(final_filename) +
"\": Image format not supported";
340 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
342 cv::Mat cvI = cv::imread(final_filename, CV_LOAD_IMAGE_COLOR);
343 if (cvI.cols == 0 && cvI.rows == 0) {
344 std::string message =
"Cannot read file \"" + std::string(final_filename) +
"\": Image format not supported";
349 std::string message =
"Cannot read file \"" + std::string(final_filename) +
"\": Image format not supported";
370 bool try_opencv_writer =
false;
372 switch(getFormat(filename)){
378 #ifdef VISP_HAVE_JPEG
381 try_opencv_writer =
true;
388 try_opencv_writer =
true;
396 case FORMAT_JPEG2000 :
397 case FORMAT_UNKNOWN :
398 try_opencv_writer =
true;
402 if (try_opencv_writer) {
403 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
407 cv::imwrite(filename, cvI);
409 vpCERROR <<
"Cannot write file: Image format not supported..." << std::endl;
411 "Cannot write file: Image format not supported")) ;
431 bool try_opencv_writer =
false;
433 switch(getFormat(filename)){
439 #ifdef VISP_HAVE_JPEG
442 try_opencv_writer =
true;
449 try_opencv_writer =
true;
457 case FORMAT_JPEG2000 :
458 case FORMAT_UNKNOWN :
459 try_opencv_writer =
true;
463 if (try_opencv_writer) {
464 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
468 cv::imwrite(filename, cvI);
470 vpCERROR <<
"Cannot write file: Image format not supported..." << std::endl;
472 "Cannot write file: Image format not supported")) ;
496 if (filename.empty()) {
498 "Cannot write PFM image: filename empty")) ;
501 fd = fopen(filename.c_str(),
"wb");
505 "Cannot create PFM file \"%s\"", filename.c_str())) ;
511 fprintf(fd,
"255\n");
517 ierr = fwrite(I.
bitmap,
sizeof(
float), nbyte, fd) ;
521 "Cannot save PFM file \"%s\": only %d bytes over %d saved ", filename.c_str(), ierr, nbyte)) ;
547 if (filename.empty()) {
549 "Cannot create PGM file: filename empty")) ;
552 fd = fopen(filename.c_str(),
"wb");
556 "Cannot create PGM file \"%s\"", filename.c_str())) ;
562 fprintf(fd,
"255\n");
568 ierr = fwrite(I.
bitmap,
sizeof(
unsigned char), nbyte, fd) ;
572 "Cannot save PGM file \"%s\": only %d over %d bytes saved", filename.c_str(), ierr, nbyte)) ;
595 for (
unsigned int i=0 ; i < nrows * ncols ; i++)
616 if (filename.empty()) {
618 "Cannot create PGM file: filename empty")) ;
621 fd = fopen(filename.c_str(),
"wb");
625 "Cannot create PGM file \"%s\"", filename.c_str())) ;
631 fprintf(fd,
"255\n");
640 ierr = fwrite(Itmp.
bitmap,
sizeof(
unsigned char), nbyte, fd) ;
644 "Cannot save PGM file \"%s\": only %d over %d bytes saved", filename.c_str(), ierr, nbyte)) ;
670 unsigned int w=0, h=0, maxval=0;
671 unsigned int w_max = 100000, h_max = 100000, maxval_max = 255;
672 std::string magic(
"P8");
674 std::ifstream fd(filename.c_str(), std::ios::binary);
681 vp_decodeHeaderPNM(filename, fd, magic, w, h, maxval);
683 if (w > w_max || h > h_max) {
687 if (maxval > maxval_max)
691 "Bad maxval in \"%s\"", filename.c_str()));
699 fd.read ((
char *)I.
bitmap,
sizeof(
float) * nbyte);
703 "Read only %d of %d bytes in file \"%s\"", fd.gcount(), nbyte, filename.c_str()));
728 unsigned int w=0, h=0, maxval=0;
729 unsigned int w_max = 100000, h_max = 100000, maxval_max = 255;
730 std::string magic(
"P5");
732 std::ifstream fd(filename.c_str(), std::ios::binary);
739 vp_decodeHeaderPNM(filename, fd, magic, w, h, maxval);
741 if (w > w_max || h > h_max) {
745 if (maxval > maxval_max)
749 "Bad maxval in \"%s\"", filename.c_str()));
757 fd.read ((
char *)I.
bitmap, nbyte);
761 "Read only %d of %d bytes in file \"%s\"", fd.gcount(), nbyte, filename.c_str()));
841 unsigned int w=0, h=0, maxval=0;
842 unsigned int w_max = 100000, h_max = 100000, maxval_max = 255;
843 std::string magic(
"P6");
845 std::ifstream fd(filename.c_str(), std::ios::binary);
852 vp_decodeHeaderPNM(filename, fd, magic, w, h, maxval);
854 if (w > w_max || h > h_max) {
858 if (maxval > maxval_max)
862 "Bad maxval in \"%s\"", filename.c_str()));
869 for(
unsigned int i=0;i<I.
getHeight();i++) {
870 for(
unsigned int j=0;j<I.
getWidth();j++) {
871 unsigned char rgb[3];
872 fd.read((
char *)&rgb, 3);
877 "Read only %d of %d bytes in file \"%s\"",
878 (i*I.
getWidth() + j)*3 + fd.gcount(), I.
getSize()*3, filename.c_str()));
925 if (filename.empty()) {
927 "Cannot create PPM file: filename empty")) ;
930 f = fopen(filename.c_str(),
"wb");
934 "Cannot create PPM file \"%s\"", filename.c_str())) ;
939 fprintf(f,
"%d\n", 255);
941 for(
unsigned int i=0;i<I.
getHeight();i++)
943 for(
unsigned int j=0;j<I.
getWidth();j++)
946 unsigned char rgb[3];
951 size_t res = fwrite(&rgb, 1, 3, f);
955 "cannot write file \"%s\"", filename.c_str())) ;
968 #if defined(VISP_HAVE_JPEG)
980 struct jpeg_compress_struct cinfo;
981 struct jpeg_error_mgr jerr;
984 cinfo.err = jpeg_std_error(&jerr);
985 jpeg_create_compress(&cinfo);
988 if (filename.empty()) {
990 "Cannot create JPEG file: filename empty")) ;
993 file = fopen(filename.c_str(),
"wb");
997 "Cannot create JPEG file \"%s\"", filename.c_str())) ;
1003 jpeg_stdio_dest(&cinfo, file);
1005 cinfo.image_width = width;
1006 cinfo.image_height = height;
1007 cinfo.input_components = 1;
1008 cinfo.in_color_space = JCS_GRAYSCALE;
1009 jpeg_set_defaults(&cinfo);
1011 jpeg_start_compress(&cinfo,TRUE);
1013 unsigned char *line;
1014 line =
new unsigned char[width];
1015 unsigned char* input = (
unsigned char*)I.
bitmap;
1016 while (cinfo.next_scanline < cinfo.image_height)
1018 for (
unsigned int i = 0; i < width; i++)
1023 jpeg_write_scanlines(&cinfo, &line, 1);
1026 jpeg_finish_compress(&cinfo);
1027 jpeg_destroy_compress(&cinfo);
1042 struct jpeg_compress_struct cinfo;
1043 struct jpeg_error_mgr jerr;
1046 cinfo.err = jpeg_std_error(&jerr);
1047 jpeg_create_compress(&cinfo);
1050 if (filename.empty()) {
1052 "Cannot create JPEG file: filename empty")) ;
1055 file = fopen(filename.c_str(),
"wb");
1059 "Cannot create JPEG file \"%s\"", filename.c_str())) ;
1065 jpeg_stdio_dest(&cinfo, file);
1067 cinfo.image_width = width;
1068 cinfo.image_height = height;
1069 cinfo.input_components = 3;
1070 cinfo.in_color_space = JCS_RGB;
1071 jpeg_set_defaults(&cinfo);
1073 jpeg_start_compress(&cinfo,TRUE);
1075 unsigned char *line;
1076 line =
new unsigned char[3*width];
1077 unsigned char* input = (
unsigned char*)I.
bitmap;
1078 while (cinfo.next_scanline < cinfo.image_height)
1080 for (
unsigned int i = 0; i < width; i++)
1082 line[i*3] = *(input); input++;
1083 line[i*3+1] = *(input); input++;
1084 line[i*3+2] = *(input); input++;
1087 jpeg_write_scanlines(&cinfo, &line, 1);
1090 jpeg_finish_compress(&cinfo);
1091 jpeg_destroy_compress(&cinfo);
1115 struct jpeg_decompress_struct cinfo;
1116 struct jpeg_error_mgr jerr;
1119 cinfo.err = jpeg_std_error(&jerr);
1120 jpeg_create_decompress(&cinfo);
1123 if (filename.empty()) {
1125 "Cannot read JPEG image: filename empty")) ;
1128 file = fopen(filename.c_str(),
"rb");
1132 "Cannot read JPEG file \"%s\"", filename.c_str())) ;
1135 jpeg_stdio_src(&cinfo, file);
1136 jpeg_read_header(&cinfo, TRUE);
1138 unsigned int width = cinfo.image_width;
1139 unsigned int height = cinfo.image_height;
1144 jpeg_start_decompress(&cinfo);
1146 unsigned int rowbytes = cinfo.output_width * (
unsigned int)(cinfo.output_components);
1147 JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray)
1148 ((j_common_ptr) &cinfo, JPOOL_IMAGE, rowbytes, 1);
1150 if (cinfo.out_color_space == JCS_RGB) {
1152 unsigned char* output = (
unsigned char*)Ic.
bitmap;
1153 while (cinfo.output_scanline<cinfo.output_height) {
1154 jpeg_read_scanlines(&cinfo,buffer,1);
1155 for (
unsigned int i = 0; i < width; i++) {
1156 *(output++) = buffer[0][i*3];
1157 *(output++) = buffer[0][i*3+1];
1158 *(output++) = buffer[0][i*3+2];
1165 else if (cinfo.out_color_space == JCS_GRAYSCALE)
1167 while (cinfo.output_scanline<cinfo.output_height)
1169 unsigned int row = cinfo.output_scanline;
1170 jpeg_read_scanlines(&cinfo,buffer,1);
1171 memcpy(I[row], buffer[0], rowbytes);
1175 jpeg_finish_decompress(&cinfo);
1176 jpeg_destroy_decompress(&cinfo);
1201 struct jpeg_decompress_struct cinfo;
1202 struct jpeg_error_mgr jerr;
1205 cinfo.err = jpeg_std_error(&jerr);
1206 jpeg_create_decompress(&cinfo);
1209 if (filename.empty()) {
1211 "Cannot read JPEG image: filename empty")) ;
1214 file = fopen(filename.c_str(),
"rb");
1218 "Cannot read JPEG file \"%s\"", filename.c_str())) ;
1221 jpeg_stdio_src(&cinfo, file);
1223 jpeg_read_header(&cinfo, TRUE);
1225 unsigned int width = cinfo.image_width;
1226 unsigned int height = cinfo.image_height;
1231 jpeg_start_decompress(&cinfo);
1233 unsigned int rowbytes = cinfo.output_width * (
unsigned int)(cinfo.output_components);
1234 JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray)
1235 ((j_common_ptr) &cinfo, JPOOL_IMAGE, rowbytes, 1);
1237 if (cinfo.out_color_space == JCS_RGB)
1239 unsigned char* output = (
unsigned char*)I.
bitmap;
1240 while (cinfo.output_scanline<cinfo.output_height)
1242 jpeg_read_scanlines(&cinfo,buffer,1);
1243 for (
unsigned int i = 0; i < width; i++) {
1244 *(output++) = buffer[0][i*3];
1245 *(output++) = buffer[0][i*3+1];
1246 *(output++) = buffer[0][i*3+2];
1252 else if (cinfo.out_color_space == JCS_GRAYSCALE)
1256 while (cinfo.output_scanline<cinfo.output_height)
1258 unsigned int row = cinfo.output_scanline;
1259 jpeg_read_scanlines(&cinfo,buffer,1);
1260 memcpy(Ig[row], buffer[0], rowbytes);
1266 jpeg_finish_decompress(&cinfo);
1267 jpeg_destroy_decompress(&cinfo);
1271 #elif defined(VISP_HAVE_OPENCV)
1283 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408)
1286 cv::imwrite(filename.c_str(), Ip);
1288 IplImage* Ip = NULL;
1291 cvSaveImage(filename.c_str(), Ip);
1293 cvReleaseImage(&Ip);
1307 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408)
1310 cv::imwrite(filename.c_str(), Ip);
1312 IplImage* Ip = NULL;
1315 cvSaveImage(filename.c_str(), Ip);
1317 cvReleaseImage(&Ip);
1340 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
1341 cv::Mat Ip = cv::imread(filename.c_str(), cv::IMREAD_GRAYSCALE);
1346 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020408)
1347 cv::Mat Ip = cv::imread(filename.c_str(), CV_LOAD_IMAGE_GRAYSCALE);
1353 IplImage* Ip = NULL;
1354 Ip = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_GRAYSCALE);
1359 "Can't read the image")) ;
1360 cvReleaseImage(&Ip);
1385 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
1386 cv::Mat Ip = cv::imread(filename.c_str(), cv::IMREAD_GRAYSCALE);
1391 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020408)
1392 cv::Mat Ip = cv::imread(filename.c_str(), CV_LOAD_IMAGE_GRAYSCALE);
1398 IplImage* Ip = NULL;
1399 Ip = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_COLOR);
1404 cvReleaseImage(&Ip);
1419 #if defined(VISP_HAVE_PNG)
1434 if (filename.empty()) {
1436 "Cannot create PNG file: filename empty")) ;
1439 file = fopen(filename.c_str(),
"wb");
1443 "Cannot create PNG file \"%s\"", filename.c_str())) ;
1447 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL, NULL, NULL);
1453 "PNG write error")) ;
1456 png_infop info_ptr = png_create_info_struct(png_ptr);
1460 png_destroy_write_struct (&png_ptr, NULL);
1463 "PNG write error")) ;
1467 if (setjmp (png_jmpbuf (png_ptr)))
1470 png_destroy_write_struct (&png_ptr, &info_ptr);
1473 "PNG write error")) ;
1477 png_init_io (png_ptr, file);
1482 int color_type = PNG_COLOR_TYPE_GRAY;
1485 if (setjmp (png_jmpbuf (png_ptr)))
1488 png_destroy_write_struct (&png_ptr, &info_ptr);
1491 "PNG write error")) ;
1494 png_set_IHDR(png_ptr, info_ptr, width, height,
1495 bit_depth, color_type, PNG_INTERLACE_NONE,
1496 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
1498 png_write_info(png_ptr, info_ptr);
1500 png_bytep* row_ptrs =
new png_bytep[height];
1501 for (
unsigned int i = 0; i < height; i++)
1502 row_ptrs[i] =
new png_byte[width];
1504 unsigned char* input = (
unsigned char*)I.
bitmap;
1506 for (
unsigned int i = 0; i < height; i++)
1508 png_byte* row = row_ptrs[i];
1509 for(
unsigned int j = 0; j < width; j++)
1516 png_write_image(png_ptr, row_ptrs);
1518 png_write_end(png_ptr, NULL);
1520 for(
unsigned int j = 0; j < height; j++)
1521 delete[] row_ptrs[j];
1525 png_destroy_write_struct (&png_ptr, &info_ptr);
1543 if (filename.empty()) {
1545 "Cannot create PNG file: filename empty")) ;
1548 file = fopen(filename.c_str(),
"wb");
1552 "Cannot create PNG file \"%s\"", filename.c_str())) ;
1556 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL, NULL, NULL);
1562 "PNG write error")) ;
1565 png_infop info_ptr = png_create_info_struct(png_ptr);
1569 png_destroy_write_struct (&png_ptr, NULL);
1572 "PNG write error")) ;
1576 if (setjmp (png_jmpbuf (png_ptr)))
1579 png_destroy_write_struct (&png_ptr, &info_ptr);
1582 "PNG write error")) ;
1586 png_init_io (png_ptr, file);
1591 int color_type = PNG_COLOR_TYPE_RGB;
1594 if (setjmp (png_jmpbuf (png_ptr)))
1597 png_destroy_write_struct (&png_ptr, &info_ptr);
1600 "PNG write error")) ;
1603 png_set_IHDR(png_ptr, info_ptr, width, height,
1604 bit_depth, color_type, PNG_INTERLACE_NONE,
1605 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
1607 png_write_info(png_ptr, info_ptr);
1609 png_bytep* row_ptrs =
new png_bytep[height];
1610 for (
unsigned int i = 0; i < height; i++)
1611 row_ptrs[i] =
new png_byte[3*width];
1613 unsigned char* input = (
unsigned char*)I.
bitmap;;
1615 for (
unsigned int i = 0; i < height; i++)
1617 png_byte* row = row_ptrs[i];
1618 for(
unsigned int j = 0; j < width; j++)
1620 row[3*j] = *(input);input++;
1621 row[3*j+1] = *(input);input++;
1622 row[3*j+2] = *(input);input++;
1627 png_write_image(png_ptr, row_ptrs);
1629 png_write_end(png_ptr, NULL);
1631 for(
unsigned int j = 0; j < height; j++)
1632 delete[] row_ptrs[j];
1636 png_destroy_write_struct (&png_ptr, &info_ptr);
1663 if (filename.empty()) {
1665 "Cannot read PNG image: filename empty")) ;
1668 file = fopen(filename.c_str(),
"rb");
1672 "Cannot read file \"%s\"", filename.c_str())) ;
1676 if (fread (magic, 1,
sizeof (magic), file) !=
sizeof (magic))
1680 "Cannot read magic number in file \"%s\"", filename.c_str())) ;
1684 if (png_sig_cmp (magic,0,
sizeof (magic)))
1688 "Cannot read PNG file: \"%s\" is not a valid PNG image", filename.c_str())) ;
1693 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
1694 if (png_ptr == NULL)
1696 fprintf (stderr,
"error: can't create a png read structure!\n");
1699 "error reading png file")) ;
1703 png_infop info_ptr = png_create_info_struct (png_ptr);
1704 if (info_ptr == NULL)
1706 fprintf (stderr,
"error: can't create a png info structure!\n");
1708 png_destroy_read_struct (&png_ptr, NULL, NULL);
1710 "error reading png file")) ;
1714 if (setjmp (png_jmpbuf (png_ptr)))
1717 png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
1720 "PNG read error")) ;
1724 png_init_io (png_ptr, file);
1727 png_set_sig_bytes (png_ptr,
sizeof (magic));
1730 png_read_info (png_ptr, info_ptr);
1732 unsigned int width = png_get_image_width(png_ptr, info_ptr);
1733 unsigned int height = png_get_image_height(png_ptr, info_ptr);
1735 unsigned int bit_depth, channels, color_type;
1737 bit_depth = png_get_bit_depth (png_ptr, info_ptr);
1738 channels = png_get_channels(png_ptr, info_ptr);
1739 color_type = png_get_color_type (png_ptr, info_ptr);
1742 if (color_type == PNG_COLOR_TYPE_PALETTE)
1743 png_set_palette_to_rgb (png_ptr);
1746 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
1747 png_set_expand (png_ptr);
1752 if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
1753 png_set_strip_alpha(png_ptr);
1755 if (bit_depth == 16)
1756 png_set_strip_16 (png_ptr);
1757 else if (bit_depth < 8)
1758 png_set_packing (png_ptr);
1761 png_read_update_info (png_ptr, info_ptr);
1763 channels = png_get_channels(png_ptr, info_ptr);
1768 png_bytep* rowPtrs =
new png_bytep[height];
1770 unsigned int stride = width * bit_depth * channels / 8;
1771 unsigned char* data =
new unsigned char[stride * height];
1773 for (
unsigned int i =0; i < height; i++)
1774 rowPtrs[i] = (png_bytep)data + (i * stride);
1776 png_read_image(png_ptr, rowPtrs);
1779 unsigned char* output;
1784 output = (
unsigned char*)I.
bitmap;
1785 for (
unsigned int i = 0; i < width*height; i++)
1787 *(output++) = data[i];
1791 output = (
unsigned char*)I.
bitmap;
1792 for (
unsigned int i = 0; i < width*height; i++)
1794 *(output++) = data[i*2];
1799 output = (
unsigned char*)Ic.
bitmap;
1800 for (
unsigned int i = 0; i < width*height; i++)
1802 *(output++) = data[i*3];
1803 *(output++) = data[i*3+1];
1804 *(output++) = data[i*3+2];
1810 output = (
unsigned char*)Ic.
bitmap;
1811 for (
unsigned int i = 0; i < width*height; i++)
1813 *(output++) = data[i*4];
1814 *(output++) = data[i*4+1];
1815 *(output++) = data[i*4+2];
1816 *(output++) = data[i*4+3];
1822 delete [] (png_bytep)rowPtrs;
1824 png_read_end (png_ptr, NULL);
1825 png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
1854 if (filename.empty()) {
1856 "Cannot read PNG image: filename empty")) ;
1859 file = fopen(filename.c_str(),
"rb");
1863 "Cannot read file \"%s\"", filename.c_str())) ;
1867 if (fread (magic, 1,
sizeof (magic), file) !=
sizeof (magic))
1871 "Cannot read magic number in file \"%s\"", filename.c_str())) ;
1875 if (png_sig_cmp (magic,0,
sizeof (magic)))
1879 "Cannot read PNG file: \"%s\" is not a valid PNG image", filename.c_str())) ;
1883 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
1889 "PNG read error")) ;
1893 png_infop info_ptr = png_create_info_struct (png_ptr);
1897 png_destroy_read_struct (&png_ptr, NULL, NULL);
1900 "PNG read error")) ;
1904 if (setjmp (png_jmpbuf (png_ptr)))
1907 png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
1910 "PNG read error")) ;
1914 png_init_io (png_ptr, file);
1917 png_set_sig_bytes (png_ptr,
sizeof (magic));
1920 png_read_info (png_ptr, info_ptr);
1922 unsigned int width = png_get_image_width(png_ptr, info_ptr);
1923 unsigned int height = png_get_image_height(png_ptr, info_ptr);
1925 unsigned int bit_depth, channels, color_type;
1927 bit_depth = png_get_bit_depth (png_ptr, info_ptr);
1928 channels = png_get_channels(png_ptr, info_ptr);
1929 color_type = png_get_color_type (png_ptr, info_ptr);
1932 if (color_type == PNG_COLOR_TYPE_PALETTE)
1933 png_set_palette_to_rgb (png_ptr);
1936 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
1937 png_set_expand (png_ptr);
1942 if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
1943 png_set_strip_alpha(png_ptr);
1945 if (bit_depth == 16)
1946 png_set_strip_16 (png_ptr);
1947 else if (bit_depth < 8)
1948 png_set_packing (png_ptr);
1951 png_read_update_info (png_ptr, info_ptr);
1953 channels = png_get_channels(png_ptr, info_ptr);
1958 png_bytep* rowPtrs =
new png_bytep[height];
1960 unsigned int stride = width * bit_depth * channels / 8;
1961 unsigned char* data =
new unsigned char[stride * height];
1964 for (
unsigned int i =0; i < height; i++)
1965 rowPtrs[i] = (png_bytep)data + (i * stride);
1967 png_read_image(png_ptr, rowPtrs);
1970 unsigned char* output;
1975 output = (
unsigned char*)Ig.
bitmap;
1976 for (
unsigned int i = 0; i < width*height; i++)
1978 *(output++) = data[i];
1983 output = (
unsigned char*)Ig.
bitmap;
1984 for (
unsigned int i = 0; i < width*height; i++)
1986 *(output++) = data[i*2];
1992 output = (
unsigned char*)I.
bitmap;
1993 for (
unsigned int i = 0; i < width*height; i++)
1995 *(output++) = data[i*3];
1996 *(output++) = data[i*3+1];
1997 *(output++) = data[i*3+2];
2002 output = (
unsigned char*)I.
bitmap;
2003 for (
unsigned int i = 0; i < width*height; i++)
2005 *(output++) = data[i*4];
2006 *(output++) = data[i*4+1];
2007 *(output++) = data[i*4+2];
2008 *(output++) = data[i*4+3];
2013 delete [] (png_bytep)rowPtrs;
2015 png_read_end (png_ptr, NULL);
2016 png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
2020 #elif defined(VISP_HAVE_OPENCV)
2032 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408)
2035 cv::imwrite(filename.c_str(), Ip);
2037 IplImage* Ip = NULL;
2040 cvSaveImage(filename.c_str(), Ip);
2042 cvReleaseImage(&Ip);
2056 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408)
2059 cv::imwrite(filename.c_str(), Ip);
2061 IplImage* Ip = NULL;
2064 cvSaveImage(filename.c_str(), Ip);
2066 cvReleaseImage(&Ip);
2089 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
2090 cv::Mat Ip = cv::imread(filename.c_str(), cv::IMREAD_GRAYSCALE);
2095 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020408)
2096 cv::Mat Ip = cv::imread(filename.c_str(), CV_LOAD_IMAGE_GRAYSCALE);
2102 IplImage* Ip = NULL;
2103 Ip = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_GRAYSCALE);
2108 "Can't read the image")) ;
2109 cvReleaseImage(&Ip);
2134 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
2135 cv::Mat Ip = cv::imread(filename.c_str(), cv::IMREAD_GRAYSCALE);
2140 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020408)
2141 cv::Mat Ip = cv::imread(filename.c_str(), CV_LOAD_IMAGE_GRAYSCALE);
2147 IplImage* Ip = NULL;
2148 Ip = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_COLOR);
2153 "Can't read the image")) ;
2154 cvReleaseImage(&Ip);
static void writeJPEG(const vpImage< unsigned char > &I, const std::string &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
error that can be emited by ViSP classes.
Error that can be emited by the vpImage class and its derivates.
unsigned char G
Green component.
static void writePPM(const vpImage< unsigned char > &I, const std::string &filename)
static void write(const vpImage< unsigned char > &I, const std::string &filename)
static void readPPM(vpImage< unsigned char > &I, const std::string &filename)
unsigned int getSize() const
static void writePFM(const vpImage< float > &I, const std::string &filename)
void resize(const unsigned int h, const unsigned int w)
resize the image : Image initialization
static void readPFM(vpImage< float > &I, const std::string &filename)
static void read(vpImage< unsigned char > &I, const std::string &filename)
static void readPNG(vpImage< unsigned char > &I, const std::string &filename)
unsigned char R
Red component.
unsigned int getHeight() const
static void readPGM(vpImage< unsigned char > &I, const std::string &filename)
static void writePGM(const vpImage< unsigned char > &I, const std::string &filename)
static void readJPEG(vpImage< unsigned char > &I, const std::string &filename)
static void writePNG(const vpImage< unsigned char > &I, const std::string &filename)