Search/Driver/HTML24.pm

filter: Statementstmt 95.6% 66 / 69 Branchbran 75.0% 9 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 80.0% 4 / 5 totaltotal 92.7% 90 / 97 CC 10 SCAR 10.6
CC
10
Cov
93%
pattern_line
5.0
match_object
4.4
image_string
2.0
CRAP 2.9
« Search/Driver.pm ↑ Summary Search/Driver/BMP24.pm »
1 package Imager::Search::Driver::HTML24;
2  
3 # Basic search driver implemented in terms of 8-bit
4 # HTML-style strings ( #003399 )
5  
6 5 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Imager::Search::Match ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Imager::Search::Driver ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 ▶use vars qw{$VERSION @ISA};
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 $VERSION = '1.01';
14 5 @ISA = 'Imager::Search::Driver';
15 }
16  
17  
18  
19  
20  
21 #####################################################################
22 # Imager::Search::Driver Methods
23  
24 3 ▶sub image_string {
Subroutine image_string: called CC=2 SCAR=6.9
Pod documented
25 3 my $self = shift;
26 3 my $imager = shift;
27 3 my $string = '';
28 3 my $height = $imager->getheight;
29 3 foreach my $row ( 0 .. $height - 1 ) {
30 # Get the string for the row
31 $string .= join('',
32 103 map { sprintf( "#%02X%02X%02X", ($_->rgba)[0..2] ) }
33 $imager->getscanline( y => $row )
34 );
35 }
36 3 return \$string;
37 }
38  
39 5 ▶sub pattern_lines {
Subroutine pattern_lines: called CC=2 SCAR=6.9
Pod documented
40 5 my $self = shift;
41 5 my $imager = shift;
42 5 my @lines = ();
43 5 my $height = $imager->getheight;
44 5 foreach my $row ( 0 .. $height - 1 ) {
45 54 $lines[$row] = $self->pattern_line($imager, $row);
46 }
47 5 return \@lines;
48 }
49  
50 54 ▶sub pattern_line {
Subroutine pattern_line: called CC=5 SCAR=16.1
Pod undocumented
51 54 my ($self, $imager, $row) = @_;
52  
53 # Get the colour array
54 54 my $line = '';
55 54 my $this = '';
56 54 my $more = 1;
57 54 foreach my $color ( $imager->getscanline( y => $row ) ) {
58 678 my ($r, $g, $b, undef) = $color->rgba;
59 678 my $string = sprintf("#%02X%02X%02X", $r, $g, $b);
60 678 ▶ if ( $this eq $string ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this eq $string) 484 194 678
61 484 $more++;
62 484 next;
63 }
64 194 ▶ $line .= ($more > 1) ? "(?:$this){$more}" : $this; # if $this; (conveniently works without the if) :)
Branch: 1 branch
BranchTrueFalseTotal
$more > 1 ? : 80 114 194
65 194 $more = 1;
66 194 $this = $string;
67 }
68 54 ▶ $line .= ($more > 1) ? "(?:$this){$more}" : $this;
Branch: 1 branch
BranchTrueFalseTotal
$more > 1 ? : 52 2 54
69  
70 54 return $line;
71 }
72  
73 4 ▶sub pattern_regexp {
Subroutine pattern_regexp: called CC=1 SCAR=0.0
Pod documented
74 4 my $self = shift;
75 4 my $pattern = shift;
76 4 my $width = shift;
77  
78 # Assemble the regular expression
79 4 my $pixels = $width - $pattern->width;
80 4 my $newline = '.{' . ($pixels * 7) . '}';
81 4 my $lines = $pattern->lines;
82 4 my $string = join( $newline, @$lines );
83  
84 4 return qr/$string/si;
85 }
86  
87 7 ▶sub match_object {
Subroutine match_object: called CC=4 SCAR=14.9
Pod documented
88 7 my $self = shift;
89 7 my $image = shift;
90 7 my $pattern = shift;
91 7 my $byte = shift;
92 7 my $pixel = $byte / 7;
93  
94 # If the pixel position isn't an integer we matched
95 # at a position that is not a pixel boundary, and thus
96 # this match is a false positive. Shortcut to fail.
97 7 ▶ unless ( $pixel == int($pixel) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pixel == int $pixel) 0 7 7
98 0 return; # undef or null list
99 }
100  
101 # Calculate the basic geometry of the match
102 7 my $top = int( $pixel / $image->width );
103 7 my $left = $pixel % $image->width;
104  
105 # If the match overlaps the newline boundary or falls off the bottom
106 # of the image, this is also a false positive. Shortcut to fail.
107 7 ▶ if ( $left > $image->width - $pattern->width ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($left > $image->width - $pattern->width) 0 7 7
108 0 return; # undef or null list
109 }
110 7 ▶ if ( $top > $image->height - $pattern->height ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($top > $image->height - $pattern->height) 0 7 7
111 0 return; # undef or null list
112 }
113  
114 # This is a legitimate match.
115 # Convert to a match object and return.
116 7 return Imager::Search::Match->new(
117 name => $pattern->name,
118 top => $top,
119 left => $left,
120 height => $pattern->height,
121 width => $pattern->width,
122 );
123 }
124  
125 5 1;
126  
127 =pod
128  
129 =head1 NAME
130  
131 Imager::Search::Driver::HTML24 - Simple Imager::Search reference driver
132  
133 =head1 DESCRIPTION
134  
135 B<Imager::Search::Driver::HTML24> is a simple reference driver for
136 L<Imager::Search>.
137  
138 It uses a HTML color string (such as #RRGGBB) for each pixel, providing
139 both a simple text expression of the colour, as well as a hash pixel
140 separator.
141  
142 This colour pattern provides for 24-bit (3 channel, 8-bits per challel)
143 colour depth, suitable for use with the 24-bit L<Imager>.
144  
145 Search patterns are compressed, so that a horizontal stream of identical
146 pixels are represented as a single match group.
147  
148 =head1 SUPPORT
149  
150 See the SUPPORT section of the main L<Imager::Search> module.
151  
152 =head1 AUTHOR
153  
154 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
155  
156 =head1 COPYRIGHT
157  
158 Copyright 2007 Adam Kennedy.
159  
160 This program is free software; you can redistribute
161 it and/or modify it under the same terms as Perl itself.
162  
163 The full text of the license can be found in the
164 LICENSE file included with this module.
165  
166 =cut
« Search/Driver.pm ↑ Summary Search/Driver/BMP24.pm »