1 #ifndef IMAGE_H_INCLUDED 2 #define IMAGE_H_INCLUDED 79 size_t mark = str.find_first_of(
"#");
80 if (mark != std::string::npos)
82 ifs.ignore(256,
'\n');
91 type =
static_cast<imtype>(str.at(1) - 48);
92 if (str.at(0) !=
'P' || type >
PPM_B)
97 cols = atoi(str.c_str());
101 rows = atoi(str.c_str());
108 else if (maxval == 0)
110 maxval = atoi(str.c_str());
127 const arma::uword _cols,
const int _maxval,
128 const std::string comments)
134 ofs <<
"P" << type << std::endl;
135 ofs <<
"# " << comments << std::endl;
136 ofs << cols <<
" " << rows << std::endl;
138 ofs << maxval << std::endl;
150 bool write(std::string fname,
const imtype _type,
const arma::cube &img,
151 const std::string info =
"")
154 ofs.open(fname.c_str(), std::ofstream::binary);
157 std::cout <<
"Could not open " << fname << std::endl;
162 write_header(_type, img.n_rows, img.n_cols, (
int)img.max(), info);
168 for (arma::uword r = 0; r <
rows; r++)
171 for (arma::uword c = 0; c <
cols; c++)
173 ofs << img(r, c, 0) <<
" " << img(r, c, 1) <<
" " << img(r, c, 2)
180 else if (type ==
PPM_B)
182 for (arma::uword r = 0; r <
rows; r++)
183 for (arma::uword c = 0; c <
cols; c++)
186 bb =
static_cast<unsigned char>(img(r, c, 0));
187 ofs.write(reinterpret_cast<char *>(&bb), 1);
188 bb =
static_cast<unsigned char>(img(r, c, 1));
189 ofs.write(reinterpret_cast<char *>(&bb), 1);
190 bb =
static_cast<unsigned char>(img(r, c, 2));
191 ofs.write(reinterpret_cast<char *>(&bb), 1);
209 const std::string info =
"")
212 ofs.open(fname.c_str(), std::ofstream::binary);
215 std::cout <<
"Could not open " << fname << std::endl;
220 write_header(_type, img.n_rows, img.n_cols, (
int)img.max(), info);
227 for (arma::mat::iterator ii = img.begin(); ii != img.end(); ++ii)
234 else if (type ==
PBM_B)
237 for (arma::uword r = 0; r <
rows; r++)
238 for (arma::uword c = 0; c <
cols; c++)
240 arma::uword ix = 7 - (c % 8);
241 b[ix] = (img(r, c) > 0);
242 if (ix == 0 || c == cols - 1)
244 ofs.write(reinterpret_cast<char *>(&b), 1);
249 else if (type ==
PGM_B)
253 for (arma::uword r = 0; r <
rows; r++)
254 for (arma::uword c = 0; c <
cols; c++)
256 unsigned char bb =
static_cast<unsigned char>(img(r, c));
257 ofs.write(reinterpret_cast<char *>(&bb), 1);
262 for (arma::uword r = 0; r <
rows; r++)
263 for (arma::uword c = 0; c <
cols; c++)
266 bb = ((
static_cast<unsigned int>(img(r, c))) >> 8) &
268 ofs.write(reinterpret_cast<char *>(&bb), 1);
269 bb =
static_cast<unsigned int>(img(r, c)) & 0x00ff;
270 ofs.write(reinterpret_cast<char *>(&bb), 1);
286 std::cout <<
"Type: P" << type << std::endl;
287 std::cout <<
"cols: " << cols << std::endl;
288 std::cout <<
"rows: " << rows << std::endl;
290 std::cout <<
"Maxval: " << maxval << std::endl;
321 bool read(std::string fname, arma::cube &img)
324 ifs.open(fname.c_str(), std::ifstream::binary);
327 std::cout <<
"Could not open " << fname << std::endl;
335 img.set_size(rows, cols, 3);
336 arma::uword r = 0, c = 0;
342 while (ifs >> str && r < rows)
345 size_t mark = str.find_first_of(
"#");
346 if (mark != std::string::npos)
348 ifs.ignore(256,
'\n');
354 int pix = atoi(str.c_str());
356 img(r, c, i % 3) = pix;
366 else if (type ==
PPM_B)
368 for (arma::uword r = 0; r <
rows; r++)
369 for (arma::uword c = 0; c <
cols; c++)
372 ifs.read(reinterpret_cast<char *>(&bb), 1);
374 ifs.read(reinterpret_cast<char *>(&bb), 1);
376 ifs.read(reinterpret_cast<char *>(&bb), 1);
392 bool read(std::string fname, arma::mat &img)
395 ifs.open(fname.c_str(), std::ifstream::binary);
398 std::cout <<
"Could not open " << fname << std::endl;
406 img.set_size(rows, cols);
407 arma::uword r = 0, c = 0;
412 while (ifs >> str && r < rows)
415 size_t mark = str.find_first_of(
"#");
416 if (mark != std::string::npos)
418 ifs.ignore(256,
'\n');
424 int pix = atoi(str.c_str());
434 else if (type ==
PBM_B)
437 while (ifs.read(reinterpret_cast<char *>(&ch), 1) &&
440 std::bitset<8> pix(ch);
441 for (
int b = 7; b >= 0; b--)
453 else if (type ==
PGM_B)
457 for (arma::uword r = 0; r <
rows; r++)
458 for (arma::uword c = 0; c <
cols; c++)
461 ifs.read(reinterpret_cast<char *>(&bb), 1);
467 for (arma::uword r = 0; r <
rows; r++)
468 for (arma::uword c = 0; c <
cols; c++)
471 ifs.read(reinterpret_cast<char *>(bb), 2);
472 img(r, c) = (bb[0] << 8) + bb[1];
bool write(std::string fname, const imtype _type, const arma::cube &img, const std::string info="")
Write the .pnm file.
arma::uword get_cols()
Get nr of cols.
std::ifstream ifs
Input stream handle.
void read_header()
Reads the .pnm header.
int maxval
Maximum pixel value in image.
bool read(std::string fname, arma::mat &img)
Read image.
bool read(std::string fname, arma::cube &img)
Read image.
std::ofstream ofs
Output stream handle.
void clear(void)
Clears the internal variables.
enum sp::PNM::imtype type
Image format.
int get_maxval()
Get maxval.
arma::uword rows
Nr of rows in image.
void write_header(const imtype _type, const arma::uword _rows, const arma::uword _cols, const int _maxval, const std::string comments)
Write the .pnm header.
void get_info()
Prints header info.
arma::uword cols
Nr of columns in image.
bool write(std::string fname, const imtype _type, arma::mat &img, const std::string info="")
Write the .pnm file.
arma::uword get_rows()
Get nr of rows.
Portable anymap format class.