]> git.imager.perl.org - imager.git/blame - samples/quad_to_square.pl
correct an old bug link
[imager.git] / samples / quad_to_square.pl
CommitLineData
3309187a
TC
1#!/usr/bin/perl -w
2use strict;
3# Convert quadrilateral to square
4
5use Imager;
6
7my $src=Imager->new();
8$src->open(file=>"oldmap_200px.jpg");
9
10# example co-ordinates of quadrilateral
11my $x0=12; my $y0=4; # top left
12my $x1=157; my $y1=0; # top right
13my $x2=140; my $y2=150; # bottom right
14my $x3=27; my $y3=159; # bottom left
15
16my $code=<<EOF;
17xa=((h-y)*x0+y*x3)/h; ya=((h-y)*y0+y*y3)/h;
18xb=((h-y)*x1+y*x2)/h; yb=((h-y)*y1+y*y2)/h;
19xc=((w-x)*x0+x*x1)/w; yc=((w-x)*y0+x*y1)/w;
20xd=((w-x)*x3+x*x2)/w; yd=((w-x)*y3+x*y2)/w;
21
22d=det(xa-xb,ya-yb,xc-xd,yc-yd);
23d=if(d==0,1,d);
24
25px=det(det(xa,ya,xb,yb),xa-xb,det(xc,yc,xd,yd),xc-xd)/d;
26py=det(det(xa,ya,xb,yb),ya-yb,det(xc,yc,xd,yd),yc-yd)/d;
27return getp1(px,py);
28EOF
29
30my $newimg=Imager::transform2({
31expr=>$code,
32width=>200,
33height=>200,
34constants=>{x0=>$x0,y0=>$y0,
35x1=>$x1,y1=>$y1,
36x2=>$x2,y2=>$y2,
37x3=>$x3,y3=>$y3}},
38($src));
39$newimg->write(file=>"output_imager.jpg");
40
41=head1 NAME
42
43quad_to_square.pl - transform an arbitrary quadrilateral to a square.
44
45=head1 SYNOPSIS
46
47 perl quad_to_square.pl
48
49=head1 DESCRIPTION
50
5715f7c3
TC
51=for stopwords Fairhurst resized
52
3309187a
TC
53Courtesy Richard Fairhurst:
54
55I've been using it to rectify ("square up") a load of roughly scanned
56maps, so that an arbitrary quadrilateral is resized into a square. The
57transform2 function is ideal for that.
58
59Thought you might be interested to see what people are doing with
60Imager - feel free to include this in the sample code.
61
62=head1 AUTHOR
63
64Richard Fairhurst
65
66=cut