#!/usr/bin/perl
use Math::FFT;
use Math::MatrixReal;
use GD;

my $pic = 'block.png';    # modified to use a .png in this directory.  - Jim
my $truecolor = '1';
my $img = GD::Image->newFromPng($pic, $truecolor);
my ($width, $height) = $img->getBounds();
my $wblock = ($width / 8);
my $hblock = ($height / 8);
my $blocks = ($wblock * $hblock);

my $YVals  = new Math::MatrixReal($width - 1 , $height - 1);
my $CbVals = new Math::MatrixReal($width - 1, $height - 1);
my $CrVals = new Math::MatrixReal($width - 1, $height - 1 );

for ($xct = 1; $xct != $width; $xct++)
        {
        for ($yct = 1; $yct != $height; $yct++)
                {
                my ($r,$g,$b) = $img->rgb($img->getPixel($x,$y));
                my $Y  = ( 0.299  * $r +   0.587 * $g + 0.114  * $b +   0);
                my $Cb = (-0.1687 * $r - 0.03313 * $g + 0.5    * $b + 128);
                my $Cr = ( 0.5    * $r - 0.4187  * $g - 0.0813 * $b + 128);
                $y++;
                $xrow = ($x + 1);
                $yrow = ($y );
                $YVals ->assign($xrow, $yrow, $Y );
                $CbVals->assign($xrow, $yrow, $Cb);
                $CrVals->assign($xrow, $yrow, $Cr);
               }
        $y = 0;
        $x++;
        }
$string = $YVals->as_matlab((format =>"%.0f"));
my $data;
$string =~ s/ //;
$string =~ s/\]//g;
$string =~ s/\[//g;
$string =~ s/;//g;
$string =~ s/\n//g;
print "$string \n\n";
@foo = split / /, $string;
$k = 0;
foreach (@foo)
	{
	$data->[$k] = $_;	
	}
print "starting dct\n";
my $fft = new Math::FFT($data) or die "build fft object : @! \n";;
print "running dct\n";
my $reply = $fft->ddct() or die "running fft : $!\n";
print "printing reply\n";
print "$reply";
 
