1#ifndef IMAGE_H_INCLUDED
2#define IMAGE_H_INCLUDED
81 size_t mark = str.find_first_of(
"#" );
82 if( mark != std::string::npos )
84 ifs.ignore( 256,
'\n' );
93 type =
static_cast<imtype>( str.at( 1 ) - 48 );
99 cols = atoi( str.c_str() );
103 rows = atoi( str.c_str() );
112 maxval = atoi( str.c_str() );
128 void write_header(
const imtype _type,
const arma::uword _rows,
const arma::uword _cols,
const int _maxval,
const std::string comments )
134 ofs <<
"P" <<
type << std::endl;
135 ofs <<
"# " << comments << std::endl;
150 bool write( std::string fname,
const imtype _type,
const arma::cube& img,
const std::string info =
"" )
153 ofs.open( fname.c_str(), std::ofstream::binary );
156 std::cout <<
"Could not open " << fname << std::endl;
161 write_header( _type, img.n_rows, img.n_cols, (
int)img.max(), info );
167 for( arma::uword r = 0; r <
rows; r++ )
170 for( arma::uword c = 0; c <
cols; c++ )
172 ofs << img( r, c, 0 ) <<
" " << img( r, c, 1 ) <<
" " << img( r, c, 2 ) <<
" ";
180 for( arma::uword r = 0; r <
rows; r++ )
181 for( arma::uword c = 0; c <
cols; c++ )
184 bb =
static_cast<unsigned char>( img( r, c, 0 ) );
185 ofs.write(
reinterpret_cast<char*
>( &bb ), 1 );
186 bb =
static_cast<unsigned char>( img( r, c, 1 ) );
187 ofs.write(
reinterpret_cast<char*
>( &bb ), 1 );
188 bb =
static_cast<unsigned char>( img( r, c, 2 ) );
189 ofs.write(
reinterpret_cast<char*
>( &bb ), 1 );
206 bool write( std::string fname,
const imtype _type, arma::mat& img,
const std::string info =
"" )
209 ofs.open( fname.c_str(), std::ofstream::binary );
212 std::cout <<
"Could not open " << fname << std::endl;
217 write_header( _type, img.n_rows, img.n_cols, (
int)img.max(), info );
224 for( arma::mat::iterator ii = img.begin(); ii != img.end(); ++ii )
234 for( arma::uword r = 0; r <
rows; r++ )
235 for( arma::uword c = 0; c <
cols; c++ )
237 arma::uword ix = 7 - ( c % 8 );
238 b[ix] = ( img( r, c ) > 0 );
239 if( ix == 0 || c ==
cols - 1 )
241 ofs.write(
reinterpret_cast<char*
>( &b ), 1 );
250 for( arma::uword r = 0; r <
rows; r++ )
251 for( arma::uword c = 0; c <
cols; c++ )
253 unsigned char bb =
static_cast<unsigned char>( img( r, c ) );
254 ofs.write(
reinterpret_cast<char*
>( &bb ), 1 );
259 for( arma::uword r = 0; r <
rows; r++ )
260 for( arma::uword c = 0; c <
cols; c++ )
263 bb = ( (
static_cast<unsigned int>( img( r, c ) ) ) >> 8 ) & 0x00ff;
264 ofs.write(
reinterpret_cast<char*
>( &bb ), 1 );
265 bb =
static_cast<unsigned int>( img( r, c ) ) & 0x00ff;
266 ofs.write(
reinterpret_cast<char*
>( &bb ), 1 );
282 std::cout <<
"Type: P" <<
type << std::endl;
283 std::cout <<
"cols: " <<
cols << std::endl;
284 std::cout <<
"rows: " <<
rows << std::endl;
286 std::cout <<
"Maxval: " <<
maxval << std::endl;
326 bool read( std::string fname, arma::cube& img )
329 ifs.open( fname.c_str(), std::ifstream::binary );
332 std::cout <<
"Could not open " << fname << std::endl;
341 arma::uword r = 0, c = 0;
347 while(
ifs >> str && r <
rows )
350 size_t mark = str.find_first_of(
"#" );
351 if( mark != std::string::npos )
353 ifs.ignore( 256,
'\n' );
354 str.erase( mark, 1 );
359 int pix = atoi( str.c_str() );
361 img( r, c, i % 3 ) = pix;
373 for( arma::uword r = 0; r <
rows; r++ )
374 for( arma::uword c = 0; c <
cols; c++ )
377 ifs.read(
reinterpret_cast<char*
>( &bb ), 1 );
379 ifs.read(
reinterpret_cast<char*
>( &bb ), 1 );
381 ifs.read(
reinterpret_cast<char*
>( &bb ), 1 );
397 bool read( std::string fname, arma::mat& img )
400 ifs.open( fname.c_str(), std::ifstream::binary );
403 std::cout <<
"Could not open " << fname << std::endl;
412 arma::uword r = 0, c = 0;
417 while(
ifs >> str && r <
rows )
420 size_t mark = str.find_first_of(
"#" );
421 if( mark != std::string::npos )
423 ifs.ignore( 256,
'\n' );
424 str.erase( mark, 1 );
429 int pix = atoi( str.c_str() );
442 while(
ifs.read(
reinterpret_cast<char*
>( &ch ), 1 ) && r <
rows )
444 std::bitset<8> pix( ch );
445 for(
int b = 7; b >= 0; b-- )
447 img( r, c ) = pix[b];
461 for( arma::uword r = 0; r <
rows; r++ )
462 for( arma::uword c = 0; c <
cols; c++ )
465 ifs.read(
reinterpret_cast<char*
>( &bb ), 1 );
471 for( arma::uword r = 0; r <
rows; r++ )
472 for( arma::uword c = 0; c <
cols; c++ )
475 ifs.read(
reinterpret_cast<char*
>( bb ), 2 );
476 img( r, c ) = ( bb[0] << 8 ) + bb[1];
Portable anymap format class.
bool write(std::string fname, const imtype _type, arma::mat &img, const std::string info="")
Write the .pnm file.
std::ofstream ofs
Output stream handle.
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.
bool read(std::string fname, arma::mat &img)
Read image.
arma::uword get_rows()
Get nr of rows.
arma::uword cols
Nr of columns in image.
arma::uword get_cols()
Get nr of cols.
std::ifstream ifs
Input stream handle.
void clear(void)
Clears the internal variables.
int get_maxval()
Get maxval.
enum sp::PNM::imtype type
Image format.
int maxval
Maximum pixel value in image.
arma::uword rows
Nr of rows in image.
bool write(std::string fname, const imtype _type, const arma::cube &img, const std::string info="")
Write the .pnm file.
void get_info()
Prints header info.
bool read(std::string fname, arma::cube &img)
Read image.
void read_header()
Reads the .pnm header.