#!/usr/bin/perl -w use strict; use Imager; use XML::Simple; use Data::Dumper; my $file = shift or die "Need input filename"; my $outfile = shift or die "Need output filename"; my $img = Imager->new(); $img->read(file=>$file) or die $img->errstr("Cannot load file"); #print Dumper $img->getcolors(); #print Dumper $img->bits(); #print Dumper $img->getpixel(10,20); # Get image parameters my $width = $img->getwidth(); my $height = $img->getheight(); # Setup new xml images #my $red = Imager->new(xsize=>$width, ysize=>$height); #$red->box(filled=>1, color=>"green"); my %red, %green, %blue; my($x, $y); for ($x = 0; $x < $img->getheight(); $x++) { for ($y = 0; $y < $img->getwidth(); $y++) { #print "x: $x, y: $y\n"; if (defined($img->getpixel(x=>$x, y=>$y))) { my @color = $img->getpixel(x=>$x, y=>$y)->rgba(); #print Dumper @color; my $r = shift @color; my $g = shift @color; my $b = shift @color; my $a = shift @color; if ($r > 0) { $red{$x,$y} = 1; } if ($g > 0) { $green{$x,$y} = 1; } if ($b > 0) { $blue{$x,$y} = 1; } #foreach $c ( @color ) { #print "Value: $c\t"; #} #print "R: $r, G: $g, B: $b, (A: $a)\n"; } } } #$red->write(file=>$outfile) or die $red->errstr; my $xmldatared,$xmldatablue,$xmldatagreen; my $id = 0; my ($i, $ii); for ($i=0; $i<$height; $i++) { for ($ii=0; $ii<$width; $ii++) { $xmldatared->{CELL}[$id]->{ID}[0]=$id; $xmldatared->{CELL}[$id]->{X}[0]=$ii; $xmldatared->{CELL}[$id]->{Y}[0]=$i; my $value; if (defined $red{$i,$ii}) { $value = 255; } else { $value = 0; } $xmldatared->{CELL}[$id]->{VALUE}[0]=$value; if (defined $green{$i,$ii}) { $value = 255; } else { $value = 0; } $xmldatagreen->{CELL}[$id]->{VALUE}[0]=$value; $id++; } } open(OUTPUT, ">$outfile") || die "\ncan't create the file $outfile!\n"; print OUTPUT XMLout($xmldata, rootname =>"RASTER"); close(OUTPUT);