#!/usr/local/bin/perl -w
###########################################
# graphdraw - Draw RGB line values
# Mike Schilli, 2008 ([email protected])
###########################################
use strict;
use Imager;
use Imager::Plot;
use Log::Log4perl;

my($file) = @ARGV;
die "No file given" unless defined $file;

my $img = Imager->new();
$img->read( file => $file ) or
 die $img->errstr();

$img->filter(
 type   => "gaussian",
 stddev => 10 ) or die $img->errstr;

my $y     = int( $img->getheight() / 2 );
my $width = $img->getwidth();

my $data = {};

for my $x (0..$width-1) {
 push @{ $data->{ x } }, $x;

 my $color = $img->getpixel( x => $x,
                             y => $y );
 my @components = $color->rgba();
 for my $color_name (qw(red green blue)) {
   push @{ $data->{ $color_name } },
        shift @components;
   }
}

my $plot = Imager::Plot->new(
 Width  => 550,
 Height => 350,
 GlobalFont =>
 '/usr/share/fonts/truetype/msttcorefonts/Verdana.ttf');

for my $color_name (qw(red green blue)) {
 $plot->AddDataSet(
   X => $data->{x},
   Y => $data->{$color_name},
   style => {
     marker => {
       size   => 2,
       symbol => 'circle',
       color => Imager::Color->new($color_name),
     }
   }
 );
}

my $graph = Imager->new(
       xsize => 600,
       ysize => 400);

$graph->box(filled => 1, color => 'white');

   # Add text
$plot->{'Ylabel'} = 'RGB Values';
$plot->{'Xlabel'} = 'X-Pixel';
$plot->{'Title'}  = 'RGB-Distribution';

$plot->Render(
 Image => $graph,
 Xoff  => 40,
 Yoff  => 370);

$graph->write(file => "graph.png") or die $graph->errstr();