Skip to content

Instantly share code, notes, and snippets.

@kulp
Created January 28, 2013 01:51
Show Gist options
  • Save kulp/4652192 to your computer and use it in GitHub Desktop.
Save kulp/4652192 to your computer and use it in GitHub Desktop.
Convert well-behaved NAFE (nafe.sf.net) TXT output to a linear PNG
#!/usr/bin/env perl
use strict;
use GD;
my ($char_count, $char_width, $char_height);
my $im;
my ($fg, $bg);
while (<>) {
/^\+\+font-text-file/ && next;
/^\+\+chars/ && chomp($char_count = <>);
/^\+\+width/ && chomp($char_width = <>);
/^\+\+height/ && chomp($char_height = <>);
/^\+\+---[0-9]+-(0x[[:xdigit:]]+)-.*$/ && do {
do_inits() unless $im;
my @lines = map { chomp(my $f = scalar <>); $f } 1 .. $char_height;
process_char($im, hex $1, \@lines);
};
}
print $im->png;
sub do_inits
{
my $image_width = $char_count * $char_width;
$im = GD::Image->new($image_width, $char_height);
$fg = $im->colorAllocate(255,255,255);
$bg = $im->colorAllocate(0,0,0);
}
sub process_char
{
my ($im, $i, $lines) = @_;
my $y = 0;
for my $line (@$lines) {
my $x = $char_width * $i;
for my $pix (split //, $line) {
my $colour = ($fg, $bg)[$pix eq ' '];
$im->setPixel($x++, $y, $colour);
}
$y++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment