#!/usr/bin/perl -w

die "Usage: gen5551.pl <input Photoshop RGBA32 raw format> <output> <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");

while( read(INFILE, $byte, 4) != 0 ) {
    $r = vec($byte, 0, 8);
    $g = vec($byte, 1, 8);
    $b = vec($byte, 2, 8);

#    if ($r<4.0 && $g<4.0 && $b<4.0) {
#        $rgb = 0;
#    } else {
#        $rgb = ($r<<11 & 0xf800) | ($g<<6 & 0x07c0) | ($b<<1 & 0x003e) | 1;
#    }
    $idx++;
    if ( $idx > 10 ) { printf OUTFILE "\n"; $idx = 0; }
    print( OUTFILE $r, " ", $g, " ", $b, " " );
}
close INFILE;
close OUTFILE;
