39 #include "vpImageIoBackend.h"
40 #include <visp3/core/vpImageConvert.h>
41 #include <visp3/core/vpIoTools.h>
42 #include <visp3/core/vpEndian.h>
46 #ifndef DOXYGEN_SHOULD_SKIP_THIS
56 void vp_decodeHeaderPNM(
const std::string &filename, std::ifstream &fd,
const std::string &magic,
unsigned int &w,
57 unsigned int &h,
unsigned int &maxval)
60 unsigned int nb_elt = 4, cpt_elt = 0;
61 while (cpt_elt != nb_elt) {
63 while (std::getline(fd, line) && (line.compare(0, 1,
"#") == 0 || line.size() == 0)) {
73 if (header.size() == 0) {
79 if (header[0].compare(0, magic.size(), magic) != 0) {
82 filename.c_str(), magic.c_str()));
85 header.erase(header.begin(),
88 while (header.size()) {
90 std::istringstream ss(header[0]);
93 header.erase(header.begin(),
96 else if (cpt_elt == 2) {
97 std::istringstream ss(header[0]);
100 header.erase(header.begin(),
103 else if (cpt_elt == 3) {
104 std::istringstream ss(header[0]);
107 header.erase(header.begin(),
114 void vp_decodeHeaderPFM(
const std::string &filename, std::ifstream &fd, std::string &magic,
unsigned int &w,
115 unsigned int &h,
double &scale,
bool &littleEndian)
118 const unsigned int nb_elt = 4;
119 unsigned int cpt_elt = 0;
120 while (cpt_elt != nb_elt) {
122 while (std::getline(fd, line) && (line.compare(0, 1,
"#") == 0 || line.size() == 0)) {
132 if (header.empty()) {
139 if (magic !=
"PF" && magic !=
"Pf") {
142 "\"%s\" is not a PFM file with PF (RGB) or Pf (gray) magic number", filename.c_str()));
145 header.erase(header.begin(),
148 while (header.size()) {
150 std::istringstream ss(header[0]);
153 header.erase(header.begin(),
156 else if (cpt_elt == 2) {
157 std::istringstream ss(header[0]);
160 header.erase(header.begin(),
163 else if (cpt_elt == 3) {
164 std::istringstream ss(header[0]);
166 littleEndian = scale < 0;
168 header.erase(header.begin(),
188 void vp_writePFM(
const vpImage<float> &I,
const std::string &filename)
193 if (filename.empty()) {
197 fd = fopen(filename.c_str(),
"wb");
206 fprintf(fd,
"255\n");
212 ierr = fwrite(I.
bitmap,
sizeof(
float), nbyte, fd);
216 filename.c_str(), ierr, nbyte));
223 void vp_writePFM_HDR(
const vpImage<float> &I,
const std::string &filename)
226 if (filename.empty()) {
230 FILE *fd = fopen(filename.c_str(),
"wb");
238 #ifdef VISP_LITTLE_ENDIAN
239 fprintf(fd,
"%f\n", -1.0f);
241 fprintf(fd,
"%f\n", 1.0f);
246 for (
int i =
static_cast<int>(I.
getHeight()) - 1; i >= 0; i--) {
247 size_t ierr = fwrite(I[i],
sizeof(
float), nbyte, fd);
251 filename.c_str(), ierr, nbyte));
259 void vp_writePFM_HDR(
const vpImage<vpRGBf> &I,
const std::string &filename)
262 if (filename.empty()) {
266 FILE *fd = fopen(filename.c_str(),
"wb");
274 #ifdef VISP_LITTLE_ENDIAN
275 fprintf(fd,
"%f\n", -1.0f);
277 fprintf(fd,
"%f\n", 1.0f);
282 for (
int i =
static_cast<int>(I.
getHeight()) - 1; i >= 0; i--) {
283 size_t ierr = fwrite(I[i],
sizeof(
float), nbyte, fd);
287 filename.c_str(), ierr, nbyte));
311 if (filename.empty()) {
315 fd = fopen(filename.c_str(),
"wb");
324 fprintf(fd,
"255\n");
330 ierr = fwrite(I.
bitmap,
sizeof(
unsigned char), nbyte, fd);
334 filename.c_str(), ierr, nbyte));
348 void vp_writePGM(
const vpImage<short> &I,
const std::string &filename)
356 for (
unsigned int i = 0; i < nrows * ncols; ++i)
359 vp_writePGM(Iuc, filename);
370 void vp_writePGM(
const vpImage<vpRGBa> &I,
const std::string &filename)
376 if (filename.empty()) {
380 fd = fopen(filename.c_str(),
"wb");
389 fprintf(fd,
"255\n");
398 ierr = fwrite(Itmp.
bitmap,
sizeof(
unsigned char), nbyte, fd);
402 filename.c_str(), ierr, nbyte));
425 unsigned int w = 0, h = 0, maxval = 0;
426 const unsigned int w_max = 100000, h_max = 100000, maxval_max = 255;
427 const std::string magic(
"P8");
429 std::ifstream fd(filename.c_str(), std::ios::binary);
436 vp_decodeHeaderPNM(filename, fd, magic, w, h, maxval);
438 if (w > w_max || h > h_max) {
442 if (maxval > maxval_max) {
452 fd.read((
char *)I.
bitmap,
sizeof(
float) * nbyte);
477 void vp_readPFM_HDR(
vpImage<float> &I,
const std::string &filename)
479 std::ifstream fd(filename.c_str(), std::ios::binary);
486 const unsigned int w_max = 100000, h_max = 100000;
487 const std::string magicRGB(
"PF"), magicGray(
"Pf");
489 unsigned int w = 0, h = 0;
491 bool littleEndian =
true;
492 vp_decodeHeaderPFM(filename, fd, magic, w, h, scale, littleEndian);
494 if (w > w_max || h > h_max) {
499 unsigned int channels = (magic == magicRGB) ? 3 : 1;
501 I.
resize(h, channels * w);
504 #ifdef VISP_LITTLE_ENDIAN
505 bool swapEndianness = !littleEndian;
507 bool swapEndianness = littleEndian;
509 for (
int i = I.
getHeight() - 1; i >= 0; i--) {
510 fd.read((
char *)I[i],
sizeof(float) * w * channels);
511 if (swapEndianness) {
512 for (
unsigned int j = 0; j < w * channels; ++j) {
525 if (std::fabs(scale) > 0.0f) {
526 for (
unsigned int i = 0; i < I.
getHeight(); ++i) {
527 for (
unsigned int j = 0; j < I.
getWidth(); ++j) {
528 I[i][j] *= 1.0f /
static_cast<float>(std::fabs(scale));
551 std::ifstream fd(filename.c_str(), std::ios::binary);
558 const unsigned int w_max = 100000, h_max = 100000;
559 const std::string magicRGB(
"PF"), magicGray(
"Pf");
561 unsigned int w = 0, h = 0;
563 bool littleEndian =
true;
564 vp_decodeHeaderPFM(filename, fd, magic, w, h, scale, littleEndian);
566 if (w > w_max || h > h_max) {
571 unsigned int channels = (magic == magicRGB) ? 3 : 1;
572 if (magic != magicRGB) {
579 #ifdef VISP_LITTLE_ENDIAN
580 bool swapEndianness = !littleEndian;
582 bool swapEndianness = littleEndian;
584 for (
int i = I.
getHeight() - 1; i >= 0; i--) {
585 fd.read((
char *)I[i],
sizeof(float) * w * channels);
586 if (swapEndianness) {
587 for (
unsigned int j = 0; j < w; ++j) {
602 if (std::fabs(scale) > 0.0f) {
603 for (
unsigned int i = 0; i < I.
getHeight(); ++i) {
604 for (
unsigned int j = 0; j < I.
getWidth(); ++j) {
605 I[i][j].R *= 1.0f /
static_cast<float>(std::fabs(scale));
606 I[i][j].G *= 1.0f /
static_cast<float>(std::fabs(scale));
607 I[i][j].B *= 1.0f /
static_cast<float>(std::fabs(scale));
629 unsigned int w = 0, h = 0, maxval = 0;
630 unsigned int w_max = 100000, h_max = 100000, maxval_max = 255;
631 std::string magic(
"P5");
633 std::ifstream fd(filename.c_str(), std::ios::binary);
640 vp_decodeHeaderPNM(filename, fd, magic, w, h, maxval);
642 if (w > w_max || h > h_max) {
646 if (maxval > maxval_max) {
656 fd.read((
char *)I.
bitmap, nbyte);
687 vp_readPGM(Itmp, filename);
715 vp_readPPM(Itmp, filename);
733 unsigned int w = 0, h = 0, maxval = 0;
734 unsigned int w_max = 100000, h_max = 100000, maxval_max = 255;
735 std::string magic(
"P6");
737 std::ifstream fd(filename.c_str(), std::ios::binary);
744 vp_decodeHeaderPNM(filename, fd, magic, w, h, maxval);
746 if (w > w_max || h > h_max) {
750 if (maxval > maxval_max) {
759 for (
unsigned int i = 0; i < I.
getHeight(); ++i) {
760 for (
unsigned int j = 0; j < I.
getWidth(); ++j) {
761 unsigned char rgb[3];
762 fd.read((
char *)&rgb, 3);
767 (i * I.
getWidth() + j) * 3 + fd.gcount(), I.
getSize() * 3, filename.c_str()));
794 vp_writePPM(Itmp, filename);
804 void vp_writePPM(
const vpImage<vpRGBa> &I,
const std::string &filename)
809 if (filename.empty()) {
813 f = fopen(filename.c_str(),
"wb");
821 fprintf(f,
"%d\n", 255);
823 for (
unsigned int i = 0; i < I.
getHeight(); ++i) {
824 for (
unsigned int j = 0; j < I.
getWidth(); ++j) {
826 unsigned char rgb[3];
831 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)