47 #include <visp/vpImage.h>
48 #include <visp/vpImageIo.h>
49 #include <visp/vpImageConvert.h>
51 const int vpImageIo::vpMAX_LEN = 100;
62 vpImageIo::openFileRead(
const char *filename)
68 if (filename ==
'\0') {
75 if ((fd = fopen(filename,
"r")) == NULL)
79 "cannot open file")) ;
97 vpImageIo::openFileWrite(
const char *filename,
const char *mode)
102 if (filename ==
'\0')
106 "filename empty ")) ;
110 if ((fd = fopen(filename, mode)) == NULL)
114 "cannot open file")) ;
128 vpImageIo::openFileRead(
const std::string filename)
134 if (filename.empty()) {
137 "filename empty ")) ;
141 if ((fd = fopen(filename.c_str(),
"r")) == NULL)
145 "cannot open file")) ;
163 vpImageIo::openFileWrite(
const std::string filename,
164 const std::string mode)
169 if (filename.empty())
173 "filename empty ")) ;
177 if ((fd = fopen(filename.c_str(), mode.c_str())) == NULL)
181 "cannot open file")) ;
186 vpImageIo::vpImageFormatType
187 vpImageIo::getFormat(
const char *filename)
189 std::string sfilename(filename);
191 std::string ext = vpImageIo::getExtension(sfilename);
193 if (ext.compare(
".PGM") == 0)
195 else if (ext.compare(
".pgm") == 0)
197 else if (ext.compare(
".PPM") == 0)
199 else if (ext.compare(
".ppm") == 0)
201 else if (ext.compare(
".JPG") == 0)
203 else if (ext.compare(
".jpg") == 0)
205 else if (ext.compare(
".JPEG") == 0)
207 else if (ext.compare(
".jpeg") == 0)
209 else if (ext.compare(
".PNG") == 0)
211 else if (ext.compare(
".png") == 0)
214 else if (ext.compare(
".TIFF") == 0)
216 else if (ext.compare(
".tiff") == 0)
218 else if (ext.compare(
".TIF") == 0)
220 else if (ext.compare(
".tif") == 0)
222 else if (ext.compare(
".BMP") == 0)
224 else if (ext.compare(
".bmp") == 0)
226 else if (ext.compare(
".DIB") == 0)
228 else if (ext.compare(
".dib") == 0)
230 else if (ext.compare(
".PBM") == 0)
232 else if (ext.compare(
".pbm") == 0)
234 else if (ext.compare(
".SR") == 0)
235 return FORMAT_RASTER;
236 else if (ext.compare(
".sr") == 0)
237 return FORMAT_RASTER;
238 else if (ext.compare(
".RAS") == 0)
239 return FORMAT_RASTER;
240 else if (ext.compare(
".ras") == 0)
241 return FORMAT_RASTER;
242 else if (ext.compare(
".JP2") == 0)
243 return FORMAT_JPEG2000;
244 else if (ext.compare(
".jp2") == 0)
245 return FORMAT_JPEG2000;
247 return FORMAT_UNKNOWN;
251 std::string vpImageIo::getExtension(
const std::string &filename)
254 size_t dot = filename.find_last_of(
".");
255 std::string ext = filename.substr(dot, filename.size()-1);
279 bool try_opencv_reader =
false;
281 switch(getFormat(filename)){
287 #ifdef VISP_HAVE_LIBJPEG
290 try_opencv_reader =
true;
294 #if defined(VISP_HAVE_LIBPNG)
297 try_opencv_reader =
true;
305 case FORMAT_JPEG2000 :
306 case FORMAT_UNKNOWN :
307 try_opencv_reader =
true;
311 if (try_opencv_reader) {
312 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
314 cv::Mat cvI = cv::imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
317 vpCERROR <<
"Cannot read file: Image format not supported..." << std::endl;
319 "Cannot read file: Image format not supported")) ;
342 read(I,filename.c_str());
363 bool try_opencv_reader =
false;
365 switch(getFormat(filename)){
371 #ifdef VISP_HAVE_LIBJPEG
374 try_opencv_reader =
true;
378 #if defined(VISP_HAVE_LIBPNG)
381 try_opencv_reader =
true;
389 case FORMAT_JPEG2000 :
390 case FORMAT_UNKNOWN :
391 try_opencv_reader =
true;
395 if (try_opencv_reader) {
396 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
398 cv::Mat cvI = cv::imread(filename, CV_LOAD_IMAGE_COLOR);
401 vpCERROR <<
"Cannot read file: Image format not supported..." << std::endl;
403 "Cannot read file: Image format not supported")) ;
426 read(I,filename.c_str());
444 bool try_opencv_writer =
false;
446 switch(getFormat(filename)){
452 #ifdef VISP_HAVE_LIBJPEG
455 try_opencv_writer =
true;
459 #ifdef VISP_HAVE_LIBPNG
462 try_opencv_writer =
true;
470 case FORMAT_JPEG2000 :
471 case FORMAT_UNKNOWN :
472 try_opencv_writer =
true;
476 if (try_opencv_writer) {
477 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
481 cv::imwrite(filename, cvI);
483 vpCERROR <<
"Cannot write file: Image format not supported..." << std::endl;
485 "Cannot write file: Image format not supported")) ;
504 write(I,filename.c_str());
521 bool try_opencv_writer =
false;
523 switch(getFormat(filename)){
529 #ifdef VISP_HAVE_LIBJPEG
532 try_opencv_writer =
true;
536 #ifdef VISP_HAVE_LIBPNG
539 try_opencv_writer =
true;
547 case FORMAT_JPEG2000 :
548 case FORMAT_UNKNOWN :
549 try_opencv_writer =
true;
553 if (try_opencv_writer) {
554 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
558 cv::imwrite(filename, cvI);
560 vpCERROR <<
"Cannot write file: Image format not supported..." << std::endl;
562 "Cannot write file: Image format not supported")) ;
581 write(I,filename.c_str());
598 const char *filename)
604 if (filename ==
'\0') {
610 fd = fopen(filename,
"wb");
615 "cannot write file")) ;
621 fprintf(fd,
"255\n");
627 ierr = fwrite(I.
bitmap,
sizeof(
float), nbyte, fd) ;
633 "cannot write file")) ;
654 const char *filename)
660 if (filename ==
'\0') {
666 fd = fopen(filename,
"wb");
671 "cannot write file")) ;
677 fprintf(fd,
"255\n");
683 ierr = fwrite(I.
bitmap,
sizeof(
unsigned char), nbyte, fd) ;
689 "cannot write file")) ;
712 for (
unsigned int i=0 ; i < nrows * ncols ; i++)
735 if (filename ==
'\0') {
741 fd = fopen(filename,
"wb");
746 "cannot write file")) ;
752 fprintf(fd,
"255\n");
762 ierr = fwrite(Itmp.
bitmap,
sizeof(
unsigned char), nbyte, fd) ;
768 "cannot write file")) ;
804 if (filename ==
'\0')
813 fd = fopen(filename,
"rb");
818 "couldn't read file")) ;
824 err = fgets(str, vpMAX_LEN - 1, fd);
829 vpERROR_TRACE(
"couldn't read line %d of file \"%s\"\n", line, filename) ;
831 "couldn't read file")) ;
839 "this is not a pfm file")) ;
843 if (strcmp(str,
"P8") != 0)
848 "this is not a pgm file")) ;
853 err = fgets(str, vpMAX_LEN - 1, fd);
856 fprintf(stderr,
"couldn't read line %d of file \"%s\"\n", line, filename);
859 }
while ((str[0] ==
'#') || (str[0] ==
'\n'));
862 ierr = sscanf(str,
"%d %d", &w, &h);
865 err = fgets(str, vpMAX_LEN - 1, fd);
868 fprintf(stderr,
"couldn't read line %d of file \"%s\"\n", line, filename);
871 }
while ((str[0] ==
'#') || (str[0] ==
'\n'));
872 ierr = sscanf(str,
"%d", &h);
877 vpERROR_TRACE(
"couldn't read line %d of file \"%s\"\n",line, filename) ;
879 "couldn't read file")) ;
897 err = fgets(str, vpMAX_LEN - 1, fd);
901 vpERROR_TRACE(
"couldn't read line %d of file \"%s\"\n",line, filename) ;
903 "couldn't read file")) ;
906 ierr = sscanf(str,
"%d", &is255);
909 vpERROR_TRACE(
"couldn't read line %d of file \"%s\"\n", line, filename) ;
911 "couldn't read file")) ;
917 vpERROR_TRACE(
"MAX_VAL is not 255 in file \"%s\"\n", filename) ;
919 "error reading pfm file")) ;
923 if (fread (I.
bitmap,
sizeof(
float), nbyte, fd ) != nbyte)
926 vpERROR_TRACE(
"couldn't read %d bytes in file \"%s\"\n", nbyte, filename) ;
928 "error reading pfm file")) ;
966 if (filename ==
'\0')
975 fd = fopen(filename,
"rb");
980 "couldn't read file")) ;
986 err = fgets(str, vpMAX_LEN - 1, fd);
991 vpERROR_TRACE(
"couldn't read line %d of file \"%s\"\n", line, filename) ;
993 "couldn't read file")) ;
1001 "this is not a pgm file")) ;
1005 if (strcmp(str,
"P5") != 0)
1010 "this is not a pgm file")) ;
1015 err = fgets(str, vpMAX_LEN - 1, fd);
1018 fprintf(stderr,
"couldn't read line %d of file \"%s\"\n", line, filename);
1021 }
while ((str[0] ==
'#') || (str[0] ==
'\n'));
1024 ierr = sscanf(str,
"%d %d", &w, &h);
1027 err = fgets(str, vpMAX_LEN - 1, fd);
1030 fprintf(stderr,
"couldn't read line %d of file \"%s\"\n", line, filename);
1033 }
while ((str[0] ==
'#') || (str[0] ==
'\n'));
1034 ierr = sscanf(str,
"%d", &h);
1039 vpERROR_TRACE(
"couldn't read line %d of file \"%s\"\n",line, filename) ;
1041 "couldn't read file")) ;
1059 err = fgets(str, vpMAX_LEN - 1, fd);
1063 vpERROR_TRACE(
"couldn't read line %d of file \"%s\"\n",line, filename) ;
1065 "couldn't read file")) ;
1068 ierr = sscanf(str,
"%d", &is255);
1071 vpERROR_TRACE(
"couldn't read line %d of file \"%s\"\n", line, filename) ;
1073 "couldn't read file")) ;
1079 vpERROR_TRACE(
"MAX_VAL is not 255 in file \"%s\"\n", filename) ;
1081 "error reading pgm file")) ;
1085 if (fread (I.
bitmap,
sizeof(
unsigned char), nbyte, fd ) != nbyte)
1088 vpERROR_TRACE(
"couldn't read %d bytes in file \"%s\"\n", nbyte, filename) ;
1090 "error reading pgm file")) ;
1199 char str[vpMAX_LEN];
1203 if (filename ==
'\0')
1212 fd = fopen(filename,
"rb");
1217 "couldn't read file")) ;
1223 err = fgets(str, vpMAX_LEN - 1, fd);
1228 vpERROR_TRACE(
"couldn't read line %d of file \"%s\"\n", line, filename) ;
1230 "couldn't read file")) ;
1233 if (strlen(str) < 3)
1238 "this is not a ppm file")) ;
1242 if (strcmp(str,
"P6") != 0)
1247 "this is not a ppm file")) ;
1252 err = fgets(str, vpMAX_LEN - 1, fd);
1255 fprintf(stderr,
"couldn't read line %d of file \"%s\"\n", line, filename);
1258 }
while ((str[0] ==
'#') || (str[0] ==
'\n'));
1261 ierr = sscanf(str,
"%d %d", &w, &h);
1264 err = fgets(str, vpMAX_LEN - 1, fd);
1267 fprintf(stderr,
"couldn't read line %d of file \"%s\"\n", line, filename);
1270 }
while ((str[0] ==
'#') || (str[0] ==
'\n'));
1271 ierr = sscanf(str,
"%d", &h);
1276 vpERROR_TRACE(
"couldn't read line %d of file \"%s\"\n",line, filename) ;
1278 "couldn't read file")) ;
1296 err = fgets(str, vpMAX_LEN - 1, fd);
1300 vpERROR_TRACE(
"couldn't read line %d of file \"%s\"\n",line, filename) ;
1302 "couldn't read file")) ;
1305 ierr = sscanf(str,
"%d", &is255);
1308 vpERROR_TRACE(
"couldn't read line %d of file \"%s\"\n", line, filename) ;
1310 "couldn't read file")) ;
1316 vpERROR_TRACE(
"MAX_VAL is not 255 in file \"%s\"\n", filename) ;
1318 "error reading ppm file")) ;
1321 for(
unsigned int i=0;i<I.
getHeight();i++)
1323 for(
unsigned int j=0;j<I.
getWidth();j++)
1326 size_t res = fread(&v.
R,
sizeof(v.
R),1,fd) ;
1327 res |= fread(&v.
G,
sizeof(v.
G),1,fd) ;
1328 res |= fread(&v.
B,
sizeof(v.
B),1,fd) ;
1332 vpERROR_TRACE(
"couldn't read bytes in file \"%s\"\n", filename) ;
1334 "error reading ppm file")) ;
1388 if (filename ==
'\0') {
1394 f = fopen(filename,
"wb");
1399 "cannot write file")) ;
1406 fprintf(f,
"%d\n",255);
1408 for(
unsigned int i=0;i<I.
getHeight();i++)
1410 for(
unsigned int j=0;j<I.
getWidth();j++)
1417 res = fwrite(&tmp,
sizeof(tmp),1,f) ;
1423 "cannot write file")) ;
1426 res = fwrite(&tmp,
sizeof(tmp),1,f) ;
1432 "cannot write file")) ;
1435 res = fwrite(&tmp,
sizeof(tmp),1,f) ;
1441 "cannot write file")) ;
1504 const std::string filename)
1618 #if defined(VISP_HAVE_LIBJPEG)
1630 struct jpeg_compress_struct cinfo;
1631 struct jpeg_error_mgr jerr;
1634 cinfo.err = jpeg_std_error(&jerr);
1635 jpeg_create_compress(&cinfo);
1638 if (filename ==
'\0') {
1644 file = fopen(filename,
"wb");
1649 "cannot write file")) ;
1655 jpeg_stdio_dest(&cinfo, file);
1657 cinfo.image_width = width;
1658 cinfo.image_height = height;
1659 cinfo.input_components = 1;
1660 cinfo.in_color_space = JCS_GRAYSCALE;
1661 jpeg_set_defaults(&cinfo);
1663 jpeg_start_compress(&cinfo,TRUE);
1665 unsigned char *line;
1666 line =
new unsigned char[width];
1667 unsigned char* input = (
unsigned char*)I.
bitmap;
1668 while (cinfo.next_scanline < cinfo.image_height)
1670 for (
unsigned int i = 0; i < width; i++)
1675 jpeg_write_scanlines(&cinfo, &line, 1);
1678 jpeg_finish_compress(&cinfo);
1679 jpeg_destroy_compress(&cinfo);
1709 struct jpeg_compress_struct cinfo;
1710 struct jpeg_error_mgr jerr;
1713 cinfo.err = jpeg_std_error(&jerr);
1714 jpeg_create_compress(&cinfo);
1717 if (filename ==
'\0') {
1723 file = fopen(filename,
"wb");
1728 "cannot write file")) ;
1734 jpeg_stdio_dest(&cinfo, file);
1736 cinfo.image_width = width;
1737 cinfo.image_height = height;
1738 cinfo.input_components = 3;
1739 cinfo.in_color_space = JCS_RGB;
1740 jpeg_set_defaults(&cinfo);
1742 jpeg_start_compress(&cinfo,TRUE);
1744 unsigned char *line;
1745 line =
new unsigned char[3*width];
1746 unsigned char* input = (
unsigned char*)I.
bitmap;
1747 while (cinfo.next_scanline < cinfo.image_height)
1749 for (
unsigned int i = 0; i < width; i++)
1751 line[i*3] = *(input); input++;
1752 line[i*3+1] = *(input); input++;
1753 line[i*3+2] = *(input); input++;
1756 jpeg_write_scanlines(&cinfo, &line, 1);
1759 jpeg_finish_compress(&cinfo);
1760 jpeg_destroy_compress(&cinfo);
1799 struct jpeg_decompress_struct cinfo;
1800 struct jpeg_error_mgr jerr;
1803 cinfo.err = jpeg_std_error(&jerr);
1804 jpeg_create_decompress(&cinfo);
1807 if (filename ==
'\0') {
1813 file = fopen(filename,
"rb");
1818 "cannot read file")) ;
1821 jpeg_stdio_src(&cinfo, file);
1822 jpeg_read_header(&cinfo, TRUE);
1824 unsigned int width = cinfo.image_width;
1825 unsigned int height = cinfo.image_height;
1830 jpeg_start_decompress(&cinfo);
1832 unsigned int rowbytes = cinfo.output_width * (
unsigned int)(cinfo.output_components);
1833 JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray)
1834 ((j_common_ptr) &cinfo, JPOOL_IMAGE, rowbytes, 1);
1836 if (cinfo.out_color_space == JCS_RGB) {
1838 unsigned char* output = (
unsigned char*)Ic.
bitmap;
1839 while (cinfo.output_scanline<cinfo.output_height) {
1840 jpeg_read_scanlines(&cinfo,buffer,1);
1841 for (
unsigned int i = 0; i < width; i++) {
1842 *(output++) = buffer[0][i*3];
1843 *(output++) = buffer[0][i*3+1];
1844 *(output++) = buffer[0][i*3+2];
1851 else if (cinfo.out_color_space == JCS_GRAYSCALE)
1854 while (cinfo.output_scanline<cinfo.output_height)
1856 row = cinfo.output_scanline;
1857 jpeg_read_scanlines(&cinfo,buffer,1);
1858 memcpy(I[row], buffer[0], rowbytes);
1862 jpeg_finish_decompress(&cinfo);
1863 jpeg_destroy_decompress(&cinfo);
1912 struct jpeg_decompress_struct cinfo;
1913 struct jpeg_error_mgr jerr;
1916 cinfo.err = jpeg_std_error(&jerr);
1917 jpeg_create_decompress(&cinfo);
1920 if (filename ==
'\0') {
1926 file = fopen(filename,
"rb");
1931 "cannot read file")) ;
1934 jpeg_stdio_src(&cinfo, file);
1936 jpeg_read_header(&cinfo, TRUE);
1938 unsigned int width = cinfo.image_width;
1939 unsigned int height = cinfo.image_height;
1944 jpeg_start_decompress(&cinfo);
1946 unsigned int rowbytes = cinfo.output_width * (
unsigned int)(cinfo.output_components);
1947 JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray)
1948 ((j_common_ptr) &cinfo, JPOOL_IMAGE, rowbytes, 1);
1950 if (cinfo.out_color_space == JCS_RGB)
1952 unsigned char* output = (
unsigned char*)I.
bitmap;
1953 while (cinfo.output_scanline<cinfo.output_height)
1955 jpeg_read_scanlines(&cinfo,buffer,1);
1956 for (
unsigned int i = 0; i < width; i++) {
1957 *(output++) = buffer[0][i*3];
1958 *(output++) = buffer[0][i*3+1];
1959 *(output++) = buffer[0][i*3+2];
1965 else if (cinfo.out_color_space == JCS_GRAYSCALE)
1970 while (cinfo.output_scanline<cinfo.output_height)
1972 row = cinfo.output_scanline;
1973 jpeg_read_scanlines(&cinfo,buffer,1);
1974 memcpy(Ig[row], buffer[0], rowbytes);
1980 jpeg_finish_decompress(&cinfo);
1981 jpeg_destroy_decompress(&cinfo);
2010 #elif defined(VISP_HAVE_OPENCV)
2022 IplImage* Ip = NULL;
2025 cvSaveImage(filename, Ip);
2027 cvReleaseImage(&Ip);
2055 IplImage* Ip = NULL;
2058 cvSaveImage(filename, Ip);
2060 cvReleaseImage(&Ip);
2097 IplImage* Ip = NULL;
2098 Ip = cvLoadImage(filename, CV_LOAD_IMAGE_GRAYSCALE);
2103 "Can't read the image")) ;
2104 cvReleaseImage(&Ip);
2152 IplImage* Ip = NULL;
2153 Ip = cvLoadImage(filename, CV_LOAD_IMAGE_COLOR);
2158 "Can't read the image")) ;
2159 cvReleaseImage(&Ip);
2198 #if defined(VISP_HAVE_LIBPNG)
2213 if (filename ==
'\0') {
2219 file = fopen(filename,
"wb");
2224 "cannot write file")) ;
2228 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL, NULL, NULL);
2234 "PNG write error")) ;
2237 png_infop info_ptr = png_create_info_struct(png_ptr);
2241 png_destroy_write_struct (&png_ptr, NULL);
2244 "PNG write error")) ;
2248 if (setjmp (png_jmpbuf (png_ptr)))
2251 png_destroy_write_struct (&png_ptr, &info_ptr);
2254 "PNG write error")) ;
2258 png_init_io (png_ptr, file);
2263 int color_type = PNG_COLOR_TYPE_GRAY;
2266 if (setjmp (png_jmpbuf (png_ptr)))
2269 png_destroy_write_struct (&png_ptr, &info_ptr);
2272 "PNG write error")) ;
2275 png_set_IHDR(png_ptr, info_ptr, width, height,
2276 bit_depth, color_type, PNG_INTERLACE_NONE,
2277 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
2279 png_write_info(png_ptr, info_ptr);
2281 png_bytep* row_ptrs =
new png_bytep[height];
2282 for (
unsigned int i = 0; i < height; i++)
2283 row_ptrs[i] =
new png_byte[width];
2285 unsigned char* input = (
unsigned char*)I.
bitmap;
2287 for (
unsigned int i = 0; i < height; i++)
2289 png_byte* row = row_ptrs[i];
2290 for(
unsigned int j = 0; j < width; j++)
2297 if (setjmp (png_jmpbuf (png_ptr)))
2300 png_destroy_write_struct (&png_ptr, &info_ptr);
2301 for(
unsigned int j = 0; j < height; j++)
2302 delete[] row_ptrs[j];
2307 "PNG write error")) ;
2310 png_write_image(png_ptr, row_ptrs);
2312 if (setjmp (png_jmpbuf (png_ptr)))
2315 png_destroy_write_struct (&png_ptr, &info_ptr);
2316 for(
unsigned int j = 0; j < height; j++)
2317 delete[] row_ptrs[j];
2322 "PNG write error")) ;
2325 png_write_end(png_ptr, NULL);
2327 for(
unsigned int j = 0; j < height; j++)
2328 delete[] row_ptrs[j];
2332 png_destroy_write_struct (&png_ptr, &info_ptr);
2365 if (filename ==
'\0') {
2371 file = fopen(filename,
"wb");
2376 "cannot write file")) ;
2380 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL, NULL, NULL);
2386 "PNG write error")) ;
2389 png_infop info_ptr = png_create_info_struct(png_ptr);
2393 png_destroy_write_struct (&png_ptr, NULL);
2396 "PNG write error")) ;
2400 if (setjmp (png_jmpbuf (png_ptr)))
2403 png_destroy_write_struct (&png_ptr, &info_ptr);
2406 "PNG write error")) ;
2410 png_init_io (png_ptr, file);
2415 int color_type = PNG_COLOR_TYPE_RGB;
2418 if (setjmp (png_jmpbuf (png_ptr)))
2421 png_destroy_write_struct (&png_ptr, &info_ptr);
2424 "PNG write error")) ;
2427 png_set_IHDR(png_ptr, info_ptr, width, height,
2428 bit_depth, color_type, PNG_INTERLACE_NONE,
2429 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
2431 png_write_info(png_ptr, info_ptr);
2433 png_bytep* row_ptrs =
new png_bytep[height];
2434 for (
unsigned int i = 0; i < height; i++)
2435 row_ptrs[i] =
new png_byte[3*width];
2437 unsigned char* input = (
unsigned char*)I.
bitmap;;
2439 for (
unsigned int i = 0; i < height; i++)
2441 png_byte* row = row_ptrs[i];
2442 for(
unsigned int j = 0; j < width; j++)
2444 row[3*j] = *(input);input++;
2445 row[3*j+1] = *(input);input++;
2446 row[3*j+2] = *(input);input++;
2451 if (setjmp (png_jmpbuf (png_ptr)))
2454 png_destroy_write_struct (&png_ptr, &info_ptr);
2455 for(
unsigned int j = 0; j < height; j++)
2456 delete[] row_ptrs[j];
2461 "PNG write error")) ;
2464 png_write_image(png_ptr, row_ptrs);
2466 if (setjmp (png_jmpbuf (png_ptr)))
2469 png_destroy_write_struct (&png_ptr, &info_ptr);
2470 for(
unsigned int j = 0; j < height; j++)
2471 delete[] row_ptrs[j];
2476 "PNG write error")) ;
2479 png_write_end(png_ptr, NULL);
2481 for(
unsigned int j = 0; j < height; j++)
2482 delete[] row_ptrs[j];
2486 png_destroy_write_struct (&png_ptr, &info_ptr);
2527 if (filename ==
'\0') {
2533 file = fopen(filename,
"rb");
2538 "cannot read file")) ;
2542 if (fread (magic, 1,
sizeof (magic), file) !=
sizeof (magic))
2545 vpERROR_TRACE(
"couldn't read magic number in file \"%s\"\n", filename) ;
2547 "error reading png file")) ;
2551 if (png_sig_cmp (magic,0,
sizeof (magic)))
2553 fprintf (stderr,
"error: \"%s\" is not a valid PNG image!\n",filename);
2556 "error reading png file")) ;
2561 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
2562 if (png_ptr == NULL)
2564 fprintf (stderr,
"error: can't create a png read structure!\n");
2567 "error reading png file")) ;
2571 png_infop info_ptr = png_create_info_struct (png_ptr);
2572 if (info_ptr == NULL)
2574 fprintf (stderr,
"error: can't create a png info structure!\n");
2576 png_destroy_read_struct (&png_ptr, NULL, NULL);
2578 "error reading png file")) ;
2582 if (setjmp (png_jmpbuf (png_ptr)))
2585 png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
2588 "PNG read error")) ;
2592 png_init_io (png_ptr, file);
2595 png_set_sig_bytes (png_ptr,
sizeof (magic));
2598 png_read_info (png_ptr, info_ptr);
2600 unsigned int width = png_get_image_width(png_ptr, info_ptr);
2601 unsigned int height = png_get_image_height(png_ptr, info_ptr);
2603 unsigned int bit_depth, channels, color_type;
2605 bit_depth = png_get_bit_depth (png_ptr, info_ptr);
2606 channels = png_get_channels(png_ptr, info_ptr);
2607 color_type = png_get_color_type (png_ptr, info_ptr);
2610 if (color_type == PNG_COLOR_TYPE_PALETTE)
2611 png_set_palette_to_rgb (png_ptr);
2614 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
2615 png_set_expand (png_ptr);
2620 if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2621 png_set_strip_alpha(png_ptr);
2623 if (bit_depth == 16)
2624 png_set_strip_16 (png_ptr);
2625 else if (bit_depth < 8)
2626 png_set_packing (png_ptr);
2629 png_read_update_info (png_ptr, info_ptr);
2631 channels = png_get_channels(png_ptr, info_ptr);
2636 png_bytep* rowPtrs =
new png_bytep[height];
2638 unsigned int stride = width * bit_depth * channels / 8;
2639 unsigned char* data =
new unsigned char[stride * height];
2641 for (
unsigned int i =0; i < height; i++)
2642 rowPtrs[i] = (png_bytep)data + (i * stride);
2644 png_read_image(png_ptr, rowPtrs);
2647 unsigned char* output;
2652 output = (
unsigned char*)I.
bitmap;
2653 for (
unsigned int i = 0; i < width*height; i++)
2655 *(output++) = data[i];
2659 output = (
unsigned char*)I.
bitmap;
2660 for (
unsigned int i = 0; i < width*height; i++)
2662 *(output++) = data[i*2];
2667 output = (
unsigned char*)Ic.
bitmap;
2668 for (
unsigned int i = 0; i < width*height; i++)
2670 *(output++) = data[i*3];
2671 *(output++) = data[i*3+1];
2672 *(output++) = data[i*3+2];
2678 output = (
unsigned char*)Ic.
bitmap;
2679 for (
unsigned int i = 0; i < width*height; i++)
2681 *(output++) = data[i*4];
2682 *(output++) = data[i*4+1];
2683 *(output++) = data[i*4+2];
2684 *(output++) = data[i*4+3];
2690 delete [] (png_bytep)rowPtrs;
2692 png_read_end (png_ptr, NULL);
2693 png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
2746 if (filename ==
'\0') {
2752 file = fopen(filename,
"rb");
2757 "cannot read file")) ;
2761 if (fread (magic, 1,
sizeof (magic), file) !=
sizeof (magic))
2764 vpERROR_TRACE(
"couldn't read magic number in file \"%s\"\n", filename) ;
2766 "error reading pgm file")) ;
2770 if (png_sig_cmp (magic,0,
sizeof (magic)))
2773 vpERROR_TRACE(
"error: \"%s\" is not a valid PNG image!\n",filename);
2775 "PNG read error")) ;
2779 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
2785 "PNG read error")) ;
2789 png_infop info_ptr = png_create_info_struct (png_ptr);
2793 png_destroy_read_struct (&png_ptr, NULL, NULL);
2796 "PNG read error")) ;
2800 if (setjmp (png_jmpbuf (png_ptr)))
2803 png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
2806 "PNG read error")) ;
2810 png_init_io (png_ptr, file);
2813 png_set_sig_bytes (png_ptr,
sizeof (magic));
2816 png_read_info (png_ptr, info_ptr);
2818 unsigned int width = png_get_image_width(png_ptr, info_ptr);
2819 unsigned int height = png_get_image_height(png_ptr, info_ptr);
2821 unsigned int bit_depth, channels, color_type;
2823 bit_depth = png_get_bit_depth (png_ptr, info_ptr);
2824 channels = png_get_channels(png_ptr, info_ptr);
2825 color_type = png_get_color_type (png_ptr, info_ptr);
2828 if (color_type == PNG_COLOR_TYPE_PALETTE)
2829 png_set_palette_to_rgb (png_ptr);
2832 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
2833 png_set_expand (png_ptr);
2838 if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2839 png_set_strip_alpha(png_ptr);
2841 if (bit_depth == 16)
2842 png_set_strip_16 (png_ptr);
2843 else if (bit_depth < 8)
2844 png_set_packing (png_ptr);
2847 png_read_update_info (png_ptr, info_ptr);
2849 channels = png_get_channels(png_ptr, info_ptr);
2854 png_bytep* rowPtrs =
new png_bytep[height];
2856 unsigned int stride = width * bit_depth * channels / 8;
2857 unsigned char* data =
new unsigned char[stride * height];
2860 for (
unsigned int i =0; i < height; i++)
2861 rowPtrs[i] = (png_bytep)data + (i * stride);
2863 png_read_image(png_ptr, rowPtrs);
2866 unsigned char* output;
2871 output = (
unsigned char*)Ig.
bitmap;
2872 for (
unsigned int i = 0; i < width*height; i++)
2874 *(output++) = data[i];
2879 output = (
unsigned char*)Ig.
bitmap;
2880 for (
unsigned int i = 0; i < width*height; i++)
2882 *(output++) = data[i*2];
2888 output = (
unsigned char*)I.
bitmap;
2889 for (
unsigned int i = 0; i < width*height; i++)
2891 *(output++) = data[i*3];
2892 *(output++) = data[i*3+1];
2893 *(output++) = data[i*3+2];
2898 output = (
unsigned char*)I.
bitmap;
2899 for (
unsigned int i = 0; i < width*height; i++)
2901 *(output++) = data[i*4];
2902 *(output++) = data[i*4+1];
2903 *(output++) = data[i*4+2];
2904 *(output++) = data[i*4+3];
2909 delete [] (png_bytep)rowPtrs;
2911 png_read_end (png_ptr, NULL);
2912 png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
2941 #elif defined(VISP_HAVE_OPENCV)
2953 IplImage* Ip = NULL;
2956 cvSaveImage(filename, Ip);
2958 cvReleaseImage(&Ip);
2986 IplImage* Ip = NULL;
2989 cvSaveImage(filename, Ip);
2991 cvReleaseImage(&Ip);
3028 IplImage* Ip = NULL;
3029 Ip = cvLoadImage(filename, CV_LOAD_IMAGE_GRAYSCALE);
3034 "Can't read the image")) ;
3035 cvReleaseImage(&Ip);
3083 IplImage* Ip = NULL;
3084 Ip = cvLoadImage(filename, CV_LOAD_IMAGE_COLOR);
3089 "Can't read the image")) ;
3090 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
void resize(const unsigned int height, const unsigned int width)
set the size of the image
static void writePGM(const vpImage< unsigned char > &I, const char *filename)
static void writePNG(const vpImage< unsigned char > &I, const char *filename)
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.
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)