Home  >  Article  >  Backend Development  >  C program to write image in PGM format

C program to write image in PGM format

WBOY
WBOYforward
2023-09-16 22:01:01799browse

PGM is a portable grayscale map. If we want to store a 2D array in C as an image in PNG, JPEG or any other image format, we have to do a lot of work to encode the data in some specified format before writing to the file.

The Netpbm format provides a simple and portable solution. Netpbm is an open source graphics package, basically used on Linux or Unix platforms. It also runs under Microsoft Windows systems.

Every file starts with a two-byte magic number. This magic number is used to identify the type of file. Types include PBM, PGM, PPM, etc. It also identifies the encoding (ASCII or binary). A magic number is a capital P followed by a number.

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

How to write PGM files?

  • Set magic number P2
  • Add spaces (space, tab, CR, LF)
  • Add width, format is decimal ASCII characters
  • Add blank
  • Add height, format is ASCII decimal character
  • Add blank
  • Enter the maximum grayscale value, also use ASCII decimal format
  • Add spaces
  • Width x height grayscale value, each value is in ASCII decimal (range between 0 and maximum), separated by spaces from top to bottom.

Sample 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</p><p>"); // Writing Magic Number to the File
   fprintf(pgmimg, "%d %d</p><p>", w, h); // Writing Width and Height into the
   file
   fprintf(pgmimg, "255</p><p>"); // 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, "</p><p>");
   }
   fclose(pgmimg);
}

PGM image is shown below

Output

C program to write image in PGM format

The above is the detailed content of C program to write image in PGM format. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete