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 || *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 || *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 >= 0x030000
320 cv::Mat cvI = cv::imread(filename, cv::IMREAD_GRAYSCALE);
321 if (cvI.cols == 0 && cvI.rows == 0) {
322 std::string message =
"Cannot read file \"" + std::string(filename) +
"\": Image format not supported";
326 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
328 cv::Mat cvI = cv::imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
329 if (cvI.cols == 0 && cvI.rows == 0) {
330 std::string message =
"Cannot read file \"" + std::string(filename) +
"\": Image format not supported";
335 std::string message =
"Cannot read file \"" + std::string(filename) +
"\": Image format not supported";
359 read(I,filename.c_str());
382 std::string message =
"Cannot read file: \"" + std::string(filename) +
"\" doesn't exist";
386 bool try_opencv_reader =
false;
388 switch(getFormat(filename)){
394 #ifdef VISP_HAVE_LIBJPEG
397 try_opencv_reader =
true;
401 #if defined(VISP_HAVE_LIBPNG)
404 try_opencv_reader =
true;
412 case FORMAT_JPEG2000 :
413 case FORMAT_UNKNOWN :
414 try_opencv_reader =
true;
418 if (try_opencv_reader) {
419 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
421 cv::Mat cvI = cv::imread(filename, cv::IMREAD_COLOR);
422 if (cvI.cols == 0 && cvI.rows == 0) {
423 std::string message =
"Cannot read file \"" + std::string(filename) +
"\": Image format not supported";
427 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
429 cv::Mat cvI = cv::imread(filename, CV_LOAD_IMAGE_COLOR);
430 if (cvI.cols == 0 && cvI.rows == 0) {
431 std::string message =
"Cannot read file \"" + std::string(filename) +
"\": Image format not supported";
436 std::string message =
"Cannot read file \"" + std::string(filename) +
"\": Image format not supported";
460 read(I,filename.c_str());
478 bool try_opencv_writer =
false;
480 switch(getFormat(filename)){
486 #ifdef VISP_HAVE_LIBJPEG
489 try_opencv_writer =
true;
493 #ifdef VISP_HAVE_LIBPNG
496 try_opencv_writer =
true;
504 case FORMAT_JPEG2000 :
505 case FORMAT_UNKNOWN :
506 try_opencv_writer =
true;
510 if (try_opencv_writer) {
511 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
515 cv::imwrite(filename, cvI);
517 vpCERROR <<
"Cannot write file: Image format not supported..." << std::endl;
519 "Cannot write file: Image format not supported")) ;
538 write(I,filename.c_str());
555 bool try_opencv_writer =
false;
557 switch(getFormat(filename)){
563 #ifdef VISP_HAVE_LIBJPEG
566 try_opencv_writer =
true;
570 #ifdef VISP_HAVE_LIBPNG
573 try_opencv_writer =
true;
581 case FORMAT_JPEG2000 :
582 case FORMAT_UNKNOWN :
583 try_opencv_writer =
true;
587 if (try_opencv_writer) {
588 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
592 cv::imwrite(filename, cvI);
594 vpCERROR <<
"Cannot write file: Image format not supported..." << std::endl;
596 "Cannot write file: Image format not supported")) ;
615 write(I,filename.c_str());
632 const char *filename)
638 if (!filename || *filename ==
'\0') {
644 fd = fopen(filename,
"wb");
649 "cannot write file")) ;
655 fprintf(fd,
"255\n");
661 ierr = fwrite(I.
bitmap,
sizeof(
float), nbyte, fd) ;
667 "cannot write file")) ;
688 const char *filename)
694 if (!filename || *filename ==
'\0') {
700 fd = fopen(filename,
"wb");
705 "cannot write file")) ;
711 fprintf(fd,
"255\n");
717 ierr = fwrite(I.
bitmap,
sizeof(
unsigned char), nbyte, fd) ;
723 "cannot write file")) ;
746 for (
unsigned int i=0 ; i < nrows * ncols ; i++)
769 if (!filename || *filename ==
'\0') {
775 fd = fopen(filename,
"wb");
780 "cannot write file")) ;
786 fprintf(fd,
"255\n");
796 ierr = fwrite(Itmp.
bitmap,
sizeof(
unsigned char), nbyte, fd) ;
802 "cannot write file")) ;
838 if (!filename || *filename ==
'\0')
847 fd = fopen(filename,
"rb");
852 "couldn't read file")) ;
858 err = fgets(str, vpMAX_LEN - 1, fd);
863 vpERROR_TRACE(
"couldn't read line %d of file \"%s\"\n", line, filename) ;
865 "couldn't read file")) ;
873 "this is not a PFM file")) ;
877 if (strcmp(str,
"P8") != 0)
882 "this is not a PFM file")) ;
887 err = fgets(str, vpMAX_LEN - 1, fd);
890 fprintf(stderr,
"couldn't read line %d of file \"%s\"\n", line, filename);
893 "Cannot read content of PFM file")) ;
895 }
while ((str[0] ==
'#') || (str[0] ==
'\n'));
898 ierr = sscanf(str,
"%d %d", &w, &h);
899 if (w > 100000 || h>100000) {
906 err = fgets(str, vpMAX_LEN - 1, fd);
909 fprintf(stderr,
"couldn't read line %d of file \"%s\"\n", line, filename);
912 "Cannot read content of PFM file")) ;
914 }
while ((str[0] ==
'#') || (str[0] ==
'\n'));
915 ierr = sscanf(str,
"%d", &h);
920 vpERROR_TRACE(
"couldn't read line %d of file \"%s\"\n",line, filename) ;
922 "Cannot read content of PFM file")) ;
935 "Cannot read content of PFM file")) ;
940 err = fgets(str, vpMAX_LEN - 1, fd);
944 vpERROR_TRACE(
"couldn't read line %d of file \"%s\"\n",line, filename) ;
946 "Cannot read content of PFM file")) ;
949 ierr = sscanf(str,
"%d", &is255);
952 vpERROR_TRACE(
"couldn't read line %d of file \"%s\"\n", line, filename) ;
954 "Cannot read content of PFM file")) ;
960 vpERROR_TRACE(
"MAX_VAL is not 255 in file \"%s\"\n", filename) ;
962 "Cannot read content of PFM file")) ;
966 if (fread (I.
bitmap,
sizeof(
float), nbyte, fd ) != nbyte)
969 vpERROR_TRACE(
"couldn't read %d bytes in file \"%s\"\n", nbyte, filename) ;
971 "Cannot read content of PFM file")) ;
1001 char str[vpMAX_LEN];
1002 unsigned int magic=5, w=0, h=0, maxval=255;
1005 if (!filename || *filename ==
'\0') {
1011 if ((fd = fopen(filename,
"rb")) == NULL) {
1013 "Cannot read file \"%s\"", filename)) ;
1016 while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] ==
'#') || (str[0] ==
'\n'))) {};
1020 "Cannot read header of file \"%s\"", filename));
1022 if ((ierr = sscanf(str,
"P%u %u %u %u", &magic, &w, &h, &maxval)) == 0) {
1025 "Cannot read header of file \"%s\"", filename));
1031 "\"%s\" is not a PGM P5 file", filename));
1042 while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] ==
'#') || (str[0] ==
'\n'))) {};
1046 "Cannot read header of file \"%s\"", filename));
1048 if (((ierr = sscanf(str,
"%u %u %u", &w, &h, &maxval)) == 0) || (ierr != 1 && ierr != 2 && ierr != 3)) {
1051 "Cannot read header of file \"%s\"", filename));
1059 while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] ==
'#') || (str[0] ==
'\n'))) {};
1063 "Cannot read header of file \"%s\"", filename));
1065 if (((ierr = sscanf(str,
"%u %u", &h, &maxval)) == 0) || (ierr != 1 && ierr != 2)) {
1068 "Cannot read header of file \"%s\"", filename));
1072 while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] ==
'#') || (str[0] ==
'\n'))) {};
1076 "Cannot read header of file \"%s\"", filename));
1078 if ((ierr = sscanf(str,
"%u", &maxval)) != 1) {
1081 "Cannot read header of file \"%s\"", filename));
1088 else if (ierr == 2) {
1091 while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] ==
'#') || (str[0] ==
'\n'))) {};
1095 "Cannot read header of file \"%s\"", filename));
1097 if ((ierr = sscanf(str,
"%u", &maxval)) != 1) {
1100 "Cannot read header of file \"%s\"", filename));
1108 else if (ierr == 2) {
1110 while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] ==
'#') || (str[0] ==
'\n'))) {};
1114 "Cannot read header of file \"%s\"", filename));
1116 if (((ierr = sscanf(str,
"%u %u", &h, &maxval)) == 0) || (ierr != 1 && ierr != 2)) {
1119 "Cannot read header of file \"%s\"", filename));
1123 while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] ==
'#') || (str[0] ==
'\n'))) {};
1127 "Cannot read header of file \"%s\"", filename));
1129 if ((ierr = sscanf(str,
"%u", &maxval)) != 1) {
1132 "Cannot read header of file \"%s\"", filename));
1140 else if (ierr == 3) {
1142 while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] ==
'#') || (str[0] ==
'\n'))) {};
1146 "Cannot read header of file \"%s\"", filename));
1148 if ((ierr = sscanf(str,
"%u", &maxval)) != 1) {
1151 "Cannot read header of file \"%s\"", filename));
1159 if (w > 100000 || h>100000) {
1167 "Bad maxval in \"%s\"", filename));
1176 if ((n = fread (I.
bitmap,
sizeof(
unsigned char), nbyte, fd)) != nbyte) {
1179 "Read only %d of %d bytes in file \"%s\"", n, nbyte, filename));
1281 char str[vpMAX_LEN];
1282 unsigned int magic=5, w=0, h=0, maxval=255;
1285 if (!filename || *filename ==
'\0') {
1291 if ((fd = fopen(filename,
"rb")) == NULL) {
1293 "Cannot read file \"%s\"", filename)) ;
1296 while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] ==
'#') || (str[0] ==
'\n'))) {};
1300 "Cannot read header of file \"%s\"", filename));
1302 if ((ierr = sscanf(str,
"P%u %u %u %u", &magic, &w, &h, &maxval)) == 0) {
1305 "Cannot read header of file \"%s\"", filename));
1311 "\"%s\" is not a PGM P6 file", filename));
1322 while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] ==
'#') || (str[0] ==
'\n'))) {};
1326 "Cannot read header of file \"%s\"", filename));
1328 if (((ierr = sscanf(str,
"%u %u %u", &w, &h, &maxval)) == 0) || (ierr != 1 && ierr != 2 && ierr != 3)) {
1331 "Cannot read header of file \"%s\"", filename));
1339 while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] ==
'#') || (str[0] ==
'\n'))) {};
1343 "Cannot read header of file \"%s\"", filename));
1345 if (((ierr = sscanf(str,
"%u %u", &h, &maxval)) == 0) || (ierr != 1 && ierr != 2)) {
1348 "Cannot read header of file \"%s\"", filename));
1352 while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] ==
'#') || (str[0] ==
'\n'))) {};
1356 "Cannot read header of file \"%s\"", filename));
1358 if ((ierr = sscanf(str,
"%u", &maxval)) != 1) {
1361 "Cannot read header of file \"%s\"", filename));
1368 else if (ierr == 2) {
1371 while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] ==
'#') || (str[0] ==
'\n'))) {};
1375 "Cannot read header of file \"%s\"", filename));
1377 if ((ierr = sscanf(str,
"%u", &maxval)) != 1) {
1380 "Cannot read header of file \"%s\"", filename));
1388 else if (ierr == 2) {
1390 while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] ==
'#') || (str[0] ==
'\n'))) {};
1394 "Cannot read header of file \"%s\"", filename));
1396 if (((ierr = sscanf(str,
"%u %u", &h, &maxval)) == 0) || (ierr != 1 && ierr != 2)) {
1399 "Cannot read header of file \"%s\"", filename));
1403 while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] ==
'#') || (str[0] ==
'\n'))) {};
1407 "Cannot read header of file \"%s\"", filename));
1409 if ((ierr = sscanf(str,
"%u", &maxval)) != 1) {
1412 "Cannot read header of file \"%s\"", filename));
1420 else if (ierr == 3) {
1422 while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] ==
'#') || (str[0] ==
'\n'))) {};
1426 "Cannot read header of file \"%s\"", filename));
1428 if ((ierr = sscanf(str,
"%u", &maxval)) != 1) {
1431 "Cannot read header of file \"%s\"", filename));
1439 if (w > 100000 || h>100000) {
1447 "Bad maxval in \"%s\"", filename));
1454 for(
unsigned int i=0;i<I.
getHeight();i++)
1456 for(
unsigned int j=0;j<I.
getWidth();j++)
1459 size_t res = fread(&v.
R,
sizeof(v.
R),1,fd) ;
1460 res |= fread(&v.
G,
sizeof(v.
G),1,fd) ;
1461 res |= fread(&v.
B,
sizeof(v.
B),1,fd) ;
1466 "Cannot read bytes in file \"%s\"\n", filename));
1520 if (!filename || *filename ==
'\0') {
1526 f = fopen(filename,
"wb");
1531 "cannot write file")) ;
1538 fprintf(f,
"%d\n",255);
1540 for(
unsigned int i=0;i<I.
getHeight();i++)
1542 for(
unsigned int j=0;j<I.
getWidth();j++)
1549 res = fwrite(&tmp,
sizeof(tmp),1,f) ;
1555 "cannot write file")) ;
1558 res = fwrite(&tmp,
sizeof(tmp),1,f) ;
1564 "cannot write file")) ;
1567 res = fwrite(&tmp,
sizeof(tmp),1,f) ;
1573 "cannot write file")) ;
1636 const std::string filename)
1750 #if defined(VISP_HAVE_LIBJPEG)
1762 struct jpeg_compress_struct cinfo;
1763 struct jpeg_error_mgr jerr;
1766 cinfo.err = jpeg_std_error(&jerr);
1767 jpeg_create_compress(&cinfo);
1770 if (!filename || *filename ==
'\0') {
1776 file = fopen(filename,
"wb");
1781 "cannot write file")) ;
1787 jpeg_stdio_dest(&cinfo, file);
1789 cinfo.image_width = width;
1790 cinfo.image_height = height;
1791 cinfo.input_components = 1;
1792 cinfo.in_color_space = JCS_GRAYSCALE;
1793 jpeg_set_defaults(&cinfo);
1795 jpeg_start_compress(&cinfo,TRUE);
1797 unsigned char *line;
1798 line =
new unsigned char[width];
1799 unsigned char* input = (
unsigned char*)I.
bitmap;
1800 while (cinfo.next_scanline < cinfo.image_height)
1802 for (
unsigned int i = 0; i < width; i++)
1807 jpeg_write_scanlines(&cinfo, &line, 1);
1810 jpeg_finish_compress(&cinfo);
1811 jpeg_destroy_compress(&cinfo);
1841 struct jpeg_compress_struct cinfo;
1842 struct jpeg_error_mgr jerr;
1845 cinfo.err = jpeg_std_error(&jerr);
1846 jpeg_create_compress(&cinfo);
1849 if (!filename || *filename ==
'\0') {
1855 file = fopen(filename,
"wb");
1860 "cannot write file")) ;
1866 jpeg_stdio_dest(&cinfo, file);
1868 cinfo.image_width = width;
1869 cinfo.image_height = height;
1870 cinfo.input_components = 3;
1871 cinfo.in_color_space = JCS_RGB;
1872 jpeg_set_defaults(&cinfo);
1874 jpeg_start_compress(&cinfo,TRUE);
1876 unsigned char *line;
1877 line =
new unsigned char[3*width];
1878 unsigned char* input = (
unsigned char*)I.
bitmap;
1879 while (cinfo.next_scanline < cinfo.image_height)
1881 for (
unsigned int i = 0; i < width; i++)
1883 line[i*3] = *(input); input++;
1884 line[i*3+1] = *(input); input++;
1885 line[i*3+2] = *(input); input++;
1888 jpeg_write_scanlines(&cinfo, &line, 1);
1891 jpeg_finish_compress(&cinfo);
1892 jpeg_destroy_compress(&cinfo);
1931 struct jpeg_decompress_struct cinfo;
1932 struct jpeg_error_mgr jerr;
1935 cinfo.err = jpeg_std_error(&jerr);
1936 jpeg_create_decompress(&cinfo);
1939 if (!filename || *filename ==
'\0') {
1945 file = fopen(filename,
"rb");
1950 "cannot read file")) ;
1953 jpeg_stdio_src(&cinfo, file);
1954 jpeg_read_header(&cinfo, TRUE);
1956 unsigned int width = cinfo.image_width;
1957 unsigned int height = cinfo.image_height;
1962 jpeg_start_decompress(&cinfo);
1964 unsigned int rowbytes = cinfo.output_width * (
unsigned int)(cinfo.output_components);
1965 JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray)
1966 ((j_common_ptr) &cinfo, JPOOL_IMAGE, rowbytes, 1);
1968 if (cinfo.out_color_space == JCS_RGB) {
1970 unsigned char* output = (
unsigned char*)Ic.
bitmap;
1971 while (cinfo.output_scanline<cinfo.output_height) {
1972 jpeg_read_scanlines(&cinfo,buffer,1);
1973 for (
unsigned int i = 0; i < width; i++) {
1974 *(output++) = buffer[0][i*3];
1975 *(output++) = buffer[0][i*3+1];
1976 *(output++) = buffer[0][i*3+2];
1983 else if (cinfo.out_color_space == JCS_GRAYSCALE)
1986 while (cinfo.output_scanline<cinfo.output_height)
1988 row = cinfo.output_scanline;
1989 jpeg_read_scanlines(&cinfo,buffer,1);
1990 memcpy(I[row], buffer[0], rowbytes);
1994 jpeg_finish_decompress(&cinfo);
1995 jpeg_destroy_decompress(&cinfo);
2044 struct jpeg_decompress_struct cinfo;
2045 struct jpeg_error_mgr jerr;
2048 cinfo.err = jpeg_std_error(&jerr);
2049 jpeg_create_decompress(&cinfo);
2052 if (!filename || *filename ==
'\0') {
2058 file = fopen(filename,
"rb");
2063 "cannot read file")) ;
2066 jpeg_stdio_src(&cinfo, file);
2068 jpeg_read_header(&cinfo, TRUE);
2070 unsigned int width = cinfo.image_width;
2071 unsigned int height = cinfo.image_height;
2076 jpeg_start_decompress(&cinfo);
2078 unsigned int rowbytes = cinfo.output_width * (
unsigned int)(cinfo.output_components);
2079 JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray)
2080 ((j_common_ptr) &cinfo, JPOOL_IMAGE, rowbytes, 1);
2082 if (cinfo.out_color_space == JCS_RGB)
2084 unsigned char* output = (
unsigned char*)I.
bitmap;
2085 while (cinfo.output_scanline<cinfo.output_height)
2087 jpeg_read_scanlines(&cinfo,buffer,1);
2088 for (
unsigned int i = 0; i < width; i++) {
2089 *(output++) = buffer[0][i*3];
2090 *(output++) = buffer[0][i*3+1];
2091 *(output++) = buffer[0][i*3+2];
2097 else if (cinfo.out_color_space == JCS_GRAYSCALE)
2102 while (cinfo.output_scanline<cinfo.output_height)
2104 row = cinfo.output_scanline;
2105 jpeg_read_scanlines(&cinfo,buffer,1);
2106 memcpy(Ig[row], buffer[0], rowbytes);
2112 jpeg_finish_decompress(&cinfo);
2113 jpeg_destroy_decompress(&cinfo);
2142 #elif defined(VISP_HAVE_OPENCV)
2154 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408)
2157 cv::imwrite(filename, Ip);
2159 IplImage* Ip = NULL;
2162 cvSaveImage(filename, Ip);
2164 cvReleaseImage(&Ip);
2193 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408)
2196 cv::imwrite(filename, Ip);
2198 IplImage* Ip = NULL;
2201 cvSaveImage(filename, Ip);
2203 cvReleaseImage(&Ip);
2241 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
2242 cv::Mat Ip = cv::imread(filename, cv::IMREAD_GRAYSCALE);
2247 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020408)
2248 cv::Mat Ip = cv::imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
2254 IplImage* Ip = NULL;
2255 Ip = cvLoadImage(filename, CV_LOAD_IMAGE_GRAYSCALE);
2260 "Can't read the image")) ;
2261 cvReleaseImage(&Ip);
2310 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
2311 cv::Mat Ip = cv::imread(filename, cv::IMREAD_GRAYSCALE);
2316 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020408)
2317 cv::Mat Ip = cv::imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
2323 IplImage* Ip = NULL;
2324 Ip = cvLoadImage(filename, CV_LOAD_IMAGE_COLOR);
2329 cvReleaseImage(&Ip);
2369 #if defined(VISP_HAVE_LIBPNG)
2384 if (!filename || *filename ==
'\0') {
2390 file = fopen(filename,
"wb");
2395 "cannot write file")) ;
2399 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL, NULL, NULL);
2405 "PNG write error")) ;
2408 png_infop info_ptr = png_create_info_struct(png_ptr);
2412 png_destroy_write_struct (&png_ptr, NULL);
2415 "PNG write error")) ;
2419 if (setjmp (png_jmpbuf (png_ptr)))
2422 png_destroy_write_struct (&png_ptr, &info_ptr);
2425 "PNG write error")) ;
2429 png_init_io (png_ptr, file);
2434 int color_type = PNG_COLOR_TYPE_GRAY;
2437 if (setjmp (png_jmpbuf (png_ptr)))
2440 png_destroy_write_struct (&png_ptr, &info_ptr);
2443 "PNG write error")) ;
2446 png_set_IHDR(png_ptr, info_ptr, width, height,
2447 bit_depth, color_type, PNG_INTERLACE_NONE,
2448 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
2450 png_write_info(png_ptr, info_ptr);
2452 png_bytep* row_ptrs =
new png_bytep[height];
2453 for (
unsigned int i = 0; i < height; i++)
2454 row_ptrs[i] =
new png_byte[width];
2456 unsigned char* input = (
unsigned char*)I.
bitmap;
2458 for (
unsigned int i = 0; i < height; i++)
2460 png_byte* row = row_ptrs[i];
2461 for(
unsigned int j = 0; j < width; j++)
2468 if (setjmp (png_jmpbuf (png_ptr)))
2471 png_destroy_write_struct (&png_ptr, &info_ptr);
2472 for(
unsigned int j = 0; j < height; j++)
2473 delete[] row_ptrs[j];
2478 "PNG write error")) ;
2481 png_write_image(png_ptr, row_ptrs);
2483 if (setjmp (png_jmpbuf (png_ptr)))
2486 png_destroy_write_struct (&png_ptr, &info_ptr);
2487 for(
unsigned int j = 0; j < height; j++)
2488 delete[] row_ptrs[j];
2493 "PNG write error")) ;
2496 png_write_end(png_ptr, NULL);
2498 for(
unsigned int j = 0; j < height; j++)
2499 delete[] row_ptrs[j];
2503 png_destroy_write_struct (&png_ptr, &info_ptr);
2536 if (!filename || *filename ==
'\0') {
2542 file = fopen(filename,
"wb");
2547 "cannot write file")) ;
2551 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL, NULL, NULL);
2557 "PNG write error")) ;
2560 png_infop info_ptr = png_create_info_struct(png_ptr);
2564 png_destroy_write_struct (&png_ptr, NULL);
2567 "PNG write error")) ;
2571 if (setjmp (png_jmpbuf (png_ptr)))
2574 png_destroy_write_struct (&png_ptr, &info_ptr);
2577 "PNG write error")) ;
2581 png_init_io (png_ptr, file);
2586 int color_type = PNG_COLOR_TYPE_RGB;
2589 if (setjmp (png_jmpbuf (png_ptr)))
2592 png_destroy_write_struct (&png_ptr, &info_ptr);
2595 "PNG write error")) ;
2598 png_set_IHDR(png_ptr, info_ptr, width, height,
2599 bit_depth, color_type, PNG_INTERLACE_NONE,
2600 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
2602 png_write_info(png_ptr, info_ptr);
2604 png_bytep* row_ptrs =
new png_bytep[height];
2605 for (
unsigned int i = 0; i < height; i++)
2606 row_ptrs[i] =
new png_byte[3*width];
2608 unsigned char* input = (
unsigned char*)I.
bitmap;;
2610 for (
unsigned int i = 0; i < height; i++)
2612 png_byte* row = row_ptrs[i];
2613 for(
unsigned int j = 0; j < width; j++)
2615 row[3*j] = *(input);input++;
2616 row[3*j+1] = *(input);input++;
2617 row[3*j+2] = *(input);input++;
2622 if (setjmp (png_jmpbuf (png_ptr)))
2625 png_destroy_write_struct (&png_ptr, &info_ptr);
2626 for(
unsigned int j = 0; j < height; j++)
2627 delete[] row_ptrs[j];
2632 "PNG write error")) ;
2635 png_write_image(png_ptr, row_ptrs);
2637 if (setjmp (png_jmpbuf (png_ptr)))
2640 png_destroy_write_struct (&png_ptr, &info_ptr);
2641 for(
unsigned int j = 0; j < height; j++)
2642 delete[] row_ptrs[j];
2647 "PNG write error")) ;
2650 png_write_end(png_ptr, NULL);
2652 for(
unsigned int j = 0; j < height; j++)
2653 delete[] row_ptrs[j];
2657 png_destroy_write_struct (&png_ptr, &info_ptr);
2698 if (!filename || *filename ==
'\0') {
2704 file = fopen(filename,
"rb");
2709 "cannot read file")) ;
2713 if (fread (magic, 1,
sizeof (magic), file) !=
sizeof (magic))
2716 vpERROR_TRACE(
"couldn't read magic number in file \"%s\"\n", filename) ;
2718 "error reading png file")) ;
2722 if (png_sig_cmp (magic,0,
sizeof (magic)))
2724 fprintf (stderr,
"error: \"%s\" is not a valid PNG image!\n",filename);
2727 "error reading png file")) ;
2732 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
2733 if (png_ptr == NULL)
2735 fprintf (stderr,
"error: can't create a png read structure!\n");
2738 "error reading png file")) ;
2742 png_infop info_ptr = png_create_info_struct (png_ptr);
2743 if (info_ptr == NULL)
2745 fprintf (stderr,
"error: can't create a png info structure!\n");
2747 png_destroy_read_struct (&png_ptr, NULL, NULL);
2749 "error reading png file")) ;
2753 if (setjmp (png_jmpbuf (png_ptr)))
2756 png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
2759 "PNG read error")) ;
2763 png_init_io (png_ptr, file);
2766 png_set_sig_bytes (png_ptr,
sizeof (magic));
2769 png_read_info (png_ptr, info_ptr);
2771 unsigned int width = png_get_image_width(png_ptr, info_ptr);
2772 unsigned int height = png_get_image_height(png_ptr, info_ptr);
2774 unsigned int bit_depth, channels, color_type;
2776 bit_depth = png_get_bit_depth (png_ptr, info_ptr);
2777 channels = png_get_channels(png_ptr, info_ptr);
2778 color_type = png_get_color_type (png_ptr, info_ptr);
2781 if (color_type == PNG_COLOR_TYPE_PALETTE)
2782 png_set_palette_to_rgb (png_ptr);
2785 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
2786 png_set_expand (png_ptr);
2791 if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2792 png_set_strip_alpha(png_ptr);
2794 if (bit_depth == 16)
2795 png_set_strip_16 (png_ptr);
2796 else if (bit_depth < 8)
2797 png_set_packing (png_ptr);
2800 png_read_update_info (png_ptr, info_ptr);
2802 channels = png_get_channels(png_ptr, info_ptr);
2807 png_bytep* rowPtrs =
new png_bytep[height];
2809 unsigned int stride = width * bit_depth * channels / 8;
2810 unsigned char* data =
new unsigned char[stride * height];
2812 for (
unsigned int i =0; i < height; i++)
2813 rowPtrs[i] = (png_bytep)data + (i * stride);
2815 png_read_image(png_ptr, rowPtrs);
2818 unsigned char* output;
2823 output = (
unsigned char*)I.
bitmap;
2824 for (
unsigned int i = 0; i < width*height; i++)
2826 *(output++) = data[i];
2830 output = (
unsigned char*)I.
bitmap;
2831 for (
unsigned int i = 0; i < width*height; i++)
2833 *(output++) = data[i*2];
2838 output = (
unsigned char*)Ic.
bitmap;
2839 for (
unsigned int i = 0; i < width*height; i++)
2841 *(output++) = data[i*3];
2842 *(output++) = data[i*3+1];
2843 *(output++) = data[i*3+2];
2849 output = (
unsigned char*)Ic.
bitmap;
2850 for (
unsigned int i = 0; i < width*height; i++)
2852 *(output++) = data[i*4];
2853 *(output++) = data[i*4+1];
2854 *(output++) = data[i*4+2];
2855 *(output++) = data[i*4+3];
2861 delete [] (png_bytep)rowPtrs;
2863 png_read_end (png_ptr, NULL);
2864 png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
2917 if (!filename || *filename ==
'\0') {
2923 file = fopen(filename,
"rb");
2928 "cannot read file")) ;
2932 if (fread (magic, 1,
sizeof (magic), file) !=
sizeof (magic))
2935 vpERROR_TRACE(
"couldn't read magic number in file \"%s\"\n", filename) ;
2937 "error reading pgm file")) ;
2941 if (png_sig_cmp (magic,0,
sizeof (magic)))
2944 vpERROR_TRACE(
"error: \"%s\" is not a valid PNG image!\n",filename);
2946 "PNG read error")) ;
2950 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
2956 "PNG read error")) ;
2960 png_infop info_ptr = png_create_info_struct (png_ptr);
2964 png_destroy_read_struct (&png_ptr, NULL, NULL);
2967 "PNG read error")) ;
2971 if (setjmp (png_jmpbuf (png_ptr)))
2974 png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
2977 "PNG read error")) ;
2981 png_init_io (png_ptr, file);
2984 png_set_sig_bytes (png_ptr,
sizeof (magic));
2987 png_read_info (png_ptr, info_ptr);
2989 unsigned int width = png_get_image_width(png_ptr, info_ptr);
2990 unsigned int height = png_get_image_height(png_ptr, info_ptr);
2992 unsigned int bit_depth, channels, color_type;
2994 bit_depth = png_get_bit_depth (png_ptr, info_ptr);
2995 channels = png_get_channels(png_ptr, info_ptr);
2996 color_type = png_get_color_type (png_ptr, info_ptr);
2999 if (color_type == PNG_COLOR_TYPE_PALETTE)
3000 png_set_palette_to_rgb (png_ptr);
3003 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
3004 png_set_expand (png_ptr);
3009 if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
3010 png_set_strip_alpha(png_ptr);
3012 if (bit_depth == 16)
3013 png_set_strip_16 (png_ptr);
3014 else if (bit_depth < 8)
3015 png_set_packing (png_ptr);
3018 png_read_update_info (png_ptr, info_ptr);
3020 channels = png_get_channels(png_ptr, info_ptr);
3025 png_bytep* rowPtrs =
new png_bytep[height];
3027 unsigned int stride = width * bit_depth * channels / 8;
3028 unsigned char* data =
new unsigned char[stride * height];
3031 for (
unsigned int i =0; i < height; i++)
3032 rowPtrs[i] = (png_bytep)data + (i * stride);
3034 png_read_image(png_ptr, rowPtrs);
3037 unsigned char* output;
3042 output = (
unsigned char*)Ig.
bitmap;
3043 for (
unsigned int i = 0; i < width*height; i++)
3045 *(output++) = data[i];
3050 output = (
unsigned char*)Ig.
bitmap;
3051 for (
unsigned int i = 0; i < width*height; i++)
3053 *(output++) = data[i*2];
3059 output = (
unsigned char*)I.
bitmap;
3060 for (
unsigned int i = 0; i < width*height; i++)
3062 *(output++) = data[i*3];
3063 *(output++) = data[i*3+1];
3064 *(output++) = data[i*3+2];
3069 output = (
unsigned char*)I.
bitmap;
3070 for (
unsigned int i = 0; i < width*height; i++)
3072 *(output++) = data[i*4];
3073 *(output++) = data[i*4+1];
3074 *(output++) = data[i*4+2];
3075 *(output++) = data[i*4+3];
3080 delete [] (png_bytep)rowPtrs;
3082 png_read_end (png_ptr, NULL);
3083 png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
3112 #elif defined(VISP_HAVE_OPENCV)
3124 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408)
3127 cv::imwrite(filename, Ip);
3129 IplImage* Ip = NULL;
3132 cvSaveImage(filename, Ip);
3134 cvReleaseImage(&Ip);
3163 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408)
3166 cv::imwrite(filename, Ip);
3168 IplImage* Ip = NULL;
3171 cvSaveImage(filename, Ip);
3173 cvReleaseImage(&Ip);
3211 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
3212 cv::Mat Ip = cv::imread(filename, cv::IMREAD_GRAYSCALE);
3217 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020408)
3218 cv::Mat Ip = cv::imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
3224 IplImage* Ip = NULL;
3225 Ip = cvLoadImage(filename, CV_LOAD_IMAGE_GRAYSCALE);
3230 "Can't read the image")) ;
3231 cvReleaseImage(&Ip);
3280 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
3281 cv::Mat Ip = cv::imread(filename, cv::IMREAD_GRAYSCALE);
3286 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020408)
3287 cv::Mat Ip = cv::imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
3293 IplImage* Ip = NULL;
3294 Ip = cvLoadImage(filename, CV_LOAD_IMAGE_COLOR);
3299 "Can't read the image")) ;
3300 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 without initializing it.
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)