33
5
Background
ASCII art is the practice of creating images by using ASCII text to form shapes.
Aliasing is the effect created by the large "pixels" of ASCII art, which are the size of characters. The image becomes blocky and hard to see. Anti-aliasing removes this effect by creating a gradient and by softening the hard edges of the ASCII art.
The Challenge
Your challenge is to write the shortest program possible that will take a piece of ASCII art and will output a version that has been anti-aliased.
What sort of anti-aliasing?
All of the ASCII art will consist of two types of symbols: Spaces and non-whitespace. For each non-whitespace character, your program must determine whether it is in a position where it need to be anti-aliased. If it is, then you need to replace it with the correct character. If it isn't, then the character stays the same.
How do you know if a character needs to be anti-aliased? The answer depends on the characters that are immediately above, below, left, and right of the character (not the diagonals). Here is a chart of when anti-aliasing is required, where ?
and x
can stand for any non-whitespace character.
x? -> d?
? ?
?x -> ?b
? ?
? ?
?x -> ?F
? ?
x? -> Y?
x -> ; Note: This character has been changed from _ to ;
? ?
? ?
x -> V
?x -> ?>
x? -> <?
x -> @
Input (and example pre-anti-aliasing ASCII art)
First, there will be two lines of input (to STDIN), a number H followed by a number W. There will then be H lines of exactly W characters each (excluding the newline). These following lines will be the ASCII art that needs to be anti-aliased. Here is an example input (not beautiful, but a test):
7
9
888888
888888
999 98 7
666666
666666
6666
6
Output (and example anti-aliased art)
Your program should output to STDOUT the ASCII art (of the same dimensions), which has been anti-aliased. Here is the output for the above input. Notice how the the border characters are treated as bordering whitespace.
d8888>
d8888F
<99 98 @
Y6666b
Y6666>
Y66F
V
This might not look that good (due to the space between lines in the code block), it looks better with larger ASCII art, and the quality depends on the exact font used.
Another Example
Input
12
18
xx xxx xxx
xxxx xxx xxx
xxxxxx xxx xxx
xxx xxx xxx xxx
xxxx xxx xxx xxx
xxxxxx xxx xxx
xxxx xxx xxx
x xx xxx xxx x
xx xxx xxx xx
xxx xxx xxx xxx
xxxx xxx xxx xx
xxxxx xxx xxx x
Output
db <xb <xb
dxxb Yxb Yxb
dxxxxb Yxb Yxb
dxx xxb xxb xxb
Yxxb xxF xxF xxF
YxxxxF dxF dxF
YxxF dxF dxF
; YF dxF dxF ;
xb dxF dxF dx
xxb <xF <xF <xx
xxxb Yxb Yxb Yx
Yxxx> Yx> Yx> V
Rules, Restrictions, and Notes
Your program should be written in printable ASCII characters only, so that we can make art out of the programs. Other than that, the standard code-golf rules apply.
Since there are no answers yet, I have changed one character in the anti-aliasing chart.
_
has become;
because it works better. – PhiNotPi – 2012-04-11T15:15:39.240This might be my favorite code golf question of all time. Working on like 4 different awesome solutions. – captncraig – 2012-04-12T01:46:33.357
Although I am confused. You say diagonals don't count, but your diagrams all show question marks filling out the diagonals. From what I see in the examples, it may be safe to only look to the sides, but I get confused? Do diagonals ever matter? – captncraig – 2012-04-12T03:17:47.907
No, the diagonals never matter. It will probably be more clear if I removed the diagonals from the chart. – PhiNotPi – 2012-04-12T03:23:43.217
I think there might be a typo in your example; I believe the right-hand column should have Ys on the inside edge. Enjoyed coming up with the answer to this one though, good question :D – Ed James – 2012-04-12T19:16:53.187
Sorry. I'll fix that. – PhiNotPi – 2012-04-12T19:19:43.057