Lib/Support/pgm.cpp
/**
 * These format limits need to be taken into account:
 *
 * - Max line length of 70 characters.
 * - Max gray value is 65536
 * - Max color value is 255 (standard in file header)
 */
These format limits need to be taken into account:
- Max line length of 70 characters.
- Max gray value is 65536
- Max color value is 255 (standard in file header)
These format limits need to be taken into account : - Max line length of 70 characters . - Max gray value is 65536 - Max color value is 255 ( standard in file header )
void output_ppm_file(
  std::string const &header,
  int width,
  int height,
  const char *filename,
  std::function<std::string (int)> f) {
  assert(!header.empty());
  int const LINE_LIMIT = 70;
  FILE *fd = fopen(filename, "w") ;
  if (fd == nullptr) {
    printf("can't open file for graphics output\n");
    return;
  }
  fprintf(fd, "%s\n", header.c_str());
  std::string curLine;
  for (int y = 0; y < height; y++) {
    for (int x = 0; x < width; x++) {
      std::string tmp = f(x + width*y);
      std::string newLine = curLine;
      if (!newLine.empty()) {
        newLine << " ";
      }
      newLine << tmp;
      if (newLine.length() > LINE_LIMIT) {
        fprintf(fd, "%s\n", curLine.c_str());
        curLine = tmp;
      } else {
        curLine = newLine;
      }
    }
  }
  if (!curLine.empty()) {
    fprintf(fd, "%s\n", curLine.c_str());
  }
  fclose(fd);
}
void output_ppm_file ( std :: string const & header , int width , int height , const char * filename , std :: function < std :: string ( int ) > f ) { assert ( ! header . empty ( ) ) ; int const LINE_LIMIT = 70 ; FILE * fd = fopen ( filename , " " ) ; if ( fd == nullptr ) { printf ( " \n " ) ; return ; } fprintf ( fd , " \n " , header . c_str ( ) ) ; std :: string curLine ; for ( int y = 0 ; y < height ; y ++ ) { for ( int x = 0 ; x < width ; x ++ ) { std :: string tmp = f ( x + width * y ) ; std :: string newLine = curLine ; if ( ! newLine . empty ( ) ) { newLine << " " ; } newLine << tmp ; if ( newLine . length ( ) > LINE_LIMIT ) { fprintf ( fd , " \n " , curLine . c_str ( ) ) ; curLine = tmp ; } else { curLine = newLine ; } } } if ( ! curLine . empty ( ) ) { fprintf ( fd , " \n " , curLine . c_str ( ) ) ; } fclose ( fd ) ; }
These format limits need to be taken into account:
 - Max line length of 70 characters.
These format limits need to be taken into account : - Max line length of 70 characters .