#! /usr/bin/perl

##[[[[[[[[[[[[[[[[[[[[[[[
#Blur.cgi
#takes a jpeg and, applies a cosine transformation, shows the transform,
# filters the transformation, and then displays the filtered image.
#NOTE: this is very slow and should only be used on small images. (128x192ish)
#
#by Ian Smith-Heisters ian@0x09.com
#March 8th, 2004
##]]]]]]]]]]]]]]]]]]]]]]]

use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
use GD;
use strict;
use warnings;

#--- jim
#print "is it running?\n";
#exit;

#initialize global variable hash
my %g = (filename => "test.jpg",
	);

#set truecolor
my $truecolor = 1;		# on

#Load any global variables from the URL
for my $key (keys %g){
  $g{$key} = param($key) if defined param($key);
}

#create an image from filename
my $image = GD::Image -> newFromJpeg($g{filename}, $truecolor) || 
  die "couldn't load original jpeg";

#get the width and height of the image
#  getBounds returns a two member list (width,height)
my @bounds = $image -> getBounds();
my $width  = shift @bounds;
my $height = pop   @bounds;

# +++ returns a hash containing three arrays, each containing R, G, or B 
# information for each pixel in the image
sub getColors {
  my $img = shift;
  my %temp;
  for (my $x = 0;$x<$width;$x++){ #start with by looping over the columns
    for (my $y = 0;$y<$height;$y++){ #then loop over each row

      my $index = $img -> getPixel($x,$y); # get the index of the pixel
      my @rgb = $image->rgb($index); # coinvert the index to RGB format

      my $red = shift @rgb;	# put the red information in red
      my $green = shift @rgb;	# put the g info in green
      my $blue = shift @rgb;	# and (surprise!) put the b info in blue

      #throw red green and blue into the hash at their pixel coordinate
      $temp{r}[$x][$y] = $red;
      $temp{g}[$x][$y] = $green;
      $temp{b}[$x][$y] = $blue;
    }
  }
  return %temp;			# return the hash
}

# +++ return an image given an rgb hash
sub getImage {
  my $rgb = shift;
  my $img = GD::Image->new($width, $height, $truecolor) || 
    die "Couldn't create the new jpeg."; # create a new image

  for (my $x=0; $x<$width; $x++) { # cycle through x pixels
    for (my $y=0; $y<$height; $y++){ # cycle through y pixels

      # create a list for the pixel we're working on from the rgb hash info
      my @rgbList = ($rgb->{r}[$x][$y], 
		     $rgb->{g}[$x][$y], 
		     $rgb->{b}[$x][$y]);

      # set the pixel to the data in rgbList
      $img -> setPixel($x, $y, $img->colorClosest(@rgbList));
    }
  }
  return $img;			# return the image.
}

##SO! Now we can get an image, extract the rgb info into arrays, and put
## that information back into a new image.


#use constant PI = 4*atan2(1,1); # this will come in handy
#
## +++ return an array containing the DFT matrix for the image.
## pass it either $height or $width for columns or rows respectively
#sub getDFT {
#  my $direction = shift;
#  @temp = ();
#
#  for my $j (0..$direction-1){
#    for my $k (0..$direction-1){
#      $temp[$j][$k] = cos(PI/$width * $j * ($k+0.5));
#    }
#  }
#  return @temp;
#}
#
#sub applyDFT {
#  for my $h (0..$height-1){
#    my @newRed   = ();                # for each row,
#    my @newGreen = ();
#    my @newBlue  = ();
#    for my $w (0..$width-1){
#      $newRed[$w]  = 0;              # We're doing a sum,
#      $newGreen[$w]= 0;              # so start off with zero.
#      $newBlue[$w] = 0;
#      for my $k (0..$width-1){
#	$newRed[$w] += $red[$k][$h]     * $dft[$w][$k];
#	$newBlue[$w] += $blue[$k][$h]   * $dft[$w][$k];
#	$newGreen[$w] += $green[$k][$h] * $dft[$w][$k];
#      }
#    }
#    for my $w (0..$width-1){         # Copy these transformed rows back.
#      $red[$w][$h] = $newRed[$w];
#      $blue[$w][$h] = $newBlue[$w];
#      $green[$w][$h] = $newGreen[$w];
#    }
#  }
#}
#
#sub applyDFT {
#  my $direction = shift;
#
#  
