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 return FORMAT_UNKNOWN;
218 std::string vpImageIo::getExtension(
const std::string &filename)
221 size_t dot = filename.find_last_of(
".");
222 std::string ext = filename.substr(dot, filename.size()-1);
241 switch(getFormat(filename)){
247 #if (defined(VISP_HAVE_LIBJPEG) || defined(VISP_HAVE_OPENCV))
250 vpCERROR <<
"You need the libjpeg library to open JPEG files "
255 #if (defined(VISP_HAVE_LIBPNG) || defined(VISP_HAVE_OPENCV))
258 vpCERROR <<
"You need the libpng library to open PNG files "
262 case FORMAT_UNKNOWN :
263 vpCERROR <<
"Error: Only PNM (PGM P5 and PPM P6), JPEG and PNG " << std::endl
264 <<
" image format are implemented..." << std::endl;
266 "cannot read file")) ;
284 read(I,filename.c_str());
300 switch(getFormat(filename)){
306 #if (defined(VISP_HAVE_LIBJPEG) || defined(VISP_HAVE_OPENCV))
309 vpCERROR <<
"You need the libjpeg library to open JPEG files "
314 #if (defined(VISP_HAVE_LIBPNG) || defined(VISP_HAVE_OPENCV))
317 vpCERROR <<
"You need the libpng library to open PNG files "
321 case FORMAT_UNKNOWN :
322 vpCERROR <<
"Error: Only PNM (PGM P5 and PPM P6), JPEG and PNG " << std::endl
323 <<
" image format are implemented..." << std::endl;
325 "cannot read file")) ;
343 read(I,filename.c_str());
357 switch(getFormat(filename)){
363 #if (defined(VISP_HAVE_LIBJPEG) || defined(VISP_HAVE_OPENCV))
366 vpCERROR <<
"You need the libjpeg library to write JPEG files "
371 #if (defined(VISP_HAVE_LIBPNG) || defined(VISP_HAVE_OPENCV))
374 vpCERROR <<
"You need the libpng library to write PNG files "
378 case FORMAT_UNKNOWN :
379 vpCERROR <<
"Error: Only PNM (PGM P5 and PPM P6) JPEG and PNG " << std::endl
380 <<
" image format are implemented..." << std::endl;
382 "cannot write file")) ;
397 write(I,filename.c_str());
410 switch(getFormat(filename)){
416 #if (defined(VISP_HAVE_LIBJPEG) || defined(VISP_HAVE_OPENCV))
419 vpCERROR <<
"You need the libjpeg library to write JPEG files "
424 #if (defined(VISP_HAVE_LIBPNG) || defined(VISP_HAVE_OPENCV))
427 vpCERROR <<
"You need the libpng library to write PNG files "
431 case FORMAT_UNKNOWN :
432 vpCERROR <<
"Error: Only PNM (PGM P5 and PPM P6), JPEG and PNG " << std::endl
433 <<
" image format are implemented..." << std::endl;
435 "cannot write file")) ;
450 write(I,filename.c_str());
467 const char *filename)
473 if (filename ==
'\0') {
479 fd = fopen(filename,
"wb");
484 "cannot write file")) ;
490 fprintf(fd,
"255\n");
496 ierr = fwrite(I.
bitmap,
sizeof(
float), nbyte, fd) ;
502 "cannot write file")) ;
523 const char *filename)
529 if (filename ==
'\0') {
535 fd = fopen(filename,
"wb");
540 "cannot write file")) ;
546 fprintf(fd,
"255\n");
552 ierr = fwrite(I.
bitmap,
sizeof(
unsigned char), nbyte, fd) ;
558 "cannot write file")) ;
581 for (
unsigned int i=0 ; i < nrows * ncols ; i++)
604 if (filename ==
'\0') {
610 fd = fopen(filename,
"wb");
615 "cannot write file")) ;
621 fprintf(fd,
"255\n");
631 ierr = fwrite(Itmp.
bitmap,
sizeof(
unsigned char), nbyte, fd) ;
637 "cannot write file")) ;
673 if (filename ==
'\0')
682 fd = fopen(filename,
"rb");
687 "couldn't read file")) ;
693 err = fgets(str, vpMAX_LEN - 1, fd);
698 vpERROR_TRACE(
"couldn't read line %d of file \"%s\"\n", line, filename) ;
700 "couldn't read file")) ;
708 "this is not a pfm file")) ;
712 if (strcmp(str,
"P8") != 0)
717 "this is not a pgm file")) ;
722 err = fgets(str, vpMAX_LEN - 1, fd);
725 fprintf(stderr,
"couldn't read line %d of file \"%s\"\n", line, filename);
728 }
while ((str[0] ==
'#') || (str[0] ==
'\n'));
731 ierr = sscanf(str,
"%d %d", &w, &h);
734 err = fgets(str, vpMAX_LEN - 1, fd);
737 fprintf(stderr,
"couldn't read line %d of file \"%s\"\n", line, filename);
740 }
while ((str[0] ==
'#') || (str[0] ==
'\n'));
741 ierr = sscanf(str,
"%d", &h);
746 vpERROR_TRACE(
"couldn't read line %d of file \"%s\"\n",line, filename) ;
748 "couldn't read file")) ;
766 err = fgets(str, vpMAX_LEN - 1, fd);
770 vpERROR_TRACE(
"couldn't read line %d of file \"%s\"\n",line, filename) ;
772 "couldn't read file")) ;
775 ierr = sscanf(str,
"%d", &is255);
778 vpERROR_TRACE(
"couldn't read line %d of file \"%s\"\n", line, filename) ;
780 "couldn't read file")) ;
786 vpERROR_TRACE(
"MAX_VAL is not 255 in file \"%s\"\n", filename) ;
788 "error reading pfm file")) ;
792 if (fread (I.
bitmap,
sizeof(
float), nbyte, fd ) != nbyte)
795 vpERROR_TRACE(
"couldn't read %d bytes in file \"%s\"\n", nbyte, filename) ;
797 "error reading pfm file")) ;
835 if (filename ==
'\0')
844 fd = fopen(filename,
"rb");
849 "couldn't read file")) ;
855 err = fgets(str, vpMAX_LEN - 1, fd);
860 vpERROR_TRACE(
"couldn't read line %d of file \"%s\"\n", line, filename) ;
862 "couldn't read file")) ;
870 "this is not a pgm file")) ;
874 if (strcmp(str,
"P5") != 0)
879 "this is not a pgm file")) ;
884 err = fgets(str, vpMAX_LEN - 1, fd);
887 fprintf(stderr,
"couldn't read line %d of file \"%s\"\n", line, filename);
890 }
while ((str[0] ==
'#') || (str[0] ==
'\n'));
893 ierr = sscanf(str,
"%d %d", &w, &h);
896 err = fgets(str, vpMAX_LEN - 1, fd);
899 fprintf(stderr,
"couldn't read line %d of file \"%s\"\n", line, filename);
902 }
while ((str[0] ==
'#') || (str[0] ==
'\n'));
903 ierr = sscanf(str,
"%d", &h);
908 vpERROR_TRACE(
"couldn't read line %d of file \"%s\"\n",line, filename) ;
910 "couldn't read file")) ;
928 err = fgets(str, vpMAX_LEN - 1, fd);
932 vpERROR_TRACE(
"couldn't read line %d of file \"%s\"\n",line, filename) ;
934 "couldn't read file")) ;
937 ierr = sscanf(str,
"%d", &is255);
940 vpERROR_TRACE(
"couldn't read line %d of file \"%s\"\n", line, filename) ;
942 "couldn't read file")) ;
948 vpERROR_TRACE(
"MAX_VAL is not 255 in file \"%s\"\n", filename) ;
950 "error reading pgm file")) ;
954 if (fread (I.
bitmap,
sizeof(
unsigned char), nbyte, fd ) != nbyte)
957 vpERROR_TRACE(
"couldn't read %d bytes in file \"%s\"\n", nbyte, filename) ;
959 "error reading pgm file")) ;
1068 char str[vpMAX_LEN];
1072 if (filename ==
'\0')
1081 fd = fopen(filename,
"rb");
1086 "couldn't read file")) ;
1092 err = fgets(str, vpMAX_LEN - 1, fd);
1097 vpERROR_TRACE(
"couldn't read line %d of file \"%s\"\n", line, filename) ;
1099 "couldn't read file")) ;
1102 if (strlen(str) < 3)
1107 "this is not a ppm file")) ;
1111 if (strcmp(str,
"P6") != 0)
1116 "this is not a ppm file")) ;
1121 err = fgets(str, vpMAX_LEN - 1, fd);
1124 fprintf(stderr,
"couldn't read line %d of file \"%s\"\n", line, filename);
1127 }
while ((str[0] ==
'#') || (str[0] ==
'\n'));
1130 ierr = sscanf(str,
"%d %d", &w, &h);
1133 err = fgets(str, vpMAX_LEN - 1, fd);
1136 fprintf(stderr,
"couldn't read line %d of file \"%s\"\n", line, filename);
1139 }
while ((str[0] ==
'#') || (str[0] ==
'\n'));
1140 ierr = sscanf(str,
"%d", &h);
1145 vpERROR_TRACE(
"couldn't read line %d of file \"%s\"\n",line, filename) ;
1147 "couldn't read file")) ;
1165 err = fgets(str, vpMAX_LEN - 1, fd);
1169 vpERROR_TRACE(
"couldn't read line %d of file \"%s\"\n",line, filename) ;
1171 "couldn't read file")) ;
1174 ierr = sscanf(str,
"%d", &is255);
1177 vpERROR_TRACE(
"couldn't read line %d of file \"%s\"\n", line, filename) ;
1179 "couldn't read file")) ;
1185 vpERROR_TRACE(
"MAX_VAL is not 255 in file \"%s\"\n", filename) ;
1187 "error reading ppm file")) ;
1190 for(
unsigned int i=0;i<I.
getHeight();i++)
1192 for(
unsigned int j=0;j<I.
getWidth();j++)
1195 size_t res = fread(&v.
R,
sizeof(v.
R),1,fd) ;
1196 res |= fread(&v.
G,
sizeof(v.
G),1,fd) ;
1197 res |= fread(&v.
B,
sizeof(v.
B),1,fd) ;
1201 vpERROR_TRACE(
"couldn't read bytes in file \"%s\"\n", filename) ;
1203 "error reading ppm file")) ;
1257 if (filename ==
'\0') {
1263 f = fopen(filename,
"wb");
1268 "cannot write file")) ;
1275 fprintf(f,
"%d\n",255);
1277 for(
unsigned int i=0;i<I.
getHeight();i++)
1279 for(
unsigned int j=0;j<I.
getWidth();j++)
1286 res = fwrite(&tmp,
sizeof(tmp),1,f) ;
1292 "cannot write file")) ;
1295 res = fwrite(&tmp,
sizeof(tmp),1,f) ;
1301 "cannot write file")) ;
1304 res = fwrite(&tmp,
sizeof(tmp),1,f) ;
1310 "cannot write file")) ;
1373 const std::string filename)
1487 #if defined(VISP_HAVE_LIBJPEG)
1499 struct jpeg_compress_struct cinfo;
1500 struct jpeg_error_mgr jerr;
1503 cinfo.err = jpeg_std_error(&jerr);
1504 jpeg_create_compress(&cinfo);
1507 if (filename ==
'\0') {
1513 file = fopen(filename,
"wb");
1518 "cannot write file")) ;
1524 jpeg_stdio_dest(&cinfo, file);
1526 cinfo.image_width = width;
1527 cinfo.image_height = height;
1528 cinfo.input_components = 1;
1529 cinfo.in_color_space = JCS_GRAYSCALE;
1530 jpeg_set_defaults(&cinfo);
1532 jpeg_start_compress(&cinfo,TRUE);
1534 unsigned char *line;
1535 line =
new unsigned char[width];
1536 unsigned char* input = (
unsigned char*)I.
bitmap;
1537 while (cinfo.next_scanline < cinfo.image_height)
1539 for (
unsigned int i = 0; i < width; i++)
1544 jpeg_write_scanlines(&cinfo, &line, 1);
1547 jpeg_finish_compress(&cinfo);
1548 jpeg_destroy_compress(&cinfo);
1578 struct jpeg_compress_struct cinfo;
1579 struct jpeg_error_mgr jerr;
1582 cinfo.err = jpeg_std_error(&jerr);
1583 jpeg_create_compress(&cinfo);
1586 if (filename ==
'\0') {
1592 file = fopen(filename,
"wb");
1597 "cannot write file")) ;
1603 jpeg_stdio_dest(&cinfo, file);
1605 cinfo.image_width = width;
1606 cinfo.image_height = height;
1607 cinfo.input_components = 3;
1608 cinfo.in_color_space = JCS_RGB;
1609 jpeg_set_defaults(&cinfo);
1611 jpeg_start_compress(&cinfo,TRUE);
1613 unsigned char *line;
1614 line =
new unsigned char[3*width];
1615 unsigned char* input = (
unsigned char*)I.
bitmap;
1616 while (cinfo.next_scanline < cinfo.image_height)
1618 for (
unsigned int i = 0; i < width; i++)
1620 line[i*3] = *(input); input++;
1621 line[i*3+1] = *(input); input++;
1622 line[i*3+2] = *(input); input++;
1625 jpeg_write_scanlines(&cinfo, &line, 1);
1628 jpeg_finish_compress(&cinfo);
1629 jpeg_destroy_compress(&cinfo);
1668 struct jpeg_decompress_struct cinfo;
1669 struct jpeg_error_mgr jerr;
1672 cinfo.err = jpeg_std_error(&jerr);
1673 jpeg_create_decompress(&cinfo);
1676 if (filename ==
'\0') {
1682 file = fopen(filename,
"rb");
1687 "cannot read file")) ;
1690 jpeg_stdio_src(&cinfo, file);
1691 jpeg_read_header(&cinfo, TRUE);
1693 unsigned int width = cinfo.image_width;
1694 unsigned int height = cinfo.image_height;
1699 jpeg_start_decompress(&cinfo);
1701 unsigned int rowbytes = cinfo.output_width * (
unsigned int)(cinfo.output_components);
1702 JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray)
1703 ((j_common_ptr) &cinfo, JPOOL_IMAGE, rowbytes, 1);
1705 if (cinfo.out_color_space == JCS_RGB) {
1707 unsigned char* output = (
unsigned char*)Ic.
bitmap;
1708 while (cinfo.output_scanline<cinfo.output_height) {
1709 jpeg_read_scanlines(&cinfo,buffer,1);
1710 for (
unsigned int i = 0; i < width; i++) {
1711 *(output++) = buffer[0][i*3];
1712 *(output++) = buffer[0][i*3+1];
1713 *(output++) = buffer[0][i*3+2];
1720 else if (cinfo.out_color_space == JCS_GRAYSCALE)
1723 while (cinfo.output_scanline<cinfo.output_height)
1725 row = cinfo.output_scanline;
1726 jpeg_read_scanlines(&cinfo,buffer,1);
1727 memcpy(I[row], buffer[0], rowbytes);
1731 jpeg_finish_decompress(&cinfo);
1732 jpeg_destroy_decompress(&cinfo);
1781 struct jpeg_decompress_struct cinfo;
1782 struct jpeg_error_mgr jerr;
1785 cinfo.err = jpeg_std_error(&jerr);
1786 jpeg_create_decompress(&cinfo);
1789 if (filename ==
'\0') {
1795 file = fopen(filename,
"rb");
1800 "cannot read file")) ;
1803 jpeg_stdio_src(&cinfo, file);
1805 jpeg_read_header(&cinfo, TRUE);
1807 unsigned int width = cinfo.image_width;
1808 unsigned int height = cinfo.image_height;
1813 jpeg_start_decompress(&cinfo);
1815 unsigned int rowbytes = cinfo.output_width * (
unsigned int)(cinfo.output_components);
1816 JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray)
1817 ((j_common_ptr) &cinfo, JPOOL_IMAGE, rowbytes, 1);
1819 if (cinfo.out_color_space == JCS_RGB)
1821 unsigned char* output = (
unsigned char*)I.
bitmap;
1822 while (cinfo.output_scanline<cinfo.output_height)
1824 jpeg_read_scanlines(&cinfo,buffer,1);
1825 for (
unsigned int i = 0; i < width; i++) {
1826 *(output++) = buffer[0][i*3];
1827 *(output++) = buffer[0][i*3+1];
1828 *(output++) = buffer[0][i*3+2];
1834 else if (cinfo.out_color_space == JCS_GRAYSCALE)
1839 while (cinfo.output_scanline<cinfo.output_height)
1841 row = cinfo.output_scanline;
1842 jpeg_read_scanlines(&cinfo,buffer,1);
1843 memcpy(Ig[row], buffer[0], rowbytes);
1849 jpeg_finish_decompress(&cinfo);
1850 jpeg_destroy_decompress(&cinfo);
1879 #elif defined(VISP_HAVE_OPENCV)
1891 IplImage* Ip = NULL;
1894 cvSaveImage(filename, Ip);
1896 cvReleaseImage(&Ip);
1924 IplImage* Ip = NULL;
1927 cvSaveImage(filename, Ip);
1929 cvReleaseImage(&Ip);
1966 IplImage* Ip = NULL;
1967 Ip = cvLoadImage(filename, CV_LOAD_IMAGE_GRAYSCALE);
1972 "Can't read the image")) ;
1973 cvReleaseImage(&Ip);
2021 IplImage* Ip = NULL;
2022 Ip = cvLoadImage(filename, CV_LOAD_IMAGE_COLOR);
2027 "Can't read the image")) ;
2028 cvReleaseImage(&Ip);
2067 #if defined(VISP_HAVE_LIBPNG)
2082 if (filename ==
'\0') {
2088 file = fopen(filename,
"wb");
2093 "cannot write file")) ;
2097 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL, NULL, NULL);
2103 "PNG write error")) ;
2106 png_infop info_ptr = png_create_info_struct(png_ptr);
2110 png_destroy_write_struct (&png_ptr, NULL);
2113 "PNG write error")) ;
2117 if (setjmp (png_jmpbuf (png_ptr)))
2120 png_destroy_write_struct (&png_ptr, &info_ptr);
2123 "PNG write error")) ;
2127 png_init_io (png_ptr, file);
2132 int color_type = PNG_COLOR_TYPE_GRAY;
2135 if (setjmp (png_jmpbuf (png_ptr)))
2138 png_destroy_write_struct (&png_ptr, &info_ptr);
2141 "PNG write error")) ;
2144 png_set_IHDR(png_ptr, info_ptr, width, height,
2145 bit_depth, color_type, PNG_INTERLACE_NONE,
2146 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
2148 png_write_info(png_ptr, info_ptr);
2150 png_bytep* row_ptrs =
new png_bytep[height];
2151 for (
unsigned int i = 0; i < height; i++)
2152 row_ptrs[i] =
new png_byte[width];
2154 unsigned char* input = (
unsigned char*)I.
bitmap;
2156 for (
unsigned int i = 0; i < height; i++)
2158 png_byte* row = row_ptrs[i];
2159 for(
unsigned int j = 0; j < width; j++)
2166 if (setjmp (png_jmpbuf (png_ptr)))
2169 png_destroy_write_struct (&png_ptr, &info_ptr);
2170 for(
unsigned int j = 0; j < height; j++)
2171 delete[] row_ptrs[j];
2176 "PNG write error")) ;
2179 png_write_image(png_ptr, row_ptrs);
2181 if (setjmp (png_jmpbuf (png_ptr)))
2184 png_destroy_write_struct (&png_ptr, &info_ptr);
2185 for(
unsigned int j = 0; j < height; j++)
2186 delete[] row_ptrs[j];
2191 "PNG write error")) ;
2194 png_write_end(png_ptr, NULL);
2196 for(
unsigned int j = 0; j < height; j++)
2197 delete[] row_ptrs[j];
2201 png_destroy_write_struct (&png_ptr, &info_ptr);
2234 if (filename ==
'\0') {
2240 file = fopen(filename,
"wb");
2245 "cannot write file")) ;
2249 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL, NULL, NULL);
2255 "PNG write error")) ;
2258 png_infop info_ptr = png_create_info_struct(png_ptr);
2262 png_destroy_write_struct (&png_ptr, NULL);
2265 "PNG write error")) ;
2269 if (setjmp (png_jmpbuf (png_ptr)))
2272 png_destroy_write_struct (&png_ptr, &info_ptr);
2275 "PNG write error")) ;
2279 png_init_io (png_ptr, file);
2284 int color_type = PNG_COLOR_TYPE_RGB;
2287 if (setjmp (png_jmpbuf (png_ptr)))
2290 png_destroy_write_struct (&png_ptr, &info_ptr);
2293 "PNG write error")) ;
2296 png_set_IHDR(png_ptr, info_ptr, width, height,
2297 bit_depth, color_type, PNG_INTERLACE_NONE,
2298 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
2300 png_write_info(png_ptr, info_ptr);
2302 png_bytep* row_ptrs =
new png_bytep[height];
2303 for (
unsigned int i = 0; i < height; i++)
2304 row_ptrs[i] =
new png_byte[3*width];
2306 unsigned char* input = (
unsigned char*)I.
bitmap;;
2308 for (
unsigned int i = 0; i < height; i++)
2310 png_byte* row = row_ptrs[i];
2311 for(
unsigned int j = 0; j < width; j++)
2313 row[3*j] = *(input);input++;
2314 row[3*j+1] = *(input);input++;
2315 row[3*j+2] = *(input);input++;
2320 if (setjmp (png_jmpbuf (png_ptr)))
2323 png_destroy_write_struct (&png_ptr, &info_ptr);
2324 for(
unsigned int j = 0; j < height; j++)
2325 delete[] row_ptrs[j];
2330 "PNG write error")) ;
2333 png_write_image(png_ptr, row_ptrs);
2335 if (setjmp (png_jmpbuf (png_ptr)))
2338 png_destroy_write_struct (&png_ptr, &info_ptr);
2339 for(
unsigned int j = 0; j < height; j++)
2340 delete[] row_ptrs[j];
2345 "PNG write error")) ;
2348 png_write_end(png_ptr, NULL);
2350 for(
unsigned int j = 0; j < height; j++)
2351 delete[] row_ptrs[j];
2355 png_destroy_write_struct (&png_ptr, &info_ptr);
2397 if (filename ==
'\0') {
2403 file = fopen(filename,
"rb");
2408 "cannot read file")) ;
2412 if (fread (magic, 1,
sizeof (magic), file) !=
sizeof (magic))
2415 vpERROR_TRACE(
"couldn't read magic number in file \"%s\"\n", filename) ;
2417 "error reading png file")) ;
2421 if (png_sig_cmp (magic,0,
sizeof (magic)))
2423 fprintf (stderr,
"error: \"%s\" is not a valid PNG image!\n",filename);
2426 "error reading png file")) ;
2431 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
2432 if (png_ptr == NULL)
2434 fprintf (stderr,
"error: can't create a png read structure!\n");
2437 "error reading png file")) ;
2441 png_infop info_ptr = png_create_info_struct (png_ptr);
2442 if (info_ptr == NULL)
2444 fprintf (stderr,
"error: can't create a png info structure!\n");
2446 png_destroy_read_struct (&png_ptr, NULL, NULL);
2448 "error reading png file")) ;
2452 if (setjmp (png_jmpbuf (png_ptr)))
2455 png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
2458 "PNG read error")) ;
2462 png_init_io (png_ptr, file);
2465 png_set_sig_bytes (png_ptr,
sizeof (magic));
2468 png_read_info (png_ptr, info_ptr);
2470 unsigned int width = png_get_image_width(png_ptr, info_ptr);
2471 unsigned int height = png_get_image_height(png_ptr, info_ptr);
2473 unsigned int bit_depth, channels, color_type;
2475 bit_depth = png_get_bit_depth (png_ptr, info_ptr);
2476 channels = png_get_channels(png_ptr, info_ptr);
2477 color_type = png_get_color_type (png_ptr, info_ptr);
2480 if (color_type == PNG_COLOR_TYPE_PALETTE)
2481 png_set_palette_to_rgb (png_ptr);
2484 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
2485 png_set_expand (png_ptr);
2490 if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2491 png_set_strip_alpha(png_ptr);
2493 if (bit_depth == 16)
2494 png_set_strip_16 (png_ptr);
2495 else if (bit_depth < 8)
2496 png_set_packing (png_ptr);
2499 png_read_update_info (png_ptr, info_ptr);
2501 channels = png_get_channels(png_ptr, info_ptr);
2506 png_bytep* rowPtrs =
new png_bytep[height];
2508 unsigned int stride = width * bit_depth * channels / 8;
2509 unsigned char* data =
new unsigned char[stride * height];
2511 for (
unsigned int i =0; i < height; i++)
2512 rowPtrs[i] = (png_bytep)data + (i * stride);
2514 png_read_image(png_ptr, rowPtrs);
2517 unsigned char* output;
2522 output = (
unsigned char*)I.
bitmap;
2523 for (
unsigned int i = 0; i < width*height; i++)
2525 *(output++) = data[i];
2529 output = (
unsigned char*)I.
bitmap;
2530 for (
unsigned int i = 0; i < width*height; i++)
2532 *(output++) = data[i*2];
2537 output = (
unsigned char*)Ic.
bitmap;
2538 for (
unsigned int i = 0; i < width*height; i++)
2540 *(output++) = data[i*3];
2541 *(output++) = data[i*3+1];
2542 *(output++) = data[i*3+2];
2548 output = (
unsigned char*)Ic.
bitmap;
2549 for (
unsigned int i = 0; i < width*height; i++)
2551 *(output++) = data[i*4];
2552 *(output++) = data[i*4+1];
2553 *(output++) = data[i*4+2];
2554 *(output++) = data[i*4+3];
2560 delete [] (png_bytep)rowPtrs;
2562 png_read_end (png_ptr, NULL);
2563 png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
2616 if (filename ==
'\0') {
2622 file = fopen(filename,
"rb");
2627 "cannot read file")) ;
2631 if (fread (magic, 1,
sizeof (magic), file) !=
sizeof (magic))
2634 vpERROR_TRACE(
"couldn't read magic number in file \"%s\"\n", filename) ;
2636 "error reading pgm file")) ;
2640 if (png_sig_cmp (magic,0,
sizeof (magic)))
2643 vpERROR_TRACE(
"error: \"%s\" is not a valid PNG image!\n",filename);
2645 "PNG read error")) ;
2649 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
2655 "PNG read error")) ;
2659 png_infop info_ptr = png_create_info_struct (png_ptr);
2663 png_destroy_read_struct (&png_ptr, NULL, NULL);
2666 "PNG read error")) ;
2670 if (setjmp (png_jmpbuf (png_ptr)))
2673 png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
2676 "PNG read error")) ;
2680 png_init_io (png_ptr, file);
2683 png_set_sig_bytes (png_ptr,
sizeof (magic));
2686 png_read_info (png_ptr, info_ptr);
2688 unsigned int width = png_get_image_width(png_ptr, info_ptr);
2689 unsigned int height = png_get_image_height(png_ptr, info_ptr);
2691 unsigned int bit_depth, channels, color_type;
2693 bit_depth = png_get_bit_depth (png_ptr, info_ptr);
2694 channels = png_get_channels(png_ptr, info_ptr);
2695 color_type = png_get_color_type (png_ptr, info_ptr);
2698 if (color_type == PNG_COLOR_TYPE_PALETTE)
2699 png_set_palette_to_rgb (png_ptr);
2702 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
2703 png_set_expand (png_ptr);
2708 if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2709 png_set_strip_alpha(png_ptr);
2711 if (bit_depth == 16)
2712 png_set_strip_16 (png_ptr);
2713 else if (bit_depth < 8)
2714 png_set_packing (png_ptr);
2717 png_read_update_info (png_ptr, info_ptr);
2719 channels = png_get_channels(png_ptr, info_ptr);
2724 png_bytep* rowPtrs =
new png_bytep[height];
2726 unsigned int stride = width * bit_depth * channels / 8;
2727 unsigned char* data =
new unsigned char[stride * height];
2730 for (
unsigned int i =0; i < height; i++)
2731 rowPtrs[i] = (png_bytep)data + (i * stride);
2733 png_read_image(png_ptr, rowPtrs);
2736 unsigned char* output;
2741 output = (
unsigned char*)Ig.
bitmap;
2742 for (
unsigned int i = 0; i < width*height; i++)
2744 *(output++) = data[i];
2749 output = (
unsigned char*)Ig.
bitmap;
2750 for (
unsigned int i = 0; i < width*height; i++)
2752 *(output++) = data[i*2];
2758 output = (
unsigned char*)I.
bitmap;
2759 for (
unsigned int i = 0; i < width*height; i++)
2761 *(output++) = data[i*3];
2762 *(output++) = data[i*3+1];
2763 *(output++) = data[i*3+2];
2768 output = (
unsigned char*)I.
bitmap;
2769 for (
unsigned int i = 0; i < width*height; i++)
2771 *(output++) = data[i*4];
2772 *(output++) = data[i*4+1];
2773 *(output++) = data[i*4+2];
2774 *(output++) = data[i*4+3];
2779 delete [] (png_bytep)rowPtrs;
2781 png_read_end (png_ptr, NULL);
2782 png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
2811 #elif defined(VISP_HAVE_OPENCV)
2823 IplImage* Ip = NULL;
2826 cvSaveImage(filename, Ip);
2828 cvReleaseImage(&Ip);
2856 IplImage* Ip = NULL;
2859 cvSaveImage(filename, Ip);
2861 cvReleaseImage(&Ip);
2898 IplImage* Ip = NULL;
2899 Ip = cvLoadImage(filename, CV_LOAD_IMAGE_GRAYSCALE);
2904 "Can't read the image")) ;
2905 cvReleaseImage(&Ip);
2953 IplImage* Ip = NULL;
2954 Ip = cvLoadImage(filename, CV_LOAD_IMAGE_COLOR);
2959 "Can't read the image")) ;
2960 cvReleaseImage(&Ip);