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