39 #include "vpImageIoBackend.h"
40 #include <visp3/core/vpImageConvert.h>
41 #include <visp3/core/vpIoTools.h>
42 #include <visp3/core/vpEndian.h>
44 #ifndef DOXYGEN_SHOULD_SKIP_THIS
54 void vp_decodeHeaderPNM(
const std::string &filename, std::ifstream &fd,
const std::string &magic,
unsigned int &w,
55 unsigned int &h,
unsigned int &maxval)
58 unsigned int nb_elt = 4, cpt_elt = 0;
59 while (cpt_elt != nb_elt) {
61 while (std::getline(fd, line) && (line.compare(0, 1,
"#") == 0 || line.size() == 0)) {
71 if (header.size() == 0) {
77 if (header[0].compare(0, magic.size(), magic) != 0) {
80 filename.c_str(), magic.c_str()));
83 header.erase(header.begin(),
86 while (header.size()) {
88 std::istringstream ss(header[0]);
91 header.erase(header.begin(),
94 else if (cpt_elt == 2) {
95 std::istringstream ss(header[0]);
98 header.erase(header.begin(),
101 else if (cpt_elt == 3) {
102 std::istringstream ss(header[0]);
105 header.erase(header.begin(),
112 void vp_decodeHeaderPFM(
const std::string &filename, std::ifstream &fd, std::string &magic,
unsigned int &w,
113 unsigned int &h,
double &scale,
bool &littleEndian)
116 const unsigned int nb_elt = 4;
117 unsigned int cpt_elt = 0;
118 while (cpt_elt != nb_elt) {
120 while (std::getline(fd, line) && (line.compare(0, 1,
"#") == 0 || line.size() == 0)) {
130 if (header.empty()) {
137 if (magic !=
"PF" && magic !=
"Pf") {
140 "\"%s\" is not a PFM file with PF (RGB) or Pf (gray) magic number", filename.c_str()));
143 header.erase(header.begin(),
146 while (header.size()) {
148 std::istringstream ss(header[0]);
151 header.erase(header.begin(),
154 else if (cpt_elt == 2) {
155 std::istringstream ss(header[0]);
158 header.erase(header.begin(),
161 else if (cpt_elt == 3) {
162 std::istringstream ss(header[0]);
164 littleEndian = scale < 0;
166 header.erase(header.begin(),
186 void vp_writePFM(
const vpImage<float> &I,
const std::string &filename)
191 if (filename.empty()) {
195 fd = fopen(filename.c_str(),
"wb");
204 fprintf(fd,
"255\n");
210 ierr = fwrite(I.
bitmap,
sizeof(
float), nbyte, fd);
214 filename.c_str(), ierr, nbyte));
221 void vp_writePFM_HDR(
const vpImage<float> &I,
const std::string &filename)
224 if (filename.empty()) {
228 FILE *fd = fopen(filename.c_str(),
"wb");
236 #ifdef VISP_LITTLE_ENDIAN
237 fprintf(fd,
"%f\n", -1.0f);
239 fprintf(fd,
"%f\n", 1.0f);
244 for (
int i =
static_cast<int>(I.
getHeight()) - 1; i >= 0; i--) {
245 size_t ierr = fwrite(I[i],
sizeof(
float), nbyte, fd);
249 filename.c_str(), ierr, nbyte));
257 void vp_writePFM_HDR(
const vpImage<vpRGBf> &I,
const std::string &filename)
260 if (filename.empty()) {
264 FILE *fd = fopen(filename.c_str(),
"wb");
272 #ifdef VISP_LITTLE_ENDIAN
273 fprintf(fd,
"%f\n", -1.0f);
275 fprintf(fd,
"%f\n", 1.0f);
280 for (
int i =
static_cast<int>(I.
getHeight()) - 1; i >= 0; i--) {
281 size_t ierr = fwrite(I[i],
sizeof(
float), nbyte, fd);
285 filename.c_str(), ierr, nbyte));
309 if (filename.empty()) {
313 fd = fopen(filename.c_str(),
"wb");
322 fprintf(fd,
"255\n");
328 ierr = fwrite(I.
bitmap,
sizeof(
unsigned char), nbyte, fd);
332 filename.c_str(), ierr, nbyte));
346 void vp_writePGM(
const vpImage<short> &I,
const std::string &filename)
354 for (
unsigned int i = 0; i < nrows * ncols; ++i)
357 vp_writePGM(Iuc, filename);
368 void vp_writePGM(
const vpImage<vpRGBa> &I,
const std::string &filename)
374 if (filename.empty()) {
378 fd = fopen(filename.c_str(),
"wb");
387 fprintf(fd,
"255\n");
396 ierr = fwrite(Itmp.
bitmap,
sizeof(
unsigned char), nbyte, fd);
400 filename.c_str(), ierr, nbyte));
423 unsigned int w = 0, h = 0, maxval = 0;
424 const unsigned int w_max = 100000, h_max = 100000, maxval_max = 255;
425 const std::string magic(
"P8");
427 std::ifstream fd(filename.c_str(), std::ios::binary);
434 vp_decodeHeaderPNM(filename, fd, magic, w, h, maxval);
436 if (w > w_max || h > h_max) {
440 if (maxval > maxval_max) {
450 fd.read((
char *)I.
bitmap,
sizeof(
float) * nbyte);
475 void vp_readPFM_HDR(
vpImage<float> &I,
const std::string &filename)
477 std::ifstream fd(filename.c_str(), std::ios::binary);
484 const unsigned int w_max = 100000, h_max = 100000;
485 const std::string magicRGB(
"PF"), magicGray(
"Pf");
487 unsigned int w = 0, h = 0;
489 bool littleEndian =
true;
490 vp_decodeHeaderPFM(filename, fd, magic, w, h, scale, littleEndian);
492 if (w > w_max || h > h_max) {
497 unsigned int channels = (magic == magicRGB) ? 3 : 1;
499 I.
resize(h, channels * w);
502 #ifdef VISP_LITTLE_ENDIAN
503 bool swapEndianness = !littleEndian;
505 bool swapEndianness = littleEndian;
507 for (
int i = I.
getHeight() - 1; i >= 0; i--) {
508 fd.read((
char *)I[i],
sizeof(float) * w * channels);
509 if (swapEndianness) {
510 for (
unsigned int j = 0; j < w * channels; ++j) {
523 if (std::fabs(scale) > 0.0f) {
524 for (
unsigned int i = 0; i < I.
getHeight(); ++i) {
525 for (
unsigned int j = 0; j < I.
getWidth(); ++j) {
526 I[i][j] *= 1.0f /
static_cast<float>(std::fabs(scale));
549 std::ifstream fd(filename.c_str(), std::ios::binary);
556 const unsigned int w_max = 100000, h_max = 100000;
557 const std::string magicRGB(
"PF"), magicGray(
"Pf");
559 unsigned int w = 0, h = 0;
561 bool littleEndian =
true;
562 vp_decodeHeaderPFM(filename, fd, magic, w, h, scale, littleEndian);
564 if (w > w_max || h > h_max) {
569 unsigned int channels = (magic == magicRGB) ? 3 : 1;
570 if (magic != magicRGB) {
577 #ifdef VISP_LITTLE_ENDIAN
578 bool swapEndianness = !littleEndian;
580 bool swapEndianness = littleEndian;
582 for (
int i = I.
getHeight() - 1; i >= 0; i--) {
583 fd.read((
char *)I[i],
sizeof(float) * w * channels);
584 if (swapEndianness) {
585 for (
unsigned int j = 0; j < w; ++j) {
600 if (std::fabs(scale) > 0.0f) {
601 for (
unsigned int i = 0; i < I.
getHeight(); ++i) {
602 for (
unsigned int j = 0; j < I.
getWidth(); ++j) {
603 I[i][j].R *= 1.0f /
static_cast<float>(std::fabs(scale));
604 I[i][j].G *= 1.0f /
static_cast<float>(std::fabs(scale));
605 I[i][j].B *= 1.0f /
static_cast<float>(std::fabs(scale));
627 unsigned int w = 0, h = 0, maxval = 0;
628 unsigned int w_max = 100000, h_max = 100000, maxval_max = 255;
629 std::string magic(
"P5");
631 std::ifstream fd(filename.c_str(), std::ios::binary);
638 vp_decodeHeaderPNM(filename, fd, magic, w, h, maxval);
640 if (w > w_max || h > h_max) {
644 if (maxval > maxval_max) {
654 fd.read((
char *)I.
bitmap, nbyte);
685 vp_readPGM(Itmp, filename);
713 vp_readPPM(Itmp, filename);
731 unsigned int w = 0, h = 0, maxval = 0;
732 unsigned int w_max = 100000, h_max = 100000, maxval_max = 255;
733 std::string magic(
"P6");
735 std::ifstream fd(filename.c_str(), std::ios::binary);
742 vp_decodeHeaderPNM(filename, fd, magic, w, h, maxval);
744 if (w > w_max || h > h_max) {
748 if (maxval > maxval_max) {
757 for (
unsigned int i = 0; i < I.
getHeight(); ++i) {
758 for (
unsigned int j = 0; j < I.
getWidth(); ++j) {
759 unsigned char rgb[3];
760 fd.read((
char *)&rgb, 3);
765 (i * I.
getWidth() + j) * 3 + fd.gcount(), I.
getSize() * 3, filename.c_str()));
792 vp_writePPM(Itmp, filename);
802 void vp_writePPM(
const vpImage<vpRGBa> &I,
const std::string &filename)
807 if (filename.empty()) {
811 f = fopen(filename.c_str(),
"wb");
819 fprintf(f,
"%d\n", 255);
821 for (
unsigned int i = 0; i < I.
getHeight(); ++i) {
822 for (
unsigned int j = 0; j < I.
getWidth(); ++j) {
824 unsigned char rgb[3];
829 size_t res = fwrite(&rgb, 1, 3, f);
error that can be emitted by ViSP classes.
@ badValue
Used to indicate that a value is not in the allowed range.
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
Error that can be emitted by the vpImage class and its derivatives.
unsigned int getWidth() const
void resize(unsigned int h, unsigned int w)
resize the image : Image initialization
unsigned int getSize() const
Type * bitmap
points toward the bitmap
unsigned int getHeight() const
unsigned char B
Blue component.
unsigned char R
Red component.
unsigned char G
Green component.
VISP_EXPORT float swapFloat(float f)