mat.cpp
// Write a pgm file  (8 bit gray scale)
// It uses the readable character P2 representation of a picture, rather than
// the binary P5 representation.  Line length is unrestricted.
// WARNING: the user is entrusted with the task of using the pgm file extension
// in the filename
Write a pgm file  (8 bit gray scale)
It uses the readable character P2 representation of a picture, rather than
the binary P5 representation.  Line length is unrestricted.
WARNING: the user is entrusted with the task of using the pgm file extension
in the filename
Write a pgm file ( 8 bit gray scale ) It uses the readable character P2 representation of a picture rather than the binary P5 representation . Line length is unrestricted . WARNING : the user is entrusted with the task of using the pgm file extension in the filename
void Matrix::writeImagePgm(std::string filename, std::string comment)
{
    FILE *OUT;
    assertDefined("writeImagePgm");
    if (filename.length()>0) {
        OUT = fopen(filename.c_str(), "w");
        if (OUT==NULL) {
            printf("ERROR(writeImagePgm): Trying to open file \"%s\" but failed.\n", filename.c_str());
            exit(1);
        }
    }
    else {
        OUT = stdout;
    }
    fprintf(OUT, "P2\n");
    if (name.length()>0) fprintf(OUT, "# Name: %s\n", name.c_str());
    if (comment.length()>0) fprintf(OUT, "# %s\n", comment.c_str());
    fprintf(OUT, "# 8 bit gray scale\n");
    fprintf(OUT, "%d %d\n", maxc, maxr);        
    fprintf(OUT, "255\n");                      
    for (int r=0; r<maxr; r++) {
        for (int c=0; c<maxc-1; c++) {
            fprintf(OUT, "%d ", byteValue(m[r][c]));
        }
        fprintf(OUT, "%d\n", byteValue(m[r][maxc-1]));
    }
    fclose(OUT);
}
void Matrix :: writeImagePgm ( std :: string filename , std :: string comment ) { FILE * OUT ; assertDefined ( " " ) ; if ( filename . length ( ) > 0 ) { OUT = fopen ( filename . c_str ( ) , " " ) ; if ( OUT == NULL ) { printf ( " \" \" \n " , filename . c_str ( ) ) ; exit ( 1 ) ; } } else { OUT = stdout ; } fprintf ( OUT , " \n " ) ; if ( name . length ( ) > 0 ) fprintf ( OUT , " \n " , name . c_str ( ) ) ; if ( comment . length ( ) > 0 ) fprintf ( OUT , " \n " , comment . c_str ( ) ) ; fprintf ( OUT , " \n " ) ; fprintf ( OUT , " \n " , maxc , maxr ) ; fprintf ( OUT , " \n " ) ; for ( int r = 0 ; r < maxr ; r ++ ) { for ( int c = 0 ; c < maxc - 1 ; c ++ ) { fprintf ( OUT , " " , byteValue ( m [ r ] [ c ] ) ) ; } fprintf ( OUT , " \n " , byteValue ( m [ r ] [ maxc - 1 ] ) ) ; } fclose ( OUT ) ; }
Write a pgm file  (8 bit gray scale)
It uses the readable character P2 representation of a picture, rather than
the binary P5 representation.
Write a pgm file ( 8 bit gray scale ) It uses the readable character P2 representation of a picture rather than the binary P5 representation .
// NOTE: columns then rows! // maximum level of gray