ShapeSorter – a simple PGM processing example

This is a simple PGM image processing example program.

//
// shapesorter.c - Loads, modifies and saves a PGM file
// written by Ted Burke - last modified 21-11-2011
//
// To compile shapesorter:
//
//		gcc -o shapesorter.exe shapesorter.c
//
// The default input file is input.pgm
// The default output file is output.pgm
// Other input and output filenames can be specified
// as command line arguments. For example,
//
//		shapesorter.exe image001.pgm modified.pgm
//

#include <stdio.h>

// 2D pixel array, like a spreadsheet with 1000 rows & 1000 columns
int p[1000][1000];

// Other image properties - width, height, white value
int w, h, maxval;

int main(int argc, char *argv[])
{
	// Variables
	FILE *f;
	char line[200];
	int x, y;

	///////////////////////////////////////////////
	/// LOAD PIXEL DATA FROM FILE INTO 2D ARRAY ///
	///////////////////////////////////////////////

	// Open input image file
	printf("Opening input file\n");
	if (argc > 1)
	{
		f = fopen(argv[1], "r");
	}
	else
	{
		f = fopen("input.pgm", "r");
	}

	// Read 4 header lines.
	// The first two are simply ignored. The next two
	// contain the width, height and white value.
	printf("Reading file header info\n");
	fscanf(f, "%[^\n]\n", line);
	fscanf(f, "%[^\n]\n", line);
	fscanf(f, "%d %d\n", &w, &h);
	fscanf(f, "%d\n", &maxval);

	// Read pixel data into 2-D array
	printf("Reading pixel data from file\n");
	y = 0;
	while(y < h)
	{
		x = 0;
		while(x < w)
		{
			fscanf(f, "%d", &p[y][x]);
			x = x + 1;
		}
		y = y + 1;
	}

	// Close input file
	printf("Closing input file\n");
	fclose(f);

	//////////////////////////////////////
	/// MODIFY ALL PIXELS IN THE IMAGE ///
	//////////////////////////////////////

	// Modify all pixel values one at a time
	printf("Modifying pixel data\n");
	y = 0;
	while(y < h)
	{
		x = 0;
		while(x < w)
		{
			// Just as an example, invert pixel colour
			p[y][x] = 255 - p[y][x];

			x = x + 1;
		}
		y = y + 1;
	}

	//////////////////////////////
	/// OUTPUT IMAGE TO A FILE ///
	//////////////////////////////

	// open output file
	printf("Opening output file\n");
	if (argc > 2)
	{
		f = fopen(argv[2], "w");
	}
	else
	{
		f = fopen("output.pgm", "w");
	}

	// Print header info into file
	printf("Printing file header to file\n");
	fprintf(f, "P2\n");
	fprintf(f, "# My PGM file\n");
	fprintf(f, "%d %d\n", w, h);
	fprintf(f, "%d\n", maxval);

	// Print pixel data into file
	printf("Printing pixel data to file\n");
	y = 0;
	while(y < h)
	{
		x = 0;
		while(x < w)
		{
			fprintf(f, "%d ", p[y][x]);
			x = x + 1;
		}
		fprintf(f, "\n");
		y = y + 1;
	}

	// Close file
	printf("Closing output file\n");
	fclose(f);

	// exit normally
	return 0;
}
Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s