blib/lib/Image/GIF/Encoder/PP.pm

filter: Statementstmt 84.9% 237 / 279 Branchbran 69.8% 81 / 116 Conditioncond 40.0% 6 / 15 MC/DCmcdc 20.0% 2 / 10 Subroutinesub 92.8% 26 / 28 Podpod 26.6% 4 / 15 totaltotal 76.8% 356 / 463 CC 64 SCAR 20.9
CC
64
Cov
79%
add_frame_with_transparency
14.8
scale
13.1
new
12.2
CRAP 8.1
↑ Summary
1 package Image::GIF::Encoder::PP;
2 # Copyright (c) 2021-2022 Gavin Hayes, see LICENSE in the root of the project
3 1 ▶use version 0.77; our $VERSION = qv(v0.1.1);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 516 ▶sub write_num {
Subroutine write_num: called CC=1 SCAR=0.0
Pod undocumented
8 516 my ($fh, $val) = @_;
9 516 print $fh pack('v', $val);
10 }
11  
12 12160 ▶sub new_node {
Subroutine new_node: called CC=1 SCAR=0.0
Pod undocumented
13 12160 my ($key, $degree) = @_;
14 12160 my %node = (
15 'key' => $key,
16 'children' => []
17 );
18  
19 12160 return \%node;
20 }
21  
22 102 ▶sub new_trie {
Subroutine new_trie: called CC=2 SCAR=6.9
Pod undocumented
23 102 my ($degree, $nkeys) = @_;
24 102 my $root = new_node(0, $degree);
25 # Create nodes for single pixels.
26 102 ▶ for($$nkeys = 0; $$nkeys < $degree; $$nkeys += 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$nkeys < $degree) 432 102 534
27 432 $root->{'children'}[$$nkeys] = new_node($$nkeys, $degree);
28 }
29 102 $$nkeys += 2; #skip clear code and stop code
30 102 return $root;
31 }
32  
33 102 ▶sub del_trie {
Subroutine del_trie: called CC=1 SCAR=0.0
Pod undocumented
34 # nothing needed to free in perl
35 }
36  
37 2 ▶sub put_loop {
Subroutine put_loop: called CC=1 SCAR=0.0
Pod undocumented
38 2 my ($gif, $loop) = @_;
39 2 print {$gif->{'fh'}} pack('CCC', 0x21, 0xFF, 0x0B);
40 2 print {$gif->{'fh'}} "NETSCAPE2.0";
41 2 print {$gif->{'fh'}} pack('CC', 0x03, 0x01);
42 2 write_num($gif->{'fh'}, $loop);
43 2 print {$gif->{'fh'}} "\0";
44 }
45  
46 # Add packed key to buffer, updating offset and partial.
47 # $gif->{'offset'} holds position to put next *bit*
48 # $gif->{'partial'} holds bits to include in next byte
49 11932 ▶sub put_key {
Subroutine put_key: called CC=4 SCAR=13.9
Pod undocumented
50 11932 my ($gif, $key, $key_size) = @_;
51  
52 11932 my $byte_offset = int($gif->{'offset'} / 8);
53 11932 my $bit_offset = $gif->{'offset'} % 8;
54 11932 $gif->{'partial'} |= ($key << $bit_offset);
55 11932 my $bits_to_write = $bit_offset + $key_size;
56 11932 ▶ while ($bits_to_write >= 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bits_to_write >= 8) 10253 11932 22185
57 10253 vec($gif->{'buffer'}, $byte_offset++, 8) = $gif->{'partial'} & 0xFF;
58 10253 ▶ if ($byte_offset == 0xFF) {
Branch: 1 branch
BranchTrueFalseTotal
if ($byte_offset == 255) 3 10250 10253
59 3 print {$gif->{'fh'}} "\xFF";
60 3 ▶ length($gif->{'buffer'}) == 0xFF or die("misport");
Branch: 1 branch
BranchTrueFalseTotal
length $gif->{"buffer"} == 255 or die "misport" 3 0 3
61 3 print {$gif->{'fh'}} $gif->{'buffer'};
62 3 $byte_offset = 0;
63 }
64 10253 $gif->{'partial'} >>= 8;
65 10253 $bits_to_write -= 8;
66 }
67
68 11932 $gif->{'offset'} = ($gif->{'offset'} + $key_size) % (0xFF * 8);
69 }
70  
71 102 ▶sub end_key {
Subroutine end_key: called CC=3 SCAR=11.0
Pod undocumented
72 102 my ($gif) = @_;
73 102 my $byte_offset = int($gif->{'offset'} / 8);
74 102 ▶ if ($gif->{'offset'} % 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gif->{"offset"} % 8) 101 1 102
75 101 vec($gif->{'buffer'}, $byte_offset++, 8) = $gif->{'partial'} & 0xFF;
76 }
77 102 ▶ if ($byte_offset) {
Branch: 1 branch
BranchTrueFalseTotal
if ($byte_offset) 102 0 102
78 102 print {$gif->{'fh'}} pack('C', $byte_offset);
79 102 print {$gif->{'fh'}} substr($gif->{'buffer'}, 0, $byte_offset);
80 }
81 102 print {$gif->{'fh'}} "\0";
82 102 $gif->{'offset'} = $gif->{'partial'} = 0;
83 }
84  
85 1 ▶use constant FRAME_CUR => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
86 1 ▶use constant FRAME_LAST => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
87  
88 102 ▶sub put_image {
Subroutine put_image: called CC=7 SCAR=19.6
Pod undocumented
89 102 my ($gif, $frameindex, $w, $h, $x, $y) = @_;
90 102 ▶ my $frameref = ($frameindex == FRAME_CUR) ? \$gif->{'frame'} : \$gif->{'back'};
Branch: 1 branch
BranchTrueFalseTotal
$frameindex == 0 ? : 0 102 102
91 102 my $degree = 1 << $gif->{'depth'};
92  
93 102 print {$gif->{'fh'}} ",";
94 102 write_num($gif->{'fh'}, $x);
95 102 write_num($gif->{'fh'}, $y);
96 102 write_num($gif->{'fh'}, $w);
97 102 write_num($gif->{'fh'}, $h);
98 102 print {$gif->{'fh'}} pack('CC', 0x0, $gif->{'depth'});
99 102 my $nkeys;
100 102 my $node = new_trie($degree, \$nkeys);
101 102 my $root = $node;
102 102 my $key_size = $gif->{'depth'} + 1;
103
104 102 put_key($gif, $degree, $key_size); # clear code
105  
106 102 ▶ for (my $i = $y; $i < $y+$h; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $y + $h) 4272 102 4374
107 4272 ▶ for (my $j = $x; $j < $x+$w; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $x + $w) 232982 4272 237254
108 232982 my $pixel = vec($$frameref, $i*$gif->{'w'}+$j, 8) & ($degree - 1);
109 232982 my $child = $node->{'children'}[$pixel];
110 232982 ▶ if ($child) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child) { } 221356 11626 232982
111 221356 $node = $child;
112 } else {
113 11626 put_key($gif, $node->{'key'}, $key_size);
114 11626 ▶ if ($nkeys < 0x1000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nkeys < 4096) { } 11626 0 11626
115 11626 ▶ if ($nkeys == (1 << $key_size)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nkeys == 1 << $key_size) 263 11363 11626
116 263 $key_size++;
117 }
118 11626 $node->{'children'}[$pixel] = new_node($nkeys++, $degree);
119 } else {
120 0 put_key($gif, $degree, $key_size); # clear code
121 0 del_trie($root, $degree);
122 0 $root = $node = new_trie($degree, \$nkeys);
123 0 $key_size = $gif->{'depth'} + 1;
124 }
125 11626 $node = $root->{'children'}[$pixel];
126 }
127 }
128 }
129 102 put_key($gif, $node->{'key'}, $key_size);
130 102 put_key($gif, $degree + 1, $key_size); # stop code
131 102 end_key($gif);
132 102 del_trie($root, $degree);
133 }
134  
135 100 ▶sub get_bbox {
Subroutine get_bbox: called CC=10 SCAR=23.1
Pod undocumented
136 100 my ($gif, $w, $h, $x, $y) = @_;
137 100 my $left = $gif->{'w'}; my $right = 0;
138 100 my $top = $gif->{'h'}; my $bottom = 0;
139 100 my $k = 0;
140 100 ▶ for (my $i = 0; $i < $gif->{'h'}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $gif->{"h"}) 9964 100 10064
141 9964 ▶ for (my $j = 0; $j < $gif->{'w'}; $j++, $k++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $gif->{"w"}) 994096 9964 1004060
142 994096 ▶ if (vec($gif->{'frame'}, $k, 8) != vec($gif->{'back'}, $k, 8)) {
Branch: 1 branch
BranchTrueFalseTotal
if (vec($gif->{"frame"}, $k, 8) != vec($gif->{"back"}, $k, 8)) 163092 831004 994096
143 163092 ▶ if ($j < $left) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $left) 109 162983 163092
144 109 $left = $j;
145 }
146 163092 ▶ if ($j > $right) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j > $right) 2152 160940 163092
147 2152 $right = $j;
148 }
149 163092 ▶ if ($i < $top) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $top) 100 162992 163092
150 100 $top = $i;
151 }
152 163092 ▶ if ($i > $bottom) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > $bottom) 4107 158985 163092
153 4107 $bottom = $i;
154 }
155 }
156 }
157 }
158 100 ▶ if ($left != $gif->{'w'} && $top != $gif->{'h'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($left != $gif->{'w'} and $top != $gif->{'h'}) { } 100 0 100
Condition33%1/3
$left != $gif->{"w"} and $top != $gif->{"h"}
!ll&&!rl&&r
00100
MC/DC0%0/2
$left != $gif->{"w"} and $top != $gif->{"h"}
$left != $gif->{"w"} $top != $gif->{"h"}
Truth table33%1/3
A and B
A= $left != $gif->{"w"}B= $top != $gif->{"h"}
ABresult
0X0
100
111
159 100 $$x = $left; $$y = $top;
160 100 $$w = $right - $left + 1;
161 100 $$h = $bottom - $top + 1;
162 100 return 1;
163 } else {
164 0 return 0;
165 }
166 }
167  
168 1 ▶use constant DM_UNSPEC => 0 << 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
169 1 ▶use constant DM_DND => 1 << 2; # Do Not Dispose
Subroutine BEGIN: called CC=1 SCAR=0.0
170 1 ▶use constant DM_RTB => 2 << 2; # Restore To Background (clear pixel)
Subroutine BEGIN: called CC=1 SCAR=0.0
171 1 ▶use constant DM_RTP => 3 << 2; # Restore To Previous (not currently used)
Subroutine BEGIN: called CC=1 SCAR=0.0
172  
173 102 ▶sub add_graphics_control_extension {
Subroutine add_graphics_control_extension: called CC=3 SCAR=11.0
Pod undocumented
174 102 my ($gif, $d, $dm) = @_;
175 102 my $out = "!\xF9\x04".pack('C', $dm);
176 102 ▶ if($gif->{'transparent_index'} != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gif->{"transparent_index"} != -1) 102 0 102
177 102 vec($out, 3, 8) |= 0x1; # transparent color flag
178 }
179 102 print {$gif->{'fh'}} $out;
180 102 write_num($gif->{'fh'}, $d);
181 102 vec($out, 0, 8) = 0x0;
182 102 vec($out, 1, 8) = 0x0;
183 102 ▶ if($gif->{'transparent_index'} != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gif->{"transparent_index"} != -1) 102 0 102
184 102 vec($out, 0, 8) = $gif->{'transparent_index'};
185 }
186 102 print {$gif->{'fh'}} substr($out, 0, 2);
187 }
188  
189  
190 # external interface
191 2 ▶sub new {
Subroutine new: called CC=10 SCAR=25.0
Pod documented
192 2 my ($class, $filename, $width, $height, $palette, $depth, $loop, $transparent_index) = @_;
193 2 my $gif = {
194 'w' => $width,
195 'h' => $height,
196 'depth' => 0,
197 'transparent_index' => $transparent_index,
198 'has_unencoded_frame' => 0,
199 'fd' => undef,
200 'offset' => 0,
201 'nframes' => 0,
202 'frame' => '',
203 'back' => '',
204 'partial' => 0,
205 'buffer' => ''
206 };
207 2 vec($gif->{'frame'}, $width*$height-1, 8) = 0;
208 2 vec($gif->{'back'}, $width*$height-1, 8) = 0;
209 2 vec($gif->{'buffer'}, 0xFF-1, 8) = 0;
210 2 ▶ if($filename) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename) { } 2 0 2
211 2 ▶ open($gif->{'fh'}, '>', $filename) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
open $gif->{"fh"}, ">", $filename or return undef 2 0 2
212 }
213 else {
214 0 $gif->{'fh'} = *STDOUT;
215 }
216  
217 2 bless $gif, $class;
218  
219 2 print {$gif->{'fh'}} "GIF89a";
220 2 write_num($gif->{'fh'}, $width);
221 2 write_num($gif->{'fh'}, $height);
222 2 my $store_gct; my $custom_gct;
223 2 ▶ if ($palette) {
Branch: 1 branch
BranchTrueFalseTotal
if ($palette) 2 0 2
224 2 ▶ if ($depth < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth < 0) { } 0 2 2
225 0 $store_gct = 1;
226 }
227 else {
228 2 $custom_gct = 1;
229 }
230 }
231 2 ▶ if ($depth < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth < 0) 0 2 2
232 0 $depth = -$depth;
233 }
234 2 ▶ $gif->{'depth'} = $depth > 1 ? $depth : 2;
Branch: 1 branch
BranchTrueFalseTotal
$depth > 1 ? : 1 1 2
235 2 print {$gif->{'fh'}} pack('CCC', (0xF0 | ($depth-1)), 0x00, 0x00);
236 2 ▶ if ($custom_gct) {
Branch: 1 branch
BranchTrueFalseTotal
if ($custom_gct) { } 2 0 2
237 2 print {$gif->{'fh'}} substr($palette, 0, 3 << $depth);
238 }
239 else {
240 0 warn("unimplemented mode");
241 0 return undef;
242 }
243  
244 2 ▶ if ($loop >= 0 && $loop <= 0xFFFF) {
Branch: 1 branch
BranchTrueFalseTotal
if ($loop >= 0 and $loop <= 65535) 2 0 2
Condition33%1/3
$loop >= 0 and $loop <= 65535
!ll&&!rl&&r
002
MC/DC0%0/2
$loop >= 0 and $loop <= 65535
$loop >= 0 $loop <= 65535
Truth table33%1/3
A and B
A= $loop >= 0B= $loop <= 65535
ABresult
0X0
100
111
245 2 put_loop($gif, $loop);
246 }
247  
248 2 return $gif;
249 }
250  
251 102 ▶sub add_frame_with_transparency {
Subroutine add_frame_with_transparency: called CC=13 SCAR=26.9
Pod undocumented
252 102 my ($gif, $has_new_frame) = @_;
253 102 $gif->{'has_unencoded_frame'} = 0;
254 102 my $dm = DM_DND;
255 102 my $w = $gif->{'unencoded_w'};
256 102 my $h = $gif->{'unencoded_h'};
257 102 my $x = $gif->{'unencoded_x'};
258 102 my $y = $gif->{'unencoded_y'};
259 102 ▶ if($has_new_frame)
Branch: 1 branch
BranchTrueFalseTotal
if ($has_new_frame) 100 2 102
260 {
261 # if the new frame has any new transparent pixels (not already transparent) RTB is required
262 100 ▶ for(my $i = 0; $i < $gif->{'h'}; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $gif->{"h"}) 9964 100 10064
263 {
264 9964 ▶ for(my $j = 0; $j < $gif->{w}; $j++)
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $gif->{"w"}) 994096 9964 1004060
265 {
266 994096 ▶ if((vec($gif->{frame}, ($i*$gif->{w}) + $j, 8) == $gif->{'transparent_index'}) &&
Branch: 1 branch
BranchTrueFalseTotal
if (vec($gif->{"frame"}, $i * $gif->{"w"} + $j, 8) == $gif->{"transparent_index"} and vec($gif->{"back"}, $i * $gif->{"w"} + $j, 8) != $gif->{"transparent_index"}) 650 993446 994096
Condition100%3/3
vec($gif->{"frame"}, $i * $gif->{"w"} + $j, 8) == $gif->{"transparent_index"} and vec($gif->{"back"}, $i * $gif->{"w"} + $j, 8) != $gif->{"transparent_index"}
!ll&&!rl&&r
319941673505650
MC/DC100%2/2
vec($gif->{"frame"}, $i * $gif->{"w"} + $j, 8) == $gif->{"transparent_index"} and vec($gif->{"back"}, $i * $gif->{"w"} + $j, 8) != $gif->{"transparent_index"}
vec($gif->{"frame"}, $i * $gif->{"w"} + $j, 8) == $gif->{"transparent_index"} vec($gif->{"back"}, $i * $gif->{"w"} + $j, 8) != $gif->{"transparent_index"}
Truth table100%3/3
A and B
A= vec($gif->{"frame"}, $i * $gif->{"w"} + $j, 8) == $gif->{"transparent_index"}B= vec($gif->{"back"}, $i * $gif->{"w"} + $j, 8) != $gif->{"transparent_index"}
ABresult
0X0
100
111
267 (vec($gif->{back}, ($i*$gif->{w}) + $j, 8) != $gif->{'transparent_index'})) {
268 650 $dm = DM_RTB;
269 # adjust the BB so the pixel will be cleared on RTB
270 650 ▶ if($i < $y)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $y) 0 650 650
271 {
272 0 my $delta = $y-$i;
273 0 $y = $i;
274 0 $h += $delta;
275 }
276  
277 650 ▶ if($j < $x)
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $x) 0 650 650
278 {
279 0 my $delta = $x-$j;
280 0 $x = $j;
281 0 $w += $delta;
282 }
283  
284 650 ▶ if($i >= ($y+$gif->{h}))
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= $y + $gif->{"h"}) 0 650 650
285 {
286 0 $h += ($i-($y+$gif->{h})+1);
287 }
288  
289 650 ▶ if($j >= ($x+$gif->{w}))
Branch: 1 branch
BranchTrueFalseTotal
if ($j >= $x + $gif->{"w"}) 0 650 650
290 {
291 0 $w += ($j-($x+$gif->{w})+1);
292 }
293 }
294 }
295 }
296  
297 }
298 102 add_graphics_control_extension($gif, $gif->{'unencoded_delay'}, $dm);
299 102 put_image($gif, FRAME_LAST, $w, $h, $x, $y);
300  
301 102 ▶ if($dm == DM_RTB)
Branch: 1 branch
BranchTrueFalseTotal
if ($dm == 8) 51 51 102
302 {
303 # RTB our internal model, used by get_bbox
304 51 ▶ for(my $i = $y; $i < ($y+$h); $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $y + $h) 4080 51 4131
305 {
306 4080 ▶ for(my $j = $x; $j < ($x+$w); $j++)
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $x + $w) 225560 4080 229640
307 {
308 225560 vec($gif->{back}, $i*$gif->{w} + $j, 8) = $gif->{'transparent_index'};
309 }
310 }
311 }
312 }
313  
314 102 ▶sub add_frame {
Subroutine add_frame: called CC=6 SCAR=19.0
Pod documented
315 102 my ($gif, $delay) = @_;
316  
317 # encode an old frame if needed
318 102 ▶ if($gif->{'has_unencoded_frame'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gif->{"has_unencoded_frame"}) 100 2 102
319 100 add_frame_with_transparency($gif, 1);
320 }
321  
322 # determine the changed area since the last frame
323 102 my ($w, $h, $x, $y);
324 102 ▶ if (($gif->{nframes} == 0)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($gif->{'nframes'} == 0) { } 2 100 102
elsif (not get_bbox($gif, \$w, \$h, \$x, \$y)) { } 0 100 100
325 2 $w = $gif->{'w'};
326 2 $h = $gif->{'h'};
327 2 $x = $y = 0;
328 } elsif (!get_bbox($gif, \$w, \$h, \$x, \$y)) {
329 # image's not changed; save one pixel just to add delay
330 0 $w = $h = 1;
331 0 $x = $y = 0;
332 }
333  
334 # encode the frame now if transparency isn't used at all
335 102 ▶ if($gif->{'transparent_index'} == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gif->{'transparent_index'} == -1) { } 0 102 102
336 0 ▶ if($delay) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delay) 0 0 0
337 0 add_graphics_control_extension($gif, $delay, DM_DND);
338 }
339 0 put_image($gif, FRAME_CUR, $w, $h, $x, $y);
340 }
341 else {
342 102 $gif->{'has_unencoded_frame'} = 1;
343 102 $gif->{'unencoded_w'} = $w;
344 102 $gif->{'unencoded_h'} = $h;
345 102 $gif->{'unencoded_x'} = $x;
346 102 $gif->{'unencoded_y'} = $y;
347 102 $gif->{'unencoded_delay'} = $delay;
348 }
349  
350 # move on to the next frame, swap the buffers
351 102 $gif->{'nframes'}++;
352 102 my $tmp = $gif->{'back'};
353 102 $gif->{'back'} = $gif->{'frame'};
354 102 $gif->{'frame'} = $tmp;
355 }
356  
357 2 ▶sub _finish {
Subroutine _finish: called CC=2 SCAR=7.0
358 2 my ($gif) = @_;
359 # encode an old frame if needed
360 2 ▶ if($gif->{'has_unencoded_frame'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gif->{"has_unencoded_frame"}) 2 0 2
361 2 add_frame_with_transparency($gif, 0);
362 }
363 2 print {$gif->{'fh'}} ';';
364 }
365  
366 2 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
367 2 $_[0]->_finish();
368 }
369  
370 # helper functions
371 0 ▶sub expand_frame {
Subroutine expand_frame: not called CC=3 SCAR=24.8
Pod documented
372 0 my ($data, $srcbitsperpixel, $desiredbitsperpixel) = @_;
373 0 ▶ (length($data) % $srcbitsperpixel) == 0 or return undef;
Branch: 1 branch
BranchTrueFalseTotal
length($data) % $srcbitsperpixel == 0 or return undef 0 0 0
374 0 my $count = (length($data) * 8) / $srcbitsperpixel;
375 0 my $dest;
376 0 vec($dest, $count-1, $desiredbitsperpixel) = 0;
377 0 ▶ for(my $i = 0; $i < $count; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $count) 0 0 0
378 0 vec($dest, $i, $desiredbitsperpixel) = vec($data, $i, $srcbitsperpixel);
379 }
380 0 return $dest;
381 }
382  
383 2 ▶sub _scaleUp {
Subroutine _scaleUp: called CC=5 SCAR=16.1
384 2 my ($dest, $data, $w, $h, $times) = @_;
385 2 my $desti = 0;
386 2 ▶ for(my $y = 0; $y < $h; $y++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y < $h) 32 2 34
387 32 my $ystop = $desti + ($w * $times * $times);
388 32 ▶ while($desti < $ystop) {
Branch: 1 branch
BranchTrueFalseTotal
if ($desti < $ystop) 128 32 160
389 128 ▶ for(my $x = 0; $x < $w; $x++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < $w) 2048 128 2176
390 2048 my $stop = $desti + $times;
391 2048 ▶ while($desti < $stop) {
Branch: 1 branch
BranchTrueFalseTotal
if ($desti < $stop) 8192 2048 10240
392 8192 vec($$dest, $desti++, 8) = vec($data, ($y * $w) + $x, 8);
393 }
394 }
395 }
396 }
397  
398 2 return 1;
399 }
400  
401 0 ▶sub _scaleDown {
Subroutine _scaleDown: not called CC=3 SCAR=24.8
402 0 my ($dest, $data, $w, $h, $every) = @_;
403 0 my $desti = 0;
404 0 ▶ for(my $y = 0; $y < $h; $y += $every) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y < $h) 0 0 0
405 0 ▶ for(my $x = 0; $x < $w; $x += $every) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < $w) 0 0 0
406 0 vec($$dest, $desti++, 8) = vec($data, ($y * $w) + $x, 8);
407 }
408 }
409  
410 0 return 1;
411 }
412  
413 2 ▶sub scale {
Subroutine scale: called CC=6 SCAR=25.8
Pod documented
414 2 my ($data, $w, $h, $times, $dest) = @_;
415 2 ▶ ($times == int($times)) && ($times != 0) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$times == int $times and $times != 0 or return undef 2 0 2
Condition33%1/3
$times == int $times and $times != 0
!ll&&!rl&&r
002
MC/DC0%0/2
$times == int $times and $times != 0
$times == int $times $times != 0
Truth table33%1/3
A and B
A= $times == int $timesB= $times != 0
ABresult
0X0
100
111
416 2 my ($neww, $newh);
417 2 ▶ if($times > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($times > 0) { } 2 0 2
418 2 $neww = $w * $times;
419 2 $newh = $h * $times;
420 2 return _scaleUp($dest, $data, $w, $h, $times);
421 }
422 else {
423 0 my $div = -$times;
424 0 $neww = $w / $div;
425 0 $newh = $h / $div;
426 0 ▶ ($neww == int($neww)) && ($newh == int($newh)) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$neww == int $neww and $newh == int $newh or return undef 0 0 0
Condition0%0/3
$neww == int $neww and $newh == int $newh
!ll&&!rl&&r
000
MC/DC0%0/2
$neww == int $neww and $newh == int $newh
$neww == int $neww $newh == int $newh
Truth table0%0/3
A and B
A= $neww == int $newwB= $newh == int $newh
ABresult
0X0
100
111
427 0 return _scaleDown($dest, $data, $w, $h, -$times);
428 }
429 }
430  
431 1 1;
432  
433 __END__
↑ Summary