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_infop info_ptr = png_create_info_struct(png_ptr);
2107 png_destroy_write_struct (&png_ptr, &info_ptr);
2111 if (setjmp (png_jmpbuf (png_ptr)))
2114 png_destroy_write_struct (&png_ptr, &info_ptr);
2117 "PNG write error")) ;
2121 png_init_io (png_ptr, file);
2126 int color_type = PNG_COLOR_TYPE_GRAY;
2129 if (setjmp (png_jmpbuf (png_ptr)))
2132 png_destroy_write_struct (&png_ptr, &info_ptr);
2135 "PNG write error")) ;
2138 png_set_IHDR(png_ptr, info_ptr, width, height,
2139 bit_depth, color_type, PNG_INTERLACE_NONE,
2140 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
2142 png_write_info(png_ptr, info_ptr);
2144 png_bytep* row_ptrs =
new png_bytep[height];
2145 for (
unsigned int i = 0; i < height; i++)
2146 row_ptrs[i] =
new png_byte[width];
2148 unsigned char* input = (
unsigned char*)I.
bitmap;
2150 for (
unsigned int i = 0; i < height; i++)
2152 png_byte* row = row_ptrs[i];
2153 for(
unsigned int j = 0; j < width; j++)
2160 if (setjmp (png_jmpbuf (png_ptr)))
2163 png_destroy_write_struct (&png_ptr, &info_ptr);
2166 "PNG write error")) ;
2169 png_write_image(png_ptr, row_ptrs);
2171 if (setjmp (png_jmpbuf (png_ptr)))
2174 png_destroy_write_struct (&png_ptr, &info_ptr);
2177 "PNG write error")) ;
2180 png_write_end(png_ptr, NULL);
2182 for(
unsigned int j = 0; j < height; j++)
2183 delete[] row_ptrs[j];
2185 delete[] (png_bytep)row_ptrs;
2187 png_destroy_write_struct (&png_ptr, &info_ptr);
2220 if (filename ==
'\0') {
2226 file = fopen(filename,
"wb");
2231 "cannot write file")) ;
2235 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL, NULL, NULL);
2241 png_infop info_ptr = png_create_info_struct(png_ptr);
2245 png_destroy_write_struct (&png_ptr, &info_ptr);
2249 if (setjmp (png_jmpbuf (png_ptr)))
2252 png_destroy_write_struct (&png_ptr, &info_ptr);
2255 "PNG write error")) ;
2259 png_init_io (png_ptr, file);
2264 int color_type = PNG_COLOR_TYPE_RGB;
2267 if (setjmp (png_jmpbuf (png_ptr)))
2270 png_destroy_write_struct (&png_ptr, &info_ptr);
2273 "PNG write error")) ;
2276 png_set_IHDR(png_ptr, info_ptr, width, height,
2277 bit_depth, color_type, PNG_INTERLACE_NONE,
2278 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
2280 png_write_info(png_ptr, info_ptr);
2282 png_bytep* row_ptrs =
new png_bytep[height];
2283 for (
unsigned int i = 0; i < height; i++)
2284 row_ptrs[i] =
new png_byte[3*width];
2286 unsigned char* input = (
unsigned char*)I.
bitmap;;
2288 for (
unsigned int i = 0; i < height; i++)
2290 png_byte* row = row_ptrs[i];
2291 for(
unsigned int j = 0; j < width; j++)
2293 row[3*j] = *(input);input++;
2294 row[3*j+1] = *(input);input++;
2295 row[3*j+2] = *(input);input++;
2300 if (setjmp (png_jmpbuf (png_ptr)))
2303 png_destroy_write_struct (&png_ptr, &info_ptr);
2306 "PNG write error")) ;
2309 png_write_image(png_ptr, row_ptrs);
2311 if (setjmp (png_jmpbuf (png_ptr)))
2314 png_destroy_write_struct (&png_ptr, &info_ptr);
2317 "PNG write error")) ;
2320 png_write_end(png_ptr, NULL);
2322 for(
unsigned int j = 0; j < height; j++)
2323 delete[] row_ptrs[j];
2325 delete[] (png_bytep)row_ptrs;
2327 png_destroy_write_struct (&png_ptr, &info_ptr);
2369 if (filename ==
'\0') {
2375 file = fopen(filename,
"rb");
2380 "cannot read file")) ;
2384 if (fread (magic, 1,
sizeof (magic), file) !=
sizeof (magic))
2387 vpERROR_TRACE(
"couldn't read magic number in file \"%s\"\n", filename) ;
2389 "error reading png file")) ;
2393 if (png_sig_cmp (magic,0,
sizeof (magic)))
2395 fprintf (stderr,
"error: \"%s\" is not a valid PNG image!\n",filename);
2398 "error reading png file")) ;
2403 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
2404 if (png_ptr == NULL)
2406 fprintf (stderr,
"error: can't create a png read structure!\n");
2409 "error reading png file")) ;
2413 png_infop info_ptr = png_create_info_struct (png_ptr);
2414 if (info_ptr == NULL)
2416 fprintf (stderr,
"error: can't create a png info structure!\n");
2418 png_destroy_read_struct (&png_ptr, NULL, NULL);
2420 "error reading png file")) ;
2424 if (setjmp (png_jmpbuf (png_ptr)))
2427 png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
2430 "PNG read error")) ;
2434 png_init_io (png_ptr, file);
2437 png_set_sig_bytes (png_ptr,
sizeof (magic));
2440 png_read_info (png_ptr, info_ptr);
2442 unsigned int width = png_get_image_width(png_ptr, info_ptr);
2443 unsigned int height = png_get_image_height(png_ptr, info_ptr);
2445 unsigned int bit_depth, channels, color_type;
2447 bit_depth = png_get_bit_depth (png_ptr, info_ptr);
2448 channels = png_get_channels(png_ptr, info_ptr);
2449 color_type = png_get_color_type (png_ptr, info_ptr);
2452 if (color_type == PNG_COLOR_TYPE_PALETTE)
2453 png_set_palette_to_rgb (png_ptr);
2456 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
2457 png_set_expand (png_ptr);
2462 if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2463 png_set_strip_alpha(png_ptr);
2465 if (bit_depth == 16)
2466 png_set_strip_16 (png_ptr);
2467 else if (bit_depth < 8)
2468 png_set_packing (png_ptr);
2471 png_read_update_info (png_ptr, info_ptr);
2473 channels = png_get_channels(png_ptr, info_ptr);
2478 png_bytep* rowPtrs =
new png_bytep[height];
2480 unsigned int stride = width * bit_depth * channels / 8;
2481 unsigned char* data =
new unsigned char[stride * height];
2483 for (
unsigned int i =0; i < height; i++)
2484 rowPtrs[i] = (png_bytep)data + (i * stride);
2486 png_read_image(png_ptr, rowPtrs);
2489 unsigned char* output;
2494 output = (
unsigned char*)I.
bitmap;
2495 for (
unsigned int i = 0; i < width*height; i++)
2497 *(output++) = data[i];
2501 output = (
unsigned char*)I.
bitmap;
2502 for (
unsigned int i = 0; i < width*height; i++)
2504 *(output++) = data[i*2];
2509 output = (
unsigned char*)Ic.
bitmap;
2510 for (
unsigned int i = 0; i < width*height; i++)
2512 *(output++) = data[i*3];
2513 *(output++) = data[i*3+1];
2514 *(output++) = data[i*3+2];
2520 output = (
unsigned char*)Ic.
bitmap;
2521 for (
unsigned int i = 0; i < width*height; i++)
2523 *(output++) = data[i*4];
2524 *(output++) = data[i*4+1];
2525 *(output++) = data[i*4+2];
2526 *(output++) = data[i*4+3];
2532 delete [] (png_bytep)rowPtrs;
2534 png_read_end (png_ptr, NULL);
2535 png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
2588 if (filename ==
'\0') {
2594 file = fopen(filename,
"rb");
2599 "cannot read file")) ;
2603 if (fread (magic, 1,
sizeof (magic), file) !=
sizeof (magic))
2606 vpERROR_TRACE(
"couldn't read magic number in file \"%s\"\n", filename) ;
2608 "error reading pgm file")) ;
2612 if (png_sig_cmp (magic,0,
sizeof (magic)))
2614 fprintf (stderr,
"error: \"%s\" is not a valid PNG image!\n",filename);
2619 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
2626 png_infop info_ptr = png_create_info_struct (png_ptr);
2630 png_destroy_read_struct (&png_ptr, NULL, NULL);
2634 if (setjmp (png_jmpbuf (png_ptr)))
2637 png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
2640 "PNG read error")) ;
2644 png_init_io (png_ptr, file);
2647 png_set_sig_bytes (png_ptr,
sizeof (magic));
2650 png_read_info (png_ptr, info_ptr);
2652 unsigned int width = png_get_image_width(png_ptr, info_ptr);
2653 unsigned int height = png_get_image_height(png_ptr, info_ptr);
2655 unsigned int bit_depth, channels, color_type;
2657 bit_depth = png_get_bit_depth (png_ptr, info_ptr);
2658 channels = png_get_channels(png_ptr, info_ptr);
2659 color_type = png_get_color_type (png_ptr, info_ptr);
2662 if (color_type == PNG_COLOR_TYPE_PALETTE)
2663 png_set_palette_to_rgb (png_ptr);
2666 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
2667 png_set_expand (png_ptr);
2672 if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2673 png_set_strip_alpha(png_ptr);
2675 if (bit_depth == 16)
2676 png_set_strip_16 (png_ptr);
2677 else if (bit_depth < 8)
2678 png_set_packing (png_ptr);
2681 png_read_update_info (png_ptr, info_ptr);
2683 channels = png_get_channels(png_ptr, info_ptr);
2688 png_bytep* rowPtrs =
new png_bytep[height];
2690 unsigned int stride = width * bit_depth * channels / 8;
2691 unsigned char* data =
new unsigned char[stride * height];
2694 for (
unsigned int i =0; i < height; i++)
2695 rowPtrs[i] = (png_bytep)data + (i * stride);
2697 png_read_image(png_ptr, rowPtrs);
2700 unsigned char* output;
2705 output = (
unsigned char*)Ig.
bitmap;
2706 for (
unsigned int i = 0; i < width*height; i++)
2708 *(output++) = data[i];
2713 output = (
unsigned char*)Ig.
bitmap;
2714 for (
unsigned int i = 0; i < width*height; i++)
2716 *(output++) = data[i*2];
2722 output = (
unsigned char*)I.
bitmap;
2723 for (
unsigned int i = 0; i < width*height; i++)
2725 *(output++) = data[i*3];
2726 *(output++) = data[i*3+1];
2727 *(output++) = data[i*3+2];
2732 output = (
unsigned char*)I.
bitmap;
2733 for (
unsigned int i = 0; i < width*height; i++)
2735 *(output++) = data[i*4];
2736 *(output++) = data[i*4+1];
2737 *(output++) = data[i*4+2];
2738 *(output++) = data[i*4+3];
2743 delete [] (png_bytep)rowPtrs;
2745 png_read_end (png_ptr, NULL);
2746 png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
2775 #elif defined(VISP_HAVE_OPENCV)
2787 IplImage* Ip = NULL;
2790 cvSaveImage(filename, Ip);
2792 cvReleaseImage(&Ip);
2820 IplImage* Ip = NULL;
2823 cvSaveImage(filename, Ip);
2825 cvReleaseImage(&Ip);
2862 IplImage* Ip = NULL;
2863 Ip = cvLoadImage(filename, CV_LOAD_IMAGE_GRAYSCALE);
2868 "Can't read the image")) ;
2869 cvReleaseImage(&Ip);
2917 IplImage* Ip = NULL;
2918 Ip = cvLoadImage(filename, CV_LOAD_IMAGE_COLOR);
2923 "Can't read the image")) ;
2924 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)