41 #include "vpImageIoBackend.h" 42 #include <visp3/core/vpIoTools.h> 43 #include <visp3/core/vpImageConvert.h> 46 void vp_decodeHeaderPNM(
const std::string &filename, std::ifstream &fd,
const std::string &magic,
unsigned int &w,
47 unsigned int &h,
unsigned int &maxval);
49 #ifndef DOXYGEN_SHOULD_SKIP_THIS 59 void vp_decodeHeaderPNM(
const std::string &filename, std::ifstream &fd,
const std::string &magic,
unsigned int &w,
60 unsigned int &h,
unsigned int &maxval)
63 unsigned int nb_elt = 4, cpt_elt = 0;
64 while (cpt_elt != nb_elt) {
66 while (std::getline(fd, line) && (line.compare(0, 1,
"#") == 0 || line.size() == 0)) {
76 if (header.size() == 0) {
82 if (header[0].compare(0, magic.size(), magic) != 0) {
85 filename.c_str(), magic.c_str()));
88 header.erase(header.begin(),
91 while (header.size()) {
93 std::istringstream ss(header[0]);
96 header.erase(header.begin(),
98 }
else if (cpt_elt == 2) {
99 std::istringstream ss(header[0]);
102 header.erase(header.begin(),
104 }
else if (cpt_elt == 3) {
105 std::istringstream ss(header[0]);
108 header.erase(header.begin(),
128 void vp_writePFM(
const vpImage<float> &I,
const std::string &filename)
133 if (filename.empty()) {
137 fd = fopen(filename.c_str(),
"wb");
146 fprintf(fd,
"255\n");
152 ierr = fwrite(I.
bitmap,
sizeof(
float), nbyte, fd);
156 filename.c_str(), ierr, nbyte));
179 if (filename.empty()) {
183 fd = fopen(filename.c_str(),
"wb");
192 fprintf(fd,
"255\n");
198 ierr = fwrite(I.
bitmap,
sizeof(
unsigned char), nbyte, fd);
202 filename.c_str(), ierr, nbyte));
216 void vp_writePGM(
const vpImage<short> &I,
const std::string &filename)
224 for (
unsigned int i = 0; i < nrows * ncols; i++)
227 vp_writePGM(Iuc, filename);
238 void vp_writePGM(
const vpImage<vpRGBa> &I,
const std::string &filename)
244 if (filename.empty()) {
248 fd = fopen(filename.c_str(),
"wb");
257 fprintf(fd,
"255\n");
266 ierr = fwrite(Itmp.
bitmap,
sizeof(
unsigned char), nbyte, fd);
270 filename.c_str(), ierr, nbyte));
293 unsigned int w = 0, h = 0, maxval = 0;
294 unsigned int w_max = 100000, h_max = 100000, maxval_max = 255;
295 std::string magic(
"P8");
297 std::ifstream fd(filename.c_str(), std::ios::binary);
304 vp_decodeHeaderPNM(filename, fd, magic, w, h, maxval);
306 if (w > w_max || h > h_max) {
310 if (maxval > maxval_max) {
320 fd.read((
char *)I.
bitmap,
sizeof(
float) * nbyte);
346 unsigned int w = 0, h = 0, maxval = 0;
347 unsigned int w_max = 100000, h_max = 100000, maxval_max = 255;
348 std::string magic(
"P5");
350 std::ifstream fd(filename.c_str(), std::ios::binary);
357 vp_decodeHeaderPNM(filename, fd, magic, w, h, maxval);
359 if (w > w_max || h > h_max) {
363 if (maxval > maxval_max) {
373 fd.read((
char *)I.
bitmap, nbyte);
404 vp_readPGM(Itmp, filename);
432 vp_readPPM(Itmp, filename);
450 unsigned int w = 0, h = 0, maxval = 0;
451 unsigned int w_max = 100000, h_max = 100000, maxval_max = 255;
452 std::string magic(
"P6");
454 std::ifstream fd(filename.c_str(), std::ios::binary);
461 vp_decodeHeaderPNM(filename, fd, magic, w, h, maxval);
463 if (w > w_max || h > h_max) {
467 if (maxval > maxval_max) {
476 for (
unsigned int i = 0; i < I.
getHeight(); i++) {
477 for (
unsigned int j = 0; j < I.
getWidth(); j++) {
478 unsigned char rgb[3];
479 fd.read((
char *)&rgb, 3);
484 (i * I.
getWidth() + j) * 3 + fd.gcount(), I.
getSize() * 3, filename.c_str()));
511 vp_writePPM(Itmp, filename);
521 void vp_writePPM(
const vpImage<vpRGBa> &I,
const std::string &filename)
526 if (filename.empty()) {
530 f = fopen(filename.c_str(),
"wb");
538 fprintf(f,
"%d\n", 255);
540 for (
unsigned int i = 0; i < I.
getHeight(); i++) {
541 for (
unsigned int j = 0; j < I.
getWidth(); j++) {
543 unsigned char rgb[3];
548 size_t res = fwrite(&rgb, 1, 3, f);
Used to indicate that a value is not in the allowed range.
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.
unsigned int getHeight() const
unsigned int getSize() const
unsigned char R
Red component.
unsigned int getWidth() const