Search/Driver/BMP24.pm

filter: Statementstmt 93.6% 59 / 63 Branchbran 50.0% 6 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 100% 4 / 4 totaltotal 88.8% 80 / 90 CC 7 SCAR 9.6
CC
7
Cov
87%
match_object
5.7
image_string
2.0
pattern_lines
2.0
CRAP 2.6
« Search/Driver/HTML24.pm ↑ Summary Search/Screenshot.pm »
1 package Imager::Search::Driver::BMP24;
2  
3 # Basic search driver implemented in terms of 8-bit
4 # HTML-style strings ( #003399 )
5  
6 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Imager::Search::Match ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Imager::Search::Driver ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use vars qw{$VERSION @ISA};
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 $VERSION = '1.01';
14 2 @ISA = 'Imager::Search::Driver';
15 }
16  
17 2 ▶use constant HEADER => 54;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19  
20  
21  
22  
23 #####################################################################
24 # Imager::Search::Driver Methods
25  
26 1 ▶sub image_string {
Subroutine image_string: called CC=2 SCAR=7.0
Pod documented
27 1 my $self = shift;
28 1 my $imager = shift;
29 1 my $data = '';
30 1 ▶ $imager->write(
Branch: 1 branch
BranchTrueFalseTotal
$imager->write("data", \$data, "type", "bmp") or die "Failed to generate image string" 1 0 1
31 data => \$data,
32 type => 'bmp',
33 ) or die "Failed to generate image string";
34 1 return \$data;
35 }
36  
37 1 ▶sub pattern_lines {
Subroutine pattern_lines: called CC=2 SCAR=6.9
Pod documented
38 1 my $self = shift;
39 1 my $imager = shift;
40 1 my $data = '';
41 1 ▶ $imager->write(
Branch: 1 branch
BranchTrueFalseTotal
$imager->write("data", \$data, "type", "bmp") or die "Failed to generate bmp image" 1 0 1
42 data => \$data,
43 type => 'bmp',
44 ) or die "Failed to generate bmp image";
45  
46 # The bmp will contain the raw scanline data we want in
47 # a series of byte ranges. Capture each range and quotemeta
48 # the raw bytes.
49 1 my $pixels = $imager->getwidth;
50 1 my $range = $pixels * 3;
51 1 my $width = $range + (-$range % 4);
52 return [
53 2 map { quotemeta substr( $data, $_, $range ) }
54 1 map { HEADER + $_ * $width }
55 ( 0 .. $imager->getheight - 1 )
56 ];
57 }
58  
59 2 ▶sub pattern_regexp {
Subroutine pattern_regexp: called CC=1 SCAR=0.0
Pod documented
60 2 my $self = shift;
61 2 my $pattern = shift;
62 2 my $width = shift;
63  
64 # Each BMP scan line comes in groups of 4-byte dwords.
65 # As a result, each line contains an amount of useless extra
66 # bytes needed to round it up to a multiple of 4 bytes.
67 2 my $junk = ($width * -3) % 4;
68 2 my $pixels = $width - $pattern->width;
69 2 my $newline = '.{' . ($pixels * 3 + $junk) . '}';
70  
71 # Assemble the regexp
72 2 my $lines = $pattern->lines;
73 2 my $string = join( $newline, @$lines );
74  
75 2 return qr/$string/s;
76 }
77  
78 3 ▶sub match_object {
Subroutine match_object: called CC=5 SCAR=17.3
Pod documented
79 3 my $self = shift;
80 3 my $image = shift;
81 3 my $pattern = shift;
82 3 my $byte = shift;
83  
84 # Remove the delta from the header
85 3 $byte -= HEADER;
86  
87 # If we accidentally matched somewhere in header, we need
88 # to discard the match. Shortcut to fail.
89 3 ▶ unless ( $byte >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($byte >= 0) 0 3 3
90 0 return; # undef or null list
91 }
92  
93 # The bytewidth of a line is pixel width
94 # multiplied by three, plus one for the newline.
95 3 my $pixel_width = $image->width;
96 3 my $byte_junk = ($pixel_width * -3) % 4;
97 3 my $byte_width = $pixel_width * 3 + $byte_junk;
98  
99 # Find the column for the match.
100 # If the column isn't an integer we matched at a position that is
101 # not a pixel boundary, and thus this match is a false positive.
102 # Shortcut to fail.
103 3 my $pixel_left = ($byte % $byte_width) / 3;
104 3 ▶ unless ( $pixel_left == int($pixel_left) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pixel_left == int $pixel_left) 0 3 3
105 0 return; # undef or null list
106 }
107  
108 # If the match overlaps the newline boundary this is also a
109 # false positive. Shortcut to fail.
110 3 ▶ if ( $pixel_left > $image->width - $pattern->width ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pixel_left > $image->width - $pattern->width) 0 3 3
111 0 return; # undef or null list
112 }
113  
114 # The match position represents the bottom row.
115 # If the match falls off the top of the image this is also
116 # a false positive. Shortcut to fail.
117 3 my $pixel_bottom = $image->height - int($byte / $byte_width) - 1;
118 3 ▶ if ( $pixel_bottom < $pattern->height - 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pixel_bottom < $pattern->height - 1) 0 3 3
119 0 return; # undef or null list
120 }
121  
122 # This is a legitimate match.
123 # Convert to a match object and return.
124 3 return Imager::Search::Match->new(
125 name => $pattern->name,
126 top => $pixel_bottom - $pattern->height + 1,
127 left => $pixel_left,
128 height => $pattern->height,
129 width => $pattern->width,
130 );
131 }
132  
133 2 1;
134  
135 =pod
136  
137 =head1 NAME
138  
139 Imager::Search::Driver::BMP24 - Imager::Search driver based on 24-bit BMP
140  
141 =head1 DESCRIPTION
142  
143 B<Imager::Search::Driver::BMP24> is a simple default driver for L<Imager::Search>.
144  
145 It generates a search regular expression that can scan a Windows BMP
146 directly, taking advantage of fast underlying C code that generates these
147 files.
148  
149 For a 1024x768 screen grab, the result is that the BMP24 driver is 50-100
150 times faster to generate a search image compated to the HTML24 driver.
151  
152 =head1 SUPPORT
153  
154 See the SUPPORT section of the main L<Imager::Search> module.
155  
156 =head1 AUTHOR
157  
158 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
159  
160 =head1 COPYRIGHT
161  
162 Copyright 2007 - 2011 Adam Kennedy.
163  
164 This program is free software; you can redistribute
165 it and/or modify it under the same terms as Perl itself.
166  
167 The full text of the license can be found in the
168 LICENSE file included with this module.
169  
170 =cut
« Search/Driver/HTML24.pm ↑ Summary Search/Screenshot.pm »