SGF2misc/GD.pm

filter: Statementstmt 81.8% 9 / 11 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 86.6% 13 / 15 CC 1 SCAR 0.7
CC
1
Cov
82%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« SGF2misc.pm ↑ Summary
1 # Games::Go::SGF2misc::GD
2 #
3 # Author: Orien Vandenbergh <orien@icecode.com>
4  
5 package Games::Go::SGF2misc::GD;
6  
7 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use GD;
Subroutine BEGIN: called CC=1 SCAR=2.6
12  
13 require Exporter;
14  
15 our @ISA = qw(Exporter);
16 our %EXPORT_TAGS = ( 'all' => [qw( )] );
17 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
18 our @EXPORT = qw( );
19 our $VERSION = '0.96';
20  
21 # sub new() {{{
22 sub new() {
23 my $class = shift;
24 my $self = bless( { @_ }, $class );
25 $self->init();
26 return $self;
27 }
28 # }}}
29 # sub init() {{{
30 sub init() {
31 my ($self) = @_;
32 $self->{'imagesize'} = 64 unless $self->{'imagesize'};
33 $self->{'boardsize'} = 19 unless $self->{'boardsize'};
34 $self->{'antialias'} = 1 unless defined $self->{'antialias'};
35  
36 $self->{'stonesize'} = int( $self->{'imagesize'} / $self->{'boardsize'} );
37 $self->{'border'} = int( ($self->{'imagesize'} - ($self->{'stonesize'} * $self->{'boardsize'})) /2 ) + int($self->{'stonesize'}/2);
38 $self->{'image'} = GD::Image->newTrueColor($self->{'imagesize'},$self->{'imagesize'});
39  
40 if ($self->{'gobanColor'}) {
41 $self->{'gobanColor'} = $self->{'image'}->colorAllocate(@{ $self->{'gobanColor'} });
42 } else {
43 $self->{'gobanColor'} = $self->{'image'}->colorAllocate(0xee,0xb1,0x4b);
44 }
45 if ($self->{'whiteColor'}) {
46 $self->{'whiteColor'} = $self->{'image'}->colorAllocate(@{ $self->{'whiteColor'} });
47 } else {
48 $self->{'whiteColor'} = $self->{'image'}->colorAllocate(0xff,0xff,0xff);
49 }
50 if ($self->{'blackColor'}) {
51 $self->{'blackColor'} = $self->{'image'}->colorAllocate(@{ $self->{'blackColor'} });
52 } else {
53 $self->{'blackColor'} = $self->{'image'}->colorAllocate(0x00,0x00,0x00);
54 }
55 }
56 # }}}
57 # sub gobanColor() {{{
58 sub gobanColor() {
59 my $self = shift;
60  
61 $self->{'kaya'} = $self->{'image'}->colorAllocate(@_);
62 }
63 # }}}
64 # sub whiteColor() {{{
65 sub whiteColor() {
66 my $self = shift;
67  
68 $self->{'white'} = $self->{'image'}->colorAllocate(@_);
69 }
70 # }}}
71 # sub blackColor() {{{
72 sub blackColor() {
73 my $self = shift;
74  
75 $self->{'black'} = $self->{'image'}->colorAllocate(@_);
76 }
77 # }}}
78 # sub drawGoban() {{{
79 sub drawGoban() {
80 my ($self) = shift;
81  
82 my $color = $self->{'image'}->colorAllocate(0,0,0);
83 my $bottomright = $self->{'stonesize'} * $self->{'boardsize'} - ($self->{'stonesize'} - $self->{'border'});
84  
85 $self->{'image'}->fill(0,0,$self->{'kaya'});
86 $self->{'image'}->setThickness(1);
87  
88 $self->{'image'}->rectangle($self->{'border'},$self->{'border'},$bottomright,$bottomright,$color);
89 foreach my $x (($self->{'border'} + $self->{'stonesize'})..$bottomright) {
90 next if (($x-$self->{'border'}) % $self->{'stonesize'});
91 $self->{'image'}->line($x,$self->{'border'},$x,$bottomright,$color);
92 $self->{'image'}->line($self->{'border'},$x,$bottomright,$x,$color);
93 }
94 if ($self->{'antialias'}) {
95 $self->{'image'}->setAntiAliased($color);
96 $color = gdAntiAliased;
97 }
98 my %hoshi = ( 5 => [[2,2] ],
99 7 => [[3,3] ],
100 9 => [[2,2], [6,2],
101 [4,4],
102 [2,6], [6,6] ],
103 11 => [[3,3], [7,3],
104 [5,5],
105 [3,7], [7,7] ],
106 13 => [[2,2], [6,2], [10,2],
107 [2,6], [6,6], [10,6],
108 [2,10], [6,10], [10,10] ],
109 19 => [[3,3], [9,3], [15,3],
110 [3,9], [9,9], [15,9],
111 [3,15], [9,15], [15,15] ],
112 );
113 foreach my $point (@{ $hoshi{$self->{'boardsize'}} }) {
114 my $x = $self->{'border'}+($self->{'stonesize'}*$point->[0]);
115 my $y = $self->{'border'}+($self->{'stonesize'}*$point->[1]);
116 $self->{'image'}->filledEllipse($x,$y,$self->{'stonesize'}/4,$self->{'stonesize'}/4,$color)
117 }
118 }
119 # }}}
120 # sub calcXY {{{
121 sub calcXY {
122 my $self = shift;
123 my ($pos) = @_;
124  
125 my ($x,$y);
126 if (ref($pos) eq 'ARRAY') {
127 ($x,$y) = @{ $pos };
128 } else {
129 $pos = lc($pos);
130 $pos =~ /([a-z])([a-z])/;
131 ($x,$y) = ((ord($1)-ord('a')),(ord($2)-ord('a')));
132 }
133  
134 return ($self->{'border'}+($self->{'stonesize'}*$x),$self->{'border'}+($self->{'stonesize'}*$y));
135 }
136 # }}}
137  
138 # sub placeStone {{{
139 sub placeStone {
140 my $self = shift;
141 my ($player, $move) = @_;
142
143 my ($x,$y) = $self->calcXY($move);
144  
145 my $color;
146 if ( $player =~ /b/i ) {
147 $color=$self->{'black'};
148 if ($self->{'antialias'}) {
149 $self->{'image'}->setAntiAliased($color);
150 $color = gdAntiAliased
151 }
152 $self->{'image'}->filledEllipse($x,$y,$self->{'stonesize'},$self->{'stonesize'},$color);
153 } else {
154 $color=$self->{'white'};
155 if ($self->{'antialias'}) {
156 $self->{'image'}->setAntiAliased($color);
157 $color = gdAntiAliased
158 }
159 $self->{'image'}->filledEllipse($x,$y,$self->{'stonesize'},$self->{'stonesize'},$color);
160 if ($self->{'stonesize'} > 9) {
161 $color=$self->{'black'};
162 if ($self->{'antialias'}) {
163 $self->{'image'}->setAntiAliased($color);
164 $color = gdAntiAliased
165 }
166 $self->{'image'}->ellipse($x,$y,$self->{'stonesize'},$self->{'stonesize'},$color);
167 }
168 }
169 }
170 # }}}
171 # sub addCircle {{{
172 sub addCircle {
173 my $self = shift;
174 return unless ($self->{'stonesize'} > 5);
175 my ($move,$stone) = @_;
176 my $ratio = 1.5;
177
178 my ($x,$y) = $self->calcXY($move);
179
180 my @base = $self->{'image'}->rgb($self->{'image'}->getPixel($x,$y));
181 my $color = $self->{'black'};
182 if ( $stone ) {
183 $color = $self->{'image'}->colorAllocate(255-$base[0], 255-$base[1], 255-$base[2]);
184 }
185  
186 if ($self->{'antialias'}) {
187 $self->{'image'}->setAntiAliased($color);
188 $color = gdAntiAliased
189 }
190 $self->{'image'}->ellipse($x,$y,$self->{'stonesize'}/$ratio,$self->{'stonesize'}/$ratio,$color);
191 }
192 # }}}
193 # sub addLetter {{{
194 sub addLetter {
195 my $self = shift;
196 return unless ($self->{'stonesize'} > 5);
197 my ($move,$letter,$stone) = @_;
198 my $ratio = 1.3;
199 my $font = $ENV{TTFONT} ? $ENV{TTFONT} : return;
200
201 my ($x,$y) = $self->calcXY($move);
202
203 my @base = $self->{'image'}->rgb($self->{'image'}->getPixel($x,$y));
204 my $color = $self->{'black'};
205 if ( $stone ) {
206 $color = $self->{'image'}->colorAllocate(255-$base[0], 255-$base[1], 255-$base[2]);
207 } else {
208 my $d = ($self->{'stonesize'})/2;
209 $self->{'image'}->filledRectangle($x-$d,$y-$d,$x+$d,$y+$d,$self->{'kaya'});
210 }
211  
212 my $max = $self->{'stonesize'}/$ratio;
213 my $points = $max+1;
214 my ($width,$height);
215 do {
216 $points--;
217 my @bounds = GD::Image->stringFT($color,$font,$points,0,$x,$y,$letter) or return;
218 $height = $bounds[3] - $bounds[5];
219 $width = $bounds[2] - $bounds[0];
220 } while (($height>$max) or ($width>$max));
221  
222 $self->{'image'}->stringFT($color,$font,$points,0,$x-($width/2),$y+($height/2),$letter);
223 }
224 # }}}
225 # sub addSquare {{{
226 sub addSquare {
227 my $self = shift;
228 return unless ($self->{'stonesize'} > 5);
229 my ($move,$stone) = @_;
230 my $ratio = 2;
231
232 my ($x,$y) = $self->calcXY($move);
233
234 my @base = $self->{'image'}->rgb($self->{'image'}->getPixel($x,$y));
235 my $color = $self->{'black'};
236 if ( $stone ) {
237 $color = $self->{'image'}->colorAllocate(255-$base[0], 255-$base[1], 255-$base[2]);
238 }
239  
240 my $d = ($self->{'stonesize'}/$ratio)/2;
241  
242 $self->{'image'}->rectangle($x-$d,$y-$d,$x+$d,$y+$d,$color);
243 }
244 # }}}
245 # sub addTriangle {{{
246 sub addTriangle {
247 my $self = shift;
248 return unless ($self->{'stonesize'} > 5);
249 my ($move,$stone) = @_;
250 my $ratio = 1.5;
251 my $pi = 3.1415927;
252  
253 my ($x,$y) = $self->calcXY($move);
254  
255 my @base = $self->{'image'}->rgb($self->{'image'}->getPixel($x,$y));
256 my $color = $self->{'black'};
257 if ( $stone ) {
258 $color = $self->{'image'}->colorAllocate(255-$base[0], 255-$base[1], 255-$base[2]);
259 }
260  
261 my $b = ($self->{'stonesize'}/$ratio)*sin($pi/3);
262 my $c = ($self->{'stonesize'}/$ratio)*cos($pi/3);
263
264 my $tri = new GD::Polygon;
265 $tri->addPt($x,$y-(2*$b/3));
266 $tri->addPt($x-$c,$y+($b/3));
267 $tri->addPt($x+$c,$y+($b/3));
268  
269 if ($self->{'antialias'}) {
270 $self->{'image'}->setAntiAliased($color);
271 $color = gdAntiAliased;
272 }
273 $self->{'image'}->polygon($tri,$color);
274 }
275 # }}}
276 # sub save {{{
277 sub save {
278 my $self = shift;
279 my ($filename,$format) = @_;
280  
281 open IMG, ">$filename" or die "Unable to open $filename: $!, stopped ";
282 binmode IMG;
283  
284 $format = 'png' unless $format;
285
286 if ( $format =~ /jp(e?)g/i ) {
287 print IMG $self->{'image'}->jpeg;
288 } elsif ( $format =~ /gd2/i ) {
289 print IMG $self->{'image'}->gd2;
290 } elsif ( $format =~ /gd/i ) {
291 print IMG $self->{'image'}->gd;
292 } else {
293 print IMG $self->{'image'}->png;
294 }
295 close IMG;
296 }
297 # }}}
298 # sub dump {{{
299 sub dump {
300 my $self = shift;
301 my ($format) = @_;
302  
303 $format = 'png' unless $format;
304
305 if ( $format =~ /jp(e?)g/i ) {
306 return $self->{'image'}->jpeg;
307 } elsif ( $format =~ /gd2/i ) {
308 return $self->{'image'}->gd2;
309 } elsif ( $format =~ /gd/i ) {
310 return $self->{'image'}->gd;
311 } else {
312 return $self->{'image'}->png;
313 }
314 }
315 # }}}
316  
317 __END__
« SGF2misc.pm ↑ Summary