blib/lib/Imager/Fill.pm

filter: Statementstmt 82.6% 76 / 92 Branchbran 75.0% 33 / 44 Conditioncond 71.8% 23 / 32 MC/DCmcdc 50.0% 10 / 20 Subroutinesub 80.0% 4 / 5 Podpod 100% 3 / 3 totaltotal 76.0% 149 / 196 CC 39 SCAR 39.4
CC
39
Cov
79%
new
56.5
hatches
2.0
BEGIN
1.0
CRAP 51.4
« blib/lib/Imager/Expr/Assem.pm ↑ Summary blib/lib/Imager/Color.pm »
1 package Imager::Fill;
2 11 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 11 our $VERSION = "1.013";
6  
7 # this needs to be kept in sync with the array of hatches in fills.c
8 11 my @hatch_types =
9 qw/check1x1 check2x2 check4x4 vline1 vline2 vline4
10 hline1 hline2 hline4 slash1 slosh1 slash2 slosh2
11 grid1 grid2 grid4 dots1 dots4 dots16 stipple weave cross1 cross2
12 vlozenge hlozenge scalesdown scalesup scalesleft scalesright stipple2
13 tile_L stipple3/;
14 11 my %hatch_types;
15 11 @hatch_types{@hatch_types} = 0..$#hatch_types;
16  
17 11 *_color = \&Imager::_color;
18  
19 69 ▶sub new {
Subroutine new: called CC=39 SCAR=40.3
Pod documented
20 69 my ($class, %hsh) = @_;
21  
22 69 my $self = bless { }, $class;
23 69 $hsh{combine} = Imager->_combine($hsh{combine}, 0);
24 69 ▶ if ($hsh{solid}) {
Branch: 5 branches
BranchTrueFalseTotal
if ($hsh{'solid'}) { } 22 47 69
elsif (defined $hsh{'hatch'}) { } 24 23 47
elsif (defined $hsh{'fountain'}) { } 7 16 23
elsif (defined $hsh{'image'}) { } 9 7 16
elsif (defined $hsh{'type'} and $hsh{'type'} eq 'opacity') { } 7 0 7
25 22 my $solid = _color($hsh{solid});
26 22 ▶ if (UNIVERSAL::isa($solid, 'Imager::Color')) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($solid, 'Imager::Color')) { } 22 0 22
elsif (UNIVERSAL::isa($solid, 'Imager::Color::Float')) { } 0 0 0
27 $self->{fill} =
28 22 Imager::i_new_fill_solid($solid, $hsh{combine});
29 }
30 elsif (UNIVERSAL::isa($solid, 'Imager::Color::Float')) {
31 $self->{fill} =
32 0 Imager::i_new_fill_solidf($solid, $hsh{combine});
33 }
34 else {
35 0 $Imager::ERRSTR = "solid isn't a color";
36 0 return undef;
37 }
38 }
39 elsif (defined $hsh{hatch}) {
40 24 ▶ $hsh{dx} ||= 0;
Condition100%2/2
$hsh{"dx"} ||= 0
l!l
420
MC/DC100%1/1
$hsh{"dx"} ||= 0
$hsh{"dx"}
Truth table100%2/2
A
A= $hsh{"dx"}
Aresult
11
00
41 24 ▶ $hsh{dy} ||= 0;
Condition100%2/2
$hsh{"dy"} ||= 0
l!l
123
MC/DC100%1/1
$hsh{"dy"} ||= 0
$hsh{"dy"}
Truth table100%2/2
A
A= $hsh{"dy"}
Aresult
11
00
42 24 ▶ $hsh{fg} ||= Imager::Color->new(0, 0, 0);
Condition66%2/3
$hsh{"fg"} ||= "Imager::Color"->new(0, 0, 0)
l!l&&r!l&&!r
11130
MC/DC0%0/2
$hsh{"fg"} ||= "Imager::Color"->new(0, 0, 0)
$hsh{"fg"} "Imager::Color"->new(0, 0, 0)
Truth table66%2/3
A ||= B
A= $hsh{"fg"}B= "Imager::Color"->new(0, 0, 0)
ABresult
1X1
011
000
43 24 ▶ if (ref $hsh{hatch}) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $hsh{'hatch'}) { } 0 24 24
elsif ($hsh{'hatch'} =~ /\D/) { } 24 0 24
44 0 $hsh{cust_hatch} = pack("C8", @{$hsh{hatch}});
45 0 $hsh{hatch} = 0;
46 }
47 elsif ($hsh{hatch} =~ /\D/) {
48 24 ▶ unless (exists($hatch_types{$hsh{hatch}})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $hatch_types{$hsh{"hatch"}}) 1 23 24
49 1 $Imager::ERRSTR = "Unknown hatch type $hsh{hatch}";
50 1 return undef;
51 }
52 23 $hsh{hatch} = $hatch_types{$hsh{hatch}};
53 }
54 23 my $fg = _color($hsh{fg});
55 23 ▶ if (UNIVERSAL::isa($fg, 'Imager::Color')) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($fg, 'Imager::Color')) { } 23 0 23
elsif (UNIVERSAL::isa($fg, 'Imager::Color::Float')) { } 0 0 0
56 23 ▶ my $bg = _color($hsh{bg} || Imager::Color->new(255, 255, 255));
Condition66%2/3
$hsh{'bg'} || 'Imager::Color'->new(255, 255, 255)
l!l&&r!l&&!r
9140
MC/DC0%0/2
$hsh{'bg'} || 'Imager::Color'->new(255, 255, 255)
$hsh{'bg'} 'Imager::Color'->new(255, 255, 255)
Truth table66%2/3
A || B
A= $hsh{'bg'}B= 'Imager::Color'->new(255, 255, 255)
ABresult
1X1
011
000
57 $self->{fill} =
58 Imager::i_new_fill_hatch($fg, $bg, $hsh{combine},
59 $hsh{hatch}, $hsh{cust_hatch},
60 23 $hsh{dx}, $hsh{dy});
61 }
62 elsif (UNIVERSAL::isa($fg, 'Imager::Color::Float')) {
63 0 ▶ my $bg = _color($hsh{bg} || Imager::Color::Float->new(1, 1, 1));
Condition0%0/3
$hsh{'bg'} || 'Imager::Color::Float'->new(1, 1, 1)
l!l&&r!l&&!r
000
MC/DC0%0/2
$hsh{'bg'} || 'Imager::Color::Float'->new(1, 1, 1)
$hsh{'bg'} 'Imager::Color::Float'->new(1, 1, 1)
Truth table0%0/3
A || B
A= $hsh{'bg'}B= 'Imager::Color::Float'->new(1, 1, 1)
ABresult
1X1
011
000
64 $self->{fill} =
65 Imager::i_new_fill_hatchf($fg, $bg, $hsh{combine},
66 $hsh{hatch}, $hsh{cust_hatch},
67 0 $hsh{dx}, $hsh{dy});
68 }
69 else {
70 0 $Imager::ERRSTR = "fg isn't a color";
71 0 return undef;
72 }
73 }
74 elsif (defined $hsh{fountain}) {
75 # make sure we track the filter's defaults
76 7 my $fount = $Imager::filters{fountain};
77 7 my $def = $fount->{defaults};
78 7 my $names = $fount->{names};
79
80 7 $hsh{ftype} = $hsh{fountain};
81 # process names of values
82 7 for my $name (keys %$names) {
83 28 ▶ if (defined $hsh{$name} && exists $names->{$name}{$hsh{$name}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hsh{$name} and exists $names->{$name}{$hsh{$name}}) 7 21 28
Condition100%3/3
defined $hsh{$name} and exists $names->{$name}{$hsh{$name}}
!ll&&!rl&&r
1477
MC/DC100%2/2
defined $hsh{$name} and exists $names->{$name}{$hsh{$name}}
defined $hsh{$name} exists $names->{$name}{$hsh{$name}}
Truth table100%3/3
A and B
A= defined $hsh{$name}B= exists $names->{$name}{$hsh{$name}}
ABresult
0X0
100
111
84 7 $hsh{$name} = $names->{$name}{$hsh{$name}};
85 }
86 }
87 # process defaults
88 7 %hsh = (%$def, %hsh);
89 7 my @parms = @{$fount->{callseq}};
90 7 shift @parms;
91 7 for my $name (@parms) {
92 70 ▶ unless (defined $hsh{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $hsh{$name}) 0 70 70
93 0 $Imager::ERRSTR =
94 "required parameter '$name' not set for fountain fill";
95 0 return undef;
96 }
97 }
98  
99 # check that the segments supplied is an array ref
100 7 ▶ unless (ref $hsh{segments} && $hsh{segments} =~ /ARRAY/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $hsh{"segments"} and $hsh{"segments"} =~ /ARRAY/) 1 6 7
Condition66%2/3
ref $hsh{"segments"} and $hsh{"segments"} =~ /ARRAY/
!ll&&!rl&&r
106
MC/DC50%1/2
ref $hsh{"segments"} and $hsh{"segments"} =~ /ARRAY/
ref $hsh{"segments"} $hsh{"segments"} =~ /ARRAY/
Truth table66%2/3
A and B
A= ref $hsh{"segments"}B= $hsh{"segments"} =~ /ARRAY/
ABresult
0X0
100
111
101 1 $Imager::ERRSTR =
102 "segments must be an array reference or Imager::Fountain object";
103 1 return;
104 }
105  
106 # make sure the segments are specified with colors
107 6 my @segments;
108 6 for my $segment (@{$hsh{segments}}) {
109 6 my @new_segment = @$segment;
110  
111 6 ▶ $_ = _color($_) or return for @new_segment[3,4];
Branch: 1 branch
BranchTrueFalseTotal
$_ = _color($_) or return 11 1 12
112 5 push @segments, \@new_segment;
113 }
114  
115 $self->{fill} =
116 Imager::i_new_fill_fount($hsh{xa}, $hsh{ya}, $hsh{xb}, $hsh{yb},
117 $hsh{ftype}, $hsh{repeat}, $hsh{combine}, $hsh{super_sample},
118 5 $hsh{ssample_param}, \@segments);
119 }
120 elsif (defined $hsh{image}) {
121 9 ▶ $hsh{xoff} ||= 0;
Condition100%2/2
$hsh{"xoff"} ||= 0
l!l
27
MC/DC100%1/1
$hsh{"xoff"} ||= 0
$hsh{"xoff"}
Truth table100%2/2
A
A= $hsh{"xoff"}
Aresult
11
00
122 9 ▶ $hsh{yoff} ||= 0;
Condition100%2/2
$hsh{"yoff"} ||= 0
l!l
18
MC/DC100%1/1
$hsh{"yoff"} ||= 0
$hsh{"yoff"}
Truth table100%2/2
A
A= $hsh{"yoff"}
Aresult
11
00
123 $self->{fill} =
124 Imager::i_new_fill_image($hsh{image}{IMG}, $hsh{matrix}, $hsh{xoff},
125 9 $hsh{yoff}, $hsh{combine});
126 9 $self->{DEPS} = [ $hsh{image}{IMG} ];
127 }
128 ▶ elsif (defined $hsh{type} && $hsh{type} eq "opacity") {
Condition33%1/3
defined $hsh{"type"} and $hsh{"type"} eq "opacity"
!ll&&!rl&&r
007
MC/DC0%0/2
defined $hsh{"type"} and $hsh{"type"} eq "opacity"
defined $hsh{"type"} $hsh{"type"} eq "opacity"
Truth table33%1/3
A and B
A= defined $hsh{"type"}B= $hsh{"type"} eq "opacity"
ABresult
0X0
100
111
129 7 my $other_fill = delete $hsh{other};
130 7 ▶ unless (defined $other_fill) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $other_fill) 1 6 7
131 1 Imager->_set_error("'other' parameter required to create opacity fill");
132 1 return;
133 }
134 6 ▶ unless (ref $other_fill &&
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $other_fill and eval { do { $other_fill->isa("Imager::Fill") } }) 2 4 6
Condition100%3/3
ref $other_fill and eval { do { $other_fill->isa("Imager::Fill") } }
!ll&&!rl&&r
114
MC/DC100%2/2
ref $other_fill and eval { do { $other_fill->isa("Imager::Fill") } }
ref $other_fill eval { do { $other_fill->isa("Imager::Fill") } }
Truth table100%3/3
A and B
A= ref $other_fillB= eval { do { $other_fill->isa("Imager::Fill") } }
ABresult
0X0
100
111
135 5 eval { $other_fill->isa("Imager::Fill") }) {
136 # try to auto convert to a fill object
137 2 ▶ if (ref $other_fill && $other_fill =~ /HASH/) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $other_fill and $other_fill =~ /HASH/) { } 1 1 2
Condition66%2/3
ref $other_fill and $other_fill =~ /HASH/
!ll&&!rl&&r
101
MC/DC50%1/2
ref $other_fill and $other_fill =~ /HASH/
ref $other_fill $other_fill =~ /HASH/
Truth table66%2/3
A and B
A= ref $other_fillB= $other_fill =~ /HASH/
ABresult
0X0
100
111
138 1 ▶ $other_fill = Imager::Fill->new(%$other_fill)
Branch: 1 branch
BranchTrueFalseTotal
$other_fill = "Imager::Fill"->new(%$other_fill) or return 1 0 1
139 or return;
140 }
141 else {
142 1 undef $other_fill;
143 }
144 2 ▶ unless ($other_fill) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($other_fill) 1 1 2
145 1 Imager->_set_error("'other' parameter must be an Imager::Fill object to create an opacity fill");
146 1 return;
147 }
148 }
149  
150 5 my $raw_fill = $other_fill->{fill};
151 5 my $opacity = delete $hsh{opacity};
152 5 ▶ defined $opacity or $opacity = 0.5; # some sort of default
Branch: 1 branch
BranchTrueFalseTotal
defined $opacity or $opacity = "0.5" 4 1 5
153 $self->{fill} =
154 5 Imager::i_new_fill_opacity($raw_fill, $opacity);
155 5 $self->{DEPS} = [ $other_fill ]; # keep reference to old fill and its deps
156 }
157 else {
158 0 $Imager::ERRSTR = "No fill type specified";
159 0 warn "No fill type!";
160 0 return undef;
161 }
162  
163 64 $self;
164 }
165  
166 0 ▶sub hatches {
Subroutine hatches: not called CC=1 SCAR=6.9
Pod documented
167 0 return @hatch_types;
168 }
169  
170 1 ▶sub combines {
Subroutine combines: called CC=1 SCAR=0.0
Pod documented
171 1 return Imager->combines;
172 }
173  
174 11 1;
175  
176 =head1 NAME
177  
178 Imager::Fill - general fill types
179  
180 =head1 SYNOPSIS
181  
182 use Imager;
183 use Imager::Fill;
184  
185 my $fill1 = Imager::Fill->new(solid=>$color, combine=>$combine);
186 my $fill2 = Imager::Fill->new(hatch=>'vline2', fg=>$color1, bg=>$color2,
187 dx=>$dx, dy=>$dy);
188 my $fill3 = Imager::Fill->new(fountain=>$type, ...);
189 my $fill4 = Imager::Fill->new(image=>$img, ...);
190 my $fill5 = Imager::Fill->new(type => "opacity", other => $fill,
191 opacity => ...);
192  
193 =head1 DESCRIPTION
194  
195 Creates fill objects for use by most filled area drawing functions.
196  
197 All fills are created with the new method.
198  
199 =over
200  
201 =item new
202  
203 my $fill = Imager::Fill->new(...);
204  
205 The parameters depend on the type of fill being created. See below
206 for details.
207  
208 =back
209  
210 The currently available fills are:
211  
212 =over
213  
214 =item *
215  
216 solid
217  
218 =item *
219  
220 hatch
221  
222 =item *
223  
224 fountain (similar to gradients in paint software)
225  
226 =item *
227  
228 image - fill with an image, possibly transformed
229  
230 =item *
231  
232 opacity - a lower opacity version of some other fill
233  
234 =back
235  
236 =head1 Common options
237  
238 =over
239  
240 =item combine
241  
242 The way in which the fill data is combined with the underlying image.
243 See L<Imager::Draw/"Combine Types">.
244  
245 =back
246  
247 In general colors can be specified as L<Imager::Color> or
248 L<Imager::Color::Float> objects. The fill object will typically store
249 both types and convert from one to the other. If a fill takes 2 color
250 objects they should have the same type.
251  
252 =head2 Solid fills
253  
254 my $fill = Imager::Fill->new(solid=>$color, combine =>$combine)
255  
256 Creates a solid fill, the only required parameter is C<solid> which
257 should be the color to fill with.
258  
259 A translucent red fill:
260  
261 my $red = Imager::Fill->new(solid => "FF000080", combine => "normal");
262  
263 =head2 Hatched fills
264  
265 my $fill = Imager::Fill->new(hatch=>$type, fg=>$fgcolor, bg=>$bgcolor,
266 dx=>$dx, $dy=>$dy);
267  
268 Creates a hatched fill. You can specify the following keywords:
269  
270 =over
271  
272 =item *
273  
274 C<hatch> - The type of hatch to perform, this can either be the
275 numeric index of the hatch (not recommended), the symbolic name of the
276 hatch, or an array of 8 integers which specify the pattern of the
277 hatch.
278  
279 Hatches are represented as cells 8x8 arrays of bits, which limits their
280 complexity.
281  
282 Current hatch names are:
283  
284 =over
285  
286 =item *
287  
288 C<check1x1>, C<check2x2>, C<check4x4> - checkerboards at various sizes
289  
290 =item *
291  
292 C<vline1>, C<vline2>, C<vline4> - 1, 2, or 4 vertical lines per cell
293  
294 =item *
295  
296 C<hline1>, C<hline2>, C<hline4> - 1, 2, or 4 horizontal lines per cell
297  
298 =item *
299  
300 C<slash1>, C<slash2> - 1 or 2 / lines per cell.
301  
302 =item *
303  
304 C<slosh1>, C<slosh2> - 1 or 2 \ lines per cell
305  
306 =item *
307  
308 C<grid1>, C<grid2>, C<grid4> - 1, 2, or 4 vertical and horizontal
309 lines per cell
310  
311 =item *
312  
313 C<dots1>, C<dots4>, C<dots16> - 1, 4 or 16 dots per cell
314  
315 =item *
316  
317 C<stipple>, C<stipple2> - see the samples
318  
319 =item *
320  
321 C<weave> - I hope this one is obvious.
322  
323 =item *
324  
325 C<cross1>, C<cross2> - 2 densities of crosshatch
326  
327 =item *
328  
329 C<vlozenge>, C<hlozenge> - something like lozenge tiles
330  
331 =item *
332  
333 C<scalesdown>, C<scalesup>, C<scalesleft>, C<scalesright> - Vaguely
334 like fish scales in each direction.
335  
336 =item *
337  
338 C<tile_L> - L-shaped tiles
339  
340 =back
341  
342 =item *
343  
344 C<fg>, C<bg> - The C<fg> color is rendered where bits are set in the
345 hatch, and the C<bg> where they are clear. If you use a transparent
346 C<fg> or C<bg>, and set combine, you can overlay the hatch onto an
347 existing image.
348  
349 C<fg> defaults to black, C<bg> to white.
350  
351 =item *
352  
353 C<dx>, C<dy> - An offset into the hatch cell. Both default to zero.
354  
355 =back
356  
357 A blue and white 4-pixel check pattern:
358  
359 my $fill = Imager::Fill->new(hatch => "check2x2", fg => "blue");
360  
361 You can call Imager::Fill->hatches for a list of hatch names.
362  
363 =head2 Fountain fills
364  
365 my $fill = Imager::Fill->new(fountain=>$ftype,
366 xa=>$xa, ya=>$ya, xb=>$xb, yb=>$yb,
367 segments=>$segments, repeat=>$repeat, combine=>$combine,
368 super_sample=>$super_sample, ssample_param=>$ssample_param);
369  
370 This fills the given region with a fountain fill. This is exactly the
371 same fill as the C<fountain> filter, but is restricted to the shape
372 you are drawing, and the fountain parameter supplies the fill type,
373 and is required.
374  
375 A radial fill from white to transparent centered on (50, 50) with a 50
376 pixel radius:
377  
378 use Imager::Fountain;
379 my $segs = Imager::Fountain->simple(colors => [ "FFFFFF", "FFFFFF00" ],
380 positions => [ 0, 1 ]);
381 my $fill = Imager::Fill->new(fountain => "radial", segments => $segs,
382 xa => 50, ya => 50, xb => 0, yb => 50,
383 combine => "normal");
384  
385  
386 =head2 Image Fills
387  
388 my $fill = Imager::Fill->new(image=>$src, xoff=>$xoff, yoff=>$yoff,
389 matrix=>$matrix, combine => $combine);
390  
391 Fills the given image with a tiled version of the given image. The
392 first non-zero value of C<xoff> or C<yoff> will provide an offset
393 along the given axis between rows or columns of tiles respectively.
394  
395 The matrix parameter performs a co-ordinate transformation from the
396 co-ordinates in the target image to the fill image co-ordinates.
397 Linear interpolation is used to determine the fill pixel. You can use
398 the L<Imager::Matrix2d> class to create transformation matrices.
399  
400 The matrix parameter will significantly slow down the fill.
401  
402 # some image to act as a texture
403 my $txim = Imager->new(...);
404  
405 # simple tiling
406 my $fill = Imager::Fill->new(image => $txim);
407  
408 # tile with a vertical offset
409 my $fill = Imager::Fill->new(image => $txim, yoff => 10);
410  
411 # tile with a horizontal offset
412 my $fill = Imager::Fill->new(image => $txim, xoff => 10);
413  
414 # rotated
415 use Imager::Matrix2d;
416 my $fill = Imager::Fill->new(image => $txim,
417 matrix => Imager::Matrix2d->rotate(degrees => 20));
418  
419 =head2 Opacity modification fill
420  
421 my $fill = Imager::Fill->new(type => "opacity",
422 other => $fill, opacity => 0.25);
423  
424 This can be used to make a fill that is a more translucent or opaque
425 version of an existing fill. This is intended for use where you
426 receive a fill object as a parameter and need to change the opacity.
427  
428 Parameters:
429  
430 =over
431  
432 =item *
433  
434 type => "opacity" - Required
435  
436 =item *
437  
438 other - the fill to produce a modified version of. This must be an
439 Imager::Fill object. Required.
440  
441 =item *
442  
443 opacity - multiplier for the source fill opacity. Default: 0.5.
444  
445 =back
446  
447 The source fills combine mode is used.
448  
449 my $hatch = Imager::Fill->new(hatch => "check4x4", combine => "normal");
450 my $fill = Imager::Fill->new(type => "opacity", other => $hatch);
451  
452 =head1 OTHER METHODS
453  
454 =over
455  
456 =item Imager::Fill->hatches
457  
458 A list of all defined hatch names.
459  
460 =item Imager::Fill->combines
461  
462 A list of all combine types.
463  
464 =back
465  
466 =head1 FUTURE PLANS
467  
468 I'm planning on adding the following types of fills:
469  
470 =over
471  
472 =item *
473  
474 C<checkerboard> - combines 2 other fills in a checkerboard
475  
476 =item *
477  
478 C<combine> - combines 2 other fills using the levels of an image
479  
480 =item *
481  
482 C<regmach> - uses the transform2() register machine to create fills
483  
484 =back
485  
486 =head1 AUTHOR
487  
488 Tony Cook <tony@develop-help.com>
489  
490 =head1 SEE ALSO
491  
492 Imager(3)
493  
494 =cut
« blib/lib/Imager/Expr/Assem.pm ↑ Summary blib/lib/Imager/Color.pm »