#!/usr/bin/perl

use GD;

GD::Image->trueColor(1);
my %fez;

$fez{A} =  
	"xxxxx\n". 
	"  x  \n". 
	"x   x\n". 
	"xxxxx\n". 
	"xxxxx\n";

$fez{A} =
	"xxxxx\n".
	"  x  \n".
	"x   x\n".
	"xxxxx\n".
	"xxxxx\n";

$fez{B} =
	"xxxxx\n".
	"x   x\n".
	"x x x\n".
	"    x\n".
	"xxxxx\n";

$fez{C} =
	"xxxxx\n".
	"xxx  \n".
	"xxx x\n".
	"xxx  \n".
	"xxxxx\n";

$fez{D} =
	"xxxxx\n".
	"xxxxx\n".
	"xxxxx\n".
	"  xxx\n".
	"x xxx\n";

$fez{E} =
	"xxxxx\n".
	"  xxx\n".
	"x xxx\n".
	"x    \n".
	"xxxxx\n";

$fez{F} =
	"xxxxx\n".
	"x x x\n".
	"    x\n".
	"x x x\n".
	"xxxxx\n";

$fez{G} =
	"xxx x\n".
	"xx  x\n".
	"xx xx\n".
	"xx  x\n".
	"xxx x\n";

$fez{H} =
	"xxxxx\n".
	"x   x\n".
	"x x x\n".
	"x   x\n".
	"xxx x\n";

$fez{I} =
	"x x x\n".
	"x   x\n".
	"xxxxx\n".
	"xxxxx\n".
	"xxxxx\n";

$fez{J} =
	"xxxxx\n".
	"xxxxx\n".
	"xxxxx\n".
	"xxx  \n".
	"xxx x\n";

$fez{K} =
	"xxx x\n".
	"xxx x\n".
	"xxx x\n".
	"x   x\n".
	"x xxx\n";

$fez{L} =
	"xxxxx\n".
	"x   x\n".
	"xx xx\n".
	"x   x\n".
	"xx xx\n";

$fez{M} =
	"xxxxx\n".
	"xxxxx\n".
	"x   x\n".
	"  x  \n".
	"xxxxx\n";

$fez{N} =
	"xxxxx\n".
	"x    \n".
	"x x x\n".
	"x   x\n".
	"xxxxx\n";

$fez{O} =
	"xxxxx\n".
	"  xxx\n".
	"x xxx\n".
	"  xxx\n".
	"xxxxx\n";

$fez{P} =
	"xxx x\n".
	"xxx  \n".
	"xxxxx\n".
	"xxxxx\n".
	"xxxxx\n";

$fez{Q} =
	"xxx x\n".
	"xxx x\n".
	"xxx x\n".
	"x   x\n".
	"x xxx\n";

$fez{R} =
	"xxxxx\n".
	"x x x\n".
	"x    \n".
	"x x x\n".
	"xxxxx\n";

$fez{S} =
	"x xxx\n".
	"x  xx\n".
	"xx xx\n".
	"x  xx\n".
	"x xxx\n";

$fez{T} =
	"x xxx\n".
	"x   x\n".
	"x x x\n".
	"x   x\n".
	"xxxxx\n";

$fez{U} =
	"xxxxx\n".
	"xxxxx\n".
	"xxxxx\n".
	"x   x\n".
	"x x x\n";

$fez{V} =
	"xxxxx\n".
	"xxxxx\n".
	"xxxxx\n".
	"x   x\n".
	"x x x\n";

$fez{W} =
	"x xxx\n".
	"  xxx\n".
	"xxxxx\n".
	"xxxxx\n".
	"xxxxx\n";

$fez{X} =
	"xxx x\n".
	"x   x\n".
	"x xxx\n".
	"x xxx\n".
	"x xxx\n";

$fez{Y} =
	"xx xx\n".
	"x   x\n".
	"xx xx\n".
	"x   x\n".
	"xxxxx\n";

$fez{Z} =
	"xxxxx\n".
	"    x\n".
	"xxx x\n".
	"xxx  \n".
	"xxxxx\n";

$fez{0} =
	"xxxxx\n".
	"x   x\n".
	"x   x\n".
	"x   x\n".
	"xxxxx\n";

$fez{1} =
	"xxxxx\n".
	"x x x\n".
	"x x x\n".
	"x   x\n".
	"xxxxx\n";

$fez{2} =
	"xxxxx\n".
	"x   x\n".
	"x xxx\n".
	"x   x\n".
	"xxxxx\n";

$fez{3} =
	"xxxxx\n".
	"x   x\n".
	"x x x\n".
	"x x x\n".
	"xxxxx\n";

$fez{3} =
	"xxxxx\n".
	"x x x\n".
	"x xxx\n".
	"x   x\n".
	"xxxxx\n";

$fez{4} =
	"xxxxx\n".
	"x   x\n".
	"xxx x\n".
	"x   x\n".
	"xxxxx\n";

$fez{4} =
	"xxxxx\n".
	"x x x\n".
	"x x x\n".
	"x x x\n".
	"xxxxx\n";

