I have an array 230x230 contains values: zero (black pixel), and values from 25 till 1200. I want to save this array as PGM image.
I made the image header: P5, 230x230, 255 (as maximum value), and I wrote the array values without changing. This gave me the image but not in very right look (a lot of white areas - 2nd image)
So, I thought I should do some scaling to the values. Again, I rescaled all values to 0-255, but this gave me totally black image.
Any advice how I can properly solve this issue? I have attached the image required to have (1), and the image I already got (2).
Find below the code I have used:
void write_pgm(double **u, long nx, long ny, char *file_name) {
int i, j;
unsigned char byte;
FILE *outimage = fopen(file_name, "wb");
fprintf(outimage, "P5\n"); /* format */
fprintf(outimage, "%ld %ld\n", nx, ny); /* image size */
fprintf(outimage, "255\n"); /* maximal value */
for (j = 0; j < ny; j++) {
for (i = 0; i < nx; i++) {
byte = (unsigned char) (u[i][j]);
fwrite(&byte, sizeof(unsigned char), 1, outimage);
}
}
fclose(outimage);
return;
}
Thank you.