
use strict;

sub newBitmap($$)
{
    my ($w,$h) = (@_);
    my %bitmap;

    $bitmap{width} = $w;
    $bitmap{height} = $h;
    $bitmap{pixels} = [];

    return \%bitmap;
}

sub setPixel(%$$$)
{
    my ($bitmap,$x,$y,$rgb) = (@_);
    my $i = $$bitmap{width} * $y + $x;
    $$bitmap{pixels}->[$i] = $rgb;
}

sub writeTga(%$)
{
    my ($bitmap,$filePath) = (@_);
    my $width = $$bitmap{width};
    my $height = $$bitmap{height};
    my $pixels = $$bitmap{pixels};

    open F,">$filePath" or die "no writey";
    binmode F;

    my $header = pack('cccccccccccccccccc',
            0,0,2,0,0,0,0,0,0,0,0,0,
            $width & 0xff,$width>>8,
            $height & 0xff,$height>>8,
            24,32);
    
    print F $header;

    my $i = 0;
    for(my $y = 0; $y < $width; $y++)
    {
        for(my $x = 0; $x < $width; $x++)
        {
            my $pixel = $$pixels[$i++];
            my $r = ($pixel >> 16);
            my $g = ($pixel >> 8) & 0xff;
            my $b = $pixel & 0xff;
            my $rgb = pack('ccc',$b,$g,$r);
            print F $rgb;
        }
    }

    close F;
}


return 1;
