#!/usr/bin/perl -w

use strict;
use GD;
use vars qw( $Image $Width $Height );


# foreach (32..255) { print( ( $_ >=127 and $_ <=160) ? '.' : chr($_), ' '); }; print "\n"; exit;
# foreach (32..255) { printf( "%s\t%d\t%o\n", ( $_ >=127 and $_ <=160) ? '.' : chr($_), $_, $_); }; print "\n"; exit;


my $filename = $ARGV[0] || '../data/brandybun_flat.png';
print "Processing $filename\n";

$Image = GD::Image->newFromPng( $filename, 1);
$Width = $Image->width;
$Height = $Image->height;
print "Image is $Width x $Height\n";

my ($transparent, $charColor, $x, $y);
$transparent = $Image->getPixel(0,$Height-1);
print(sprintf 'Transparent color is %x\n', $transparent);
$charColor = 0xffffff;

for ($x = 0; $x < $Width; ++$x) {
  $Image->setPixel($x, 0, 0);
  for ($y = 1; $y < $Height; ++$y) {
    if ($Image->getPixel($x, $y) != $transparent) {
      $Image->setPixel($x, 0, $charColor);
      print "#";
      last;
    }
  }
  print "."  if $y == $Height;
}

close OUTPUT;
open OUTPUT, ">out.png"  or die;
binmode OUTPUT;
$Image->saveAlpha(1);
print OUTPUT $Image->png;
close OUTPUT;

