#include <stdio.h>
#include <pgm.h>

#define sx 1920
#define sy 1200

int main(void) {
    FILE *f = fopen("256.pgm", "rb");
    int nrows, ncols, i, j, k, maxval;
    int total = 0;
    double scale;
    gray **x = pgm_readpgm(f, &ncols, &nrows, &maxval );
    
    fprintf(stderr, "%d %d\n", nrows, ncols);
    for(i=0; i<nrows; i++) 
        for(j=0; j<ncols; j++) {
            total += x[i][j];
    }
    scale = sx * sy * 1.0/ total;
    fprintf(stderr, "total = %d scale=%f\n", total, scale);
    printf("P6\n%d %d\n255\n", sx, sy);
    for(i=0; i<nrows; i++) {
        for(j=0; j<ncols; j++) {
            for(k=0; k < scale * x[i][j]; k++) {
                putchar(j);
                putchar(255);
                putchar(255-i);
            }
        }
    }
    return 0;        
}
