#!/usr/bin/perl -w

die "Usage: gen5551.pl <input Photoshop RGB raw format> <output> \n" unless $#ARGV==1;

$in = shift;
$out = shift;

open(INFILE, "<$in") or die "Can't open $in: $!";
open(OUTFILE, ">$out") or die "Can't open $out: $!";

while( read(INFILE, $byte, 2) != 0 ) {
    
    $rgb = vec($byte, 0, 16);

    $a = 1;# ( $rgb & 1 );
    $r = (( $rgb & 0xf800 ) >> 11) * 255/31;
    $b = (( $rgb & 0x07c0 ) >> 6)  * 255/31;
    $g = (( $rgb & 0x003e ) >> 1)  * 255/31;

#    printf ( "%d, ", $rgb);
#    printf ("%d, ", $r);

    syswrite( OUTFILE, (pack("C", $r ) ), 1 );
    syswrite( OUTFILE, (pack("C", $g ) ), 1 );
    syswrite( OUTFILE, (pack("C", $b ) ), 1 );
    syswrite( OUTFILE, (pack("C", $a ) ), 1 );
}
close INFILE;
close OUTFILE;
