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);
247 std::string message =
"Cannot read file: \"" + std::string(filename) +
"\" doesn't exist";
254 bool try_opencv_reader =
false;
256 switch (getFormat(final_filename)) {
264 #ifdef VISP_HAVE_JPEG 267 try_opencv_reader =
true;
271 #if defined(VISP_HAVE_PNG) 274 try_opencv_reader =
true;
282 case FORMAT_JPEG2000:
284 try_opencv_reader =
true;
288 if (try_opencv_reader) {
289 #if VISP_HAVE_OPENCV_VERSION >= 0x030000 291 cv::Mat cvI = cv::imread(final_filename, cv::IMREAD_GRAYSCALE);
292 if (cvI.cols == 0 && cvI.rows == 0) {
293 std::string message =
"Cannot read file \"" + std::string(final_filename) +
"\": Image format not supported";
297 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100 299 cv::Mat cvI = cv::imread(final_filename, CV_LOAD_IMAGE_GRAYSCALE);
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";
351 std::string message =
"Cannot read file: \"" + std::string(filename) +
"\" doesn't exist";
357 bool try_opencv_reader =
false;
359 switch (getFormat(final_filename)) {
367 #ifdef VISP_HAVE_JPEG 370 try_opencv_reader =
true;
374 #if defined(VISP_HAVE_PNG) 377 try_opencv_reader =
true;
385 case FORMAT_JPEG2000:
387 try_opencv_reader =
true;
391 if (try_opencv_reader) {
392 #if VISP_HAVE_OPENCV_VERSION >= 0x030000 394 cv::Mat cvI = cv::imread(final_filename, cv::IMREAD_COLOR);
395 if (cvI.cols == 0 && cvI.rows == 0) {
396 std::string message =
"Cannot read file \"" + std::string(final_filename) +
"\": Image format not supported";
400 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100 402 cv::Mat cvI = cv::imread(final_filename, CV_LOAD_IMAGE_COLOR);
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);
1394 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000) 1395 cv::Mat Ip = cv::imread(filename.c_str(), cv::IMREAD_GRAYSCALE);
1400 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020408) 1401 cv::Mat Ip = cv::imread(filename.c_str(), CV_LOAD_IMAGE_GRAYSCALE);
1407 IplImage *Ip = NULL;
1408 Ip = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_GRAYSCALE);
1413 cvReleaseImage(&Ip);
1437 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000) 1438 cv::Mat Ip = cv::imread(filename.c_str(), cv::IMREAD_GRAYSCALE);
1443 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020408) 1444 cv::Mat Ip = cv::imread(filename.c_str(), CV_LOAD_IMAGE_GRAYSCALE);
1450 IplImage *Ip = NULL;
1451 Ip = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_COLOR);
1456 cvReleaseImage(&Ip);
1462 int width = 0, height = 0, channels = 0;
1463 unsigned char *image = stbi_load(filename.c_str(), &width, &height, &channels, STBI_grey);
1464 if (image == NULL) {
1467 I.
init(image, static_cast<unsigned int>(height), static_cast<unsigned int>(width),
true);
1468 stbi_image_free(image);
1472 int width = 0, height = 0, channels = 0;
1473 unsigned char *image = stbi_load(filename.c_str(), &width, &height, &channels, STBI_rgb_alpha);
1474 if (image == NULL) {
1477 I.
init(reinterpret_cast<vpRGBa*>(image), static_cast<unsigned int>(height), static_cast<unsigned int>(width),
true);
1478 stbi_image_free(image);
1482 int res = stbi_write_jpg(filename.c_str(),
static_cast<int>(I.
getWidth()), static_cast<int>(I.
getHeight()), STBI_grey,
1483 reinterpret_cast<void*>(I.
bitmap), 90);
1490 int res = stbi_write_jpg(filename.c_str(),
static_cast<int>(I.
getWidth()), static_cast<int>(I.
getHeight()), STBI_rgb_alpha,
1491 reinterpret_cast<void*>(I.
bitmap), 90);
1502 #if defined(VISP_HAVE_PNG) 1516 if (filename.empty()) {
1520 file = fopen(filename.c_str(),
"wb");
1527 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
1534 png_infop info_ptr = png_create_info_struct(png_ptr);
1537 png_destroy_write_struct(&png_ptr, NULL);
1544 if (setjmp(png_jmpbuf(png_ptr))) {
1546 png_destroy_write_struct(&png_ptr, &info_ptr);
1553 png_init_io(png_ptr, file);
1558 int color_type = PNG_COLOR_TYPE_GRAY;
1561 if (setjmp(png_jmpbuf(png_ptr))) {
1563 png_destroy_write_struct(&png_ptr, &info_ptr);
1568 png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth, color_type, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE,
1569 PNG_FILTER_TYPE_BASE);
1571 png_write_info(png_ptr, info_ptr);
1573 png_bytep *row_ptrs =
new png_bytep[height];
1574 for (
unsigned int i = 0; i < height; i++)
1575 row_ptrs[i] =
new png_byte[width];
1577 unsigned char *input = (
unsigned char *)I.
bitmap;
1579 for (
unsigned int i = 0; i < height; i++) {
1580 png_byte *row = row_ptrs[i];
1581 for (
unsigned int j = 0; j < width; j++) {
1587 png_write_image(png_ptr, row_ptrs);
1589 png_write_end(png_ptr, NULL);
1591 for (
unsigned int j = 0; j < height; j++)
1592 delete[] row_ptrs[j];
1596 png_destroy_write_struct(&png_ptr, &info_ptr);
1613 if (filename.empty()) {
1617 file = fopen(filename.c_str(),
"wb");
1624 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
1631 png_infop info_ptr = png_create_info_struct(png_ptr);
1634 png_destroy_write_struct(&png_ptr, NULL);
1641 if (setjmp(png_jmpbuf(png_ptr))) {
1643 png_destroy_write_struct(&png_ptr, &info_ptr);
1650 png_init_io(png_ptr, file);
1655 int color_type = PNG_COLOR_TYPE_RGB;
1658 if (setjmp(png_jmpbuf(png_ptr))) {
1660 png_destroy_write_struct(&png_ptr, &info_ptr);
1665 png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth, color_type, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE,
1666 PNG_FILTER_TYPE_BASE);
1668 png_write_info(png_ptr, info_ptr);
1670 png_bytep *row_ptrs =
new png_bytep[height];
1671 for (
unsigned int i = 0; i < height; i++)
1672 row_ptrs[i] =
new png_byte[3 * width];
1674 unsigned char *input = (
unsigned char *)I.
bitmap;
1677 for (
unsigned int i = 0; i < height; i++) {
1678 png_byte *row = row_ptrs[i];
1679 for (
unsigned int j = 0; j < width; j++) {
1680 row[3 * j] = *(input);
1682 row[3 * j + 1] = *(input);
1684 row[3 * j + 2] = *(input);
1690 png_write_image(png_ptr, row_ptrs);
1692 png_write_end(png_ptr, NULL);
1694 for (
unsigned int j = 0; j < height; j++)
1695 delete[] row_ptrs[j];
1699 png_destroy_write_struct(&png_ptr, &info_ptr);
1725 if (filename.empty()) {
1729 file = fopen(filename.c_str(),
"rb");
1736 if (fread(magic, 1,
sizeof(magic), file) !=
sizeof(magic)) {
1742 if (png_sig_cmp(magic, 0,
sizeof(magic))) {
1750 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
1751 if (png_ptr == NULL) {
1752 fprintf(stderr,
"error: can't create a png read structure!\n");
1758 png_infop info_ptr = png_create_info_struct(png_ptr);
1759 if (info_ptr == NULL) {
1760 fprintf(stderr,
"error: can't create a png info structure!\n");
1762 png_destroy_read_struct(&png_ptr, NULL, NULL);
1768 if (setjmp(png_jmpbuf(png_ptr))) {
1770 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
1777 png_init_io(png_ptr, file);
1780 png_set_sig_bytes(png_ptr,
sizeof(magic));
1783 png_read_info(png_ptr, info_ptr);
1785 unsigned int width = png_get_image_width(png_ptr, info_ptr);
1786 unsigned int height = png_get_image_height(png_ptr, info_ptr);
1788 unsigned int bit_depth, channels, color_type;
1790 bit_depth = png_get_bit_depth(png_ptr, info_ptr);
1791 channels = png_get_channels(png_ptr, info_ptr);
1792 color_type = png_get_color_type(png_ptr, info_ptr);
1795 if (color_type == PNG_COLOR_TYPE_PALETTE)
1796 png_set_palette_to_rgb(png_ptr);
1799 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
1800 png_set_expand(png_ptr);
1805 if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
1806 png_set_strip_alpha(png_ptr);
1808 if (bit_depth == 16)
1809 png_set_strip_16(png_ptr);
1810 else if (bit_depth < 8)
1811 png_set_packing(png_ptr);
1814 png_read_update_info(png_ptr, info_ptr);
1816 channels = png_get_channels(png_ptr, info_ptr);
1821 png_bytep *rowPtrs =
new png_bytep[height];
1823 unsigned int stride = png_get_rowbytes(png_ptr, info_ptr);
1824 unsigned char *data =
new unsigned char[stride * height];
1826 for (
unsigned int i = 0; i < height; i++)
1827 rowPtrs[i] = (png_bytep)data + (i * stride);
1829 png_read_image(png_ptr, rowPtrs);
1832 unsigned char *output;
1836 output = (
unsigned char *)I.
bitmap;
1837 for (
unsigned int i = 0; i < width * height; i++) {
1838 *(output++) = data[i];
1843 output = (
unsigned char *)I.
bitmap;
1844 for (
unsigned int i = 0; i < width * height; i++) {
1845 *(output++) = data[i * 2];
1850 output = (
unsigned char *)Ic.
bitmap;
1851 for (
unsigned int i = 0; i < width * height; i++) {
1852 *(output++) = data[i * 3];
1853 *(output++) = data[i * 3 + 1];
1854 *(output++) = data[i * 3 + 2];
1861 output = (
unsigned char *)Ic.
bitmap;
1862 for (
unsigned int i = 0; i < width * height; i++) {
1863 *(output++) = data[i * 4];
1864 *(output++) = data[i * 4 + 1];
1865 *(output++) = data[i * 4 + 2];
1866 *(output++) = data[i * 4 + 3];
1872 delete[](png_bytep) rowPtrs;
1874 png_read_end(png_ptr, NULL);
1875 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
1903 if (filename.empty()) {
1907 file = fopen(filename.c_str(),
"rb");
1914 if (fread(magic, 1,
sizeof(magic), file) !=
sizeof(magic)) {
1920 if (png_sig_cmp(magic, 0,
sizeof(magic))) {
1927 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
1935 png_infop info_ptr = png_create_info_struct(png_ptr);
1938 png_destroy_read_struct(&png_ptr, NULL, NULL);
1945 if (setjmp(png_jmpbuf(png_ptr))) {
1947 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
1954 png_init_io(png_ptr, file);
1957 png_set_sig_bytes(png_ptr,
sizeof(magic));
1960 png_read_info(png_ptr, info_ptr);
1962 unsigned int width = png_get_image_width(png_ptr, info_ptr);
1963 unsigned int height = png_get_image_height(png_ptr, info_ptr);
1965 unsigned int bit_depth, channels, color_type;
1967 bit_depth = png_get_bit_depth(png_ptr, info_ptr);
1968 channels = png_get_channels(png_ptr, info_ptr);
1969 color_type = png_get_color_type(png_ptr, info_ptr);
1972 if (color_type == PNG_COLOR_TYPE_PALETTE)
1973 png_set_palette_to_rgb(png_ptr);
1976 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
1977 png_set_expand(png_ptr);
1982 if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
1983 png_set_strip_alpha(png_ptr);
1985 if (bit_depth == 16)
1986 png_set_strip_16(png_ptr);
1987 else if (bit_depth < 8)
1988 png_set_packing(png_ptr);
1991 png_read_update_info(png_ptr, info_ptr);
1993 channels = png_get_channels(png_ptr, info_ptr);
1998 png_bytep *rowPtrs =
new png_bytep[height];
2000 unsigned int stride = png_get_rowbytes(png_ptr, info_ptr);
2001 unsigned char *data =
new unsigned char[stride * height];
2003 for (
unsigned int i = 0; i < height; i++)
2004 rowPtrs[i] = (png_bytep)data + (i * stride);
2006 png_read_image(png_ptr, rowPtrs);
2009 unsigned char *output;
2013 output = (
unsigned char *)Ig.
bitmap;
2014 for (
unsigned int i = 0; i < width * height; i++) {
2015 *(output++) = data[i];
2021 output = (
unsigned char *)Ig.
bitmap;
2022 for (
unsigned int i = 0; i < width * height; i++) {
2023 *(output++) = data[i * 2];
2029 output = (
unsigned char *)I.
bitmap;
2030 for (
unsigned int i = 0; i < width * height; i++) {
2031 *(output++) = data[i * 3];
2032 *(output++) = data[i * 3 + 1];
2033 *(output++) = data[i * 3 + 2];
2039 output = (
unsigned char *)I.
bitmap;
2040 for (
unsigned int i = 0; i < width * height; i++) {
2041 *(output++) = data[i * 4];
2042 *(output++) = data[i * 4 + 1];
2043 *(output++) = data[i * 4 + 2];
2044 *(output++) = data[i * 4 + 3];
2049 delete[](png_bytep) rowPtrs;
2051 png_read_end(png_ptr, NULL);
2052 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
2056 #elif defined(VISP_HAVE_OPENCV) 2067 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408) 2070 cv::imwrite(filename.c_str(), Ip);
2072 IplImage *Ip = NULL;
2075 cvSaveImage(filename.c_str(), Ip);
2077 cvReleaseImage(&Ip);
2090 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408) 2093 cv::imwrite(filename.c_str(), Ip);
2095 IplImage *Ip = NULL;
2098 cvSaveImage(filename.c_str(), Ip);
2100 cvReleaseImage(&Ip);
2122 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000) 2123 cv::Mat Ip = cv::imread(filename.c_str(), cv::IMREAD_GRAYSCALE);
2128 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020408) 2129 cv::Mat Ip = cv::imread(filename.c_str(), CV_LOAD_IMAGE_GRAYSCALE);
2135 IplImage *Ip = NULL;
2136 Ip = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_GRAYSCALE);
2141 cvReleaseImage(&Ip);
2165 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000) 2166 cv::Mat Ip = cv::imread(filename.c_str(), cv::IMREAD_GRAYSCALE);
2171 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020408) 2172 cv::Mat Ip = cv::imread(filename.c_str(), CV_LOAD_IMAGE_GRAYSCALE);
2178 IplImage *Ip = NULL;
2179 Ip = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_COLOR);
2184 cvReleaseImage(&Ip);
2190 int width = 0, height = 0, channels = 0;
2191 unsigned char *image = stbi_load(filename.c_str(), &width, &height, &channels, STBI_grey);
2192 if (image == NULL) {
2195 I.
init(image, static_cast<unsigned int>(height), static_cast<unsigned int>(width),
true);
2196 stbi_image_free(image);
2200 int width = 0, height = 0, channels = 0;
2201 unsigned char *image = stbi_load(filename.c_str(), &width, &height, &channels, STBI_rgb_alpha);
2202 if (image == NULL) {
2205 I.
init(reinterpret_cast<vpRGBa*>(image), static_cast<unsigned int>(height), static_cast<unsigned int>(width),
true);
2206 stbi_image_free(image);
2210 const int stride_in_bytes =
static_cast<int>(I.
getWidth());
2211 int res = stbi_write_png(filename.c_str(),
static_cast<int>(I.
getWidth()), static_cast<int>(I.
getHeight()), STBI_grey,
2212 reinterpret_cast<void*>(I.
bitmap), stride_in_bytes);
2219 const int stride_in_bytes =
static_cast<int>(4 * I.
getWidth());
2220 int res = stbi_write_png(filename.c_str(),
static_cast<int>(I.
getWidth()), static_cast<int>(I.
getHeight()), STBI_rgb_alpha,
2221 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.
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
void init(unsigned int height, unsigned int width)
Set the size of the image.
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)
static void writePFM(const vpImage< float > &I, const std::string &filename)
static void readPFM(vpImage< float > &I, const std::string &filename)
unsigned int getHeight() const
static void read(vpImage< unsigned char > &I, const std::string &filename)
unsigned int getSize() const
static void readPNG(vpImage< unsigned char > &I, const std::string &filename)
unsigned char R
Red component.
static void readPGM(vpImage< unsigned char > &I, const std::string &filename)
unsigned int getWidth() const
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)