41 using std::istringstream;
 
   52 static void pnm_read_comments(istream & i, 
string & comments);
 
   55 static void pnm_write_comments(ostream & o, 
const string & comments);
 
   58 static bool pnm_read_header(ifstream & file, 
char & 
pnm_type,
 
   59                             int & width, 
int & height, 
int & max_val,
 
   60                             string & comments, 
char pnm_type_required = 
'0');
 
   62 static bool pnm_write_header(ofstream & file, 
char type,
 
   63                              int width, 
int height, 
int max_val,
 
   64                              const string & comments);
 
   70 char pnm_type(
const string & filename)
 
   75   file.open(filename.c_str(), ifstream::in | ifstream::binary);
 
   78   int width, height, max_val;
 
   79   pnm_read_header(file, pnm_type, width, height, max_val, comments);
 
   86 bool pnm_info(
const string & filename, 
char & pnm_type,
 
   87               int & width, 
int & height, 
int & max_val,
 
   92   file.open(filename.c_str(), ifstream::in | ifstream::binary);
 
   94   pnm_read_header(file, pnm_type, width, height, max_val, comments);
 
  104 bool pgm_read(
const string & filename,
 
  105               imat & m, 
string & comments)
 
  108   int width, height, max_val, i, j;
 
  111   file.open(filename.c_str(), ifstream::in | ifstream::binary);
 
  115   if (!pnm_read_header(file, pnm_type, width, height, max_val, comments, 
'5'))
 
  119   m.set_size(height, width, 
false);
 
  122   for (i = 0 ; i < height; i++)
 
  123     for (j = 0; j < width; j++)
 
  124       m(i, j) = file.get();
 
  132 imat 
pgm_read(
const string & filename)
 
  136   if (!
pgm_read(filename, I, comments)) {
 
  137     it_warning(
"pgm_read (PGM file->imat) failed ");
 
  144 bool pgm_read(
const string & filename, imat &m,
 
  145               int r1, 
int r2, 
int c1, 
int c2)
 
  148   int width, height, max_val, i, j;
 
  154   file.open(filename.c_str(), ifstream::in | ifstream::binary);
 
  157   if (!pnm_read_header(file, pnm_type, width, height, max_val, comments, 
'5'))
 
  174               "Bad parameter value: row and column number must be >=0");
 
  175   it_error_if((r2 >= height) || (c1 >= width), 
"Bad parameter value: " 
  176               "row or column number exceeds the image heigth");
 
  178   m.set_size(r2 - r1 + 1, c2 - c1 + 1, 
false);
 
  179   file.seekg(r1 * width + c1, ios::cur);
 
  181   for (i = 0 ; i < m.rows() ; i++) {
 
  182     for (j = 0 ; j < m.cols() ; j++)
 
  183       m(i, j) = file.get();
 
  184     file.seekg(width - (c2 - c1 + 1), ios::cur);
 
  193                const imat &m, 
const string & comments)
 
  199   file.open(filename.c_str(), ofstream::out | ofstream::binary);
 
  201   if (!pnm_write_header(file, 
'5', m.cols(), m.rows(), 255, comments))
 
  204   for (i = 0; i < m.rows(); i++)
 
  205     for (j = 0; j < m.cols(); j++)
 
  206       file.put(static_cast<char>(m(i, j)));
 
  219 bool ppm_read(
const string & filename,
 
  220               imat &r, imat &g, imat &b,
 
  224   int width, height, max_val, i, j;
 
  226   file.open(filename.c_str(), ifstream::in | ifstream::binary);
 
  229   if (!pnm_read_header(file, pnm_type, width, height, max_val, comments, 
'6'))
 
  232   r.set_size(height, width, 
false);
 
  233   g.set_size(height, width, 
false);
 
  234   b.set_size(height, width, 
false);
 
  235   for (i = 0; i < height; i++)
 
  236     for (j = 0; j < width; j++) {
 
  237       r(i, j) = file.get();
 
  238       g(i, j) = file.get();
 
  239       b(i, j) = file.get();
 
  248 bool ppm_read(
const string & filename,
 
  249               imat &r, imat &g, imat &b)
 
  253   return ppm_read(filename, r, g, b, comments);
 
  257 bool ppm_read(
const string & filename,
 
  258               imat &r, imat &g, imat &b,
 
  259               int r1, 
int r2, 
int c1, 
int c2)
 
  262   int width, height, max_val, i, j;
 
  267   file.open(filename.c_str(), ifstream::in | ifstream::binary);
 
  270   if (!pnm_read_header(file, pnm_type, width, height, max_val, comments, 
'6'))
 
  289               "Bad parameter value: row and column number must be >=0");
 
  290   it_error_if((r2 >= height) || (c1 >= width), 
"Bad parameter value: " 
  291               "row or column number exceeds the image heigth");
 
  293   r.set_size(r2 - r1 + 1, c2 - c1 + 1, 
false);
 
  294   g.set_size(r2 - r1 + 1, c2 - c1 + 1, 
false);
 
  295   b.set_size(r2 - r1 + 1, c2 - c1 + 1, 
false);
 
  296   file.seekg(3 *(r1 * width + c1), ios::cur);
 
  298   for (i = 0; i < r.rows(); i++) {
 
  299     for (j = 0; j < r.cols(); j++) {
 
  300       r(i, j) = file.get();
 
  301       g(i, j) = file.get();
 
  302       b(i, j) = file.get();
 
  304     file.seekg(3 * (width - (c2 - c1 + 1)), ios::cur);
 
  313                const imat &r, 
const imat &g, 
const imat &b,
 
  314                const string & comments,
 
  321                   r.rows() == g.rows() && g.rows() == b.rows(),
 
  322                   "Matrices r, g and b must have the same size in ppm_write()");
 
  324   file.open(filename.c_str(), ofstream::out | ofstream::binary);
 
  326   if (max_val < 0 || max_val > 65535) {
 
  327     it_warning(
"Proposed maximal value is incorrect");
 
  331   if (!pnm_write_header(file, 
'6', r.cols(), r.rows(), max_val, comments))
 
  334   for (i = 0; i < r.rows(); i++)
 
  335     for (j = 0; j < r.cols(); j++) {
 
  336       file.put(static_cast<char>(r(i, j)));
 
  337       file.put(static_cast<char>(g(i, j)));
 
  338       file.put(static_cast<char>(b(i, j)));
 
  355   imat M(m.rows(), m.cols());
 
  357   for (i = 0 ; i < m.rows() ; i++)
 
  358     for (j = 0 ; j < m.cols() ; j++)
 
  359       if (m(i, j) <= double_min)
 
  362       else if (m(i, j) >= double_max)
 
  366         M(i, j) = (int)(max_val * (m(i, j) - double_min)
 
  367                         / (double_max - double_min) + 0.5);
 
  379   mat M(m.rows(), m.cols());
 
  381   for (i = 0 ; i < m.rows() ; i++)
 
  382     for (j = 0 ; j < m.cols() ; j++)
 
  384         M(i, j) = double_min;
 
  386       else if (m(i, j) >= max_val)
 
  387         M(i, j) = double_max;
 
  391         M(i, j) = double_min + (double_max - double_min)
 
  392                   * m(i, j) / (double) max_val;
 
  403 static void pnm_read_comments(istream & i, 
string & comments)
 
  405   while (isspace(i.peek())) {
 
  406     while (isspace(i.peek()))
 
  410       while (i.peek() != 
'\r' && i.peek() != 
'\n')
 
  411         comments += static_cast<char>(i.get());
 
  417 static void pnm_write_comments(ostream & o, 
const string & comments)
 
  419   istringstream comments_stream(comments);
 
  420   char comment_line[ 256 ];
 
  423   while (!comments_stream.eof()) {
 
  425     comments_stream.get(comment_line, 256);
 
  426     o << comment_line << endl;
 
  433 static bool pnm_read_header(ifstream & file, 
char & pnm_type,
 
  434                             int & width, 
int & height, 
int & max_val,
 
  435                             string & comments, 
char pnm_type_required)
 
  437   bool return_code = 
true;
 
  439   if (file.get() != 
'P')
 
  441   it_error_if(!return_code, 
"Invalid format file: code of file format has " 
  447               "Bad file code P" << pnm_type);
 
  450   if (pnm_type_required != 
'0')
 
  451     if (pnm_type_required != pnm_type) {
 
  452       string err_msg(
"Found file code P");
 
  453       err_msg += pnm_type + 
" instead of P" + pnm_type_required;
 
  458   pnm_read_comments(file, comments);
 
  460   pnm_read_comments(file, comments);
 
  462   pnm_read_comments(file, comments);
 
  464   it_error_if((height < 0) || (width < 0), 
"Bad image size");
 
  467   if (pnm_type == 
'2' || pnm_type == 
'3' || pnm_type == 
'5' || pnm_type == 
'6')
 
  475               "Invalid maximum number in pnm header");
 
  478   it_error_if((pnm_type == 
'5' || pnm_type == 
'6') && (max_val > 255),
 
  479               "Invalid maximum number in pnm header");
 
  486 static bool pnm_write_header(ofstream &file, 
char pnm_type,
 
  487                              int width, 
int height, 
int max_val,
 
  488                              const string & comments)
 
  490   file << 
'P' << pnm_type << endl;
 
  491   pnm_write_comments(file, comments);
 
  492   file << width << 
' ' << height << endl;
 
  495   if (pnm_type == 
'2' || pnm_type == 
'3' || pnm_type == 
'5' || pnm_type == 
'6')
 
  496     file << max_val << endl;