Compression/LZW.pm

filter: Statementstmt 96.2% 180 / 187 Branchbran 73.5% 50 / 68 Conditioncond 71.4% 5 / 7 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 19 / 19 Podpod 0.0% 0 / 2 totaltotal 89.2% 256 / 287 CC 38 SCAR 16.8
CC
38
Cov
90%
get_next_code
10.4
write_code
8.0
decode
6.0
CRAP 5.4
« Field.pm ↑ Summary DataType.pm »
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package MAS::TIFF::Compression::LZW::State;
5  
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 6 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
12 6 my $class = shift;
13 6 my $bytes = shift;
14
15 6 ▶ return bless {
Condition100%2/2
$bytes // ""
l!l
33
MC/DC100%1/1
$bytes // ""
$bytes
Truth table100%2/2
A
A= $bytes
Aresult
11
00
16 BYTES => $bytes // '',
17 BYTE_INDEX => 0,
18 BIT_INDEX => 0,
19 BYTE => undef,
20 DICT => { },
21 CODES => [ ],
22 }, $class;
23 }
24  
25 3 ▶sub bytes { return shift->{BYTES} }
Subroutine bytes: called CC=1 SCAR=0.0
26  
27 3 ▶sub initialize_table {
Subroutine initialize_table: called CC=1 SCAR=0.0
28 3 my $self = shift;
29
30 # print " Initializing table...\n";
31
32 3 $self->{DICT} = { };
33 3 $self->{CODES} = [ ];
34 }
35  
36 1007 ▶sub string_from_code {
Subroutine string_from_code: called CC=5 SCAR=16.4
37 1007 my $self = shift;
38 1007 my $code = shift;
39
40 1007 ▶ return chr($code) if ($code < 256);
Branch: 1 branch
BranchTrueFalseTotal
if $code < 256 604 403 1007
41
42 403 ▶ confess "Attempt to get string for CLEAR code (256)!" if $code == 256;
Branch: 1 branch
BranchTrueFalseTotal
if $code == 256 0 403 403
43 403 ▶ confess "Attempt to get string for EOI code (257)!" if $code == 257;
Branch: 1 branch
BranchTrueFalseTotal
if $code == 257 0 403 403
44
45 403 my $string = $self->{CODES}[$code - 258];
46
47 403 ▶ confess "Attempt to get string for undefined code $code!" unless defined $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 0 403 403
48
49 # printf " => %s\n", $code, unpack('H*', $string);
50
51 403 return $string;
52 }
53  
54 553 ▶sub code_from_string {
Subroutine code_from_string: called CC=5 SCAR=16.4
55 553 my $self = shift;
56 553 my $string = shift;
57
58 553 ▶ confess "Attempt to get code for undefined string!" unless defined $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 0 553 553
59 553 ▶ confess "Attempt to get code for empty string!" if $string eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $string eq "" 0 553 553
60  
61 553 ▶ return ord($string) if length($string) == 1;
Branch: 1 branch
BranchTrueFalseTotal
if length $string == 1 303 250 553
62
63 250 my $code = $self->{DICT}{$string};
64
65 250 ▶ confess "Attempt to get code for string '" . unpack('H*', $string) . "' failed!" unless defined $code;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code 0 250 250
66
67 250 return $code;
68 }
69  
70 550 ▶sub is_code_in_table {
Subroutine is_code_in_table: called CC=1 SCAR=0.0
71 550 my $self = shift;
72 550 my $code = shift;
73
74 550 return (scalar(@{$self->{CODES}}) + 258) > $code;
75 }
76  
77 9459 ▶sub is_string_in_table {
Subroutine is_string_in_table: called CC=2 SCAR=6.9
78 9459 my $self = shift;
79 9459 my $value = shift;
80
81 9459 ▶ return 1 if length($value) == 1;
Branch: 1 branch
BranchTrueFalseTotal
if length $value == 1 3 9456 9459
82
83 9456 return exists $self->{DICT}{$value};
84 }
85  
86 1100 ▶sub add_string_to_table {
Subroutine add_string_to_table: called CC=3 SCAR=11.1
87 1100 my $self = shift;
88 1100 my $string = shift;
89  
90 1100 ▶ if (length($string) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $string == 1) { } 0 1100 1100
91 # Why bother adding length-one strings to the table beyond entries 0..255?
92 # return
93 }
94 else {
95 1100 ▶ confess "Attempt to add '" . unpack('H*', $string) . "' to table, but it is already there!" if $self->is_string_in_table($string);
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_string_in_table($string) 0 1100 1100
96 }
97
98 1100 my $code = scalar(@{$self->{CODES}}) + 258;
99
100 # printf " Code %d => %s\n", $code, unpack('H*', $string);
101
102 1100 $self->{CODES}[$code - 258] = $string;
103 1100 $self->{DICT}{$string} = $code;
104 }
105  
106 1 my @write_masks = (0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff);
107  
108 559 ▶sub write_code {
Subroutine write_code: called CC=8 SCAR=20.8
109 559 my $self = shift;
110 559 my $code = (shift) + 0;
111
112 559 my $code_count = 258 + scalar(@{$self->{CODES}});
113
114 559 my $size;
115
116 559 ▶ if ($code_count >= 2048) {
Branch: 3 branches
BranchTrueFalseTotal
if ($code_count >= 2048) { } 0 559 559
elsif ($code_count >= 1024) { } 0 559 559
elsif ($code_count >= 512) { } 40 519 559
117 0 $size = 12;
118 }
119 elsif ($code_count >= 1024) {
120 0 $size = 11;
121 }
122 elsif ($code_count >= 512) {
123 40 $size = 10;
124 }
125 else {
126 519 $size = 9;
127 }
128
129 # printf "Writing %d bit code %s\n", $size, $code;
130  
131 559 ▶ my $byte = $self->{BYTE} // 0;
Condition100%2/2
$self->{"BYTE"} // 0
l!l
49366
MC/DC100%1/1
$self->{"BYTE"} // 0
$self->{"BYTE"}
Truth table100%2/2
A
A= $self->{"BYTE"}
Aresult
11
00
132 559 my $bit_index = $self->{BIT_INDEX};
133  
134 559 my $remaining = $size;
135
136 559 ▶ while ($remaining > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remaining > 0) 1129 559 1688
137 1129 my $available = 8 - $bit_index;
138
139 1129 my $writing;
140 my $extra;
141 1129 ▶ if ($remaining <= $available) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remaining <= $available) { } 559 570 1129
142 559 $writing = $remaining;
143 559 $extra = $available - $writing;
144 }
145 else {
146 570 $writing = $available;
147 570 $extra = 0;
148 }
149
150 1129 my $bits = ($write_masks[$writing] & ($code >> ($remaining - $writing))) << $extra;
151
152 1129 $byte |= $bits;
153 1129 $bit_index += $writing;
154
155 1129 ▶ if ($bit_index == 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bit_index == 8) { } 633 496 1129
156 633 $self->{BYTES} .= chr($byte);
157 633 $self->{BYTE} = undef;
158 633 $self->{BIT_INDEX} = 0;
159 633 $byte = 0;
160 633 $bit_index = 0;
161 }
162 else {
163 496 $self->{BYTE} = $byte;
164 496 $self->{BIT_INDEX} = $bit_index;
165 }
166
167 1129 $remaining -= $writing;
168 }
169 }
170  
171 3 ▶sub finish_write {
Subroutine finish_write: called CC=2 SCAR=7.0
172 3 my $self = shift;
173
174 3 ▶ if (defined $self->{BYTE}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"BYTE"}) 3 0 3
175 3 $self->{BYTES} .= chr($self->{BYTE});
176 3 $self->{BIT_INDEX} = 0;
177 3 $self->{BYTE} = undef;
178 }
179 }
180  
181 1 my @read_masks = (0xff, 0x7f, 0x3f, 0x1f, 0x0f, 0x07, 0x03, 0x01);
182  
183 559 ▶sub get_next_code {
Subroutine get_next_code: called CC=10 SCAR=23.4
184 559 my $self = shift;
185
186 559 my $code_count = 258 + scalar(@{$self->{CODES}});
187
188 559 my $size;
189
190 559 ▶ if ($code_count >= 2047) { # Really? Not 2048?
Branch: 3 branches
BranchTrueFalseTotal
if ($code_count >= 2047) { } 0 559 559
elsif ($code_count >= 1023) { } 0 559 559
elsif ($code_count >= 511) { } 40 519 559
191 0 $size = 12;
192 }
193 elsif ($code_count >= 1023) { # Really? Not 1024?
194 0 $size = 11;
195 }
196 elsif ($code_count >= 511) { # Really? Not 512?
197 40 $size = 10;
198 }
199 else {
200 519 $size = 9;
201 }
202
203 559 my $bytes = $self->{BYTES};
204 559 my $bit_index = $self->{BIT_INDEX};
205 559 my $byte_index = $self->{BYTE_INDEX};
206  
207 559 my $input_length = length($bytes);
208 559 my $bytes_remaining = $input_length - $byte_index - 1;
209 559 my $bits_remaining = (8 - $bit_index) + (8 * $bytes_remaining);
210
211 559 ▶ return undef if ($size > $bits_remaining);
Branch: 1 branch
BranchTrueFalseTotal
if $size > $bits_remaining 0 559 559
212
213 559 my $result = 0;
214 559 my $remaining = $size;
215
216 559 ▶ while ($remaining > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remaining > 0) 1129 559 1688
217 1129 my $available = 8 - $bit_index;
218
219 1129 my $reading;
220 my $extra;
221 1129 ▶ if ($remaining <= $available) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remaining <= $available) { } 559 570 1129
222 559 $reading = $remaining;
223 559 $extra = $available - $remaining;
224 }
225 else {
226 570 $reading = $available;
227 570 $extra = 0;
228 }
229
230 # my $byte = unpack('C', substr($bytes, $byte_index, 1));
231 1129 my $byte = ord(substr($bytes, $byte_index, 1));
232 1129 my $mask = $read_masks[$bit_index];
233
234 1129 my $new_bits = ($mask & $byte) >> $extra;
235 1129 $result = ($result << $reading) | $new_bits;
236 1129 $remaining -= $reading;
237 1129 $bit_index += $reading;
238 1129 ▶ if ($bit_index == 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bit_index == 8) 633 496 1129
239 633 $bit_index = 0;
240 633 $byte_index++;
241 633 ▶ if (($byte_index == $input_length) && ($remaining > 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($byte_index == $input_length and $remaining > 0) 0 633 633
Condition33%1/3
$byte_index == $input_length and $remaining > 0
!ll&&!rl&&r
63300
MC/DC0%0/2
$byte_index == $input_length and $remaining > 0
$byte_index == $input_length $remaining > 0
Truth table33%1/3
A and B
A= $byte_index == $input_lengthB= $remaining > 0
ABresult
0X0
100
111
242 0 die "Input of $input_length bytes exhausted before finished reading $size bits. There are $remaining left to read!";
243 }
244 }
245 }
246
247 559 $self->{BIT_INDEX} = $bit_index;
248 559 $self->{BYTE_INDEX} = $byte_index;
249  
250 # if ($result == 256) {
251 # printf "There are %d codes. Read a %d bit code: CLEAR (256)\n", $code_count, $size;
252 # }
253 # elsif ($result == 257) {
254 # printf "There are %d codes. Read a %d bit code: EOI (257)\n", $code_count, $size;
255 # }
256 # elsif ($result < 256) {
257 # printf "There are %d codes. Read a %d bit code: 0x%02x\n", $code_count, $size, $result;
258 # }
259 # else {
260 # printf "There are %d codes. Read a %d bit code: %d\n", $code_count, $size, $result;
261 # }
262
263 559 return $result;
264 }
265  
266 package MAS::TIFF::Compression::LZW;
267  
268 1 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
269 1 CLEAR => 256,
270 EOI => 257,
271 1 };
272  
273 3 ▶sub encode {
Subroutine encode: called CC=4 SCAR=13.9
Pod undocumented
274 3 my $bytes = shift;
275 3 my $output = '';
276
277 3 my $state = MAS::TIFF::Compression::LZW::State->new();
278  
279 3 $state->write_code(CLEAR);
280 3 my $omega = '';
281 3 my $l = length($bytes);
282 3 ▶ for (my $i = 0; $i < $l; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $l) 8359 3 8362
283 8359 my $k = substr($bytes, $i, 1);
284 8359 my $key = $omega . $k;
285 8359 ▶ if ($state->is_string_in_table($key)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state->is_string_in_table($key)) { } 7809 550 8359
286 7809 $omega = $key;
287 }
288 else {
289 550 my $code = $state->code_from_string($omega);
290 550 $state->write_code($code);
291 550 my $new_code = $state->add_string_to_table($key);
292 550 $omega = $k;
293
294 550 ▶ if ($new_code == 4096) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_code == 4096) 0 550 550
295 0 $state->write_code(CLEAR);
296 0 $state->initialize_table;
297 }
298 }
299 }
300 3 my $code = $state->code_from_string($omega);
301 3 $state->write_code($code);
302 3 $state->write_code(EOI);
303
304 3 $state->finish_write;
305
306 3 return $state->bytes;
307 }
308  
309 3 ▶sub decode {
Subroutine decode: called CC=6 SCAR=17.9
Pod undocumented
310 3 my $bytes = shift;
311 3 my $output = '';
312
313 3 my $state = MAS::TIFF::Compression::LZW::State->new($bytes);
314  
315 3 my $code;
316 my $old_code;
317
318 3 while (1) {
319 556 $code = $state->get_next_code;
320 556 ▶ die "Unexpected end of input while reading!" unless defined $code;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code 0 556 556
321 556 ▶ last if $code == EOI;
Branch: 1 branch
BranchTrueFalseTotal
if $code == 257 3 553 556
322
323 553 ▶ if ($code == CLEAR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code == 256) { } 3 550 553
324 3 $state->initialize_table;
325 3 $code = $state->get_next_code;
326 3 ▶ last if ($code == EOI);
Branch: 1 branch
BranchTrueFalseTotal
if $code == 257 0 3 3
327 3 $output .= $state->string_from_code($code);
328 3 $old_code = $code;
329 }
330 else {
331 550 ▶ if ($state->is_code_in_table($code)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state->is_code_in_table($code)) { } 454 96 550
332 454 my $string = $state->string_from_code($code);
333 454 $output .= $string;
334 454 my $new_string = $state->string_from_code($old_code) . substr($string, 0, 1);
335 454 $state->add_string_to_table($new_string);
336 454 $old_code = $code;
337 }
338 else {
339 96 my $old_string = $state->string_from_code($old_code);
340 96 my $out_string = $old_string . substr($old_string, 0, 1); # Why append first char of string from old_code?
341 96 $output .= $out_string;
342 96 $state->add_string_to_table($out_string);
343 96 $old_code = $code;
344 }
345 }
346 }
347
348 # print "Got EOI.\n";
349
350 3 return $output;
351 }
352  
353  
354 1 1;
« Field.pm ↑ Summary DataType.pm »