MODULE PixelAveraging; (* ========================================================= Loads row of pixels in Line 2, and distributes shifted versions of pixels in Lines 0, 1, 3, and 4 Line 0: x x x x x V W X Y Z x x x Line 1: x x x x V W X Y Z x x x x Line 2: x x x V W X Y Z x x x x x Line 3: x x V W X Y Z x x x x x x Line 4: x V W X Y Z x x x x x x x Then for each pixel, applies the following equation: line[2] := ( line[0] + line[1]*2 + 2*line[2] + line[3]*2 + line[4] ) DIV 10; ========================================================= *) CONST MAXSIZE = 8; MAXLINES = 5; CONFIGURATION CM[0..MAXSIZE-1]; CONNECTION left: CM[i]->CM[i-1]; right: CM[i]->CM[i+1]; VAR marker : CM OF BOOLEAN; accu : CM OF INTEGER; line : CM OF ARRAY[0..MAXLINES-1] OF INTEGER; mLine : CM OF ARRAY[0..MAXLINES-1] OF BOOLEAN; i : CM OF INTEGER; x : CM OF INTEGER; (* -------------------------------------------------------- find -------------------------------------------------------- *) PROCEDURE find( c : INTEGER ); BEGIN IF ( marker=TRUE ) AND ( accu<>c ) THEN marker:=FALSE ; END; (* FOR *) END find; (* -------------------------------------------------------- display functions displayCM: displays contents of accumulators displayLine: display Line whose index is passed as param displayLines: display all the lines displayLineChar: display contents of line as ASCII displayLinesChar: display contents of all lines in ASCII -------------------------------------------------------- *) PROCEDURE displayCM; BEGIN WriteInt( accu, 1 ); WriteLn; END displayCM; (* -------------------------------------------------------- *) PROCEDURE displayLine( n : INTEGER ); BEGIN WriteString( "Line " ); WriteInt( n, 1 ); WriteString( ": " ); WriteInt( line[n], 1 ); WriteLn; END displayLine; (* -------------------------------------------------------- *) PROCEDURE displayLines; VAR i : INTEGER; BEGIN FOR i:= 0 TO MAXLINES-1 DO displayLine( i ); END; (* FOR *) END displayLines; (* -------------------------------------------------------- *) PROCEDURE displayLineChar( n : INTEGER ); BEGIN WriteString( "Line " ); WriteInt( n, 1 ); WriteString( ": " ); Write( CHR( line[n] ) ); WriteLn; END displayLineChar; (* -------------------------------------------------------- *) PROCEDURE displayLinesChar; VAR i : INTEGER; BEGIN FOR i:= 0 TO MAXLINES-1 DO displayLineChar( i ); END; (* FOR *) END displayLinesChar; (* -------------------------------------------------------- initLine: reads the file data.txt and stores the numbers found in the line whose index is given. The number of integers in the file should be larger than MAXSIZE. initLines: reads the file data2.txt and stores the numbers found in the lines whose indexes are given in the file. Format of the file: 2 0: 2 4 6 8 10 12 14 16 1: 1 2 3 4 5 6 7 8 First line is # of lines to be initialized first integer before colon is line index. numbers after the colon are numbers to store in line. -------------------------------------------------------- *) PROCEDURE initLine( n : INTEGER ); VAR i : INTEGER; x : INTEGER; BEGIN OpenInput( "data.txt" ); FOR i:=1 TO MAXSIZE DO ReadInt( x ); line[n] <> := x; END; (* FOR *) CloseInput; END initLine; (* -------------------------------------------------------- *) PROCEDURE initLines; VAR i, j, x, n, L : INTEGER; c : CHAR; BEGIN OpenInput( "data2.txt" ); ReadInt( L ); FOR i:=1 TO L DO ReadInt( n ); Read( c ); FOR j:=1 TO MAXSIZE DO ReadInt( x ); line[n] <> := x; END; (* FOR *) END; (* FOR *) CloseInput; END initLines; (* -------------------------------------------------------- Shifts: shiftRight: shifts the given line (n) to the right by the given number of cells (count) shiftLeft: shifts the given line (n) to the left by the given number of cells (count) -------------------------------------------------------- *) PROCEDURE shiftRight( n, count : INTEGER ); VAR i : INTEGER; BEGIN FOR i:= 1 TO count DO line[n] := MOVE.right( line[n] ); END; (* FOR *) END shiftRight; PROCEDURE shiftLeft( n, count : INTEGER ); VAR i : INTEGER; BEGIN FOR i:= 1 TO count DO line[n] := MOVE.left( line[n] ); END; (* FOR *) END shiftLeft; (* -------------------------------------------------------- M A I N -------------------------------------------------------- *) BEGIN (*--- initialize Line 2 with pixels ---*) initLines; displayLine( 2 ); (*--- make copies and shift ---*) line[1]:= line[2]; shiftRight( 1, 1 ); line[0]:= line[1]; shiftRight( 0, 1 ); line[3]:= line[2]; shiftLeft( 3, 1 ); line[4]:= line[3]; shiftLeft( 4, 1 ); displayLines; (*--- apply averaging rule to pixels 3, 4, 5, and 6 ---*) IF 3 <= ID( CM ) <= 6 THEN line[2] := ( line[0] + line[1]*2 + 2*line[2] + line[3]*2 + line[4] ) DIV 10; END; (* FOR *) WriteLn; WriteString( "Results:" ); WriteLn; displayLines; END PixelAveraging.