44 #include <visp3/core/vpImage.h> 45 #include <visp3/core/vpImageConvert.h> 46 #include <visp3/core/vpIoTools.h> 47 #include <visp3/io/vpImageIo.h> 56 #if defined(VISP_HAVE_JPEG) 61 #if defined(VISP_HAVE_PNG) 65 #if !defined(VISP_HAVE_OPENCV) 66 #if !defined(VISP_HAVE_JPEG) || !defined(VISP_HAVE_PNG) 68 #if defined __SSE2__ || defined _M_X64 || (defined _M_IX86_FP && _M_IX86_FP >= 2) 69 # define VISP_HAVE_SSE2 1 72 #ifndef VISP_HAVE_SSE2 76 #define STB_IMAGE_IMPLEMENTATION 77 #include <stb_image.h> 79 #define STB_IMAGE_WRITE_IMPLEMENTATION 80 #include <stb_image_write.h> 84 void vp_decodeHeaderPNM(
const std::string &filename, std::ifstream &fd,
const std::string &magic,
unsigned int &w,
85 unsigned int &h,
unsigned int &maxval);
87 #ifndef DOXYGEN_SHOULD_SKIP_THIS 97 void vp_decodeHeaderPNM(
const std::string &filename, std::ifstream &fd,
const std::string &magic,
unsigned int &w,
98 unsigned int &h,
unsigned int &maxval)
101 unsigned int nb_elt = 4, cpt_elt = 0;
102 while (cpt_elt != nb_elt) {
104 while (std::getline(fd, line) && (line.compare(0, 1,
"#") == 0 || line.size() == 0)) {
114 if (header.size() == 0) {
120 if (header[0].compare(0, magic.size(), magic) != 0) {
123 filename.c_str(), magic.c_str()));
126 header.erase(header.begin(),
129 while (header.size()) {
131 std::istringstream ss(header[0]);
134 header.erase(header.begin(),
136 }
else if (cpt_elt == 2) {
137 std::istringstream ss(header[0]);
140 header.erase(header.begin(),
142 }
else if (cpt_elt == 3) {
143 std::istringstream ss(header[0]);
146 header.erase(header.begin(),
154 vpImageIo::vpImageFormatType vpImageIo::getFormat(
const std::string &filename)
156 std::string ext = vpImageIo::getExtension(filename);
158 if (ext.compare(
".PGM") == 0)
160 else if (ext.compare(
".pgm") == 0)
162 else if (ext.compare(
".PPM") == 0)
164 else if (ext.compare(
".ppm") == 0)
166 else if (ext.compare(
".JPG") == 0)
168 else if (ext.compare(
".jpg") == 0)
170 else if (ext.compare(
".JPEG") == 0)
172 else if (ext.compare(
".jpeg") == 0)
174 else if (ext.compare(
".PNG") == 0)
176 else if (ext.compare(
".png") == 0)
179 else if (ext.compare(
".TIFF") == 0)
181 else if (ext.compare(
".tiff") == 0)
183 else if (ext.compare(
".TIF") == 0)
185 else if (ext.compare(
".tif") == 0)
187 else if (ext.compare(
".BMP") == 0)
189 else if (ext.compare(
".bmp") == 0)
191 else if (ext.compare(
".DIB") == 0)
193 else if (ext.compare(
".dib") == 0)
195 else if (ext.compare(
".PBM") == 0)
197 else if (ext.compare(
".pbm") == 0)
199 else if (ext.compare(
".SR") == 0)
200 return FORMAT_RASTER;
201 else if (ext.compare(
".sr") == 0)
202 return FORMAT_RASTER;
203 else if (ext.compare(
".RAS") == 0)
204 return FORMAT_RASTER;
205 else if (ext.compare(
".ras") == 0)
206 return FORMAT_RASTER;
207 else if (ext.compare(
".JP2") == 0)
208 return FORMAT_JPEG2000;
209 else if (ext.compare(
".jp2") == 0)
210 return FORMAT_JPEG2000;
212 return FORMAT_UNKNOWN;
216 std::string vpImageIo::getExtension(
const std::string &filename)
219 size_t dot = filename.find_last_of(
".");
220 std::string ext = filename.substr(dot, filename.size() - 1);
248 std::string message =
"Cannot read file: \"" + std::string(filename) +
"\" doesn't exist";
255 bool try_opencv_reader =
false;
257 switch (getFormat(final_filename)) {
265 #ifdef VISP_HAVE_JPEG 268 try_opencv_reader =
true;
272 #if defined(VISP_HAVE_PNG) 275 try_opencv_reader =
true;
283 case FORMAT_JPEG2000:
285 try_opencv_reader =
true;
289 if (try_opencv_reader) {
290 #if defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100 291 #if VISP_HAVE_OPENCV_VERSION >= 0x030200 292 int flags = cv::IMREAD_GRAYSCALE | cv::IMREAD_IGNORE_ORIENTATION;
293 #elif VISP_HAVE_OPENCV_VERSION >= 0x030000 294 int flags = cv::IMREAD_GRAYSCALE;
295 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100 296 int flags = CV_LOAD_IMAGE_GRAYSCALE;
299 cv::Mat cvI = cv::imread(final_filename, flags);
300 if (cvI.cols == 0 && cvI.rows == 0) {
301 std::string message =
"Cannot read file \"" + std::string(final_filename) +
"\": Image format not supported";
306 switch (getFormat(final_filename)) {
318 case FORMAT_JPEG2000:
321 std::string message =
"Cannot read file \"" + std::string(final_filename) +
"\": Image format not supported";
352 std::string message =
"Cannot read file: \"" + std::string(filename) +
"\" doesn't exist";
358 bool try_opencv_reader =
false;
360 switch (getFormat(final_filename)) {
368 #ifdef VISP_HAVE_JPEG 371 try_opencv_reader =
true;
375 #if defined(VISP_HAVE_PNG) 378 try_opencv_reader =
true;
386 case FORMAT_JPEG2000:
388 try_opencv_reader =
true;
392 if (try_opencv_reader) {
393 #if defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100 394 #if VISP_HAVE_OPENCV_VERSION >= 0x030200 395 int flags = cv::IMREAD_COLOR | cv::IMREAD_IGNORE_ORIENTATION;
396 #elif VISP_HAVE_OPENCV_VERSION >= 0x030000 397 int flags = cv::IMREAD_COLOR;
398 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100 399 int flags = CV_LOAD_IMAGE_COLOR;
402 cv::Mat cvI = cv::imread(final_filename, flags);
403 if (cvI.cols == 0 && cvI.rows == 0) {
404 std::string message =
"Cannot read file \"" + std::string(final_filename) +
"\": Image format not supported";
409 switch (getFormat(final_filename)) {
421 case FORMAT_JPEG2000:
424 std::string message =
"Cannot read file \"" + std::string(final_filename) +
"\": Image format not supported";
447 bool try_opencv_writer =
false;
449 switch (getFormat(filename)) {
457 #ifdef VISP_HAVE_JPEG 460 try_opencv_writer =
true;
467 try_opencv_writer =
true;
475 case FORMAT_JPEG2000:
477 try_opencv_writer =
true;
481 if (try_opencv_writer) {
482 #if VISP_HAVE_OPENCV_VERSION >= 0x020100 486 cv::imwrite(filename, cvI);
488 switch (getFormat(filename)) {
500 case FORMAT_JPEG2000:
503 vpCERROR <<
"Cannot write file: Image format not supported..." << std::endl;
526 bool try_opencv_writer =
false;
528 switch (getFormat(filename)) {
536 #ifdef VISP_HAVE_JPEG 539 try_opencv_writer =
true;
546 try_opencv_writer =
true;
554 case FORMAT_JPEG2000:
556 try_opencv_writer =
true;
560 if (try_opencv_writer) {
561 #if VISP_HAVE_OPENCV_VERSION >= 0x020100 565 cv::imwrite(filename, cvI);
567 switch (getFormat(filename)) {
579 case FORMAT_JPEG2000:
582 vpCERROR <<
"Cannot write file: Image format not supported..." << std::endl;
607 if (filename.empty()) {
611 fd = fopen(filename.c_str(),
"wb");
620 fprintf(fd,
"255\n");
626 ierr = fwrite(I.
bitmap,
sizeof(
float), nbyte, fd);
630 filename.c_str(), ierr, nbyte));
654 if (filename.empty()) {
658 fd = fopen(filename.c_str(),
"wb");
667 fprintf(fd,
"255\n");
673 ierr = fwrite(I.
bitmap,
sizeof(
unsigned char), nbyte, fd);
677 filename.c_str(), ierr, nbyte));
699 for (
unsigned int i = 0; i < nrows * ncols; i++)
719 if (filename.empty()) {
723 fd = fopen(filename.c_str(),
"wb");
732 fprintf(fd,
"255\n");
741 ierr = fwrite(Itmp.
bitmap,
sizeof(
unsigned char), nbyte, fd);
745 filename.c_str(), ierr, nbyte));
770 unsigned int w = 0, h = 0, maxval = 0;
771 unsigned int w_max = 100000, h_max = 100000, maxval_max = 255;
772 std::string magic(
"P8");
774 std::ifstream fd(filename.c_str(), std::ios::binary);
781 vp_decodeHeaderPNM(filename, fd, magic, w, h, maxval);
783 if (w > w_max || h > h_max) {
787 if (maxval > maxval_max) {
797 fd.read((
char *)I.
bitmap,
sizeof(
float) * nbyte);
824 unsigned int w = 0, h = 0, maxval = 0;
825 unsigned int w_max = 100000, h_max = 100000, maxval_max = 255;
826 std::string magic(
"P5");
828 std::ifstream fd(filename.c_str(), std::ios::binary);
835 vp_decodeHeaderPNM(filename, fd, magic, w, h, maxval);
837 if (w > w_max || h > h_max) {
841 if (maxval > maxval_max) {
851 fd.read((
char *)I.
bitmap, nbyte);
930 unsigned int w = 0, h = 0, maxval = 0;
931 unsigned int w_max = 100000, h_max = 100000, maxval_max = 255;
932 std::string magic(
"P6");
934 std::ifstream fd(filename.c_str(), std::ios::binary);
941 vp_decodeHeaderPNM(filename, fd, magic, w, h, maxval);
943 if (w > w_max || h > h_max) {
947 if (maxval > maxval_max) {
956 for (
unsigned int i = 0; i < I.
getHeight(); i++) {
957 for (
unsigned int j = 0; j < I.
getWidth(); j++) {
958 unsigned char rgb[3];
959 fd.read((
char *)&rgb, 3);
964 (i * I.
getWidth() + j) * 3 + fd.gcount(), I.
getSize() * 3, filename.c_str()));
1008 if (filename.empty()) {
1012 f = fopen(filename.c_str(),
"wb");
1020 fprintf(f,
"%d\n", 255);
1022 for (
unsigned int i = 0; i < I.
getHeight(); i++) {
1023 for (
unsigned int j = 0; j < I.
getWidth(); j++) {
1025 unsigned char rgb[3];
1030 size_t res = fwrite(&rgb, 1, 3, f);
1046 #if defined(VISP_HAVE_JPEG) 1057 struct jpeg_compress_struct cinfo;
1058 struct jpeg_error_mgr jerr;
1061 cinfo.err = jpeg_std_error(&jerr);
1062 jpeg_create_compress(&cinfo);
1065 if (filename.empty()) {
1069 file = fopen(filename.c_str(),
"wb");
1078 jpeg_stdio_dest(&cinfo, file);
1080 cinfo.image_width = width;
1081 cinfo.image_height = height;
1082 cinfo.input_components = 1;
1083 cinfo.in_color_space = JCS_GRAYSCALE;
1084 jpeg_set_defaults(&cinfo);
1086 jpeg_start_compress(&cinfo, TRUE);
1088 unsigned char *line;
1089 line =
new unsigned char[width];
1090 unsigned char *input = (
unsigned char *)I.
bitmap;
1091 while (cinfo.next_scanline < cinfo.image_height) {
1092 for (
unsigned int i = 0; i < width; i++) {
1096 jpeg_write_scanlines(&cinfo, &line, 1);
1099 jpeg_finish_compress(&cinfo);
1100 jpeg_destroy_compress(&cinfo);
1114 struct jpeg_compress_struct cinfo;
1115 struct jpeg_error_mgr jerr;
1118 cinfo.err = jpeg_std_error(&jerr);
1119 jpeg_create_compress(&cinfo);
1122 if (filename.empty()) {
1126 file = fopen(filename.c_str(),
"wb");
1135 jpeg_stdio_dest(&cinfo, file);
1137 cinfo.image_width = width;
1138 cinfo.image_height = height;
1139 cinfo.input_components = 3;
1140 cinfo.in_color_space = JCS_RGB;
1141 jpeg_set_defaults(&cinfo);
1143 jpeg_start_compress(&cinfo, TRUE);
1145 unsigned char *line;
1146 line =
new unsigned char[3 * width];
1147 unsigned char *input = (
unsigned char *)I.
bitmap;
1148 while (cinfo.next_scanline < cinfo.image_height) {
1149 for (
unsigned int i = 0; i < width; i++) {
1150 line[i * 3] = *(input);
1152 line[i * 3 + 1] = *(input);
1154 line[i * 3 + 2] = *(input);
1158 jpeg_write_scanlines(&cinfo, &line, 1);
1161 jpeg_finish_compress(&cinfo);
1162 jpeg_destroy_compress(&cinfo);
1185 struct jpeg_decompress_struct cinfo;
1186 struct jpeg_error_mgr jerr;
1189 cinfo.err = jpeg_std_error(&jerr);
1190 jpeg_create_decompress(&cinfo);
1193 if (filename.empty()) {
1197 file = fopen(filename.c_str(),
"rb");
1203 jpeg_stdio_src(&cinfo, file);
1204 jpeg_read_header(&cinfo, TRUE);
1206 unsigned int width = cinfo.image_width;
1207 unsigned int height = cinfo.image_height;
1212 jpeg_start_decompress(&cinfo);
1214 unsigned int rowbytes = cinfo.output_width * (
unsigned int)(cinfo.output_components);
1215 JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray)((j_common_ptr)&cinfo, JPOOL_IMAGE, rowbytes, 1);
1217 if (cinfo.out_color_space == JCS_RGB) {
1219 unsigned char *output = (
unsigned char *)Ic.
bitmap;
1220 while (cinfo.output_scanline < cinfo.output_height) {
1221 jpeg_read_scanlines(&cinfo, buffer, 1);
1222 for (
unsigned int i = 0; i < width; i++) {
1223 *(output++) = buffer[0][i * 3];
1224 *(output++) = buffer[0][i * 3 + 1];
1225 *(output++) = buffer[0][i * 3 + 2];
1232 else if (cinfo.out_color_space == JCS_GRAYSCALE) {
1233 while (cinfo.output_scanline < cinfo.output_height) {
1234 unsigned int row = cinfo.output_scanline;
1235 jpeg_read_scanlines(&cinfo, buffer, 1);
1236 memcpy(I[row], buffer[0], rowbytes);
1240 jpeg_finish_decompress(&cinfo);
1241 jpeg_destroy_decompress(&cinfo);
1265 struct jpeg_decompress_struct cinfo;
1266 struct jpeg_error_mgr jerr;
1269 cinfo.err = jpeg_std_error(&jerr);
1270 jpeg_create_decompress(&cinfo);
1273 if (filename.empty()) {
1277 file = fopen(filename.c_str(),
"rb");
1283 jpeg_stdio_src(&cinfo, file);
1285 jpeg_read_header(&cinfo, TRUE);
1287 unsigned int width = cinfo.image_width;
1288 unsigned int height = cinfo.image_height;
1293 jpeg_start_decompress(&cinfo);
1295 unsigned int rowbytes = cinfo.output_width * (
unsigned int)(cinfo.output_components);
1296 JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray)((j_common_ptr)&cinfo, JPOOL_IMAGE, rowbytes, 1);
1298 if (cinfo.out_color_space == JCS_RGB) {
1299 unsigned char *output = (
unsigned char *)I.
bitmap;
1300 while (cinfo.output_scanline < cinfo.output_height) {
1301 jpeg_read_scanlines(&cinfo, buffer, 1);
1302 for (
unsigned int i = 0; i < width; i++) {
1303 *(output++) = buffer[0][i * 3];
1304 *(output++) = buffer[0][i * 3 + 1];
1305 *(output++) = buffer[0][i * 3 + 2];
1311 else if (cinfo.out_color_space == JCS_GRAYSCALE) {
1314 while (cinfo.output_scanline < cinfo.output_height) {
1315 unsigned int row = cinfo.output_scanline;
1316 jpeg_read_scanlines(&cinfo, buffer, 1);
1317 memcpy(Ig[row], buffer[0], rowbytes);
1323 jpeg_finish_decompress(&cinfo);
1324 jpeg_destroy_decompress(&cinfo);
1328 #elif defined(VISP_HAVE_OPENCV) 1339 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408) 1342 cv::imwrite(filename.c_str(), Ip);
1344 IplImage *Ip = NULL;
1347 cvSaveImage(filename.c_str(), Ip);
1349 cvReleaseImage(&Ip);
1362 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408) 1365 cv::imwrite(filename.c_str(), Ip);
1367 IplImage *Ip = NULL;
1370 cvSaveImage(filename.c_str(), Ip);
1372 cvReleaseImage(&Ip);
1396 #if defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100 1397 #if VISP_HAVE_OPENCV_VERSION >= 0x030200 1398 int flags = cv::IMREAD_GRAYSCALE | cv::IMREAD_IGNORE_ORIENTATION;
1399 #elif VISP_HAVE_OPENCV_VERSION >= 0x030000 1400 int flags = cv::IMREAD_GRAYSCALE;
1401 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100 1402 int flags = CV_LOAD_IMAGE_GRAYSCALE;
1404 cv::Mat Ip = cv::imread(filename.c_str(), flags);
1410 IplImage *Ip = NULL;
1411 Ip = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_GRAYSCALE);
1416 cvReleaseImage(&Ip);
1442 #if defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100 1443 #if VISP_HAVE_OPENCV_VERSION >= 0x030200 1444 int flags = cv::IMREAD_GRAYSCALE | cv::IMREAD_IGNORE_ORIENTATION;
1445 #elif VISP_HAVE_OPENCV_VERSION >= 0x030000 1446 int flags = cv::IMREAD_GRAYSCALE;
1447 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100 1448 int flags = CV_LOAD_IMAGE_GRAYSCALE;
1450 cv::Mat Ip = cv::imread(filename.c_str(), flags);
1456 IplImage *Ip = NULL;
1457 Ip = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_COLOR);
1462 cvReleaseImage(&Ip);
1468 int width = 0, height = 0, channels = 0;
1469 unsigned char *image = stbi_load(filename.c_str(), &width, &height, &channels, STBI_grey);
1470 if (image == NULL) {
1473 I.
init(image, static_cast<unsigned int>(height), static_cast<unsigned int>(width),
true);
1474 stbi_image_free(image);
1478 int width = 0, height = 0, channels = 0;
1479 unsigned char *image = stbi_load(filename.c_str(), &width, &height, &channels, STBI_rgb_alpha);
1480 if (image == NULL) {
1483 I.
init(reinterpret_cast<vpRGBa*>(image), static_cast<unsigned int>(height), static_cast<unsigned int>(width),
true);
1484 stbi_image_free(image);
1488 int res = stbi_write_jpg(filename.c_str(),
static_cast<int>(I.
getWidth()), static_cast<int>(I.
getHeight()), STBI_grey,
1489 reinterpret_cast<void*>(I.
bitmap), 90);
1496 int res = stbi_write_jpg(filename.c_str(),
static_cast<int>(I.
getWidth()), static_cast<int>(I.
getHeight()), STBI_rgb_alpha,
1497 reinterpret_cast<void*>(I.
bitmap), 90);
1508 #if defined(VISP_HAVE_PNG) 1522 if (filename.empty()) {
1526 file = fopen(filename.c_str(),
"wb");
1533 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
1540 png_infop info_ptr = png_create_info_struct(png_ptr);
1543 png_destroy_write_struct(&png_ptr, NULL);
1550 if (setjmp(png_jmpbuf(png_ptr))) {
1552 png_destroy_write_struct(&png_ptr, &info_ptr);
1559 png_init_io(png_ptr, file);
1564 int color_type = PNG_COLOR_TYPE_GRAY;
1567 if (setjmp(png_jmpbuf(png_ptr))) {
1569 png_destroy_write_struct(&png_ptr, &info_ptr);
1574 png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth, color_type, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE,
1575 PNG_FILTER_TYPE_BASE);
1577 png_write_info(png_ptr, info_ptr);
1579 png_bytep *row_ptrs =
new png_bytep[height];
1580 for (
unsigned int i = 0; i < height; i++)
1581 row_ptrs[i] =
new png_byte[width];
1583 unsigned char *input = (
unsigned char *)I.
bitmap;
1585 for (
unsigned int i = 0; i < height; i++) {
1586 png_byte *row = row_ptrs[i];
1587 for (
unsigned int j = 0; j < width; j++) {
1593 png_write_image(png_ptr, row_ptrs);
1595 png_write_end(png_ptr, NULL);
1597 for (
unsigned int j = 0; j < height; j++)
1598 delete[] row_ptrs[j];
1602 png_destroy_write_struct(&png_ptr, &info_ptr);
1619 if (filename.empty()) {
1623 file = fopen(filename.c_str(),
"wb");
1630 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
1637 png_infop info_ptr = png_create_info_struct(png_ptr);
1640 png_destroy_write_struct(&png_ptr, NULL);
1647 if (setjmp(png_jmpbuf(png_ptr))) {
1649 png_destroy_write_struct(&png_ptr, &info_ptr);
1656 png_init_io(png_ptr, file);
1661 int color_type = PNG_COLOR_TYPE_RGB;
1664 if (setjmp(png_jmpbuf(png_ptr))) {
1666 png_destroy_write_struct(&png_ptr, &info_ptr);
1671 png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth, color_type, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE,
1672 PNG_FILTER_TYPE_BASE);
1674 png_write_info(png_ptr, info_ptr);
1676 png_bytep *row_ptrs =
new png_bytep[height];
1677 for (
unsigned int i = 0; i < height; i++)
1678 row_ptrs[i] =
new png_byte[3 * width];
1680 unsigned char *input = (
unsigned char *)I.
bitmap;
1683 for (
unsigned int i = 0; i < height; i++) {
1684 png_byte *row = row_ptrs[i];
1685 for (
unsigned int j = 0; j < width; j++) {
1686 row[3 * j] = *(input);
1688 row[3 * j + 1] = *(input);
1690 row[3 * j + 2] = *(input);
1696 png_write_image(png_ptr, row_ptrs);
1698 png_write_end(png_ptr, NULL);
1700 for (
unsigned int j = 0; j < height; j++)
1701 delete[] row_ptrs[j];
1705 png_destroy_write_struct(&png_ptr, &info_ptr);
1731 if (filename.empty()) {
1735 file = fopen(filename.c_str(),
"rb");
1742 if (fread(magic, 1,
sizeof(magic), file) !=
sizeof(magic)) {
1748 if (png_sig_cmp(magic, 0,
sizeof(magic))) {
1756 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
1757 if (png_ptr == NULL) {
1758 fprintf(stderr,
"error: can't create a png read structure!\n");
1764 png_infop info_ptr = png_create_info_struct(png_ptr);
1765 if (info_ptr == NULL) {
1766 fprintf(stderr,
"error: can't create a png info structure!\n");
1768 png_destroy_read_struct(&png_ptr, NULL, NULL);
1774 if (setjmp(png_jmpbuf(png_ptr))) {
1776 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
1783 png_init_io(png_ptr, file);
1786 png_set_sig_bytes(png_ptr,
sizeof(magic));
1789 png_read_info(png_ptr, info_ptr);
1791 unsigned int width = png_get_image_width(png_ptr, info_ptr);
1792 unsigned int height = png_get_image_height(png_ptr, info_ptr);
1794 unsigned int bit_depth, channels, color_type;
1796 bit_depth = png_get_bit_depth(png_ptr, info_ptr);
1797 channels = png_get_channels(png_ptr, info_ptr);
1798 color_type = png_get_color_type(png_ptr, info_ptr);
1801 if (color_type == PNG_COLOR_TYPE_PALETTE)
1802 png_set_palette_to_rgb(png_ptr);
1805 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
1806 png_set_expand(png_ptr);
1811 if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
1812 png_set_strip_alpha(png_ptr);
1814 if (bit_depth == 16)
1815 png_set_strip_16(png_ptr);
1816 else if (bit_depth < 8)
1817 png_set_packing(png_ptr);
1820 png_read_update_info(png_ptr, info_ptr);
1822 channels = png_get_channels(png_ptr, info_ptr);
1827 png_bytep *rowPtrs =
new png_bytep[height];
1829 unsigned int stride = png_get_rowbytes(png_ptr, info_ptr);
1830 unsigned char *data =
new unsigned char[stride * height];
1832 for (
unsigned int i = 0; i < height; i++)
1833 rowPtrs[i] = (png_bytep)data + (i * stride);
1835 png_read_image(png_ptr, rowPtrs);
1838 unsigned char *output;
1842 output = (
unsigned char *)I.
bitmap;
1843 for (
unsigned int i = 0; i < width * height; i++) {
1844 *(output++) = data[i];
1849 output = (
unsigned char *)I.
bitmap;
1850 for (
unsigned int i = 0; i < width * height; i++) {
1851 *(output++) = data[i * 2];
1856 output = (
unsigned char *)Ic.
bitmap;
1857 for (
unsigned int i = 0; i < width * height; i++) {
1858 *(output++) = data[i * 3];
1859 *(output++) = data[i * 3 + 1];
1860 *(output++) = data[i * 3 + 2];
1867 output = (
unsigned char *)Ic.
bitmap;
1868 for (
unsigned int i = 0; i < width * height; i++) {
1869 *(output++) = data[i * 4];
1870 *(output++) = data[i * 4 + 1];
1871 *(output++) = data[i * 4 + 2];
1872 *(output++) = data[i * 4 + 3];
1878 delete[](png_bytep) rowPtrs;
1880 png_read_end(png_ptr, NULL);
1881 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
1909 if (filename.empty()) {
1913 file = fopen(filename.c_str(),
"rb");
1920 if (fread(magic, 1,
sizeof(magic), file) !=
sizeof(magic)) {
1926 if (png_sig_cmp(magic, 0,
sizeof(magic))) {
1933 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
1941 png_infop info_ptr = png_create_info_struct(png_ptr);
1944 png_destroy_read_struct(&png_ptr, NULL, NULL);
1951 if (setjmp(png_jmpbuf(png_ptr))) {
1953 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
1960 png_init_io(png_ptr, file);
1963 png_set_sig_bytes(png_ptr,
sizeof(magic));
1966 png_read_info(png_ptr, info_ptr);
1968 unsigned int width = png_get_image_width(png_ptr, info_ptr);
1969 unsigned int height = png_get_image_height(png_ptr, info_ptr);
1971 unsigned int bit_depth, channels, color_type;
1973 bit_depth = png_get_bit_depth(png_ptr, info_ptr);
1974 channels = png_get_channels(png_ptr, info_ptr);
1975 color_type = png_get_color_type(png_ptr, info_ptr);
1978 if (color_type == PNG_COLOR_TYPE_PALETTE)
1979 png_set_palette_to_rgb(png_ptr);
1982 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
1983 png_set_expand(png_ptr);
1988 if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
1989 png_set_strip_alpha(png_ptr);
1991 if (bit_depth == 16)
1992 png_set_strip_16(png_ptr);
1993 else if (bit_depth < 8)
1994 png_set_packing(png_ptr);
1997 png_read_update_info(png_ptr, info_ptr);
1999 channels = png_get_channels(png_ptr, info_ptr);
2004 png_bytep *rowPtrs =
new png_bytep[height];
2006 unsigned int stride = png_get_rowbytes(png_ptr, info_ptr);
2007 unsigned char *data =
new unsigned char[stride * height];
2009 for (
unsigned int i = 0; i < height; i++)
2010 rowPtrs[i] = (png_bytep)data + (i * stride);
2012 png_read_image(png_ptr, rowPtrs);
2015 unsigned char *output;
2019 output = (
unsigned char *)Ig.
bitmap;
2020 for (
unsigned int i = 0; i < width * height; i++) {
2021 *(output++) = data[i];
2027 output = (
unsigned char *)Ig.
bitmap;
2028 for (
unsigned int i = 0; i < width * height; i++) {
2029 *(output++) = data[i * 2];
2035 output = (
unsigned char *)I.
bitmap;
2036 for (
unsigned int i = 0; i < width * height; i++) {
2037 *(output++) = data[i * 3];
2038 *(output++) = data[i * 3 + 1];
2039 *(output++) = data[i * 3 + 2];
2045 output = (
unsigned char *)I.
bitmap;
2046 for (
unsigned int i = 0; i < width * height; i++) {
2047 *(output++) = data[i * 4];
2048 *(output++) = data[i * 4 + 1];
2049 *(output++) = data[i * 4 + 2];
2050 *(output++) = data[i * 4 + 3];
2055 delete[](png_bytep) rowPtrs;
2057 png_read_end(png_ptr, NULL);
2058 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
2062 #elif defined(VISP_HAVE_OPENCV) 2073 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408) 2076 cv::imwrite(filename.c_str(), Ip);
2078 IplImage *Ip = NULL;
2081 cvSaveImage(filename.c_str(), Ip);
2083 cvReleaseImage(&Ip);
2096 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408) 2099 cv::imwrite(filename.c_str(), Ip);
2101 IplImage *Ip = NULL;
2104 cvSaveImage(filename.c_str(), Ip);
2106 cvReleaseImage(&Ip);
2130 #if defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100 2131 #if VISP_HAVE_OPENCV_VERSION >= 0x030200 2132 int flags = cv::IMREAD_GRAYSCALE | cv::IMREAD_IGNORE_ORIENTATION;
2133 #elif VISP_HAVE_OPENCV_VERSION >= 0x030000 2134 int flags = cv::IMREAD_GRAYSCALE;
2135 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100 2136 int flags = CV_LOAD_IMAGE_GRAYSCALE;
2138 cv::Mat Ip = cv::imread(filename.c_str(), flags);
2144 IplImage *Ip = NULL;
2145 Ip = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_GRAYSCALE);
2150 cvReleaseImage(&Ip);
2176 #if defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100 2177 #if VISP_HAVE_OPENCV_VERSION >= 0x030200 2178 int flags = cv::IMREAD_COLOR | cv::IMREAD_IGNORE_ORIENTATION;
2179 #elif VISP_HAVE_OPENCV_VERSION >= 0x030000 2180 int flags = cv::IMREAD_COLOR;
2181 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100 2182 int flags = CV_LOAD_IMAGE_COLOR;
2184 cv::Mat Ip = cv::imread(filename.c_str(), flags);
2190 IplImage *Ip = NULL;
2191 Ip = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_COLOR);
2196 cvReleaseImage(&Ip);
2202 int width = 0, height = 0, channels = 0;
2203 unsigned char *image = stbi_load(filename.c_str(), &width, &height, &channels, STBI_grey);
2204 if (image == NULL) {
2207 I.
init(image, static_cast<unsigned int>(height), static_cast<unsigned int>(width),
true);
2208 stbi_image_free(image);
2212 int width = 0, height = 0, channels = 0;
2213 unsigned char *image = stbi_load(filename.c_str(), &width, &height, &channels, STBI_rgb_alpha);
2214 if (image == NULL) {
2217 I.
init(reinterpret_cast<vpRGBa*>(image), static_cast<unsigned int>(height), static_cast<unsigned int>(width),
true);
2218 stbi_image_free(image);
2222 const int stride_in_bytes =
static_cast<int>(I.
getWidth());
2223 int res = stbi_write_png(filename.c_str(),
static_cast<int>(I.
getWidth()), static_cast<int>(I.
getHeight()), STBI_grey,
2224 reinterpret_cast<void*>(I.
bitmap), stride_in_bytes);
2231 const int stride_in_bytes =
static_cast<int>(4 * I.
getWidth());
2232 int res = stbi_write_png(filename.c_str(),
static_cast<int>(I.
getWidth()), static_cast<int>(I.
getHeight()), STBI_rgb_alpha,
2233 reinterpret_cast<void*>(I.
bitmap), stride_in_bytes);
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)
void resize(unsigned int h, unsigned int w)
resize the image : Image initialization
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 init(unsigned int height, unsigned int width)
Set the size of the image.
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)