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