#!/usr/bin/perl -w

# This takes a file that is a 1 byte grayscale (or, in some cases, an indexed 
# color file) and converts it into a PPM for viewing. 

die "Usage: genPPMfrom1Color.pl <input 1 color raw> <output PPM> <width> <height>\n" unless $#ARGV==3;

$in = shift;
$out = shift;
$width = shift;
$height = shift;

open(INFILE, "<$in") or die "Can't open $in: $!";
open(OUTFILE, ">$out") or die "Can't open $out: $!";

printf( OUTFILE "P3 ");
printf( OUTFILE $width );
printf( OUTFILE " " );
printf( OUTFILE $height );
printf( OUTFILE " 255\n");

$idx = 0;

while( read(INFILE, $byte, 1) != 0 ) {
    $r = vec($byte, 0, 8);

    $idx++;
    if ( $idx > 10 ) { printf OUTFILE "\n"; $idx = 0; }
    print( OUTFILE $r, " ", $r, " ", $r, " " );

}
close INFILE;
close OUTFILE;