$fez{5} =
	"xxxxx\n".
	"x x x\n".
	"xxx x\n".
	"x   x\n".
	"xxxxx\n";

$fez{5} =
	"xxxxx\n".
	"x   x\n".
	"x xxx\n".
	"x x x\n".
	"xxxxx\n";

$fez{6} =
	"xxxxx\n".
	"x   x\n".
	"xxxxx\n".
	"x   x\n".
	"xxxxx\n";

$fez{6} =
	"xxxxx\n".
	"x x x\n".
	"x xxx\n".
	"x x x\n".
	"xxxxx\n";

$fez{7} =
	"xxxxx\n".
	"x   x\n".
	"xxx x\n".
	"x x x\n".
	"xxxxx\n";

$fez{7} =
	"xxxxx\n".
	"x x x\n".
	"xxxxx\n".
	"x   x\n".
	"xxxxx\n";

$fez{8} =
	"xxxxx\n".
	"x x x\n".
	"xxx x\n".
	"x x x\n".
	"xxxxx\n";

$fez{9} =
	"xxxxx\n".
	"x   x\n".
	"xxxxx\n".
	"x x x\n".
	"xxxxx\n";

$fez{10} =
	"xxxxx\n".
	"x x x\n".
	"xxxxx\n".
	"x x x\n".
	"xxxxx\n";

my @chars = ('A' .. 'Z', 0..9);

if (!$ARGV[0]) {
	print "$0 V.01\n takes text from inputfile and creates a png file named inputfile.png encoded with fez symbols\n\n";
	print " usage: $0 inputfile\n\n";
	exit;
}

my $filename = "$ARGV[0]";

print "$filename\n";

my $text = uc(`cat $ARGV[0]`);
$text =~ s/ //mg;
my $columns = $text =~ s/\n/Z\n/mg;
my @text;
push(@text, split('', $text));


my $max = 0;
for $line (split("\n", $text)) {
    my $len = length($line);
    $max = $len if $len > $max;
}
print "$max\n";


my $width = (6  * $columns) + 1;   #5 * number of columns + 1 pixel before first column
my $height = (6 * $max) + 1; #5 * number of chars + a pixel after each letter + a pixel before first letter
my $symbol_size = 5;

my $image_red = new GD::Image($width,$height, 1) || die;

my $row=0;
my $col=0;
my $w=1;
my $h=1;
for (@text) {
	$row++;

	if ($_ =~ /\n/) {
		#need to fill in the rest of the column with random data.		
		while ($row <= $max ) {
			$row++;
			my $pixels = &encodeLetter($chars[int(rand($#chars))]);
			my @pixels;
			push(@pixels, split('', $pixels));
			for (@pixels) {
				if ($_ eq " ") { 
					$r = 0;
				} elsif ($_ eq "x") { 
					$r = 1;
				} elsif ($_ eq "\n") {
					$h++;
					$w=1;
					next;
				}
				$image_red->setPixel($col * 6 + $w,$h,$image_red->colorExact( (($r & 1<<$bit_shift)>>$bit_shift)*255,(($r & 1<<$bit_shift)>>$bit_shift)*255,(($r & 1<<$bit_shift)>>$bit_shift)*255));

				$w++;
			}

			#print a space after each letter
			for (0..4) {
				$r = 0;
				$image_red->setPixel($w,$h,$image_red->colorExact( (($r & 1<<$bit_shift)>>$bit_shift)*255,(($r & 1<<$bit_shift)>>$bit_shift)*255,(($r & 1<<$bit_shift)>>$bit_shift)*255));		
				$w++;
			}
			$h++;
			$w=1;


		}

		$col++;
		$h=1;
		$row=0;
		next;
	} else {
		printf("R%03u,C%03u [%s]\n", $row, $col, $_);
	}

	
	my $pixels = &encodeLetter($_);
	my @pixels;
	push(@pixels, split('', $pixels));
	for (@pixels) {
		if ($_ eq " ") { 
			$r = 0;
		} elsif ($_ eq "x") { 
			$r = 1;
		} elsif ($_ eq "\n") {
			$h++;
			$w=1;
			next;
		}
		$image_red->setPixel($col * 6 + $w,$h,$image_red->colorExact( (($r & 1<<$bit_shift)>>$bit_shift)*255,(($r & 1<<$bit_shift)>>$bit_shift)*255,(($r & 1<<$bit_shift)>>$bit_shift)*255));		

		$w++;
	}

	#print a space after each letter
	for (0..4) {
		$r = 0;
		$image_red->setPixel($w,$h,$image_red->colorExact( (($r & 1<<$bit_shift)>>$bit_shift)*255,(($r & 1<<$bit_shift)>>$bit_shift)*255,(($r & 1<<$bit_shift)>>$bit_shift)*255));		
		$w++;
	}
	$h++;
	$w=1;

}

print "\n";
sub encodeLetter() {
	my $letter = shift();
	my $encode;
	
	$encode = $fez{$letter};

	return $encode;
}


print "new image size: width($width) height($height)\n";


my $png_data = $image_red->png();
open (DISPLAY,">$filename".".png") || die;
binmode DISPLAY;
print DISPLAY $png_data;
close DISPLAY;

exit;

