#!/usr/bin/perl

# Nathan V. Patwardhan <nvp@colltech.com>

use Getopt::Long;
use GD;
use strict;

my($pre, $i, $name, $image, $r, $g, $b) = undef;

GetOptions('p|pre' => \$pre,
           'h|help'=> \&usage);
usage() if !(@ARGV); ($name) = (@ARGV);

open(GIF, $name) or die("can't open image file for: $name\n");
$image = GD::Image->newFromGif(\*GIF) or die("can't create new image obj\n");
close(GIF);

binmode STDOUT;

$i = 0;
my($wd, $ht) = $image->getBounds();
print "<PRE>\n" if defined($pre);
foreach my $w (1 .. $ht) {
    foreach my $h (1 .. $wd) {
	print "\n" if $i % $wd == 0;
	my $index = $image->getPixel($h, $w);
	($r,$g,$b) = $image->rgb($index);
        my $hex_c = sprintf("%X", abs(($r+$g+$b)/3));
	if($hex_c eq 'FF' || $hex_c eq 'F9') {
		print "  ";
	} else {
	    printf "%02s", $hex_c;
	}
	$i++;
    }
}
print "\n</PRE>\n" if defined($pre);
print "\n";

sub usage {
    print("Usage: $0 ...\n",
          "\t-p|-pre|-prehtml|-preformat <infile>\n");
    exit(1);
}


