Generating PBM bitmap files from ASCII text The PBM (Portable BitMap) format is a very simple ASCII black and white bitmap format.

Here's an example for the letter 'J' (copy-pasted from the wikipedia link):


P1
# This is an example bitmap of the letter "J"
6 10
0 0 0 0 1 0
0 0 0 0 1 0
0 0 0 0 1 0
0 0 0 0 1 0
0 0 0 0 1 0
0 0 0 0 1 0
1 0 0 0 1 0
0 1 1 1 0 0
0 0 0 0 0 0
0 0 0 0 0 0


It's time we build a small tool to generate files in this nifty little format!

Your goal is to write the shortest program (in any language) that conforms to the following rules:


Your program takes one string from stdin (for example CODEGOLF.STACKEXCHANGE.COM!)
It generates a PBM file with a bitmapped (readable) representation of the string.
Each character is constructed as a 8x8 grid.
You must support the characters [A-Z] (all uppercase), space, a point ('.') and an exclamation mark ('!').
No external libraries allowed (certainly no PBM related ones)!
The used character set must not simply be external to your program. Part of the challenge is storing the characters efficiently...


Testing for validness of the PBM format can be done with the GIMP (or others). Do show off sample input and output!

The shortest solution will be awarded the answer points on 2012-01-31.

Have fun golfing!

PS: I've added a bounty (percentage-wise a huge part of my codegolf reputation) to (hopefully) draw in more competitors.
Perl, 164 bytes, no zlib/gzip compression

After sleeping on the problem, I managed to figure out a much shorter solution than my first one.  The trick is to take advantage of a minor loophole in the rules: the characters need to fit in 8 by 8 pixels each, but nothing says they have to fill all that space.  So I drew my own 4 by 5 pixel font, allowing me to pack two characters into 5 bytes.

The output looks like this:

 (scaled x 4)

&nbsp; &nbsp;  (original size)

Before giving the actual code with the embedded font data, let me show a de-golfed version:

y/A-Z!./\0-\033/ for @a = &lt;&gt; =~ /./g;
say "P4 " . 8*@a . " 8";
for $p (qw'PACKED FONT DATA') {
    print chr vec $p, ord, 4 for @a;
}


In the actual code, the PACKED FONT DATA is replaced by a binary string consisting of eight whitespace-delimited rows (four 14-byte rows and one 13-byte one, plus three single null bytes for the blank rows).  I deliberately designed my font so that the packed data contains no whitespace, single quotes or backslashes, so that it could be encoded in qw'...'.

Since the packed font string contains unprintable characters, I've provided the actual script as a hex dump.  Use xxd -r to turn it back into executable Perl code:

0000000: 792f 412d 5a21 2e2f 002d 1b2f 666f 7240  y/A-Z!./.-./for@
0000010: 613d 3c3e 3d7e 2f2e 2f67 3b73 6179 2250  a=&lt;&gt;=~/./g;say"P
0000020: 3420 222e 382a 4061 2e22 2038 223b 666f  4 ".8*@a." 8";fo
0000030: 7224 7028 7177 2700 20e6 e6ff 9612 8999  r$p(qw'. .......
0000040: e6e6 7759 99f5 0420 9999 8898 128a df99  ..wY... ........
0000050: 9928 5999 1504 20ef 98ee fb12 8cb9 e9e9  .(Y... .........
0000060: 2659 6965 0420 9999 8899 928a 9989 ab21  &amp;Yie. .........!
0000070: 599f 8220 e9e6 8f96 62f9 9986 972e 2699  Y.. ....b.....&amp;.
0000080: f284 2000 2000 2729 7b70 7269 6e74 2063  .. . .'){print c
0000090: 6872 2076 6563 2470 2c6f 7264 2c34 666f  hr vec$p,ord,4fo
00000a0: 7240 617d                                r@a}


Here's how it works:


The first line (in the de-golfed version) reads a single line of input, splits it into an array of characters (conveniently omitting any trailing newlines) and maps the letters A to Z and the characters ! and . to the character codes 0 to 28, which normally correspond to unprintable control characters in ASCII / Unicode.  (A minor side effect of this is that any tabs in the input get printed as Js.)  The space character is left unmapped, since the output loop turns any codes above 28 into blanks anyway.
The second line just prints the PBM header.  It uses the Perl 5.10 say feature, so you need to run this script with perl -M5.010 for it to work.
The output loop takes a whitespace-delimited list of packed image rows and assigns each of them into $p in turn.  (I designed the font so that the packed data wouldn't contain any whitespace or ' characters.)  It then loops over the input characters in @a, using Perl's vec command to extract the 4-bit nibble corresponding to the mapped character code from the image row, pads it to an 8-bit byte and prints it.




Old answer, 268 bytes:

This is a quick and dirty first attempt.  I stole PleaseStand's font and compressed it along with my source code.  Since the resulting script is mostly unprintable, here's a hexdump; use xxd -r to turn it into executable Perl code:

0000000: 7573 6520 436f 6d70 7265 7373 275a 6c69  use Compress'Zli
0000010: 623b 6576 616c 2075 6e63 6f6d 7072 6573  b;eval uncompres
0000020: 7320 2778 da85 d03d 4b03 4118 85d1 452c  s 'x...=K.A...E,
0000030: b69c 72cb 7519 4894 552c 2c02 3319 ee5c  ..r.u.H.U,,.3..\
0000040: d7b8 5a89 6093 4634 7e82 c490 6c91 8597  ..Z.`.F4~...l...
0000050: 80fe 7267 d660 23ae e52d 0e0f dcd6 f8c3  ..rg.`#..-......
0000060: e9d1 5e6e ccec a15c ddb5 c5d5 495e 94a3  ..^n...\....I^..
0000070: 83b7 c7f9 73f3 5216 f9a8 787a 5fea 666c  ....s.R...xz_.fl
0000080: 9dd1 b763 dd98 76f8 2df6 0799 5811 7144  ...c..v.-...X.qD
0000090: 4acc ee9d b8b0 c90f 7e4a 8264 6016 cbd7  J.......~J.d`...
00000a0: 79f3 1b91 047c 4055 409e 9e54 1dda ed41  y....|@U@..T...A
00000b0: 9a20 8080 6adc 5c47 8488 7495 f621 01d7  . ..j.\G..t..!..
00000c0: 6b6c 902e b6c8 2a6a 6643 f56f e99c 115d  kl....*jfC.o...]
00000d0: 5c7a f1b2 13d0 3453 790f da74 c813 751d  \z....4Sy..t..u.
00000e0: 11ce d821 ad90 247f 2292 5b54 c14f 3c4e  ...!..$.".[T.O&lt;N
00000f0: 49c5 4c53 a1a7 c478 391c 714c f113 0747  I.LS...x9.qL...G
0000100: ab6c 4482 9fd2 177a 5677 6327            .lD....zVwc'


The decompressed Perl code consists of the following preamble:

y;A-Z.! ;;cd,say"P4 ",8*length," 8"for$t=&lt;&gt;


followed by eight repetitions of the following code:

;$_=$t;y(A-Z.! )'BITMAP DATA HERE';print


with BITMAP DATA HERE replaced with 29 bytes encoding one row of the font.