44 #include <visp3/core/vpImage.h> 45 #include <visp3/core/vpImageConvert.h> 46 #include <visp3/core/vpIoTools.h> 47 #include <visp3/io/vpImageIo.h> 49 void vp_decodeHeaderPNM(
const std::string &filename, std::ifstream &fd,
const std::string &magic,
unsigned int &w,
50 unsigned int &h,
unsigned int &maxval);
52 #ifndef DOXYGEN_SHOULD_SKIP_THIS 62 void vp_decodeHeaderPNM(
const std::string &filename, std::ifstream &fd,
const std::string &magic,
unsigned int &w,
63 unsigned int &h,
unsigned int &maxval)
66 unsigned int nb_elt = 4, cpt_elt = 0;
67 while (cpt_elt != nb_elt) {
69 while (std::getline(fd, line) && (line.compare(0, 1,
"#") == 0 || line.size() == 0)) {
79 if (header.size() == 0) {
85 if (header[0].compare(0, magic.size(), magic) != 0) {
88 filename.c_str(), magic.c_str()));
91 header.erase(header.begin(),
94 while (header.size()) {
96 std::istringstream ss(header[0]);
99 header.erase(header.begin(),
101 }
else if (cpt_elt == 2) {
102 std::istringstream ss(header[0]);
105 header.erase(header.begin(),
107 }
else if (cpt_elt == 3) {
108 std::istringstream ss(header[0]);
111 header.erase(header.begin(),
119 vpImageIo::vpImageFormatType vpImageIo::getFormat(
const std::string &filename)
121 std::string ext = vpImageIo::getExtension(filename);
123 if (ext.compare(
".PGM") == 0)
125 else if (ext.compare(
".pgm") == 0)
127 else if (ext.compare(
".PPM") == 0)
129 else if (ext.compare(
".ppm") == 0)
131 else if (ext.compare(
".JPG") == 0)
133 else if (ext.compare(
".jpg") == 0)
135 else if (ext.compare(
".JPEG") == 0)
137 else if (ext.compare(
".jpeg") == 0)
139 else if (ext.compare(
".PNG") == 0)
141 else if (ext.compare(
".png") == 0)
144 else if (ext.compare(
".TIFF") == 0)
146 else if (ext.compare(
".tiff") == 0)
148 else if (ext.compare(
".TIF") == 0)
150 else if (ext.compare(
".tif") == 0)
152 else if (ext.compare(
".BMP") == 0)
154 else if (ext.compare(
".bmp") == 0)
156 else if (ext.compare(
".DIB") == 0)
158 else if (ext.compare(
".dib") == 0)
160 else if (ext.compare(
".PBM") == 0)
162 else if (ext.compare(
".pbm") == 0)
164 else if (ext.compare(
".SR") == 0)
165 return FORMAT_RASTER;
166 else if (ext.compare(
".sr") == 0)
167 return FORMAT_RASTER;
168 else if (ext.compare(
".RAS") == 0)
169 return FORMAT_RASTER;
170 else if (ext.compare(
".ras") == 0)
171 return FORMAT_RASTER;
172 else if (ext.compare(
".JP2") == 0)
173 return FORMAT_JPEG2000;
174 else if (ext.compare(
".jp2") == 0)
175 return FORMAT_JPEG2000;
177 return FORMAT_UNKNOWN;
181 std::string vpImageIo::getExtension(
const std::string &filename)
184 size_t dot = filename.find_last_of(
".");
185 std::string ext = filename.substr(dot, filename.size() - 1);
211 std::string message =
"Cannot read file: \"" + std::string(filename) +
"\" doesn't exist";
218 bool try_opencv_reader =
false;
220 switch (getFormat(final_filename)) {
228 #ifdef VISP_HAVE_JPEG 231 try_opencv_reader =
true;
235 #if defined(VISP_HAVE_PNG) 238 try_opencv_reader =
true;
246 case FORMAT_JPEG2000:
248 try_opencv_reader =
true;
252 if (try_opencv_reader) {
253 #if VISP_HAVE_OPENCV_VERSION >= 0x030000 255 cv::Mat cvI = cv::imread(final_filename, cv::IMREAD_GRAYSCALE);
256 if (cvI.cols == 0 && cvI.rows == 0) {
257 std::string message =
"Cannot read file \"" + std::string(final_filename) +
"\": Image format not supported";
261 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100 263 cv::Mat cvI = cv::imread(final_filename, CV_LOAD_IMAGE_GRAYSCALE);
264 if (cvI.cols == 0 && cvI.rows == 0) {
265 std::string message =
"Cannot read file \"" + std::string(final_filename) +
"\": Image format not supported";
270 std::string message =
"Cannot read file \"" + std::string(final_filename) +
"\": Image format not supported";
298 std::string message =
"Cannot read file: \"" + std::string(filename) +
"\" doesn't exist";
304 bool try_opencv_reader =
false;
306 switch (getFormat(final_filename)) {
314 #ifdef VISP_HAVE_JPEG 317 try_opencv_reader =
true;
321 #if defined(VISP_HAVE_PNG) 324 try_opencv_reader =
true;
332 case FORMAT_JPEG2000:
334 try_opencv_reader =
true;
338 if (try_opencv_reader) {
339 #if VISP_HAVE_OPENCV_VERSION >= 0x030000 341 cv::Mat cvI = cv::imread(final_filename, cv::IMREAD_COLOR);
342 if (cvI.cols == 0 && cvI.rows == 0) {
343 std::string message =
"Cannot read file \"" + std::string(final_filename) +
"\": Image format not supported";
347 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100 349 cv::Mat cvI = cv::imread(final_filename, CV_LOAD_IMAGE_COLOR);
350 if (cvI.cols == 0 && cvI.rows == 0) {
351 std::string message =
"Cannot read file \"" + std::string(final_filename) +
"\": Image format not supported";
356 std::string message =
"Cannot read file \"" + std::string(final_filename) +
"\": Image format not supported";
377 bool try_opencv_writer =
false;
379 switch (getFormat(filename)) {
387 #ifdef VISP_HAVE_JPEG 390 try_opencv_writer =
true;
397 try_opencv_writer =
true;
405 case FORMAT_JPEG2000:
407 try_opencv_writer =
true;
411 if (try_opencv_writer) {
412 #if VISP_HAVE_OPENCV_VERSION >= 0x020100 416 cv::imwrite(filename, cvI);
418 vpCERROR <<
"Cannot write file: Image format not supported..." << std::endl;
439 bool try_opencv_writer =
false;
441 switch (getFormat(filename)) {
449 #ifdef VISP_HAVE_JPEG 452 try_opencv_writer =
true;
459 try_opencv_writer =
true;
467 case FORMAT_JPEG2000:
469 try_opencv_writer =
true;
473 if (try_opencv_writer) {
474 #if VISP_HAVE_OPENCV_VERSION >= 0x020100 478 cv::imwrite(filename, cvI);
480 vpCERROR <<
"Cannot write file: Image format not supported..." << std::endl;
504 if (filename.empty()) {
508 fd = fopen(filename.c_str(),
"wb");
517 fprintf(fd,
"255\n");
523 ierr = fwrite(I.
bitmap,
sizeof(
float), nbyte, fd);
527 filename.c_str(), ierr, nbyte));
551 if (filename.empty()) {
555 fd = fopen(filename.c_str(),
"wb");
564 fprintf(fd,
"255\n");
570 ierr = fwrite(I.
bitmap,
sizeof(
unsigned char), nbyte, fd);
574 filename.c_str(), ierr, nbyte));
596 for (
unsigned int i = 0; i < nrows * ncols; i++)
616 if (filename.empty()) {
620 fd = fopen(filename.c_str(),
"wb");
629 fprintf(fd,
"255\n");
638 ierr = fwrite(Itmp.
bitmap,
sizeof(
unsigned char), nbyte, fd);
642 filename.c_str(), ierr, nbyte));
667 unsigned int w = 0, h = 0, maxval = 0;
668 unsigned int w_max = 100000, h_max = 100000, maxval_max = 255;
669 std::string magic(
"P8");
671 std::ifstream fd(filename.c_str(), std::ios::binary);
678 vp_decodeHeaderPNM(filename, fd, magic, w, h, maxval);
680 if (w > w_max || h > h_max) {
684 if (maxval > maxval_max) {
694 fd.read((
char *)I.
bitmap,
sizeof(
float) * nbyte);
721 unsigned int w = 0, h = 0, maxval = 0;
722 unsigned int w_max = 100000, h_max = 100000, maxval_max = 255;
723 std::string magic(
"P5");
725 std::ifstream fd(filename.c_str(), std::ios::binary);
732 vp_decodeHeaderPNM(filename, fd, magic, w, h, maxval);
734 if (w > w_max || h > h_max) {
738 if (maxval > maxval_max) {
748 fd.read((
char *)I.
bitmap, nbyte);
827 unsigned int w = 0, h = 0, maxval = 0;
828 unsigned int w_max = 100000, h_max = 100000, maxval_max = 255;
829 std::string magic(
"P6");
831 std::ifstream fd(filename.c_str(), std::ios::binary);
838 vp_decodeHeaderPNM(filename, fd, magic, w, h, maxval);
840 if (w > w_max || h > h_max) {
844 if (maxval > maxval_max) {
853 for (
unsigned int i = 0; i < I.
getHeight(); i++) {
854 for (
unsigned int j = 0; j < I.
getWidth(); j++) {
855 unsigned char rgb[3];
856 fd.read((
char *)&rgb, 3);
861 (i * I.
getWidth() + j) * 3 + fd.gcount(), I.
getSize() * 3, filename.c_str()));
905 if (filename.empty()) {
909 f = fopen(filename.c_str(),
"wb");
917 fprintf(f,
"%d\n", 255);
919 for (
unsigned int i = 0; i < I.
getHeight(); i++) {
920 for (
unsigned int j = 0; j < I.
getWidth(); j++) {
922 unsigned char rgb[3];
927 size_t res = fwrite(&rgb, 1, 3, f);
943 #if defined(VISP_HAVE_JPEG) 954 struct jpeg_compress_struct cinfo;
955 struct jpeg_error_mgr jerr;
958 cinfo.err = jpeg_std_error(&jerr);
959 jpeg_create_compress(&cinfo);
962 if (filename.empty()) {
966 file = fopen(filename.c_str(),
"wb");
975 jpeg_stdio_dest(&cinfo, file);
977 cinfo.image_width = width;
978 cinfo.image_height = height;
979 cinfo.input_components = 1;
980 cinfo.in_color_space = JCS_GRAYSCALE;
981 jpeg_set_defaults(&cinfo);
983 jpeg_start_compress(&cinfo, TRUE);
986 line =
new unsigned char[width];
987 unsigned char *input = (
unsigned char *)I.
bitmap;
988 while (cinfo.next_scanline < cinfo.image_height) {
989 for (
unsigned int i = 0; i < width; i++) {
993 jpeg_write_scanlines(&cinfo, &line, 1);
996 jpeg_finish_compress(&cinfo);
997 jpeg_destroy_compress(&cinfo);
1011 struct jpeg_compress_struct cinfo;
1012 struct jpeg_error_mgr jerr;
1015 cinfo.err = jpeg_std_error(&jerr);
1016 jpeg_create_compress(&cinfo);
1019 if (filename.empty()) {
1023 file = fopen(filename.c_str(),
"wb");
1032 jpeg_stdio_dest(&cinfo, file);
1034 cinfo.image_width = width;
1035 cinfo.image_height = height;
1036 cinfo.input_components = 3;
1037 cinfo.in_color_space = JCS_RGB;
1038 jpeg_set_defaults(&cinfo);
1040 jpeg_start_compress(&cinfo, TRUE);
1042 unsigned char *line;
1043 line =
new unsigned char[3 * width];
1044 unsigned char *input = (
unsigned char *)I.
bitmap;
1045 while (cinfo.next_scanline < cinfo.image_height) {
1046 for (
unsigned int i = 0; i < width; i++) {
1047 line[i * 3] = *(input);
1049 line[i * 3 + 1] = *(input);
1051 line[i * 3 + 2] = *(input);
1055 jpeg_write_scanlines(&cinfo, &line, 1);
1058 jpeg_finish_compress(&cinfo);
1059 jpeg_destroy_compress(&cinfo);
1082 struct jpeg_decompress_struct cinfo;
1083 struct jpeg_error_mgr jerr;
1086 cinfo.err = jpeg_std_error(&jerr);
1087 jpeg_create_decompress(&cinfo);
1090 if (filename.empty()) {
1094 file = fopen(filename.c_str(),
"rb");
1100 jpeg_stdio_src(&cinfo, file);
1101 jpeg_read_header(&cinfo, TRUE);
1103 unsigned int width = cinfo.image_width;
1104 unsigned int height = cinfo.image_height;
1109 jpeg_start_decompress(&cinfo);
1111 unsigned int rowbytes = cinfo.output_width * (
unsigned int)(cinfo.output_components);
1112 JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray)((j_common_ptr)&cinfo, JPOOL_IMAGE, rowbytes, 1);
1114 if (cinfo.out_color_space == JCS_RGB) {
1116 unsigned char *output = (
unsigned char *)Ic.
bitmap;
1117 while (cinfo.output_scanline < cinfo.output_height) {
1118 jpeg_read_scanlines(&cinfo, buffer, 1);
1119 for (
unsigned int i = 0; i < width; i++) {
1120 *(output++) = buffer[0][i * 3];
1121 *(output++) = buffer[0][i * 3 + 1];
1122 *(output++) = buffer[0][i * 3 + 2];
1129 else if (cinfo.out_color_space == JCS_GRAYSCALE) {
1130 while (cinfo.output_scanline < cinfo.output_height) {
1131 unsigned int row = cinfo.output_scanline;
1132 jpeg_read_scanlines(&cinfo, buffer, 1);
1133 memcpy(I[row], buffer[0], rowbytes);
1137 jpeg_finish_decompress(&cinfo);
1138 jpeg_destroy_decompress(&cinfo);
1162 struct jpeg_decompress_struct cinfo;
1163 struct jpeg_error_mgr jerr;
1166 cinfo.err = jpeg_std_error(&jerr);
1167 jpeg_create_decompress(&cinfo);
1170 if (filename.empty()) {
1174 file = fopen(filename.c_str(),
"rb");
1180 jpeg_stdio_src(&cinfo, file);
1182 jpeg_read_header(&cinfo, TRUE);
1184 unsigned int width = cinfo.image_width;
1185 unsigned int height = cinfo.image_height;
1190 jpeg_start_decompress(&cinfo);
1192 unsigned int rowbytes = cinfo.output_width * (
unsigned int)(cinfo.output_components);
1193 JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray)((j_common_ptr)&cinfo, JPOOL_IMAGE, rowbytes, 1);
1195 if (cinfo.out_color_space == JCS_RGB) {
1196 unsigned char *output = (
unsigned char *)I.
bitmap;
1197 while (cinfo.output_scanline < cinfo.output_height) {
1198 jpeg_read_scanlines(&cinfo, buffer, 1);
1199 for (
unsigned int i = 0; i < width; i++) {
1200 *(output++) = buffer[0][i * 3];
1201 *(output++) = buffer[0][i * 3 + 1];
1202 *(output++) = buffer[0][i * 3 + 2];
1208 else if (cinfo.out_color_space == JCS_GRAYSCALE) {
1211 while (cinfo.output_scanline < cinfo.output_height) {
1212 unsigned int row = cinfo.output_scanline;
1213 jpeg_read_scanlines(&cinfo, buffer, 1);
1214 memcpy(Ig[row], buffer[0], rowbytes);
1220 jpeg_finish_decompress(&cinfo);
1221 jpeg_destroy_decompress(&cinfo);
1225 #elif defined(VISP_HAVE_OPENCV) 1236 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408) 1239 cv::imwrite(filename.c_str(), Ip);
1241 IplImage *Ip = NULL;
1244 cvSaveImage(filename.c_str(), Ip);
1246 cvReleaseImage(&Ip);
1259 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408) 1262 cv::imwrite(filename.c_str(), Ip);
1264 IplImage *Ip = NULL;
1267 cvSaveImage(filename.c_str(), Ip);
1269 cvReleaseImage(&Ip);
1291 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000) 1292 cv::Mat Ip = cv::imread(filename.c_str(), cv::IMREAD_GRAYSCALE);
1297 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020408) 1298 cv::Mat Ip = cv::imread(filename.c_str(), CV_LOAD_IMAGE_GRAYSCALE);
1304 IplImage *Ip = NULL;
1305 Ip = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_GRAYSCALE);
1310 cvReleaseImage(&Ip);
1334 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000) 1335 cv::Mat Ip = cv::imread(filename.c_str(), cv::IMREAD_GRAYSCALE);
1340 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020408) 1341 cv::Mat Ip = cv::imread(filename.c_str(), CV_LOAD_IMAGE_GRAYSCALE);
1347 IplImage *Ip = NULL;
1348 Ip = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_COLOR);
1353 cvReleaseImage(&Ip);
1363 #if defined(VISP_HAVE_PNG) 1377 if (filename.empty()) {
1381 file = fopen(filename.c_str(),
"wb");
1388 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
1395 png_infop info_ptr = png_create_info_struct(png_ptr);
1398 png_destroy_write_struct(&png_ptr, NULL);
1405 if (setjmp(png_jmpbuf(png_ptr))) {
1407 png_destroy_write_struct(&png_ptr, &info_ptr);
1414 png_init_io(png_ptr, file);
1419 int color_type = PNG_COLOR_TYPE_GRAY;
1422 if (setjmp(png_jmpbuf(png_ptr))) {
1424 png_destroy_write_struct(&png_ptr, &info_ptr);
1429 png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth, color_type, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE,
1430 PNG_FILTER_TYPE_BASE);
1432 png_write_info(png_ptr, info_ptr);
1434 png_bytep *row_ptrs =
new png_bytep[height];
1435 for (
unsigned int i = 0; i < height; i++)
1436 row_ptrs[i] =
new png_byte[width];
1438 unsigned char *input = (
unsigned char *)I.
bitmap;
1440 for (
unsigned int i = 0; i < height; i++) {
1441 png_byte *row = row_ptrs[i];
1442 for (
unsigned int j = 0; j < width; j++) {
1448 png_write_image(png_ptr, row_ptrs);
1450 png_write_end(png_ptr, NULL);
1452 for (
unsigned int j = 0; j < height; j++)
1453 delete[] row_ptrs[j];
1457 png_destroy_write_struct(&png_ptr, &info_ptr);
1474 if (filename.empty()) {
1478 file = fopen(filename.c_str(),
"wb");
1485 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
1492 png_infop info_ptr = png_create_info_struct(png_ptr);
1495 png_destroy_write_struct(&png_ptr, NULL);
1502 if (setjmp(png_jmpbuf(png_ptr))) {
1504 png_destroy_write_struct(&png_ptr, &info_ptr);
1511 png_init_io(png_ptr, file);
1516 int color_type = PNG_COLOR_TYPE_RGB;
1519 if (setjmp(png_jmpbuf(png_ptr))) {
1521 png_destroy_write_struct(&png_ptr, &info_ptr);
1526 png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth, color_type, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE,
1527 PNG_FILTER_TYPE_BASE);
1529 png_write_info(png_ptr, info_ptr);
1531 png_bytep *row_ptrs =
new png_bytep[height];
1532 for (
unsigned int i = 0; i < height; i++)
1533 row_ptrs[i] =
new png_byte[3 * width];
1535 unsigned char *input = (
unsigned char *)I.
bitmap;
1538 for (
unsigned int i = 0; i < height; i++) {
1539 png_byte *row = row_ptrs[i];
1540 for (
unsigned int j = 0; j < width; j++) {
1541 row[3 * j] = *(input);
1543 row[3 * j + 1] = *(input);
1545 row[3 * j + 2] = *(input);
1551 png_write_image(png_ptr, row_ptrs);
1553 png_write_end(png_ptr, NULL);
1555 for (
unsigned int j = 0; j < height; j++)
1556 delete[] row_ptrs[j];
1560 png_destroy_write_struct(&png_ptr, &info_ptr);
1586 if (filename.empty()) {
1590 file = fopen(filename.c_str(),
"rb");
1597 if (fread(magic, 1,
sizeof(magic), file) !=
sizeof(magic)) {
1603 if (png_sig_cmp(magic, 0,
sizeof(magic))) {
1611 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
1612 if (png_ptr == NULL) {
1613 fprintf(stderr,
"error: can't create a png read structure!\n");
1619 png_infop info_ptr = png_create_info_struct(png_ptr);
1620 if (info_ptr == NULL) {
1621 fprintf(stderr,
"error: can't create a png info structure!\n");
1623 png_destroy_read_struct(&png_ptr, NULL, NULL);
1629 if (setjmp(png_jmpbuf(png_ptr))) {
1631 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
1638 png_init_io(png_ptr, file);
1641 png_set_sig_bytes(png_ptr,
sizeof(magic));
1644 png_read_info(png_ptr, info_ptr);
1646 unsigned int width = png_get_image_width(png_ptr, info_ptr);
1647 unsigned int height = png_get_image_height(png_ptr, info_ptr);
1649 unsigned int bit_depth, channels, color_type;
1651 bit_depth = png_get_bit_depth(png_ptr, info_ptr);
1652 channels = png_get_channels(png_ptr, info_ptr);
1653 color_type = png_get_color_type(png_ptr, info_ptr);
1656 if (color_type == PNG_COLOR_TYPE_PALETTE)
1657 png_set_palette_to_rgb(png_ptr);
1660 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
1661 png_set_expand(png_ptr);
1666 if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
1667 png_set_strip_alpha(png_ptr);
1669 if (bit_depth == 16)
1670 png_set_strip_16(png_ptr);
1671 else if (bit_depth < 8)
1672 png_set_packing(png_ptr);
1675 png_read_update_info(png_ptr, info_ptr);
1677 channels = png_get_channels(png_ptr, info_ptr);
1682 png_bytep *rowPtrs =
new png_bytep[height];
1684 unsigned int stride = png_get_rowbytes(png_ptr, info_ptr);
1685 unsigned char *data =
new unsigned char[stride * height];
1687 for (
unsigned int i = 0; i < height; i++)
1688 rowPtrs[i] = (png_bytep)data + (i * stride);
1690 png_read_image(png_ptr, rowPtrs);
1693 unsigned char *output;
1697 output = (
unsigned char *)I.
bitmap;
1698 for (
unsigned int i = 0; i < width * height; i++) {
1699 *(output++) = data[i];
1704 output = (
unsigned char *)I.
bitmap;
1705 for (
unsigned int i = 0; i < width * height; i++) {
1706 *(output++) = data[i * 2];
1711 output = (
unsigned char *)Ic.
bitmap;
1712 for (
unsigned int i = 0; i < width * height; i++) {
1713 *(output++) = data[i * 3];
1714 *(output++) = data[i * 3 + 1];
1715 *(output++) = data[i * 3 + 2];
1722 output = (
unsigned char *)Ic.
bitmap;
1723 for (
unsigned int i = 0; i < width * height; i++) {
1724 *(output++) = data[i * 4];
1725 *(output++) = data[i * 4 + 1];
1726 *(output++) = data[i * 4 + 2];
1727 *(output++) = data[i * 4 + 3];
1733 delete[](png_bytep) rowPtrs;
1735 png_read_end(png_ptr, NULL);
1736 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
1764 if (filename.empty()) {
1768 file = fopen(filename.c_str(),
"rb");
1775 if (fread(magic, 1,
sizeof(magic), file) !=
sizeof(magic)) {
1781 if (png_sig_cmp(magic, 0,
sizeof(magic))) {
1788 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
1796 png_infop info_ptr = png_create_info_struct(png_ptr);
1799 png_destroy_read_struct(&png_ptr, NULL, NULL);
1806 if (setjmp(png_jmpbuf(png_ptr))) {
1808 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
1815 png_init_io(png_ptr, file);
1818 png_set_sig_bytes(png_ptr,
sizeof(magic));
1821 png_read_info(png_ptr, info_ptr);
1823 unsigned int width = png_get_image_width(png_ptr, info_ptr);
1824 unsigned int height = png_get_image_height(png_ptr, info_ptr);
1826 unsigned int bit_depth, channels, color_type;
1828 bit_depth = png_get_bit_depth(png_ptr, info_ptr);
1829 channels = png_get_channels(png_ptr, info_ptr);
1830 color_type = png_get_color_type(png_ptr, info_ptr);
1833 if (color_type == PNG_COLOR_TYPE_PALETTE)
1834 png_set_palette_to_rgb(png_ptr);
1837 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
1838 png_set_expand(png_ptr);
1843 if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
1844 png_set_strip_alpha(png_ptr);
1846 if (bit_depth == 16)
1847 png_set_strip_16(png_ptr);
1848 else if (bit_depth < 8)
1849 png_set_packing(png_ptr);
1852 png_read_update_info(png_ptr, info_ptr);
1854 channels = png_get_channels(png_ptr, info_ptr);
1859 png_bytep *rowPtrs =
new png_bytep[height];
1861 unsigned int stride = png_get_rowbytes(png_ptr, info_ptr);
1862 unsigned char *data =
new unsigned char[stride * height];
1864 for (
unsigned int i = 0; i < height; i++)
1865 rowPtrs[i] = (png_bytep)data + (i * stride);
1867 png_read_image(png_ptr, rowPtrs);
1870 unsigned char *output;
1874 output = (
unsigned char *)Ig.
bitmap;
1875 for (
unsigned int i = 0; i < width * height; i++) {
1876 *(output++) = data[i];
1882 output = (
unsigned char *)Ig.
bitmap;
1883 for (
unsigned int i = 0; i < width * height; i++) {
1884 *(output++) = data[i * 2];
1890 output = (
unsigned char *)I.
bitmap;
1891 for (
unsigned int i = 0; i < width * height; i++) {
1892 *(output++) = data[i * 3];
1893 *(output++) = data[i * 3 + 1];
1894 *(output++) = data[i * 3 + 2];
1900 output = (
unsigned char *)I.
bitmap;
1901 for (
unsigned int i = 0; i < width * height; i++) {
1902 *(output++) = data[i * 4];
1903 *(output++) = data[i * 4 + 1];
1904 *(output++) = data[i * 4 + 2];
1905 *(output++) = data[i * 4 + 3];
1910 delete[](png_bytep) rowPtrs;
1912 png_read_end(png_ptr, NULL);
1913 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
1917 #elif defined(VISP_HAVE_OPENCV) 1928 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408) 1931 cv::imwrite(filename.c_str(), Ip);
1933 IplImage *Ip = NULL;
1936 cvSaveImage(filename.c_str(), Ip);
1938 cvReleaseImage(&Ip);
1951 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408) 1954 cv::imwrite(filename.c_str(), Ip);
1956 IplImage *Ip = NULL;
1959 cvSaveImage(filename.c_str(), Ip);
1961 cvReleaseImage(&Ip);
1983 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000) 1984 cv::Mat Ip = cv::imread(filename.c_str(), cv::IMREAD_GRAYSCALE);
1989 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020408) 1990 cv::Mat Ip = cv::imread(filename.c_str(), CV_LOAD_IMAGE_GRAYSCALE);
1996 IplImage *Ip = NULL;
1997 Ip = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_GRAYSCALE);
2002 cvReleaseImage(&Ip);
2026 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000) 2027 cv::Mat Ip = cv::imread(filename.c_str(), cv::IMREAD_GRAYSCALE);
2032 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020408) 2033 cv::Mat Ip = cv::imread(filename.c_str(), CV_LOAD_IMAGE_GRAYSCALE);
2039 IplImage *Ip = NULL;
2040 Ip = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_COLOR);
2045 cvReleaseImage(&Ip);
static void writeJPEG(const vpImage< unsigned char > &I, const std::string &filename)
Used to indicate that a value is not in the allowed range.
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
error that can be emited by ViSP classes.
Error that can be emited by the vpImage class and its derivates.
unsigned char G
Green component.
static void writePPM(const vpImage< unsigned char > &I, const std::string &filename)
static void write(const vpImage< unsigned char > &I, const std::string &filename)
static void readPPM(vpImage< unsigned char > &I, const std::string &filename)
unsigned int getSize() const
static void writePFM(const vpImage< float > &I, const std::string &filename)
void resize(const unsigned int h, const unsigned int w)
resize the image : Image initialization
static void readPFM(vpImage< float > &I, const std::string &filename)
static void read(vpImage< unsigned char > &I, const std::string &filename)
static void readPNG(vpImage< unsigned char > &I, const std::string &filename)
unsigned char R
Red component.
unsigned int getHeight() const
static void readPGM(vpImage< unsigned char > &I, const std::string &filename)
static void writePGM(const vpImage< unsigned char > &I, const std::string &filename)
static void readJPEG(vpImage< unsigned char > &I, const std::string &filename)
static void writePNG(const vpImage< unsigned char > &I, const std::string &filename)