#!/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, 2) != 0 ) {
    $r = vec($byte, 0, 8);
    $g = vec($byte, 0, 8);
    $b = vec($byte, 0, 8);

    $idx++;
    if ( $idx > 10 ) { printf OUTFILE "\n"; $idx = 0; }
    print( OUTFILE $r, " ", $g, " ", $b, " " );
}
close INFILE;
close OUTFILE;
