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 const int vpImageIo::vpMAX_LEN = 100;
59 vpImageIo::openFileRead(
const char *filename)
65 if (!filename || *filename ==
'\0') {
72 if ((fd = fopen(filename,
"r")) == NULL)
76 "cannot open file")) ;
94 vpImageIo::openFileWrite(
const char *filename,
const char *mode)
99 if (!filename || *filename ==
'\0')
103 "filename empty ")) ;
107 if ((fd = fopen(filename, mode)) == NULL)
111 "cannot open file")) ;
125 vpImageIo::openFileRead(
const std::string filename)
131 if (filename.empty()) {
134 "filename empty ")) ;
138 if ((fd = fopen(filename.c_str(),
"r")) == NULL)
142 "cannot open file")) ;
160 vpImageIo::openFileWrite(
const std::string filename,
161 const std::string mode)
166 if (filename.empty())
170 "filename empty ")) ;
174 if ((fd = fopen(filename.c_str(), mode.c_str())) == NULL)
178 "cannot open file")) ;
183 vpImageIo::vpImageFormatType
184 vpImageIo::getFormat(
const char *filename)
186 std::string sfilename(filename);
188 std::string ext = vpImageIo::getExtension(sfilename);
190 if (ext.compare(
".PGM") == 0)
192 else if (ext.compare(
".pgm") == 0)
194 else if (ext.compare(
".PPM") == 0)
196 else if (ext.compare(
".ppm") == 0)
198 else if (ext.compare(
".JPG") == 0)
200 else if (ext.compare(
".jpg") == 0)
202 else if (ext.compare(
".JPEG") == 0)
204 else if (ext.compare(
".jpeg") == 0)
206 else if (ext.compare(
".PNG") == 0)
208 else if (ext.compare(
".png") == 0)
211 else if (ext.compare(
".TIFF") == 0)
213 else if (ext.compare(
".tiff") == 0)
215 else if (ext.compare(
".TIF") == 0)
217 else if (ext.compare(
".tif") == 0)
219 else if (ext.compare(
".BMP") == 0)
221 else if (ext.compare(
".bmp") == 0)
223 else if (ext.compare(
".DIB") == 0)
225 else if (ext.compare(
".dib") == 0)
227 else if (ext.compare(
".PBM") == 0)
229 else if (ext.compare(
".pbm") == 0)
231 else if (ext.compare(
".SR") == 0)
232 return FORMAT_RASTER;
233 else if (ext.compare(
".sr") == 0)
234 return FORMAT_RASTER;
235 else if (ext.compare(
".RAS") == 0)
236 return FORMAT_RASTER;
237 else if (ext.compare(
".ras") == 0)
238 return FORMAT_RASTER;
239 else if (ext.compare(
".JP2") == 0)
240 return FORMAT_JPEG2000;
241 else if (ext.compare(
".jp2") == 0)
242 return FORMAT_JPEG2000;
244 return FORMAT_UNKNOWN;
248 std::string vpImageIo::getExtension(
const std::string &filename)
251 size_t dot = filename.find_last_of(
".");
252 std::string ext = filename.substr(dot, filename.size()-1);
278 std::string message =
"Cannot read file: \"" + std::string(filename) +
"\" doesn't exist";
281 bool try_opencv_reader =
false;
283 switch(getFormat(filename)){
289 #ifdef VISP_HAVE_JPEG
292 try_opencv_reader =
true;
296 #if defined(VISP_HAVE_PNG)
299 try_opencv_reader =
true;
307 case FORMAT_JPEG2000 :
308 case FORMAT_UNKNOWN :
309 try_opencv_reader =
true;
313 if (try_opencv_reader) {
314 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
316 cv::Mat cvI = cv::imread(filename, cv::IMREAD_GRAYSCALE);
317 if (cvI.cols == 0 && cvI.rows == 0) {
318 std::string message =
"Cannot read file \"" + std::string(filename) +
"\": Image format not supported";
322 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
324 cv::Mat cvI = cv::imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
325 if (cvI.cols == 0 && cvI.rows == 0) {
326 std::string message =
"Cannot read file \"" + std::string(filename) +
"\": Image format not supported";
331 std::string message =
"Cannot read file \"" + std::string(filename) +
"\": Image format not supported";
355 read(I,filename.c_str());
378 std::string message =
"Cannot read file: \"" + std::string(filename) +
"\" doesn't exist";
382 bool try_opencv_reader =
false;
384 switch(getFormat(filename)){
390 #ifdef VISP_HAVE_JPEG
393 try_opencv_reader =
true;
397 #if defined(VISP_HAVE_PNG)
400 try_opencv_reader =
true;
408 case FORMAT_JPEG2000 :
409 case FORMAT_UNKNOWN :
410 try_opencv_reader =
true;
414 if (try_opencv_reader) {
415 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
417 cv::Mat cvI = cv::imread(filename, cv::IMREAD_COLOR);
418 if (cvI.cols == 0 && cvI.rows == 0) {
419 std::string message =
"Cannot read file \"" + std::string(filename) +
"\": Image format not supported";
423 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
425 cv::Mat cvI = cv::imread(filename, CV_LOAD_IMAGE_COLOR);
426 if (cvI.cols == 0 && cvI.rows == 0) {
427 std::string message =
"Cannot read file \"" + std::string(filename) +
"\": Image format not supported";
432 std::string message =
"Cannot read file \"" + std::string(filename) +
"\": Image format not supported";
456 read(I,filename.c_str());
474 bool try_opencv_writer =
false;
476 switch(getFormat(filename)){
482 #ifdef VISP_HAVE_JPEG
485 try_opencv_writer =
true;
492 try_opencv_writer =
true;
500 case FORMAT_JPEG2000 :
501 case FORMAT_UNKNOWN :
502 try_opencv_writer =
true;
506 if (try_opencv_writer) {
507 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
511 cv::imwrite(filename, cvI);
513 vpCERROR <<
"Cannot write file: Image format not supported..." << std::endl;
515 "Cannot write file: Image format not supported")) ;
534 write(I,filename.c_str());
551 bool try_opencv_writer =
false;
553 switch(getFormat(filename)){
559 #ifdef VISP_HAVE_JPEG
562 try_opencv_writer =
true;
569 try_opencv_writer =
true;
577 case FORMAT_JPEG2000 :
578 case FORMAT_UNKNOWN :
579 try_opencv_writer =
true;
583 if (try_opencv_writer) {
584 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
588 cv::imwrite(filename, cvI);
590 vpCERROR <<
"Cannot write file: Image format not supported..." << std::endl;
592 "Cannot write file: Image format not supported")) ;
611 write(I,filename.c_str());
628 const char *filename)
634 if (!filename || *filename ==
'\0') {
640 fd = fopen(filename,
"wb");
645 "cannot write file")) ;
651 fprintf(fd,
"255\n");
657 ierr = fwrite(I.
bitmap,
sizeof(
float), nbyte, fd) ;
663 "cannot write file")) ;
684 const char *filename)
690 if (!filename || *filename ==
'\0') {
696 fd = fopen(filename,
"wb");
701 "cannot write file")) ;
707 fprintf(fd,
"255\n");
713 ierr = fwrite(I.
bitmap,
sizeof(
unsigned char), nbyte, fd) ;
719 "cannot write file")) ;
742 for (
unsigned int i=0 ; i < nrows * ncols ; i++)
765 if (!filename || *filename ==
'\0') {
771 fd = fopen(filename,
"wb");
776 "cannot write file")) ;
782 fprintf(fd,
"255\n");
792 ierr = fwrite(Itmp.
bitmap,
sizeof(
unsigned char), nbyte, fd) ;
798 "cannot write file")) ;
834 if (!filename || *filename ==
'\0')
843 fd = fopen(filename,
"rb");
848 "couldn't read file")) ;
854 err = fgets(str, vpMAX_LEN - 1, fd);
859 vpERROR_TRACE(
"couldn't read line %d of file \"%s\"\n", line, filename) ;
861 "couldn't read file")) ;
869 "this is not a PFM file")) ;
873 if (strcmp(str,
"P8") != 0)
878 "this is not a PFM file")) ;
883 err = fgets(str, vpMAX_LEN - 1, fd);
886 fprintf(stderr,
"couldn't read line %d of file \"%s\"\n", line, filename);
889 "Cannot read content of PFM file")) ;
891 }
while ((str[0] ==
'#') || (str[0] ==
'\n'));
894 ierr = sscanf(str,
"%d %d", &w, &h);
895 if (w > 100000 || h>100000) {
902 err = fgets(str, vpMAX_LEN - 1, fd);
905 fprintf(stderr,
"couldn't read line %d of file \"%s\"\n", line, filename);
908 "Cannot read content of PFM file")) ;
910 }
while ((str[0] ==
'#') || (str[0] ==
'\n'));
911 ierr = sscanf(str,
"%d", &h);
916 vpERROR_TRACE(
"couldn't read line %d of file \"%s\"\n",line, filename) ;
918 "Cannot read content of PFM file")) ;
931 "Cannot read content of PFM file")) ;
936 err = fgets(str, vpMAX_LEN - 1, fd);
940 vpERROR_TRACE(
"couldn't read line %d of file \"%s\"\n",line, filename) ;
942 "Cannot read content of PFM file")) ;
945 ierr = sscanf(str,
"%d", &is255);
948 vpERROR_TRACE(
"couldn't read line %d of file \"%s\"\n", line, filename) ;
950 "Cannot read content of PFM file")) ;
956 vpERROR_TRACE(
"MAX_VAL is not 255 in file \"%s\"\n", filename) ;
958 "Cannot read content of PFM file")) ;
962 if (fread (I.
bitmap,
sizeof(
float), nbyte, fd ) != nbyte)
965 vpERROR_TRACE(
"couldn't read %d bytes in file \"%s\"\n", nbyte, filename) ;
967 "Cannot read content of PFM file")) ;
998 unsigned int magic=5, w=0, h=0, maxval=255;
1001 if (!filename || *filename ==
'\0') {
1007 if ((fd = fopen(filename,
"rb")) == NULL) {
1009 "Cannot read file \"%s\"", filename)) ;
1012 while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] ==
'#') || (str[0] ==
'\n'))) {};
1016 "Cannot read header of file \"%s\"", filename));
1018 if ((ierr = sscanf(str,
"P%u %u %u %u", &magic, &w, &h, &maxval)) == 0) {
1021 "Cannot read header of file \"%s\"", filename));
1027 "\"%s\" is not a PGM P5 file", filename));
1038 while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] ==
'#') || (str[0] ==
'\n'))) {};
1042 "Cannot read header of file \"%s\"", filename));
1044 if (((ierr = sscanf(str,
"%u %u %u", &w, &h, &maxval)) == 0) || (ierr != 1 && ierr != 2 && ierr != 3)) {
1047 "Cannot read header of file \"%s\"", filename));
1055 while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] ==
'#') || (str[0] ==
'\n'))) {};
1059 "Cannot read header of file \"%s\"", filename));
1061 if (((ierr = sscanf(str,
"%u %u", &h, &maxval)) == 0) || (ierr != 1 && ierr != 2)) {
1064 "Cannot read header of file \"%s\"", filename));
1068 while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] ==
'#') || (str[0] ==
'\n'))) {};
1072 "Cannot read header of file \"%s\"", filename));
1074 if ((ierr = sscanf(str,
"%u", &maxval)) != 1) {
1077 "Cannot read header of file \"%s\"", filename));
1084 else if (ierr == 2) {
1087 while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] ==
'#') || (str[0] ==
'\n'))) {};
1091 "Cannot read header of file \"%s\"", filename));
1093 if ((ierr = sscanf(str,
"%u", &maxval)) != 1) {
1096 "Cannot read header of file \"%s\"", filename));
1104 else if (ierr == 2) {
1106 while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] ==
'#') || (str[0] ==
'\n'))) {};
1110 "Cannot read header of file \"%s\"", filename));
1112 if (((ierr = sscanf(str,
"%u %u", &h, &maxval)) == 0) || (ierr != 1 && ierr != 2)) {
1115 "Cannot read header of file \"%s\"", filename));
1119 while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] ==
'#') || (str[0] ==
'\n'))) {};
1123 "Cannot read header of file \"%s\"", filename));
1125 if ((ierr = sscanf(str,
"%u", &maxval)) != 1) {
1128 "Cannot read header of file \"%s\"", filename));
1136 else if (ierr == 3) {
1138 while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] ==
'#') || (str[0] ==
'\n'))) {};
1142 "Cannot read header of file \"%s\"", filename));
1144 if ((ierr = sscanf(str,
"%u", &maxval)) != 1) {
1147 "Cannot read header of file \"%s\"", filename));
1155 if (w > 100000 || h>100000) {
1163 "Bad maxval in \"%s\"", filename));
1172 if ((n = fread (I.
bitmap,
sizeof(
unsigned char), nbyte, fd)) != nbyte) {
1175 "Read only %d of %d bytes in file \"%s\"", n, nbyte, filename));
1277 char str[vpMAX_LEN];
1278 unsigned int magic=5, w=0, h=0, maxval=255;
1281 if (!filename || *filename ==
'\0') {
1287 if ((fd = fopen(filename,
"rb")) == NULL) {
1289 "Cannot read file \"%s\"", filename)) ;
1292 while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] ==
'#') || (str[0] ==
'\n'))) {};
1296 "Cannot read header of file \"%s\"", filename));
1298 if ((ierr = sscanf(str,
"P%u %u %u %u", &magic, &w, &h, &maxval)) == 0) {
1301 "Cannot read header of file \"%s\"", filename));
1307 "\"%s\" is not a PGM P6 file", filename));
1318 while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] ==
'#') || (str[0] ==
'\n'))) {};
1322 "Cannot read header of file \"%s\"", filename));
1324 if (((ierr = sscanf(str,
"%u %u %u", &w, &h, &maxval)) == 0) || (ierr != 1 && ierr != 2 && ierr != 3)) {
1327 "Cannot read header of file \"%s\"", filename));
1335 while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] ==
'#') || (str[0] ==
'\n'))) {};
1339 "Cannot read header of file \"%s\"", filename));
1341 if (((ierr = sscanf(str,
"%u %u", &h, &maxval)) == 0) || (ierr != 1 && ierr != 2)) {
1344 "Cannot read header of file \"%s\"", filename));
1348 while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] ==
'#') || (str[0] ==
'\n'))) {};
1352 "Cannot read header of file \"%s\"", filename));
1354 if ((ierr = sscanf(str,
"%u", &maxval)) != 1) {
1357 "Cannot read header of file \"%s\"", filename));
1364 else if (ierr == 2) {
1367 while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] ==
'#') || (str[0] ==
'\n'))) {};
1371 "Cannot read header of file \"%s\"", filename));
1373 if ((ierr = sscanf(str,
"%u", &maxval)) != 1) {
1376 "Cannot read header of file \"%s\"", filename));
1384 else if (ierr == 2) {
1386 while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] ==
'#') || (str[0] ==
'\n'))) {};
1390 "Cannot read header of file \"%s\"", filename));
1392 if (((ierr = sscanf(str,
"%u %u", &h, &maxval)) == 0) || (ierr != 1 && ierr != 2)) {
1395 "Cannot read header of file \"%s\"", filename));
1399 while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] ==
'#') || (str[0] ==
'\n'))) {};
1403 "Cannot read header of file \"%s\"", filename));
1405 if ((ierr = sscanf(str,
"%u", &maxval)) != 1) {
1408 "Cannot read header of file \"%s\"", filename));
1416 else if (ierr == 3) {
1418 while ((err = fgets(str, vpMAX_LEN - 1, fd)) != NULL && ((str[0] ==
'#') || (str[0] ==
'\n'))) {};
1422 "Cannot read header of file \"%s\"", filename));
1424 if ((ierr = sscanf(str,
"%u", &maxval)) != 1) {
1427 "Cannot read header of file \"%s\"", filename));
1435 if (w > 100000 || h>100000) {
1443 "Bad maxval in \"%s\"", filename));
1450 for(
unsigned int i=0;i<I.
getHeight();i++)
1452 for(
unsigned int j=0;j<I.
getWidth();j++)
1455 size_t res = fread(&v.
R,
sizeof(v.
R),1,fd) ;
1456 res |= fread(&v.
G,
sizeof(v.
G),1,fd) ;
1457 res |= fread(&v.
B,
sizeof(v.
B),1,fd) ;
1462 "Cannot read bytes in file \"%s\"\n", filename));
1516 if (!filename || *filename ==
'\0') {
1522 f = fopen(filename,
"wb");
1527 "cannot write file")) ;
1534 fprintf(f,
"%d\n",255);
1536 for(
unsigned int i=0;i<I.
getHeight();i++)
1538 for(
unsigned int j=0;j<I.
getWidth();j++)
1545 res = fwrite(&tmp,
sizeof(tmp),1,f) ;
1551 "cannot write file")) ;
1554 res = fwrite(&tmp,
sizeof(tmp),1,f) ;
1560 "cannot write file")) ;
1563 res = fwrite(&tmp,
sizeof(tmp),1,f) ;
1569 "cannot write file")) ;
1632 const std::string filename)
1746 #if defined(VISP_HAVE_JPEG)
1758 struct jpeg_compress_struct cinfo;
1759 struct jpeg_error_mgr jerr;
1762 cinfo.err = jpeg_std_error(&jerr);
1763 jpeg_create_compress(&cinfo);
1766 if (!filename || *filename ==
'\0') {
1772 file = fopen(filename,
"wb");
1777 "cannot write file")) ;
1783 jpeg_stdio_dest(&cinfo, file);
1785 cinfo.image_width = width;
1786 cinfo.image_height = height;
1787 cinfo.input_components = 1;
1788 cinfo.in_color_space = JCS_GRAYSCALE;
1789 jpeg_set_defaults(&cinfo);
1791 jpeg_start_compress(&cinfo,TRUE);
1793 unsigned char *line;
1794 line =
new unsigned char[width];
1795 unsigned char* input = (
unsigned char*)I.
bitmap;
1796 while (cinfo.next_scanline < cinfo.image_height)
1798 for (
unsigned int i = 0; i < width; i++)
1803 jpeg_write_scanlines(&cinfo, &line, 1);
1806 jpeg_finish_compress(&cinfo);
1807 jpeg_destroy_compress(&cinfo);
1837 struct jpeg_compress_struct cinfo;
1838 struct jpeg_error_mgr jerr;
1841 cinfo.err = jpeg_std_error(&jerr);
1842 jpeg_create_compress(&cinfo);
1845 if (!filename || *filename ==
'\0') {
1851 file = fopen(filename,
"wb");
1856 "cannot write file")) ;
1862 jpeg_stdio_dest(&cinfo, file);
1864 cinfo.image_width = width;
1865 cinfo.image_height = height;
1866 cinfo.input_components = 3;
1867 cinfo.in_color_space = JCS_RGB;
1868 jpeg_set_defaults(&cinfo);
1870 jpeg_start_compress(&cinfo,TRUE);
1872 unsigned char *line;
1873 line =
new unsigned char[3*width];
1874 unsigned char* input = (
unsigned char*)I.
bitmap;
1875 while (cinfo.next_scanline < cinfo.image_height)
1877 for (
unsigned int i = 0; i < width; i++)
1879 line[i*3] = *(input); input++;
1880 line[i*3+1] = *(input); input++;
1881 line[i*3+2] = *(input); input++;
1884 jpeg_write_scanlines(&cinfo, &line, 1);
1887 jpeg_finish_compress(&cinfo);
1888 jpeg_destroy_compress(&cinfo);
1927 struct jpeg_decompress_struct cinfo;
1928 struct jpeg_error_mgr jerr;
1931 cinfo.err = jpeg_std_error(&jerr);
1932 jpeg_create_decompress(&cinfo);
1935 if (!filename || *filename ==
'\0') {
1941 file = fopen(filename,
"rb");
1946 "cannot read file")) ;
1949 jpeg_stdio_src(&cinfo, file);
1950 jpeg_read_header(&cinfo, TRUE);
1952 unsigned int width = cinfo.image_width;
1953 unsigned int height = cinfo.image_height;
1958 jpeg_start_decompress(&cinfo);
1960 unsigned int rowbytes = cinfo.output_width * (
unsigned int)(cinfo.output_components);
1961 JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray)
1962 ((j_common_ptr) &cinfo, JPOOL_IMAGE, rowbytes, 1);
1964 if (cinfo.out_color_space == JCS_RGB) {
1966 unsigned char* output = (
unsigned char*)Ic.
bitmap;
1967 while (cinfo.output_scanline<cinfo.output_height) {
1968 jpeg_read_scanlines(&cinfo,buffer,1);
1969 for (
unsigned int i = 0; i < width; i++) {
1970 *(output++) = buffer[0][i*3];
1971 *(output++) = buffer[0][i*3+1];
1972 *(output++) = buffer[0][i*3+2];
1979 else if (cinfo.out_color_space == JCS_GRAYSCALE)
1982 while (cinfo.output_scanline<cinfo.output_height)
1984 row = cinfo.output_scanline;
1985 jpeg_read_scanlines(&cinfo,buffer,1);
1986 memcpy(I[row], buffer[0], rowbytes);
1990 jpeg_finish_decompress(&cinfo);
1991 jpeg_destroy_decompress(&cinfo);
2040 struct jpeg_decompress_struct cinfo;
2041 struct jpeg_error_mgr jerr;
2044 cinfo.err = jpeg_std_error(&jerr);
2045 jpeg_create_decompress(&cinfo);
2048 if (!filename || *filename ==
'\0') {
2054 file = fopen(filename,
"rb");
2059 "cannot read file")) ;
2062 jpeg_stdio_src(&cinfo, file);
2064 jpeg_read_header(&cinfo, TRUE);
2066 unsigned int width = cinfo.image_width;
2067 unsigned int height = cinfo.image_height;
2072 jpeg_start_decompress(&cinfo);
2074 unsigned int rowbytes = cinfo.output_width * (
unsigned int)(cinfo.output_components);
2075 JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray)
2076 ((j_common_ptr) &cinfo, JPOOL_IMAGE, rowbytes, 1);
2078 if (cinfo.out_color_space == JCS_RGB)
2080 unsigned char* output = (
unsigned char*)I.
bitmap;
2081 while (cinfo.output_scanline<cinfo.output_height)
2083 jpeg_read_scanlines(&cinfo,buffer,1);
2084 for (
unsigned int i = 0; i < width; i++) {
2085 *(output++) = buffer[0][i*3];
2086 *(output++) = buffer[0][i*3+1];
2087 *(output++) = buffer[0][i*3+2];
2093 else if (cinfo.out_color_space == JCS_GRAYSCALE)
2098 while (cinfo.output_scanline<cinfo.output_height)
2100 row = cinfo.output_scanline;
2101 jpeg_read_scanlines(&cinfo,buffer,1);
2102 memcpy(Ig[row], buffer[0], rowbytes);
2108 jpeg_finish_decompress(&cinfo);
2109 jpeg_destroy_decompress(&cinfo);
2138 #elif defined(VISP_HAVE_OPENCV)
2150 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408)
2153 cv::imwrite(filename, Ip);
2155 IplImage* Ip = NULL;
2158 cvSaveImage(filename, Ip);
2160 cvReleaseImage(&Ip);
2189 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408)
2192 cv::imwrite(filename, Ip);
2194 IplImage* Ip = NULL;
2197 cvSaveImage(filename, Ip);
2199 cvReleaseImage(&Ip);
2237 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
2238 cv::Mat Ip = cv::imread(filename, cv::IMREAD_GRAYSCALE);
2243 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020408)
2244 cv::Mat Ip = cv::imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
2250 IplImage* Ip = NULL;
2251 Ip = cvLoadImage(filename, CV_LOAD_IMAGE_GRAYSCALE);
2256 "Can't read the image")) ;
2257 cvReleaseImage(&Ip);
2306 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
2307 cv::Mat Ip = cv::imread(filename, cv::IMREAD_GRAYSCALE);
2312 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020408)
2313 cv::Mat Ip = cv::imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
2319 IplImage* Ip = NULL;
2320 Ip = cvLoadImage(filename, CV_LOAD_IMAGE_COLOR);
2325 cvReleaseImage(&Ip);
2365 #if defined(VISP_HAVE_PNG)
2380 if (!filename || *filename ==
'\0') {
2386 file = fopen(filename,
"wb");
2391 "cannot write file")) ;
2395 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL, NULL, NULL);
2401 "PNG write error")) ;
2404 png_infop info_ptr = png_create_info_struct(png_ptr);
2408 png_destroy_write_struct (&png_ptr, NULL);
2411 "PNG write error")) ;
2415 if (setjmp (png_jmpbuf (png_ptr)))
2418 png_destroy_write_struct (&png_ptr, &info_ptr);
2421 "PNG write error")) ;
2425 png_init_io (png_ptr, file);
2430 int color_type = PNG_COLOR_TYPE_GRAY;
2433 if (setjmp (png_jmpbuf (png_ptr)))
2436 png_destroy_write_struct (&png_ptr, &info_ptr);
2439 "PNG write error")) ;
2442 png_set_IHDR(png_ptr, info_ptr, width, height,
2443 bit_depth, color_type, PNG_INTERLACE_NONE,
2444 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
2446 png_write_info(png_ptr, info_ptr);
2448 png_bytep* row_ptrs =
new png_bytep[height];
2449 for (
unsigned int i = 0; i < height; i++)
2450 row_ptrs[i] =
new png_byte[width];
2452 unsigned char* input = (
unsigned char*)I.
bitmap;
2454 for (
unsigned int i = 0; i < height; i++)
2456 png_byte* row = row_ptrs[i];
2457 for(
unsigned int j = 0; j < width; j++)
2464 png_write_image(png_ptr, row_ptrs);
2466 png_write_end(png_ptr, NULL);
2468 for(
unsigned int j = 0; j < height; j++)
2469 delete[] row_ptrs[j];
2473 png_destroy_write_struct (&png_ptr, &info_ptr);
2506 if (!filename || *filename ==
'\0') {
2512 file = fopen(filename,
"wb");
2517 "cannot write file")) ;
2521 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL, NULL, NULL);
2527 "PNG write error")) ;
2530 png_infop info_ptr = png_create_info_struct(png_ptr);
2534 png_destroy_write_struct (&png_ptr, NULL);
2537 "PNG write error")) ;
2541 if (setjmp (png_jmpbuf (png_ptr)))
2544 png_destroy_write_struct (&png_ptr, &info_ptr);
2547 "PNG write error")) ;
2551 png_init_io (png_ptr, file);
2556 int color_type = PNG_COLOR_TYPE_RGB;
2559 if (setjmp (png_jmpbuf (png_ptr)))
2562 png_destroy_write_struct (&png_ptr, &info_ptr);
2565 "PNG write error")) ;
2568 png_set_IHDR(png_ptr, info_ptr, width, height,
2569 bit_depth, color_type, PNG_INTERLACE_NONE,
2570 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
2572 png_write_info(png_ptr, info_ptr);
2574 png_bytep* row_ptrs =
new png_bytep[height];
2575 for (
unsigned int i = 0; i < height; i++)
2576 row_ptrs[i] =
new png_byte[3*width];
2578 unsigned char* input = (
unsigned char*)I.
bitmap;;
2580 for (
unsigned int i = 0; i < height; i++)
2582 png_byte* row = row_ptrs[i];
2583 for(
unsigned int j = 0; j < width; j++)
2585 row[3*j] = *(input);input++;
2586 row[3*j+1] = *(input);input++;
2587 row[3*j+2] = *(input);input++;
2592 png_write_image(png_ptr, row_ptrs);
2594 png_write_end(png_ptr, NULL);
2596 for(
unsigned int j = 0; j < height; j++)
2597 delete[] row_ptrs[j];
2601 png_destroy_write_struct (&png_ptr, &info_ptr);
2642 if (!filename || *filename ==
'\0') {
2648 file = fopen(filename,
"rb");
2653 "cannot read file")) ;
2657 if (fread (magic, 1,
sizeof (magic), file) !=
sizeof (magic))
2660 vpERROR_TRACE(
"couldn't read magic number in file \"%s\"\n", filename) ;
2662 "error reading png file")) ;
2666 if (png_sig_cmp (magic,0,
sizeof (magic)))
2668 fprintf (stderr,
"error: \"%s\" is not a valid PNG image!\n",filename);
2671 "error reading png file")) ;
2676 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
2677 if (png_ptr == NULL)
2679 fprintf (stderr,
"error: can't create a png read structure!\n");
2682 "error reading png file")) ;
2686 png_infop info_ptr = png_create_info_struct (png_ptr);
2687 if (info_ptr == NULL)
2689 fprintf (stderr,
"error: can't create a png info structure!\n");
2691 png_destroy_read_struct (&png_ptr, NULL, NULL);
2693 "error reading png file")) ;
2697 if (setjmp (png_jmpbuf (png_ptr)))
2700 png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
2703 "PNG read error")) ;
2707 png_init_io (png_ptr, file);
2710 png_set_sig_bytes (png_ptr,
sizeof (magic));
2713 png_read_info (png_ptr, info_ptr);
2715 unsigned int width = png_get_image_width(png_ptr, info_ptr);
2716 unsigned int height = png_get_image_height(png_ptr, info_ptr);
2718 unsigned int bit_depth, channels, color_type;
2720 bit_depth = png_get_bit_depth (png_ptr, info_ptr);
2721 channels = png_get_channels(png_ptr, info_ptr);
2722 color_type = png_get_color_type (png_ptr, info_ptr);
2725 if (color_type == PNG_COLOR_TYPE_PALETTE)
2726 png_set_palette_to_rgb (png_ptr);
2729 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
2730 png_set_expand (png_ptr);
2735 if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2736 png_set_strip_alpha(png_ptr);
2738 if (bit_depth == 16)
2739 png_set_strip_16 (png_ptr);
2740 else if (bit_depth < 8)
2741 png_set_packing (png_ptr);
2744 png_read_update_info (png_ptr, info_ptr);
2746 channels = png_get_channels(png_ptr, info_ptr);
2751 png_bytep* rowPtrs =
new png_bytep[height];
2753 unsigned int stride = width * bit_depth * channels / 8;
2754 unsigned char* data =
new unsigned char[stride * height];
2756 for (
unsigned int i =0; i < height; i++)
2757 rowPtrs[i] = (png_bytep)data + (i * stride);
2759 png_read_image(png_ptr, rowPtrs);
2762 unsigned char* output;
2767 output = (
unsigned char*)I.
bitmap;
2768 for (
unsigned int i = 0; i < width*height; i++)
2770 *(output++) = data[i];
2774 output = (
unsigned char*)I.
bitmap;
2775 for (
unsigned int i = 0; i < width*height; i++)
2777 *(output++) = data[i*2];
2782 output = (
unsigned char*)Ic.
bitmap;
2783 for (
unsigned int i = 0; i < width*height; i++)
2785 *(output++) = data[i*3];
2786 *(output++) = data[i*3+1];
2787 *(output++) = data[i*3+2];
2793 output = (
unsigned char*)Ic.
bitmap;
2794 for (
unsigned int i = 0; i < width*height; i++)
2796 *(output++) = data[i*4];
2797 *(output++) = data[i*4+1];
2798 *(output++) = data[i*4+2];
2799 *(output++) = data[i*4+3];
2805 delete [] (png_bytep)rowPtrs;
2807 png_read_end (png_ptr, NULL);
2808 png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
2861 if (!filename || *filename ==
'\0') {
2867 file = fopen(filename,
"rb");
2872 "cannot read file")) ;
2876 if (fread (magic, 1,
sizeof (magic), file) !=
sizeof (magic))
2879 vpERROR_TRACE(
"couldn't read magic number in file \"%s\"\n", filename) ;
2881 "error reading pgm file")) ;
2885 if (png_sig_cmp (magic,0,
sizeof (magic)))
2888 vpERROR_TRACE(
"error: \"%s\" is not a valid PNG image!\n",filename);
2890 "PNG read error")) ;
2894 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
2900 "PNG read error")) ;
2904 png_infop info_ptr = png_create_info_struct (png_ptr);
2908 png_destroy_read_struct (&png_ptr, NULL, NULL);
2911 "PNG read error")) ;
2915 if (setjmp (png_jmpbuf (png_ptr)))
2918 png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
2921 "PNG read error")) ;
2925 png_init_io (png_ptr, file);
2928 png_set_sig_bytes (png_ptr,
sizeof (magic));
2931 png_read_info (png_ptr, info_ptr);
2933 unsigned int width = png_get_image_width(png_ptr, info_ptr);
2934 unsigned int height = png_get_image_height(png_ptr, info_ptr);
2936 unsigned int bit_depth, channels, color_type;
2938 bit_depth = png_get_bit_depth (png_ptr, info_ptr);
2939 channels = png_get_channels(png_ptr, info_ptr);
2940 color_type = png_get_color_type (png_ptr, info_ptr);
2943 if (color_type == PNG_COLOR_TYPE_PALETTE)
2944 png_set_palette_to_rgb (png_ptr);
2947 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
2948 png_set_expand (png_ptr);
2953 if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2954 png_set_strip_alpha(png_ptr);
2956 if (bit_depth == 16)
2957 png_set_strip_16 (png_ptr);
2958 else if (bit_depth < 8)
2959 png_set_packing (png_ptr);
2962 png_read_update_info (png_ptr, info_ptr);
2964 channels = png_get_channels(png_ptr, info_ptr);
2969 png_bytep* rowPtrs =
new png_bytep[height];
2971 unsigned int stride = width * bit_depth * channels / 8;
2972 unsigned char* data =
new unsigned char[stride * height];
2975 for (
unsigned int i =0; i < height; i++)
2976 rowPtrs[i] = (png_bytep)data + (i * stride);
2978 png_read_image(png_ptr, rowPtrs);
2981 unsigned char* output;
2986 output = (
unsigned char*)Ig.
bitmap;
2987 for (
unsigned int i = 0; i < width*height; i++)
2989 *(output++) = data[i];
2994 output = (
unsigned char*)Ig.
bitmap;
2995 for (
unsigned int i = 0; i < width*height; i++)
2997 *(output++) = data[i*2];
3003 output = (
unsigned char*)I.
bitmap;
3004 for (
unsigned int i = 0; i < width*height; i++)
3006 *(output++) = data[i*3];
3007 *(output++) = data[i*3+1];
3008 *(output++) = data[i*3+2];
3013 output = (
unsigned char*)I.
bitmap;
3014 for (
unsigned int i = 0; i < width*height; i++)
3016 *(output++) = data[i*4];
3017 *(output++) = data[i*4+1];
3018 *(output++) = data[i*4+2];
3019 *(output++) = data[i*4+3];
3024 delete [] (png_bytep)rowPtrs;
3026 png_read_end (png_ptr, NULL);
3027 png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
3056 #elif defined(VISP_HAVE_OPENCV)
3068 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408)
3071 cv::imwrite(filename, Ip);
3073 IplImage* Ip = NULL;
3076 cvSaveImage(filename, Ip);
3078 cvReleaseImage(&Ip);
3107 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408)
3110 cv::imwrite(filename, Ip);
3112 IplImage* Ip = NULL;
3115 cvSaveImage(filename, Ip);
3117 cvReleaseImage(&Ip);
3155 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
3156 cv::Mat Ip = cv::imread(filename, cv::IMREAD_GRAYSCALE);
3161 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020408)
3162 cv::Mat Ip = cv::imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
3168 IplImage* Ip = NULL;
3169 Ip = cvLoadImage(filename, CV_LOAD_IMAGE_GRAYSCALE);
3174 "Can't read the image")) ;
3175 cvReleaseImage(&Ip);
3224 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
3225 cv::Mat Ip = cv::imread(filename, cv::IMREAD_GRAYSCALE);
3230 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020408)
3231 cv::Mat Ip = cv::imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
3237 IplImage* Ip = NULL;
3238 Ip = cvLoadImage(filename, CV_LOAD_IMAGE_COLOR);
3243 "Can't read the image")) ;
3244 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)