(************************************************************************* ** ASCII-art converter ** ** For: 1998 UMCP Programming Contest ** Author: David Mount ** Date: Feb 26, 1998 ** ** This program inputs an image as a 2-dimensional array of real ** grayscale values in the range from 0 (black) to 1 (white), and ** outputs an ASCII approximation of the image. The input is ** assumed to have at most 100 rows and 100 columns. ** ** The image is partitioned into blocks. Each block is of a given ** height and width in pixels. The image is then partitioned into ** blocks (with any leftover portions ignored). The gray values ** in each block are averaged, and then this average is mapped to ** an ASCII character, which is output. ** ** The input consists of the image height H and width W, the block ** height h and width w, followed by a sequence of H*W reals, ** consisting of the gray values, input row-by-row, from top to ** bottom and left to right. The final average is mapped to an ** ASCII character as follows: ** ** 0.0 <= Avg < 0.1 . ** 0.1 <= Avg < 0.2 , ** 0.2 <= Avg < 0.3 ; ** 0.3 <= Avg < 0.4 ! ** 0.4 <= Avg < 0.5 v ** 0.5 <= Avg < 0.6 l ** 0.6 <= Avg < 0.7 L ** 0.7 <= Avg < 0.8 F ** 0.8 <= Avg < 0.9 E ** 0.9 <= Avg <= 1.0 $ ** **************************************************************************) program ascart(input, output); const SIZE = 100; var Image: array [1..SIZE, 1..SIZE] of real; (* image array *) ih, iw: integer; (* image height and width *) bh, bw: integer; (* block height and width *) oh, ow: integer; (* output height and width *) ii, ij : integer; (* image indices *) bi, bj : integer; (* block indices *) oi, oj : integer; (* output indices *) sum, avg: real; (* sum and average of block *) begin readln(ih, iw); (* input size and image *) readln(bh, bw); (* input block size *) oh := ih div bh; (* compute output size *) ow := iw div bw; for ii := 1 to ih do begin (* read the image *) for ij := 1 to iw do begin read(Image[ii,ij]); end; end; for oi := 0 to oh-1 do begin (* iterate over output rows *) ii := oi*bh; (* corresp. row in input *) for oj := 0 to ow-1 do begin (* iterate over output cols *) ij := oj*bw; (* corresp. col in input *) sum := 0; (* compute sum of block *) for bi := 1 to bh do begin for bj := 1 to bw do begin sum := sum + Image[ii+bi, ij+bj]; end; end; avg := sum / (bw*bh); (* average gray value *) (* write the ASCII value *) if (avg < 0.10) then write('.') else if (avg < 0.20) then write(',') else if (avg < 0.30) then write(';') else if (avg < 0.40) then write('!') else if (avg < 0.50) then write('v') else if (avg < 0.60) then write('l') else if (avg < 0.70) then write('L') else if (avg < 0.80) then write('F') else if (avg < 0.90) then write('E') else write('$') end; writeln; end; end.