#!/opt/bin/perl
# triangu.pl
#
# Copyright (c) Fabian Frederick 2001
#
# 20/06      : Polygonal linking.Rendering is better
#	       Using I(y)	
#
# 19/06/2001 : Passing gamma reference to alpha in linear scan
#	       First workable version	
#

use Gimp::Feature 'pdl';
use Gimp qw(:auto __ N_);
use Gimp::Fu;
use PDL::LiteF;
use PDL::Slices;
use POSIX;

sub triangle {
	my ($image, $drawable, $bckcolor, $xscale, $yscale, $thickness, $ground, $elevation) = @_;
	$oldforeground=gimp_palette_get_foreground();
	$oldbackground=gimp_palette_get_background();
	gimp_palette_set_background($bckcolor);
	Gimp->progress_init("Rendering...");
	gimp_selection_clear($image);
	$width=gimp_image_width($image);$height=gimp_image_height($image);
	my $src=new PixelRgn($drawable,0,0,$width,$height, 0, 0);
	for ($y=0;$y<$src->h;$y+=$yscale){
		$row=$src->get_row(0,$y,$src->w);
		for (my $x=0;$x<$src->w;$x+=$xscale){push @acq,[$x,($src->h-$row->slice(1)->at(0,$x)-$ground)/2];}
	}
	Gimp->progress_update(0.8);
	my $newimage=gimp_image_new($src->w, $src->h, 0);
	gimp_layer_new($newimage,$src->w, $src->h, RGB_IMAGE, "L1", 100, NORMAL_MODE)->gimp_image_add_layer(-1);
	$newdrawable=gimp_image_active_drawable($newimage);
	gimp_drawable_fill($newdrawable, 1);
	
	my $thickness=sprintf("0%d", $thickness);
	#gimp_brushes_set_brush("Circle (01)");
	gimp_brushes_set_brush("Circle ($thickness)");
	my $indline=1;my $exc=0;
	for(my $ind=0,my $perline=floor($src->w/$xscale)+1;$ind<($src->h/$yscale*$src->w/$xscale)-10;){
		if ($indline<($width/$xscale)){
			$elvlevel=($ind/($src->w/$xscale))*$elevation;
			gimp_palette_set_foreground([$acq[$ind][1],$acq[$ind][1],$acq[$ind][1]]);
			gimp_pencil($newdrawable, 4, [$acq[$ind+$perline][0],$acq[$ind+$perline][1]+$elvlevel,$acq[$exc][0],$acq[$exc][1]+$elvlevel]);
			gimp_pencil($newdrawable, 4, [$acq[$ind][0],$acq[$ind][1]+$elvlevel,$acq[$ind+1][0],$acq[$ind+1][1]+$elvlevel]);
			gimp_pencil($newdrawable, 4, [$acq[$ind][0], $acq[$ind][1]+$elvlevel,$acq[$ind+$perline][0], $acq[$ind+$perline][1]+$elvlevel]);
			gimp_pencil($newdrawable, 4, [$acq[$ind+$perline][0],$acq[$ind+$perline][1]+$elvlevel,$acq[$ind+1][0],$acq[$ind+1][1]+$elvlevel]);
			$exc=$ind+$perline;
			$indline=$indline+1;
		  }else{
			$indline=1;
		 }
		Gimp->progress_update($ind/($src->h/$yscale*$src->w/$xscale));
		$ind=$ind+1;
	}
	$newdrawable->merge_shadow(1);
	$newdrawable->update(0,0,$width, $height);
	Gimp->display_new($newimage);
	Gimp->progress_update(1);
	gimp_palette_set_foreground($oldforeground);
	gimp_palette_set_foreground($oldbackground);

}
register "triangulandscape",
	 "Generate a triangulation landscape",
	 "Generate a triangulation landscape",
	 "Fabian Frederick", 
	 "(c) 2001 Fabian Frederick",
	 "20010701", 
	 N_"<Image>/Filters/Render/Triangulation Landscape", 
	 "RGB",
	 [
		[PF_COLOR,  "background_color","background color", [100,100,100]],
		[PF_SLIDER, "xscan", "Stat each X coord.", 20, [1, 100]],
		[PF_SLIDER, "yscan", "Stat each Y coord.", 20, [1, 100]],
		[PF_SLIDER, "line_thickness", "Line thickness", 1, [1, 5]],
		[PF_SLIDER, "ground", "Pixels from bottom", 50, [1, 100]],
		[PF_SLIDER, "elevation", "elevation factor in pixels", 10, [1, 100]],
	 ],
	 \&triangle;
exit main;

__END__

=head1 NAME

Triangulation Landscape

=head1 SYNOPSIS

Triangulation Landscape

=head1 DESCRIPTION

Triangulation Landscape is a Gimp plugin.


=head1 AUTHOR

Written by Fabian Frederick <fabian.frederick@gmx.fr>, (c) 2001
