C program to write an image in PGM format


The PGM is the Portable Gray Map. If we want to store a 2d array in C as images in PNG, JPEG, or any other image format, we have to do lots of work to encode the data in some specified format before writing into a file.

The Netpbm format gives an easy and portable solution. The Netpbm is an open source package of graphics program and it is used basically in linux or Unix platform. It also works under Microsoft Windows systems.

Each file starts with a two-byte magic number. This magic number is used to identify the type of the file. The types are PBM, PGM, PPM etc. It also identifies the encoding (ASCII or Binary). The magic number is a capital P followed by a single digit number.

The ASCII encoding allows human readability and easy transfer to other platforms; the binary format is more efficient in file size but it may have native byte-order issues.

How to write PGM files?

  • Set the magic number P2
  • Add whitespace (Space, Tabs, CRs, LFs)
  • Add width, formatted as ASCII character in decimal
  • Add Whitespace
  • Add height, formatted as ASCII character in decimal
  • Add Whitespace
  • Put the maximum gray value, again in ASCII decimal
  • Add Whitespace
  • Width x Height gray values, each in ASCII decimal (Range between 0 and maximum value), separated by whitespace from top to bottom.

Example Code

#include <stdio.h>
main() {
   int i, j;
   int w = 13, h = 13;
   // This 2D array will be converted into an image The size is 13 x 13
   int image[13][13] = {
      { 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15 },
      { 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31},
      { 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47},
      { 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63},
      { 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79},
      { 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95 },
      { 111, 111, 111, 111, 111, 111, 111, 111, 111, 111, 111, 111, 111},
      { 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127},
      { 143, 143, 143, 143, 143, 143, 143, 143, 143, 143, 143, 143, 143},
      { 159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159},
      { 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175},
      { 191, 191, 191, 191, 191, 191, 191, 191, 191, 191, 191, 191, 191},
      { 207, 207, 207, 207, 207, 207, 207, 207, 207, 207, 207, 207, 207}
   };
   FILE* pgmimg;
   pgmimg = fopen("my_pgmimg.pgm", "wb"); //write the file in binary mode
   fprintf(pgmimg, "P2
"); // Writing Magic Number to the File fprintf(pgmimg, "%d %d
", w, h); // Writing Width and Height into the file fprintf(pgmimg, "255
"); // Writing the maximum gray value int count = 0; for (i = 0; i < h; i++) { for (j = 0; j < w; j++) { fprintf(pgmimg, "%d ", image[i][j]); //Copy gray value from array to file } fprintf(pgmimg, "
"); } fclose(pgmimg); }

The PGM image is looking like below

Output

Updated on: 30-Jul-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements