6 my ($fname, $array_name) = @ARGV;
8 die "Usage: $0 <file>\n" unless $fname;
11 open $fh, '<', $fname or die "$0: $!\n";
14 die "$0: $fname: unknown file format\n" unless $header eq "P4\n";
20 last if ($len) = $line =~ /^8 (\d+)$/;
21 redo if $line =~ /^#/;
22 die "$0: $fname: couldn't parse header\n";
26 # read rest of file and return a list of the ascii value for each byte
27 my @bin = map { ord($_) } split //, <$fh>;
29 die "$0: $fname should have $len bytes image data, got: " . scalar(@bin) . "\n"
30 unless scalar(@bin) == $len;
32 say "/* autogenerated by util/mkfont */";
33 say "const unsigned char $array_name\[$len] = {";
35 # break up in max. 8 bytes per line
36 while (my @line = splice @bin, 0, 8) {
37 # seperated by commata, depending on how many bytes we have
38 my $hexify = join ', ', ('0x%02x') x scalar(@line);
39 printf " $hexify" , @line;
40 # if there's more bytes left, seperate next block by a comma
41 print scalar(@bin) > 0 ? ",\n" : "\n";