25 Years of Programming
An open source source for C, C++, OWL, BASIC, MDB, XLS, DOT, and more...
Home   Projects   Up   Sitemap   Search   Blog   Forum+Chat   About Us   Privacy   Terms of Use   Feedback   FAQ   Images   Services   Payments   Humor   Music

Heathkit H-100 DeSmet C program to blur an image by converting its pixel values to moving averages

Some of my H-100 graphics programs saved images in a format that I called .PIC. The H-100 only had 8 color values, numbered 0-7. A .PIC file recorded the color values of a 640x225 pixel screen as 225 lines of 640 chars each.

This program converts each pixel value in the file to its 30-item moving average (in the horizontal direction only).

Additional description is in the comments below, with links to programs where this method is implemented much better.

smoothe.c

/*	smoothe.c	11/26/93
	Copyright (C)1993 Steven Whitney.
	Initially published by http://25yearsofprogramming.com.
	Published under GNU GPL (General Public License) Version 3, with ABSOLUTELY NO WARRANTY.

	REDUCES THE JAGGEDNESS OF A .PIC FILE BY CONVERTING ITS VALUES TO N-ITEM MOVING AVERAGES.
	THE MANDELBROT SET, WHEN THE ESCAPE TIMES ARE INTERPRETED AS ELEVATIONS, IS VERY SPIKY, 
	WITH HIGH PEAKS IMMEDIATELY ADJACENT TO LOW VALLEYS. IF YOU WANT TO TRY TO PLOT IT AS 
	A 3D TERRAIN, IT HELPS TO SMOOTHE THE CONTOURS SO THE ELEVATION JUMPS ARE LESS ABRUPT.  
	THE METHOD HERE IS IMPERFECT, AND THE RESULT NOT PARTICULARLY EFFECTIVE ON THE H100. 
	THE OPERATION OF AVERAGING PIXEL VALUES IS EQUIVALENT TO THE OPERATION OF BLURRING AN IMAGE 
	IN A GRAPHICS PROGRAM. MUCH IMPROVED METHODS CAN BE FOUND IN SDIB.CPP AND IMPLEMENTED IN WINBROT.CPP.

*/
#include "stdio.h"
main(argc,argv)
int argc;
char **argv;
{
FILE *infile, *outfile;
char linebuf[640];				/* read/write line of chars */
int values[640];				/* ints for computing moving averages */
int sum, movavg;
int n = 30;						/* number of items to average */
unsigned i, j;

if(argc < 2)
	abort("Usage: SMOOTHE source      (output is MOVAVG.PIC)\n");
if(!(infile = fopen(argv[1],"r")))
	abort("Input file not found.\n");
outfile = fopen("movavg.pic","w");

puts("Enter number of items to average, <CR> = 30: ");
gets(linebuf);
if(strlen(linebuf))
	n = atoi(linebuf);
									/* first, compute movavgs horizontally */
j = 0;
while(fread(linebuf,1,640,infile))		
{
	printf("Line %d of 225\n",++j);
	sum = 0;
	for(i = 0 ; i < 640 ; i++)
	{
		values[i] = (int)(linebuf[i]);		/* convert char to int */
		sum += values[i];					/* compute running total */
		if(i > (n-1))						/* after 30 items, */
			sum -= values[i - n];			/* take item (i-30) out of sum */
		movavg = sum / MIN(i+1,n);			/* 1 to N, and after that, N */
		linebuf[i] = (char)movavg;			/* put movavg back into buffer */
	}
	fwrite(linebuf,1,640,outfile);			/* write the modified line */
}
closeall();
exit(0);
}

 

 

Valid HTML 4.01 Transitional Valid CSS
Yahoo! Search
Search the web Search this site
View content labeling at ICRA.