blib/lib/Image/BMP.pm

filter: Statementstmt 56.8% 212 / 373 Branchbran 47.6% 103 / 216 Conditioncond 33.6% 35 / 104 MC/DCmcdc 11.4% 7 / 61 Subroutinesub 54.9% 28 / 51 Podpod 30.9% 13 / 42 totaltotal 46.9% 398 / 847 CC 148 SCAR 35.8
CC
148
Cov
51%
load
135.9
xy_index
56.0
xy
56.0
CRAP 35.9
↑ Summary
1 package Image::BMP;
2  
3 # Filename: Image/BMP.pm
4 # Author: David Ljung Madison <DaveSource.com>
5 # See License: http://MarginalHacks.com/License/
6 #
7 # Description: Reads a .bmp file.
8 # Can also "draw" bmp in ascii art. Cute, eh?
9 #
10 # Limitations: See perlpod doc at bottom
11 #
12 # I couldn't find a standard spec for the format. I chose the fields using:
13 # http://www.daubnet.com/formats/BMP.html
14 #
15 # If you find a simple BMP image that this can't handle, I'd be interested
16 # in seeing it, though I can't guarantee I'll update the code to make it work..
17 #
18 # CHANGELOG
19 # ---------
20 #
21 # Version 1.26 2024/02/06
22 # -----------------------
23 # * Add 'diff' code to testing so we aren't relying on system('diff')
24 #
25 # Version 1.25 2024/02/05
26 # -----------------------
27 # * Close 'bug' requesting update from indirect object creation to new method
28 #
29 # Version 1.23 2024/02/05
30 # -----------------------
31 # * Add tests and a CHANGELOG to be super professional and fancy
32 #
33 # Version 1.22 2024/02/05
34 # -----------------------
35 # * Try to fix packaging issues
36 #
37 # Version 1.20 2024/02/03
38 # -----------------------
39 # * Try to fix packaging issues
40 #
41 # Version 1.19 2016/05/22
42 # -----------------------
43 # * Stupid filehandle bug fix for view_ascii
44 #
45 # Version 1.18 2016/05/21
46 # -----------------------
47 # * Fix for non-byte indexes, reads and writes (Thanks for the inspiration, Mike Paolucci)
48 #
49 # Version 1.17 2012/06/02
50 # -----------------------
51 # * Fix for B/W images with sizes indivisible by 8 (Thanks Jiri Holec, jr.holec volny cz)
52 #
53 # Version 1.16 2008/06/19
54 # -----------------------
55 # * Handle bitfield compression (Thanks Anatoly Savchenkov, asavchenkov alarity com)
56 #
57 # Version 1.15 2007/11/29
58 # -----------------------
59 # + Fix to avoid seeing 24b images as B&W (Thanks Christian Walde, mithaldu yahoo de)
60 #
61 # Version 1.14 2006/09/07
62 # -----------------------
63 # + Fix for border case on last byte in image (Thanks Peter Dons Tychsen, pdt gnmobile com)
64 # + Fix for ColorsUsed==0 (Thanks Marton Nemeth, Marton.Nemeth knorr-bremse com)
65 # See MSDN / Administration and Management Graphics and Multimedia / Bitmaps /
66 # About Bitmaps / Bitmap storage
67 # http://windowssdk.msdn.microsoft.com/en-us/library/ms532311.aspx
68 # and MSDN / Administration and Management Graphics and Multimedia / Bitmaps /
69 # About Bitmaps / Bitmap reference / Bitmap structures / BITMAPINFOHEADER
70 # http://windowssdk.msdn.microsoft.com/en-us/library/ms532290.aspx
71 #
72 # Version 1.13 2006/06/11
73 # -----------------------
74 # + Initial public release
75  
76 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
77 1 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
78 1 ▶use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $LIBRARY);
Subroutine BEGIN: called CC=1 SCAR=0.0
79 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
80  
81 1 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
82 1 @ISA = qw(Exporter);
83 1 @EXPORT_OK = qw(open_file open_pipe close load colormap xy xy_rgb xy_index set save view_ascii debug remember_image ignore_imagemagick_bug add_pixel file);
84  
85 1 $VERSION = '1.26';
86 1 $LIBRARY = __PACKAGE__;
87  
88 ##################################################
89 # Object stuff
90 ##################################################
91 11 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
92 11 my $class = shift;
93 11 my $self = bless {}, $class;
94 11 return $self->init(@_);
95 }
96  
97 11 ▶sub init {
Subroutine init: called CC=2 SCAR=7.0
Pod undocumented
98 11 my $self = shift;
99 11 my %args = @_;
100  
101 # Init values
102 11 $self->{debug} = 0;
103 ## Pick one of the following for image->ascii conversion
104 ## Simple and good for black and white:
105 #my $ascii = ' .-xXX';
106 ## 16 colors somewhat based off of Scarecrow's ASCII Art FAQ
107 #my $ascii = ' .,;+xzmXYUCOMW%';
108 ## Scarecrow's ASCII 70 colors (but very font dependent)
109 11 my $ascii = ' .\'`^",:;Il!i><~+_-?][}{1)(|\/tfjrxnuvczXYUJCLQ0OZmwqpdbkhao*#MW&8%B@$';
110  
111 11 $self->{ascii_array} = [split(//,$ascii)];
112  
113  
114 # Get arg values
115 11 map($self->{$_}=$args{$_}, keys %args);
116  
117 11 ▶ $self->open_file() if $self->{file};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"file"} 11 0 11
118  
119 11 $self;
120 }
121  
122 END {
123 # Cleanup code
124 }
125  
126 # Access to fields
127 11 ▶sub _setget {
Subroutine _setget: called CC=2 SCAR=7.1
128 11 my ($field, $self, $val) = @_;
129 11 ▶ $self->{$field} = $val if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 11 0 11
130 11 $self->{$field};
131 }
132  
133 11 ▶sub debug { _setget('debug',@_); }
Subroutine debug: called CC=1 SCAR=0.0
Pod documented
134 0 ▶sub remember_image { _setget('remember_image',@_); }
Subroutine remember_image: not called CC=1 SCAR=6.9
Pod documented
135 0 ▶sub ignore_imagemagick_bug { _setget('ignore_imagemagick_bug',@_); }
Subroutine ignore_imagemagick_bug: not called CC=1 SCAR=6.9
Pod undocumented
136 0 ▶sub add_pixel {
Subroutine add_pixel: not called CC=4 SCAR=30.0
Pod documented
137 0 my ($self, $val) = @_;
138 0 ▶ return $self->{add_pixel} unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 0 0
139 0 ▶ if ($val) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val) { } 0 0 0
140 0 ▶ return error("add_pixel must be set to a code reference ('0' to clear)")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $val eq "CODE" 0 0 0
141 unless ref $val eq 'CODE';
142 0 $self->{add_pixel} = $val;
143 } else {
144 delete $self->{add_pixel}
145 0 }
146 }
147  
148 0 ▶sub file { open_file(@_); } # alias
Subroutine file: not called CC=1 SCAR=6.9
Pod undocumented
149  
150 ##################################################
151 # Debugging and output
152 ##################################################
153 557706 ▶sub _debug($@) {
Subroutine _debug: called CC=2 SCAR=6.9
154 557706 my ($self,$lvl) = (shift,shift);
155 557706 ▶ return unless $lvl <= $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
unless $lvl <= $self->{"debug"} 557695 11 557706
156 11 printf STDERR @_;
157 }
158 0 ▶sub error { carp "[$LIBRARY] ERROR: ",@_; return 0; }
Subroutine error: not called CC=1 SCAR=6.9
Pod undocumented
159 0 ▶sub fatal { croak "[$LIBRARY] ERROR: ",@_; }
Subroutine fatal: not called CC=1 SCAR=6.9
Pod undocumented
160  
161 ##################################################
162 # Reading the bitmap
163 ##################################################
164 11 ▶sub open_file($$) {
Subroutine open_file: called CC=10 SCAR=25.3
Pod documented
165 11 my ($bmp,$file) = @_;
166 11 ▶ $file = $file || $bmp->{file};
Condition33%1/3
$file || $bmp->{'file'}
l!l&&r!l&&!r
0110
MC/DC0%0/2
$file || $bmp->{'file'}
$file $bmp->{'file'}
Truth table33%1/3
A || B
A= $fileB= $bmp->{'file'}
ABresult
1X1
011
000
167 11 $bmp->_debug(1,"BMP: $file\n");
168 11 $bmp->{fh} = IO::File->new();
169 11 $bmp->{file} = $file;
170  
171 # Avoid using open unless we need it. Bit kludgy. Unnecessary??
172 11 ▶ $bmp->{_pipe} = ($file =~ /\|/) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$file =~ /\|/ ? : 0 11 11
173 11 ▶ if ($bmp->{_pipe}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bmp->{'_pipe'}) { } 0 11 11
174 open($bmp->{fh},$bmp->{file})
175 0 ▶ || fatal("Couldn't open pipe: $file");
Branch: 1 branch
BranchTrueFalseTotal
open $bmp->{"fh"}, $bmp->{"file"} or fatal("Couldn't open pipe: $file") 0 0 0
176 } else {
177 11 ▶ sysopen($bmp->{fh},$bmp->{file},O_RDONLY)
Branch: 1 branch
BranchTrueFalseTotal
sysopen $bmp->{"fh"}, $bmp->{"file"}, 0 or fatal("Couldn't open file: $file") 11 0 11
178 || fatal("Couldn't open file: $file");
179 }
180  
181 11 $bmp->read_header;
182 11 $bmp->read_infoheader;
183 11 $bmp->read_index;
184  
185 # Clear the internal keys
186 11 foreach my $k ( keys %$bmp ) {
187 262 ▶ delete $bmp->{$k} if $k =~ /^_/ && $k ne '_pipe' && $k ne '_colors';
Branch: 1 branch
BranchTrueFalseTotal
if $k =~ /^_/ and $k ne "_pipe" and $k ne "_colors" 0 262 262
Condition83%5/6
$k =~ /^_/ and $k ne "_pipe" and $k ne "_colors"
!ll&&!rl&&r
251110
$k =~ /^_/ and $k ne "_pipe"
!ll&&!rl&&r
2401111
MC/DC0%0/3
$k =~ /^_/ and $k ne "_pipe" and $k ne "_colors"
$k =~ /^_/ $k ne "_pipe" $k ne "_colors"
Truth table75%3/4
A and B and C
A= $k =~ /^_/B= $k ne "_pipe"C= $k ne "_colors"
ABCresult
0XX0
10X0
1100
1111
188 }
189 11 delete $bmp->{Image};
190 }
191  
192 0 ▶sub open_pipe($$) {
Subroutine open_pipe: not called CC=1 SCAR=6.9
Pod documented
193 0 my ($bmp,$pipe) = @_;
194 # Perl is just too easy.
195 0 $bmp->open_file("$pipe |");
196 }
197  
198 0 ▶sub close() {
Subroutine close: not called CC=3 SCAR=24.8
Pod documented
199 0 my ($bmp) = @_;
200 0 ▶ return unless $bmp && $bmp->{fh};
Branch: 1 branch
BranchTrueFalseTotal
unless $bmp and $bmp->{"fh"} 0 0 0
Condition0%0/3
$bmp and $bmp->{"fh"}
!ll&&!rl&&r
000
MC/DC0%0/2
$bmp and $bmp->{"fh"}
$bmp $bmp->{"fh"}
Truth table0%0/3
A and B
A= $bmpB= $bmp->{"fh"}
ABresult
0X0
100
111
201 0 close $bmp->{fh};
202 }
203  
204 122707 ▶sub read_bmp_str {
Subroutine read_bmp_str: called CC=3 SCAR=11.0
Pod undocumented
205 122707 my ($bmp,$bytes) = @_;
206 122707 my $str;
207 122707 my $num = sysread($bmp->{fh}, $str, $bytes);
208 122707 ▶ $bmp->{_byte}+=$num if defined $bmp->{_byte};
Branch: 1 branch
BranchTrueFalseTotal
if defined $bmp->{"_byte"} 121241 1466 122707
209 122707 ▶ fatal("Wanted $bytes bytes, saw $num") unless $num==$bytes;
Branch: 1 branch
BranchTrueFalseTotal
unless $num == $bytes 0 122707 122707
210 122707 $bmp->_debug(5,"read_bmp_str($bmp->{file},$bytes) = $str\n");
211 122707 $str;
212 }
213  
214 122696 ▶sub read_bmp {
Subroutine read_bmp: called CC=2 SCAR=6.9
Pod undocumented
215 122696 my ($bmp,$bytes) = @_;
216 122696 my $data = read_bmp_str($bmp,$bytes);
217 122696 my @data = unpack('C*',$data);
218 122696 my $num=0;
219 122696 foreach my $d ( reverse @data ) {
220 204407 $num = $num*256 + $d;
221 }
222 122696 $bmp->_debug(4,"read_bmp($bmp->{file},$bytes) = $num\n");
223 122696 $num;
224 }
225  
226 24839 ▶sub split_byte {
Subroutine split_byte: called CC=1 SCAR=0.0
Pod undocumented
227 24839 my ($byte) = @_;
228 24839 split('',substr(unpack("B32",pack('N',$byte)),24,8));
229 }
230  
231 238602 ▶sub read_bmp_bits {
Subroutine read_bmp_bits: called CC=5 SCAR=16.1
Pod undocumented
232 238602 my ($bmp,$bits) = @_;
233  
234 # Just read bytes if aligned
235 238602 ▶ return read_bmp($bmp,$bits/8) if ($bits%8)==0;
Branch: 1 branch
BranchTrueFalseTotal
if $bits % 8 == 0 39894 198708 238602
236  
237 # Otherwise pull needed bits (save leftover for next read_bmp_bits)
238 198708 ▶ $bmp->{_extra_bits} = [] unless $bmp->{_extra_bits};
Branch: 1 branch
BranchTrueFalseTotal
unless $bmp->{"_extra_bits"} 4 198704 198708
239 198708 my @bits;
240 198708 ▶ while ($bits-->0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bits-- > 0) 198708 198708 397416
241 198708 ▶ unless ($#{$bmp->{_extra_bits}}>=0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($#{$$bmp{"_extra_bits"};} >= 0) 24839 173869 198708
242 24839 push(@{$bmp->{_extra_bits}}, split_byte(read_bmp($bmp,1)));
243 }
244 198708 push(@bits, shift(@{$bmp->{_extra_bits}}));
245 }
246 198708 @bits;
247 }
248  
249 # Read bmp to pad out to some chunksize (or 4 bytes)
250 921 ▶sub pad_bmp {
Subroutine pad_bmp: called CC=5 SCAR=16.1
Pod undocumented
251 921 my ($bmp, $chunk) = @_;
252 921 ▶ $chunk = $chunk || 4;
Condition50%1/2
$chunk || 4
l!l
0921
MC/DC0%0/1
$chunk || 4
$chunk
Truth table50%1/2
A
A= $chunk
Aresult
11
00
253 921 my $pad = $chunk - $bmp->{_byte}%$chunk;
254 921 ▶ $pad=0 if $pad==$chunk;
Branch: 1 branch
BranchTrueFalseTotal
if $pad == $chunk 424 497 921
255 921 ▶ $pad = $bmp->{_size}-$bmp->{_byte}-1 if ($bmp->{_byte}+$pad>=$bmp->{_size});
Branch: 1 branch
BranchTrueFalseTotal
if $bmp->{"_byte"} + $pad >= $bmp->{"_size"} 9 912 921
256 # Use read_bmp_bits in case we have _extra_bits to read
257 921 ▶ read_bmp_bits($bmp,$pad*8) if $pad>0;
Branch: 1 branch
BranchTrueFalseTotal
if $pad > 0 497 424 921
258 }
259  
260 # Writing files
261 0 ▶sub write_file($$) {
Subroutine write_file: not called CC=4 SCAR=30.0
Pod undocumented
262 0 my ($bmp,$wfile) = @_;
263 0 ▶ $wfile = $wfile || $bmp->{wfile};
Condition0%0/3
$wfile || $bmp->{'wfile'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$wfile || $bmp->{'wfile'}
$wfile $bmp->{'wfile'}
Truth table0%0/3
A || B
A= $wfileB= $bmp->{'wfile'}
ABresult
1X1
011
000
264 0 ▶ return unless $wfile;
Branch: 1 branch
BranchTrueFalseTotal
unless $wfile 0 0 0
265 0 $bmp->{wfile} = $wfile;
266 0 $bmp->{wfh} = IO::File->new();
267 0 ▶ sysopen($bmp->{wfh},$bmp->{wfile},O_WRONLY|O_CREAT)
Branch: 1 branch
BranchTrueFalseTotal
sysopen $bmp->{"wfh"}, $bmp->{"wfile"}, 65 or fatal("Couldn't write file: $wfile") 0 0 0
268 || fatal("Couldn't write file: $wfile");
269 }
270  
271 0 ▶sub write_bmp_str {
Subroutine write_bmp_str: not called CC=2 SCAR=17.9
Pod undocumented
272 0 my ($bmp,$bytes, $str) = @_;
273 0 my $num = syswrite($bmp->{wfh}, $str, $bytes);
274 0 ▶ fatal("Wanted to write $bytes bytes, wrote $num [$str]") unless $num==$bytes;
Branch: 1 branch
BranchTrueFalseTotal
unless $num == $bytes 0 0 0
275 0 $bmp->_debug(5,"write_bmp_str($bmp->{wfile},$bytes,$str)\n");
276 0 $num;
277 }
278  
279 0 ▶sub write_bmp {
Subroutine write_bmp: not called CC=2 SCAR=17.9
Pod undocumented
280 0 my ($bmp,$bytes,$val) = @_;
281 0 my @data;
282 0 ▶ for(my $i=0; $i<$bytes; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $bytes) 0 0 0
283 0 push(@data, $val&255);
284 0 $val >>= 8;
285 }
286 0 my $str = pack('C*',@data);
287 0 my $num = write_bmp_str($bmp,$bytes,$str);
288 0 $bmp->_debug(4,"write_bmp($bmp->{wfile},$val) <= $str\n");
289 0 $num;
290 }
291  
292 0 ▶sub split_bits {
Subroutine split_bits: not called CC=3 SCAR=24.8
Pod undocumented
293 0 my ($val,$bits) = @_;
294 0 ▶ fatal("Can't handle >32b numbers") if $bits>32;
Branch: 1 branch
BranchTrueFalseTotal
if $bits > 32 0 0 0
295 0 ▶ fatal("Tried fitting [$val] into $bits bits") if $val>=(1<<$bits);
Branch: 1 branch
BranchTrueFalseTotal
if $val >= 1 << $bits 0 0 0
296 0 split('',substr(unpack("B32",pack("N",$val)),32-$bits));
297 }
298  
299 0 ▶sub write_bmp_bits {
Subroutine write_bmp_bits: not called CC=3 SCAR=24.8
Pod undocumented
300 0 my ($bmp,$bits,$val) = @_;
301  
302 # Just write bytes if aligned
303 0 ▶ return write_bmp($bmp,$bits/8,$val) if ($bits%8)==0;
Branch: 1 branch
BranchTrueFalseTotal
if $bits % 8 == 0 0 0 0
304  
305 # Break up bits
306 0 push(@{$bmp->{_extra_wr_bits}},split_bits($val,$bits));
307 0 ▶ while ($#{$bmp->{_extra_wr_bits}}>=7) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#{$$bmp{"_extra_wr_bits"};} >= 7) 0 0 0
308 0 my @byte = splice(@{$bmp->{_extra_wr_bits}},0,8);
309 0 my $byte = 0;
310 0 map { $byte = $byte<<1 | $_ } @byte;
311 0 write_bmp($bmp,1,$byte);
312 }
313 }
314  
315 ##################################################
316 # Header
317 ##################################################
318 ### short int of 2 bytes, int of 4 bytes, and long int of 8 bytes.
319 # typedef struct {
320 # unsigned short int type; /* Magic identifier (BM) */
321 # unsigned int size; /* File size in bytes */
322 # unsigned short int reserved1, reserved2;
323 # unsigned int offset; /* Offset to image data, bytes */
324 # } HEADER;
325 11 ▶sub read_header() {
Subroutine read_header: called CC=2 SCAR=7.0
Pod undocumented
326 11 my ($bmp) = @_;
327  
328 11 $bmp->{Signature} = read_bmp_str($bmp,2);
329 11 $bmp->{FileSize} = read_bmp($bmp,4);
330 11 read_bmp($bmp,2); # reserved1
331 11 read_bmp($bmp,2); # reserved2
332 11 $bmp->{DataOffset} = read_bmp($bmp,4);
333  
334 11 ▶ fatal("Not a bitmap: [$bmp->{file}]") unless $bmp->{Signature} eq "BM";
Branch: 1 branch
BranchTrueFalseTotal
unless $bmp->{"Signature"} eq "BM" 0 11 11
335 }
336  
337 0 ▶sub write_header() {
Subroutine write_header: not called CC=2 SCAR=17.9
Pod undocumented
338 0 my ($bmp) = @_;
339  
340 0 write_bmp_str($bmp,2, $bmp->{Signature});
341 0 my $fsize = $bmp->{DataOffset} + $bmp->{Width}*$bmp->{Height}*$bmp->{BitCount}/8;
342 0 write_bmp($bmp,4, $fsize);
343 0 write_bmp($bmp,2,0); # reserved1
344 0 write_bmp($bmp,2,0); # reserved2
345 # Arguably we should recalc DataOffset
346 0 write_bmp($bmp,4, $bmp->{DataOffset});
347  
348 0 ▶ fatal("Not a bitmap: [$bmp->{file}]") unless $bmp->{Signature} eq "BM";
Branch: 1 branch
BranchTrueFalseTotal
unless $bmp->{"Signature"} eq "BM" 0 0 0
349 }
350  
351 ##################################################
352 # Image info data
353 ##################################################
354 #typedef struct {
355 # unsigned int size; /* Header size in bytes */
356 # int width,height; /* Width and height of image */
357 # unsigned short int planes; /* Number of colour planes */
358 # unsigned short int bits; /* Bits per pixel */
359 # unsigned int compression; /* Compression type */
360 # unsigned int imagesize; /* Image size in bytes */
361 # int XpixelsPerM,YpixelsPerM; /* Pixels per meter */
362 # unsigned int ColorsUsed; /* Number of colours */
363 # unsigned int ColorsImportant; /* Important colours */
364 #} INFOHEADER;
365  
366 11 ▶sub read_infoheader() {
Subroutine read_infoheader: called CC=15 SCAR=27.4
Pod undocumented
367 11 my ($bmp) = @_;
368 11 $bmp->{HeaderSize} = read_bmp($bmp,4);
369 11 $bmp->{Width} = read_bmp($bmp,4);
370 11 $bmp->{Height} = abs(read_bmp($bmp,4)); # Can be negative if !BITMAPCOREHEADER (then image goes top->bottom)
371 11 $bmp->{Planes} = read_bmp($bmp,2);
372 11 $bmp->{BitCount} = read_bmp($bmp,2);
373 11 $bmp->{ColorBytes} = int(($bmp->{BitCount}+7)/8);
374 11 $bmp->{Compression} = read_bmp($bmp,4);
375 # Compression BI_RGB = 0; (no compression)
376 # Compression BI_RLE8 = 1;
377 # Compression BI_RLE4 = 2;
378 # Compression BI_BITFIELDS = 3;
379 11 ▶ my $compStr = (qw(BI_RGB BI_RLE8 BI_RLE4 BI_BITFIELDS))[$bmp->{Compression}] || '??';
Condition50%1/2
('BI_RGB', 'BI_RLE8', 'BI_RLE4', 'BI_BITFIELDS')[$bmp->{'Compression'}] || '??'
l!l
110
MC/DC0%0/1
('BI_RGB', 'BI_RLE8', 'BI_RLE4', 'BI_BITFIELDS')[$bmp->{'Compression'}] || '??'
('BI_RGB', 'BI_RLE8', 'BI_RLE4', 'BI_BITFIELDS')[$bmp->{'Compression'}]
Truth table50%1/2
A
A= ('BI_RGB', 'BI_RLE8', 'BI_RLE4', 'BI_BITFIELDS')[$bmp->{'Compression'}]
Aresult
11
00
380 11 $bmp->{ImageSize} = read_bmp($bmp,4);
381 11 $bmp->{XpixelsPerM} = read_bmp($bmp,4);
382 11 $bmp->{YpixelsPerM} = read_bmp($bmp,4);
383 11 $bmp->{ColorsUsed} = read_bmp($bmp,4);
384 11 $bmp->{ColorsImportant} = read_bmp($bmp,4);
385 11 ▶ $bmp->{ColorsUsed} = 1<<$bmp->{BitCount} if $bmp->{ColorsUsed} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $bmp->{"ColorsUsed"} == 0 3 8 11
386  
387 11 $bmp->_debug(1,"Image: $bmp->{BitCount}/$bmp->{ColorsUsed} colors. Geometry: $bmp->{Width}x$bmp->{Height} $bmp->{ImageSize} [comp: $compStr ($bmp->{Compression})]\n");
388  
389 11 $bmp->_debug(2,"Header Size: $bmp->{HeaderSize}B Image: $bmp->{ImageSize}B $bmp->{Width}x$bmp->{Height} $bmp->{XpixelsPerM}x$bmp->{YpixelsPerM}/meter\n");
390 11 $bmp->_debug(2,"Planes=$bmp->{Planes} Bitcount=$bmp->{BitCount} ColorBytes=$bmp->{ColorBytes} Important=$bmp->{ColorsImportant}\n");
391  
392 # Header formats we can't read (yet??)
393 11 foreach my $sh ([12,'OS21XBITMAPHEADER'], [64,'OS22XBITMAPHEADER'], [52,'BITMAPV2INFOHEADER'], [56,'BITMAPV3INFOHEADER'], [124,'BITMAPV5HEADER']) {
394 55 my ($s,$h) = @$sh;
395 55 ▶ fatal("Sorry, can't read bitmaps written with $h\n [$bmp->{file}]") if $bmp->{HeaderSize}==$s;
Branch: 1 branch
BranchTrueFalseTotal
if $bmp->{"HeaderSize"} == $s 0 55 55
396 }
397  
398 fatal("Unknown bitmap format (hdr size $bmp->{HeaderSize}!=40): [$bmp->{file}]")
399 11 ▶ unless $bmp->{HeaderSize}==40 || $bmp->{HeaderSize}==108;
Branch: 1 branch
BranchTrueFalseTotal
unless $bmp->{"HeaderSize"} == 40 or $bmp->{"HeaderSize"} == 108 0 11 11
Condition66%2/3
$bmp->{"HeaderSize"} == 40 or $bmp->{"HeaderSize"} == 108
l!l&&r!l&&!r
920
MC/DC0%0/2
$bmp->{"HeaderSize"} == 40 or $bmp->{"HeaderSize"} == 108
$bmp->{"HeaderSize"} == 40 $bmp->{"HeaderSize"} == 108
Truth table66%2/3
A or B
A= $bmp->{"HeaderSize"} == 40B= $bmp->{"HeaderSize"} == 108
ABresult
1X1
011
000
400  
401 # BITMAPV4HEADER has 68 more bytes to read
402 11 ▶ read_bmp($bmp,68) if $bmp->{HeaderSize}==108;
Branch: 1 branch
BranchTrueFalseTotal
if $bmp->{"HeaderSize"} == 108 2 9 11
403  
404 11 ▶ $bmp->{_colors} = $bmp->{ColorsUsed} || 1<<$bmp->{BitCount};
Condition33%1/3
$bmp->{'ColorsUsed'} || 1 << $bmp->{'BitCount'}
l!l&&r!l&&!r
1100
MC/DC0%0/2
$bmp->{'ColorsUsed'} || 1 << $bmp->{'BitCount'}
$bmp->{'ColorsUsed'} 1 << $bmp->{'BitCount'}
Truth table33%1/3
A || B
A= $bmp->{'ColorsUsed'}B= 1 << $bmp->{'BitCount'}
ABresult
1X1
011
000
405 11 ▶ $bmp->{_colors} = 0 if $bmp->{_colors}==(1<<24); # No truecolor map
Branch: 1 branch
BranchTrueFalseTotal
if $bmp->{"_colors"} == 16777216 1 10 11
406  
407 # Treat mask colors as color indexes (just to skip them) and reset
408 # compression to none [Thanks Anatoly Savchenkov, asavchenkov alarity com]
409 # MSDN Article: "BI_BITFIELDS"
410 # Specifies that the bitmap is not compressed and that the color table
411 # consists of three DWORD color masks that specify the red, green, and
412 # blue components, respectively, of each pixel. This is valid when used
413 # with 16- and 32-bpp bitmaps.
414 ($bmp->{_colors}, $bmp->{Compression}) = (3,0)
415 11 ▶ if $bmp->{Compression} == 3;
Branch: 1 branch
BranchTrueFalseTotal
if $bmp->{"Compression"} == 3 1 10 11
416  
417 11 my $DataOffset = 14+$bmp->{HeaderSize}+4*$bmp->{_colors};
418 error("Corrupt bitmap header? [$bmp->{file}]\n (DataOffset!=14+HeaderSize+4*Colors?)")
419 11 ▶ unless $bmp->{DataOffset} == $DataOffset;
Branch: 1 branch
BranchTrueFalseTotal
unless $bmp->{"DataOffset"} == $DataOffset 0 11 11
420 11 $bmp->{DataOffset} = $DataOffset;
421  
422 # Do we use indexed color?
423 11 $bmp->{IndexedColor} = 1;
424 11 ▶ $bmp->{IndexedColor} = 0 if $bmp->{BitCount}==24; # True color
Branch: 1 branch
BranchTrueFalseTotal
if $bmp->{"BitCount"} == 24 1 10 11
425 11 ▶ $bmp->{IndexedColor} = 0 if $bmp->{BitCount}==32; # True color
Branch: 1 branch
BranchTrueFalseTotal
if $bmp->{"BitCount"} == 32 1 10 11
426 #$bmp->{IndexedColor} = 0 if $bmp->{BitCount}==1; # B&W -> Better to read it in case it's inverted?
427 11 ▶ $bmp->{IndexedColor} = 0 if !$bmp->{ColorsUsed};
Branch: 1 branch
BranchTrueFalseTotal
if not $bmp->{"ColorsUsed"} 0 11 11
428 }
429  
430 0 ▶sub write_infoheader() {
Subroutine write_infoheader: not called CC=2 SCAR=17.9
Pod undocumented
431 0 my ($bmp) = @_;
432 0 write_bmp($bmp,4, $bmp->{HeaderSize});
433 0 write_bmp($bmp,4, $bmp->{Width});
434 0 write_bmp($bmp,4, $bmp->{Height});
435 0 write_bmp($bmp,2, $bmp->{Planes});
436 0 write_bmp($bmp,2, $bmp->{BitCount});
437 0 write_bmp($bmp,4, 0); # No compression on writing
438  
439 # Calc imagesize (width*height*bits + padding)
440 0 my $line = $bmp->{Width} * $bmp->{BitCount};
441 0 ▶ my $pad = 32-$line%32; $pad=0 if $pad==32;
Branch: 1 branch
BranchTrueFalseTotal
if $pad == 32 0 0 0
442 0 my $size = ($line+$pad)*$bmp->{Height}; #*$bmp->{BitCount};
443  
444 0 write_bmp($bmp,4, int($size/8));
445 0 write_bmp($bmp,4, $bmp->{XpixelsPerM});
446 0 write_bmp($bmp,4, $bmp->{YpixelsPerM});
447 0 write_bmp($bmp,4, $bmp->{ColorsUsed});
448 0 write_bmp($bmp,4, $bmp->{ColorsImportant});
449 }
450  
451 623622 ▶sub rgb {
Subroutine rgb: called CC=2 SCAR=7.1
Pod undocumented
452 623622 my ($rgb) = @_;
453 623622 ▶ $rgb=0 unless defined $rgb;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rgb 0 623622 623622
454 623622 ((($rgb>>16) & 0xff),
455 (($rgb>>8 ) & 0xff),
456 (($rgb>>0 ) & 0xff));
457 }
458  
459 11 ▶sub read_index() {
Subroutine read_index: called CC=4 SCAR=13.9
Pod undocumented
460 11 my ($bmp) = @_;
461  
462 11 ▶ unless ($bmp->{IndexedColor}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($bmp->{"IndexedColor"}) 2 9 11
463 # Sometimes when ColorsUsed is 0 they still have the
464 # basic greyscale map, we need to skip past it.
465 # Still read it in case it's reversed or some such..
466 2 ▶ if ($bmp->{_pipe}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bmp->{'_pipe'}) { } 0 2 2
467 0 read_bmp($bmp,4*$bmp->{_colors});
468 } else {
469 2 sysseek($bmp->{fh},$bmp->{DataOffset},SEEK_SET);
470 }
471 2 return;
472 }
473  
474 9 ▶ for(my $i=0; $i<$bmp->{ColorsUsed}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $bmp->{"ColorsUsed"}) 1288 9 1297
475 # r,g,b
476 1288 my $rgb = read_bmp($bmp,4);
477 1288 $bmp->{Index}{rgb}[$i] = $rgb;
478 1288 $bmp->{Index}{back}{$rgb} = $i;
479 }
480 }
481  
482 0 ▶sub write_index() {
Subroutine write_index: not called CC=2 SCAR=17.9
Pod undocumented
483 0 my ($bmp) = @_;
484  
485 0 unless (1 || $bmp->{IndexedColor}) {
486 # Sometimes when ColorsUsed is 0 they still have the
487 # basic greyscale map, we need to get past the DataOffset we wrote
488 # We could've recalced DataOffset above, but I'm lazy..
489 write_bmp($bmp,4*$bmp->{_colors},0);
490 return;
491 }
492  
493 0 ▶ for(my $i=0; $i<$bmp->{ColorsUsed}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $bmp->{"ColorsUsed"}) 0 0 0
494 0 write_bmp($bmp,4, $bmp->{Index}{rgb}[$i]);
495 }
496 }
497  
498 312251 ▶sub colormap {
Subroutine colormap: called CC=6 SCAR=18.3
Pod documented
499 312251 my ($bmp, $index) = @_;
500  
501 # B&W
502 return $index ? 0xffffff : 0x000000
503 312251 ▶ if $bmp->{BitCount}==1 || (!$bmp->{ColorsUsed} && $bmp->{BitCount}!=24);
Branch: 2 branches
BranchTrueFalseTotal
if $bmp->{"BitCount"} == 1 or not $bmp->{"ColorsUsed"} and $bmp->{"BitCount"} != 24 198708 113543 312251
$index ? : 197149 1559 198708
Condition50%3/6
$bmp->{"BitCount"} == 1 or not $bmp->{"ColorsUsed"} and $bmp->{"BitCount"} != 24
l!l&&r!l&&!r
1987080113543
not $bmp->{"ColorsUsed"} and $bmp->{"BitCount"} != 24
!ll&&!rl&&r
11354300
MC/DC33%1/3
$bmp->{"BitCount"} == 1 or not $bmp->{"ColorsUsed"} and $bmp->{"BitCount"} != 24
$bmp->{"BitCount"} == 1 not $bmp->{"ColorsUsed"} $bmp->{"BitCount"} != 24
Truth table50%2/4
A or not(B) and C
A= $bmp->{"BitCount"} == 1B= not $bmp->{"ColorsUsed"}C= $bmp->{"BitCount"} != 24
ABCresult
1XX1
0111
00X0
0100
504  
505 # True color
506 113543 ▶ return $index unless $bmp->{IndexedColor};
Branch: 1 branch
BranchTrueFalseTotal
unless $bmp->{"IndexedColor"} 37888 75655 113543
507  
508 75655 $bmp->{Index}{rgb}[$index];
509 }
510  
511 0 ▶sub decolormap {
Subroutine decolormap: not called CC=6 SCAR=37.4
Pod undocumented
512 0 my ($bmp, $color) = @_;
513  
514 # B&W
515 0 ▶ return $color ? 1 : 0 if $bmp->{BitCount}==1 || !$bmp->{ColorsUsed};
Branch: 2 branches
BranchTrueFalseTotal
if $bmp->{"BitCount"} == 1 or not $bmp->{"ColorsUsed"} 0 0 0
$color ? : 0 0 0
Condition0%0/3
$bmp->{"BitCount"} == 1 or not $bmp->{"ColorsUsed"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$bmp->{"BitCount"} == 1 or not $bmp->{"ColorsUsed"}
$bmp->{"BitCount"} == 1 not $bmp->{"ColorsUsed"}
Truth table0%0/3
A or not(B)
A= $bmp->{"BitCount"} == 1B= not $bmp->{"ColorsUsed"}
ABresult
1X1
011
000
516  
517 # True color
518 0 ▶ return $color unless $bmp->{IndexedColor};
Branch: 1 branch
BranchTrueFalseTotal
unless $bmp->{"IndexedColor"} 0 0 0
519  
520 0 my $index = $bmp->{Index}{back}{$color};
521 0 ▶ return $index if defined $index;
Branch: 1 branch
BranchTrueFalseTotal
if defined $index 0 0 0
522 0 fatal("Color [$color] not found in orginal colormap\nCurrently the colormap is not updated with new colors\n");
523 }
524  
525 ##################################################
526 # Image
527 ##################################################
528 312251 ▶sub next_xy {
Subroutine next_xy: called CC=7 SCAR=19.5
Pod undocumented
529 312251 my ($bmp,$x,$y,$pad) = @_;
530  
531 # Padding at end of each line
532 312251 ▶ pad_bmp($bmp) if $pad && $x==$bmp->{Width}-1;
Branch: 1 branch
BranchTrueFalseTotal
if $pad and $x == $bmp->{"Width"} - 1 921 311330 312251
Condition100%3/3
$pad and $x == $bmp->{"Width"} - 1
!ll&&!rl&&r
74146237184921
MC/DC100%2/2
$pad and $x == $bmp->{"Width"} - 1
$pad $x == $bmp->{"Width"} - 1
Truth table100%3/3
A and B
A= $padB= $x == $bmp->{"Width"} - 1
ABresult
0X0
100
111
533 312251 ▶ return (undef,undef) if $bmp->{_byte}>$bmp->{_size};
Branch: 1 branch
BranchTrueFalseTotal
if $bmp->{"_byte"} > $bmp->{"_size"} 0 312251 312251
534  
535 312251 ▶ ($x,$y) = (0, $y-1) if (++$x >= $bmp->{Width});
Branch: 1 branch
BranchTrueFalseTotal
if ++$x >= $bmp->{"Width"} 1249 311002 312251
536 312251 ▶ return (undef,undef) unless defined $y && $y>=0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $y and $y >= 0 14 312237 312251
Condition100%3/3
defined $y and $y >= 0
!ll&&!rl&&r
311312237
MC/DC100%2/2
defined $y and $y >= 0
defined $y $y >= 0
Truth table100%3/3
A and B
A= defined $yB= $y >= 0
ABresult
0X0
100
111
537 312237 ($x,$y);
538 }
539  
540 3 ▶sub error_too_big {
Subroutine error_too_big: called CC=2 SCAR=7.2
Pod undocumented
541 3 my ($bmp) = @_;
542 error("Corrupt BMP - too big.\n",
543 " (ImageMagick sometimes incorrectly places endline marker",
544 " Set option 'ignore_imagemagick_bug' to hide this message)")
545 3 ▶ unless $bmp->{ignore_imagemagick_bug}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $bmp->{"ignore_imagemagick_bug"}++ 0 3 3
546 }
547  
548 312251 ▶sub _add_pixel {
Subroutine _add_pixel: called CC=7 SCAR=24.5
549 312251 my ($bmp,$x,$y,$color) = @_;
550 312251 ▶ return error_too_big($bmp) unless defined $y && $y>=0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $y and $y >= 0 3 312248 312251
Condition66%2/3
defined $y and $y >= 0
!ll&&!rl&&r
30312248
MC/DC50%1/2
defined $y and $y >= 0
defined $y $y >= 0
Truth table66%2/3
A and B
A= defined $yB= $y >= 0
ABresult
0X0
100
111
551  
552 312248 $bmp->_debug(3,"Pixel($x,$y) = %0.2x,%0.2x,%0.2x\n",rgb($color));
553  
554 # Save it in our 2D array
555 $bmp->{Image}[$x][$y] = $color
556 312248 ▶ if !$bmp->{add_pixel} || $bmp->{remember_image};
Branch: 1 branch
BranchTrueFalseTotal
if not $bmp->{"add_pixel"} or $bmp->{"remember_image"} 312248 0 312248
Condition33%1/3
not $bmp->{"add_pixel"} or $bmp->{"remember_image"}
l!l&&r!l&&!r
31224800
MC/DC0%0/2
not $bmp->{"add_pixel"} or $bmp->{"remember_image"}
not $bmp->{"add_pixel"} $bmp->{"remember_image"}
Truth table33%1/3
not(A) or B
A= not $bmp->{"add_pixel"}B= $bmp->{"remember_image"}
ABresult
1X1
011
000
557  
558 # add_pixel function?
559 312248 ▶ return unless $bmp->{add_pixel};
Branch: 1 branch
BranchTrueFalseTotal
unless $bmp->{"add_pixel"} 312248 0 312248
560 fatal("add_pixel must be a subroutine pointer [not ".(ref $bmp->{add_pixel})."]")
561 0 ▶ unless (ref $bmp->{add_pixel} eq 'CODE');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $bmp->{"add_pixel"} eq "CODE" 0 0 0
562 0 &{$bmp->{add_pixel}}($bmp,$x,$y,rgb($color));
563 }
564  
565 11 ▶sub load() {
Subroutine load: called CC=35 SCAR=49.1
Pod documented
566 11 my ($bmp, $file) = @_;
567  
568 11 ▶ $bmp->file($file) if $file;
Branch: 1 branch
BranchTrueFalseTotal
if $file 0 11 11
569 11 ▶ return error("You haven't opened a file yet") unless $bmp->{file};
Branch: 1 branch
BranchTrueFalseTotal
unless $bmp->{"file"} 0 11 11
570  
571 11 ▶ if ($bmp->{_image_loaded}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bmp->{"_image_loaded"}) 0 11 11
572 0 ▶ if ($bmp->{_pipe}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($bmp->{'_pipe'}) { } 0 0 0
elsif ($bmp->{'_image_remembered'} and not $bmp->{'add_pixel'}) { } 0 0 0
573 0 return error("You can't call load twice on a pipe.\n Use 'remember_image' option");
574 ▶ } elsif ($bmp->{_image_remembered} && !$bmp->{add_pixel}) {
Condition0%0/3
$bmp->{"_image_remembered"} and not $bmp->{"add_pixel"}
!ll&&!rl&&r
000
MC/DC0%0/2
$bmp->{"_image_remembered"} and not $bmp->{"add_pixel"}
$bmp->{"_image_remembered"} not $bmp->{"add_pixel"}
Truth table0%0/3
A and not(B)
A= $bmp->{"_image_remembered"}B= not $bmp->{"add_pixel"}
ABresult
0X0
100
111
575 # There's no reason to do this again, unless they want
576 # to save the image, or else call their add_pixel again.
577 0 return 1;
578 }
579 0 sysseek($bmp->{fh},$bmp->{DataOffset},SEEK_SET);
580 }
581  
582 # Compressed?
583 11 ▶ my $rle = ($bmp->{Compression}==1 && $bmp->{BitCount}==8) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$bmp->{'Compression'} == 1 && $bmp->{'BitCount'} == 8 ? : 2 9 11
Condition66%2/3
$bmp->{"Compression"} == 1 and $bmp->{"BitCount"} == 8
!ll&&!rl&&r
902
MC/DC50%1/2
$bmp->{"Compression"} == 1 and $bmp->{"BitCount"} == 8
$bmp->{"Compression"} == 1 $bmp->{"BitCount"} == 8
Truth table66%2/3
A and B
A= $bmp->{"Compression"} == 1B= $bmp->{"BitCount"} == 8
ABresult
0X0
100
111
584 fatal("Can't handle this bitmap compression: [$bmp->{file}]\n\t(Try 'convert -compress None')")
585 11 ▶ if $bmp->{Compression} && !$rle;
Branch: 1 branch
BranchTrueFalseTotal
if $bmp->{"Compression"} and not $rle 0 11 11
Condition66%2/3
$bmp->{"Compression"} and not $rle
!ll&&!rl&&r
920
MC/DC0%0/2
$bmp->{"Compression"} and not $rle
$bmp->{"Compression"} not $rle
Truth table66%2/3
A and not(B)
A= $bmp->{"Compression"}B= not $rle
ABresult
0X0
100
111
586  
587 # We need to read bits for this - which would mean buffering and shit..
588 fatal("Can't handle non-byte indexes - sorry [$bmp->{BitCount} bits].")
589 11 ▶ unless $bmp->{BitCount}==1 || ($bmp->{BitCount}%8)==0;
Branch: 1 branch
BranchTrueFalseTotal
unless $bmp->{"BitCount"} == 1 or $bmp->{"BitCount"} % 8 == 0 0 11 11
Condition66%2/3
$bmp->{"BitCount"} == 1 or $bmp->{"BitCount"} % 8 == 0
l!l&&r!l&&!r
470
MC/DC0%0/2
$bmp->{"BitCount"} == 1 or $bmp->{"BitCount"} % 8 == 0
$bmp->{"BitCount"} == 1 $bmp->{"BitCount"} % 8 == 0
Truth table66%2/3
A or B
A= $bmp->{"BitCount"} == 1B= $bmp->{"BitCount"} % 8 == 0
ABresult
1X1
011
000
590  
591 # Calculate size
592 11 my $line = $bmp->{Width} * $bmp->{BitCount};
593 # Each line is padded to 4 bytes
594 11 ▶ my $pad = 32-$line%32; $pad=0 if $pad==32;
Branch: 1 branch
BranchTrueFalseTotal
if $pad == 32 4 7 11
595 11 $bmp->{_sizebits} = ($line+$pad)*$bmp->{Height}; #*$bmp->{BitCount};
596 11 $bmp->{_size} = $bmp->{_sizebits}/8;
597  
598 11 ▶ $bmp->{_size} = $bmp->{ImageSize} if $rle;
Branch: 1 branch
BranchTrueFalseTotal
if $rle 2 9 11
599 11 ▶ $bmp->{ImageSize} = $bmp->{ImageSize} || $bmp->{_size};
Condition33%1/3
$bmp->{'ImageSize'} || $bmp->{'_size'}
l!l&&r!l&&!r
1100
MC/DC0%0/2
$bmp->{'ImageSize'} || $bmp->{'_size'}
$bmp->{'ImageSize'} $bmp->{'_size'}
Truth table33%1/3
A || B
A= $bmp->{'ImageSize'}B= $bmp->{'_size'}
ABresult
1X1
011
000
600  
601 error("Error - imagesize doesn't seem to be calculated properly:\n".
602 " (imagesize < width+padding * height)")
603 11 ▶ unless $bmp->{_size} == $bmp->{ImageSize};
Branch: 1 branch
BranchTrueFalseTotal
unless $bmp->{"_size"} == $bmp->{"ImageSize"} 0 11 11
604  
605 11 $bmp->_debug(1,"Reading image data - [$bmp->{Width} x $bmp->{Height} x $bmp->{BitCount}]...\n");
606  
607 # Image starts from bottom left and reads right then up
608 11 my ($x,$y) = (0, $bmp->{Height}-1);
609 11 $bmp->{_byte}=0;
610 11 ▶ while ($bmp->{_byte}<=$bmp->{_size}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bmp->{"_byte"} <= $bmp->{"_size"}) 266359 0 266359
611 266359 ▶ if ($rle) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rle) { } 28254 238105 266359
612 28254 my $n = read_bmp($bmp,1);
613 28254 my $c = read_bmp($bmp,1);
614 28254 ▶ if ($n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n) { } 27928 326 28254
615 # Repeat next byte 'n' times
616 #TODO: Compression lvl 2 (4-bit color) needs to flip colors back and forth...
617 27928 ▶ while ($n-->0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n-- > 0) 74146 27928 102074
618 74146 _add_pixel($bmp,$x,$y,colormap($bmp,$c));
619 74146 ($x,$y) = next_xy($bmp,$x,$y);
620 }
621 27928 ▶ last unless defined $x;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $x 2 27926 27928
622 } else {
623 326 ▶ if ($c==0) {
Branch: 3 branches
BranchTrueFalseTotal
if ($c == 0) { } 326 0 326
elsif ($c == 1) { } 0 0 0
elsif ($c == 2) { } 0 0 0
624 # End of line
625 326 ▶ $x=0 if $x;
Branch: 1 branch
BranchTrueFalseTotal
if $x 326 0 326
626 #($x,$y) = (0,$y-1) if $x;
627 } elsif ($c==1) {
628 # End of bitmap
629 0 last;
630 # Sometimes there are bytes left in _size - I don't know why...
631 # Oh - actually we should be 4byte aligned - that might be it.
632  
633 } elsif ($c==2) {
634 # Delta. Following 2 bytes are offset x,y
635 # Argh.. Not tested. I need an image that uses this encoding.
636 0 print STDERR "Untested delta code.. Please send me a copy of this image for testing!\n";
637 0 my $dx = read_bmp($bmp,1);
638 0 my $dy = read_bmp($bmp,1);
639 0 $x+=$dx;
640 0 $y-=$dy;
641  
642 } else {
643 # Following 'c' bytes are regular colors. Pad if 'c' is odd.
644 0 my $pad = $c&1;
645 0 ▶ while ($c-->0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c-- > 0) 0 0 0
646 0 my $index = read_bmp($bmp,1);
647 0 _add_pixel($bmp,$x,$y,colormap($bmp,$index));
648 0 ($x,$y) = next_xy($bmp,$x,$y);
649 }
650 0 ▶ error("Corrupt BMP: pad byte should be zero")
Branch: 1 branch
BranchTrueFalseTotal
if $pad and read_bmp($bmp, 1) 0 0 0
Condition0%0/3
$pad and read_bmp($bmp, 1)
!ll&&!rl&&r
000
MC/DC0%0/2
$pad and read_bmp($bmp, 1)
$pad read_bmp($bmp, 1)
Truth table0%0/3
A and B
A= $padB= read_bmp($bmp, 1)
ABresult
0X0
100
111
651 if ($pad && read_bmp($bmp,1))
652 }
653 }
654 } else {
655 238105 my ($index) = read_bmp_bits($bmp,$bmp->{BitCount});
656 238105 my $color = colormap($bmp,$index);
657 238105 _add_pixel($bmp,$x,$y,$color);
658  
659 238105 ($x,$y) = next_xy($bmp,$x,$y,1);
660 238105 ▶ last unless defined $x;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $x 9 238096 238105
661 }
662 }
663  
664 11 $bmp->{_image_loaded} = 1;
665 11 ▶ $bmp->{_image_remembered} = (!$bmp->{add_pixel} || $bmp->{remember_image}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
!$bmp->{'add_pixel'} || $bmp->{'remember_image'} ? : 11 0 11
Condition33%1/3
not $bmp->{"add_pixel"} or $bmp->{"remember_image"}
l!l&&r!l&&!r
1100
MC/DC0%0/2
not $bmp->{"add_pixel"} or $bmp->{"remember_image"}
not $bmp->{"add_pixel"} $bmp->{"remember_image"}
Truth table33%1/3
not(A) or B
A= not $bmp->{"add_pixel"}B= $bmp->{"remember_image"}
ABresult
1X1
011
000
666  
667 # Should finish at:
668 error("Premature end of BMP file [$x,$y]")
669 11 ▶ if defined $x && ($x!=$bmp->{Width}-1 || $y);
Branch: 1 branch
BranchTrueFalseTotal
if defined $x and $x != $bmp->{"Width"} - 1 || $y 0 11 11
Condition16%1/6
defined $x and $x != $bmp->{"Width"} - 1 || $y
!ll&&!rl&&r
1100
$x != $bmp->{"Width"} - 1 or $y
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $x and $x != $bmp->{"Width"} - 1 || $y
defined $x $x != $bmp->{"Width"} - 1 $y
Truth table25%1/4
A and B or C
A= defined $xB= $x != $bmp->{"Width"} - 1C= $y
ABCresult
0XX0
1000
11X1
1011
670  
671 11 1;
672 }
673  
674 # We can't do some things until we have the image read
675 11 ▶sub needs_image {
Subroutine needs_image: called CC=4 SCAR=17.9
Pod undocumented
676 11 my ($bmp,$do) = @_;
677  
678 11 ▶ return undef if !$bmp->{_image_loaded} && !$bmp->load;
Branch: 1 branch
BranchTrueFalseTotal
unless $bmp->{"_image_loaded"} or $bmp->load 0 11 11
Condition33%1/3
$bmp->{"_image_loaded"} or $bmp->load
l!l&&r!l&&!r
0110
MC/DC0%0/2
$bmp->{"_image_loaded"} or $bmp->load
$bmp->{"_image_loaded"} $bmp->load
Truth table33%1/3
A or B
A= $bmp->{"_image_loaded"}B= $bmp->load
ABresult
1X1
011
000
679  
680 # Do we have image data?
681 11 ▶ unless ($bmp->{_image_remembered}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($bmp->{"_image_remembered"}) 0 11 11
682 0 error("Can't $do with add_pixel functions\n (Unless you set 'remember_image')\n");
683 0 return undef;
684 }
685 }
686  
687 0 ▶sub save() {
Subroutine save: not called CC=5 SCAR=34.0
Pod undocumented
688 0 my ($bmp, $file) = @_;
689  
690 0 $bmp->needs_image("save images");
691  
692 0 $bmp->write_file($file);
693  
694 0 $bmp->write_header;
695 0 $bmp->write_infoheader;
696 0 $bmp->write_index;
697  
698 0 $bmp->_debug(1,"Writing image data...\n");
699  
700 # Each line is padded to 4 bytes
701 0 my $line = $bmp->{Width} * $bmp->{BitCount};
702 0 ▶ my $pad = 32-$line%32; $pad=0 if $pad==32;
Branch: 1 branch
BranchTrueFalseTotal
if $pad == 32 0 0 0
703  
704 # Image starts from bottom left and reads right then up
705 0 ▶ for (my $y=$bmp->{Height}-1; $y>=0; $y--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y >= 0) 0 0 0
706 0 ▶ for (my $x=0; $x<$bmp->{Width}; $x++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < $bmp->{"Width"}) 0 0 0
707 0 my $color = xy($bmp,$x,$y);
708 0 my $index = $bmp->decolormap($color);
709 0 write_bmp_bits($bmp, $bmp->{BitCount}, $index);
710 }
711 # Pad each line
712 0 ▶ write_bmp($bmp,int($pad/8),0) if $pad>0;
Branch: 1 branch
BranchTrueFalseTotal
if $pad > 0 0 0 0
713 }
714 0 1;
715 }
716  
717 # "Darkness" is distance from white (0 to 1)
718 1 my $MAXDARK = sqrt(0xff*0xff*3);
719 311374 ▶sub darkness {
Subroutine darkness: called CC=2 SCAR=7.1
Pod undocumented
720 311374 my ($r,$g,$b) = @_;
721 311374 ▶ ($r,$g,$b) = rgb($r) unless defined $g;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $g 311374 0 311374
722 311374 my $dark = sqrt((0xff-$r)**2+(0xff-$g)**2+(0xff-$b)**2) / $MAXDARK;
723 }
724  
725 # Get or set a given pixel, undef on error
726 0 ▶sub xy_index {
Subroutine xy_index: not called CC=7 SCAR=40.3
Pod documented
727 0 my ($bmp,$x,$y, $index) = @_;
728  
729 0 $bmp->needs_image("use xy method");
730  
731 0 ▶ if ($x>=$bmp->{Width} || $x<0 ||
Branch: 1 branch
BranchTrueFalseTotal
if ($x >= $bmp->{"Width"} or $x < 0 or $y >= $bmp->{"Height"} or $y < 0) 0 0 0
Condition0%0/9
$x >= $bmp->{"Width"} or $x < 0 or $y >= $bmp->{"Height"} or $y < 0
l!l&&r!l&&!r
000
$x >= $bmp->{"Width"} or $x < 0 or $y >= $bmp->{"Height"}
l!l&&r!l&&!r
000
$x >= $bmp->{"Width"} or $x < 0
l!l&&r!l&&!r
000
MC/DC0%0/4
$x >= $bmp->{"Width"} or $x < 0 or $y >= $bmp->{"Height"} or $y < 0
$x >= $bmp->{"Width"} $x < 0 $y >= $bmp->{"Height"} $y < 0
Truth table0%0/5
A or B or C or D
A= $x >= $bmp->{"Width"}B= $x < 0C= $y >= $bmp->{"Height"}D= $y < 0
ABCDresult
1XXX1
01XX1
001X1
00011
00000
732 $y>=$bmp->{Height} || $y<0) {
733 0 error("xy_index($x,$y) is out of bounds [$bmp->{Width}x$bmp->{Height}]");
734 0 return undef;
735 }
736  
737 0 ▶ return $bmp->{Image}[$x][$y] = $bmp->colormap($index) if defined($index);
Branch: 1 branch
BranchTrueFalseTotal
if defined $index 0 0 0
738 0 ▶ $bmp->decolormap($bmp->{Image}[$x][$y] || 0);
Condition0%0/2
$bmp->{'Image'}[$x][$y] || 0
l!l
00
MC/DC0%0/1
$bmp->{'Image'}[$x][$y] || 0
$bmp->{'Image'}[$x][$y]
Truth table0%0/2
A
A= $bmp->{'Image'}[$x][$y]
Aresult
11
00
739 }
740  
741 0 ▶sub xy {
Subroutine xy: not called CC=7 SCAR=40.3
Pod documented
742 0 my ($bmp,$x,$y, $val) = @_;
743  
744 0 $bmp->needs_image("use xy method");
745  
746 0 ▶ if ($x>=$bmp->{Width} || $x<0 ||
Branch: 1 branch
BranchTrueFalseTotal
if ($x >= $bmp->{"Width"} or $x < 0 or $y >= $bmp->{"Height"} or $y < 0) 0 0 0
Condition0%0/9
$x >= $bmp->{"Width"} or $x < 0 or $y >= $bmp->{"Height"} or $y < 0
l!l&&r!l&&!r
000
$x >= $bmp->{"Width"} or $x < 0 or $y >= $bmp->{"Height"}
l!l&&r!l&&!r
000
$x >= $bmp->{"Width"} or $x < 0
l!l&&r!l&&!r
000
MC/DC0%0/4
$x >= $bmp->{"Width"} or $x < 0 or $y >= $bmp->{"Height"} or $y < 0
$x >= $bmp->{"Width"} $x < 0 $y >= $bmp->{"Height"} $y < 0
Truth table0%0/5
A or B or C or D
A= $x >= $bmp->{"Width"}B= $x < 0C= $y >= $bmp->{"Height"}D= $y < 0
ABCDresult
1XXX1
01XX1
001X1
00011
00000
747 $y>=$bmp->{Height} || $y<0) {
748 0 error("xy($x,$y) is out of bounds [$bmp->{Width}x$bmp->{Height}]");
749 0 return undef;
750 }
751  
752 0 ▶ return $bmp->{Image}[$x][$y] || 0 unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 0 0
Condition0%0/2
$bmp->{'Image'}[$x][$y] || 0
l!l
00
MC/DC0%0/1
$bmp->{'Image'}[$x][$y] || 0
$bmp->{'Image'}[$x][$y]
Truth table0%0/2
A
A= $bmp->{'Image'}[$x][$y]
Aresult
11
00
753 0 $bmp->{Image}[$x][$y] = $val;
754 }
755  
756 0 ▶sub xy_rgb {
Subroutine xy_rgb: not called CC=3 SCAR=24.8
Pod documented
757 0 my ($bmp,$x,$y, $r,$g,$b) = @_;
758  
759 0 ▶ if (defined($r)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $r) 0 0 0
760 0 my $color = (($r&0xff)<<16)|(($g&0xff)<<8)|(($b&0xff)<<0);
761 0 return $bmp->xy($x,$y,$color);
762 }
763 0 my $color = $bmp->xy($x,$y);
764 0 ▶ return undef unless defined $color;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $color 0 0 0
765 0 return rgb($color);
766 }
767  
768 # Simple ascii viewer
769 11 ▶sub view_ascii {
Subroutine view_ascii: called CC=9 SCAR=24.8
Pod documented
770 11 my ($bmp,$file) = @_;
771  
772 11 my $fh;
773 11 ▶ if (!$file || $file eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $file or $file eq '-') { } 0 11 11
Condition33%1/3
not $file or $file eq "-"
l!l&&r!l&&!r
0011
MC/DC0%0/2
not $file or $file eq "-"
not $file $file eq "-"
Truth table33%1/3
not(A) or B
A= not $fileB= $file eq "-"
ABresult
1X1
011
000
774 0 ▶ open($fh,'>&STDOUT') || fatal("Can't dup STDOUT for view_ascii??");
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">&STDOUT" or fatal("Can't dup STDOUT for view_ascii??") 0 0 0
775 } else {
776 11 ▶ open($fh,'>', $file) || fatal("Couldn't open view_ascii output [$file]");
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">", $file or fatal("Couldn't open view_ascii output [$file]") 11 0 11
777 }
778  
779 11 $bmp->needs_image("use view_ascii method");
780  
781 11 ▶ for(my $y=0; $y<$bmp->{Height}; $y++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y < $bmp->{"Height"}) 1249 11 1260
782 1249 ▶ for(my $x=0; $x<$bmp->{Width}; $x++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < $bmp->{"Width"}) 311374 1249 312623
783 # Go ahead. Just *try* to figure it out.
784 311374 print $fh $bmp->{ascii_array}[int($#{$bmp->{ascii_array}}*darkness($bmp->{Image}[$x][$y]))];
785 }
786 1249 print $fh "\n";
787 }
788  
789 11 ▶ !$file || $file eq '-' || CORE::close($fh);
Branch: 1 branch
BranchTrueFalseTotal
not $file or $file eq "-" or CORE::close $fh 0 11 11
Condition33%1/3
not $file or $file eq "-"
l!l&&r!l&&!r
0011
MC/DC0%0/2
not $file or $file eq "-"
not $file $file eq "-"
Truth table33%1/3
not(A) or B
A= not $fileB= $file eq "-"
ABresult
1X1
011
000
790 }
791  
792 # View it upside-down. More immediate gratification, due to upside-down
793 # nature of bitmaps. Useful for testing, but only works with some images.
794 0 ▶sub flipped_ascii {
Subroutine flipped_ascii: not called CC=1 SCAR=6.9
Pod undocumented
795 0 my ($bmp) = @_;
796 0 my $saved_pixel = $bmp->{add_pixel};
797 0 ▶ $bmp->{add_pixel} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
798 0 my ($bmp,$x,$y,$r,$g,$b) = @_;
799 0 print "\n"x ($bmp->{_lasty} - $y);
800 0 ▶ $bmp->{_lastx}=0 unless $bmp->{_lasty} == $y;
Branch: 1 branch
BranchTrueFalseTotal
unless $bmp->{"_lasty"} == $y 0 0 0
801 0 print " "x ($bmp->{_lastx} - $x - 1);
802 0 print $bmp->{ascii_array}[int($#{$bmp->{ascii_array}}*darkness($r,$g,$b))];
803 0 ($bmp->{_lastx},$bmp->{_lasty}) = ($x,$y);
804 0 };
805 0 $bmp->load;
806 0 $bmp->{add_pixel} = $saved_pixel;
807 }
808  
809 1 1;
810  
811 __END__
↑ Summary