Image/ExifTool/BPG.pm

filter: Statementstmt 93.7% 60 / 64 Branchbran 59.3% 19 / 32 Conditioncond 47.8% 11 / 23 MC/DCmcdc 14.2% 2 / 14 Subroutinesub 100% 5 / 5 Podpod 0.0% 0 / 2 totaltotal 69.2% 97 / 140 CC 25 SCAR 28.3
CC
25
Cov
76%
ProcessBPG
23.2
Get_ue7
10.5
BEGIN
1.0
CRAP 17.0
« Image/ExifTool/Minolta.pm ↑ Summary Image/ExifTool/PLUS.pm »
1 #------------------------------------------------------------------------------
2 # File: BPG.pm
3 #
4 # Description: Read BPG meta information
5 #
6 # Revisions: 2016-07-05 - P. Harvey Created
7 #
8 # References: 1) http://bellard.org/bpg/
9 #------------------------------------------------------------------------------
10  
11 package Image::ExifTool::BPG;
12  
13 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Image::ExifTool qw(:DataAccess :Utils);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 $VERSION = '1.01';
18  
19 # BPG information
20 1 %Image::ExifTool::BPG::Main = (
21 PROCESS_PROC => \&Image::ExifTool::ProcessBinaryData,
22 GROUPS => { 0 => 'File', 1 => 'File', 2 => 'Image' },
23 NOTES => q{
24 The information listed below is extracted from BPG (Better Portable
25 Graphics) images. See L<http://bellard.org/bpg/> for the specification.
26 },
27 4 => {
28 Name => 'PixelFormat',
29 Format => 'int16u',
30 Mask => 0xe000,
31 PrintConv => {
32 0 => 'Grayscale',
33 1 => '4:2:0 (chroma at 0.5, 0.5)',
34 2 => '4:2:2 (chroma at 0.5, 0)',
35 3 => '4:4:4',
36 4 => '4:2:0 (chroma at 0, 0.5)',
37 5 => '4:2:2 (chroma at 0, 0)',
38 },
39 },
40 4.1 => {
41 Name => 'Alpha',
42 Format => 'int16u',
43 Mask => 0x1004,
44 BitShift => 0,
45 PrintHex => 1,
46 PrintConv => {
47 0x0000 => 'No Alpha Plane',
48 0x1000 => 'Alpha Exists (color not premultiplied)',
49 0x1004 => 'Alpha Exists (color premultiplied)',
50 0x0004 => 'Alpha Exists (W color component)',
51 },
52 },
53 4.2 => {
54 Name => 'BitDepth',
55 Format => 'int16u',
56 Mask => 0x0f00,
57 ValueConv => '$val + 8',
58 },
59 4.3 => {
60 Name => 'ColorSpace',
61 Format => 'int16u',
62 Mask => 0x00f0,
63 PrintConv => {
64 0 => 'YCbCr (BT 601)',
65 1 => 'RGB',
66 2 => 'YCgCo',
67 3 => 'YCbCr (BT 709)',
68 4 => 'YCbCr (BT 2020)',
69 5 => 'BT 2020 Constant Luminance',
70 },
71 },
72 4.4 => {
73 Name => 'Flags',
74 Format => 'int16u',
75 Mask => 0x000b,
76 PrintConv => { BITMASK => {
77 0 => 'Animation',
78 1 => 'Limited Range',
79 3 => 'Extension Present',
80 }},
81 },
82 6 => { Name => 'ImageWidth', Format => 'var_ue7' },
83 7 => { Name => 'ImageHeight', Format => 'var_ue7' },
84 # length of image data or 0 to EOF
85 # (must be decoded so we know where the extension data starts)
86 8 => { Name => 'ImageLength', Format => 'var_ue7' },
87 );
88  
89 1 %Image::ExifTool::BPG::Extensions = (
90 GROUPS => { 0 => 'File', 1 => 'File', 2 => 'Image' },
91 VARS => { ALPHA_FIRST => 1 },
92 1 => {
93 Name => 'EXIF',
94 SubDirectory => {
95 TagTable => 'Image::ExifTool::Exif::Main',
96 ProcessProc => \&Image::ExifTool::ProcessTIFF,
97 },
98 },
99 2 => {
100 Name => 'ICC_Profile',
101 SubDirectory => { TagTable => 'Image::ExifTool::ICC_Profile::Main' },
102 },
103 3 => {
104 Name => 'XMP',
105 SubDirectory => { TagTable => 'Image::ExifTool::XMP::Main' },
106 },
107 4 => {
108 Name => 'ThumbnailBPG',
109 Binary => 1,
110 },
111 5 => {
112 Name => 'AnimationControl',
113 Binary => 1,
114 Unknown => 1,
115 },
116 );
117  
118 #------------------------------------------------------------------------------
119 # Get ue7 integer from binary data (max 32 bits)
120 # Inputs: 0) data ref, 1) location in data (undef for 0)
121 # Returns: 0) ue7 as integer or undef on error, 1) length of ue7 in bytes
122 sub Get_ue7($;$)
123 7 ▶{
Subroutine Get_ue7: called CC=9 SCAR=23.5
Pod undocumented
124 7 my $dataPt = shift;
125 7 ▶ my $pos = shift || 0;
Condition100%2/2
shift() || 0
l!l
61
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
126 7 my $size = length $$dataPt;
127 7 my $val = 0;
128 7 my $i;
129 7 for ($i=0; ; ) {
130 11 ▶ return() if $pos+$i >= $size or $i >= 5;
Branch: 1 branch
BranchTrueFalseTotal
if $pos + $i >= $size or $i >= 5 0 11 11
Condition33%1/3
$pos + $i >= $size or $i >= 5
l!l&&r!l&&!r
0011
MC/DC0%0/2
$pos + $i >= $size or $i >= 5
$pos + $i >= $size $i >= 5
Truth table33%1/3
A or B
A= $pos + $i >= $sizeB= $i >= 5
ABresult
1X1
011
000
131 11 my $byte = Get8u($dataPt, $pos + $i);
132 11 $val = ($val << 7) | ($byte & 0x7f);
133 11 ▶ unless ($byte & 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($byte & 128) 7 4 11
134 7 ▶ return() if $i == 4 and $byte & 0x70; # error if bits 32-34 are set
Branch: 1 branch
BranchTrueFalseTotal
if $i == 4 and $byte & 112 0 7 7
Condition33%1/3
$i == 4 and $byte & 112
!ll&&!rl&&r
700
MC/DC0%0/2
$i == 4 and $byte & 112
$i == 4 $byte & 112
Truth table33%1/3
A and B
A= $i == 4B= $byte & 112
ABresult
0X0
100
111
135 7 last; # this was the last byte
136 }
137 4 ▶ return() if $i == 0 and $byte == 0x80; # error if first byte is 0x80
Branch: 1 branch
BranchTrueFalseTotal
if $i == 0 and $byte == 128 0 4 4
Condition33%1/3
$i == 0 and $byte == 128
!ll&&!rl&&r
040
MC/DC0%0/2
$i == 0 and $byte == 128
$i == 0 $byte == 128
Truth table33%1/3
A and B
A= $i == 0B= $byte == 128
ABresult
0X0
100
111
138 4 ++$i; # step to the next byte
139 }
140 7 return($val, $i+1);
141 }
142  
143 #------------------------------------------------------------------------------
144 # Extract EXIF information from a BPG image
145 # Inputs: 0) ExifTool object reference, 1) dirInfo reference
146 # Returns: 1 on success, 0 if this wasn't a valid BPG file
147 sub ProcessBPG($$)
148 1 ▶{
Subroutine ProcessBPG: called CC=17 SCAR=31.4
Pod undocumented
149 1 local $_;
150 1 my ($et, $dirInfo) = @_;
151 1 my $raf = $$dirInfo{RAF};
152 1 my ($buff, $size, $n, $len, $pos);
153  
154 # verify this is a valid BPG file
155 1 ▶ return 0 unless $raf->Read($buff, 21) == 21; # (21 bytes is maximum header length)
Branch: 1 branch
BranchTrueFalseTotal
unless $raf->Read($buff, 21) == 21 0 1 1
156 1 ▶ return 0 unless $buff =~ /^BPG\xfb/;
Branch: 1 branch
BranchTrueFalseTotal
unless $buff =~ /^BPG\xfb/ 0 1 1
157 1 $et->SetFileType(); # set the FileType tag
158  
159 1 SetByteOrder('MM');
160 1 my %dirInfo = (
161 DataPt => \$buff,
162 DirStart => 0,
163 DirLen => length($buff),
164 VarFormatData => [ ],
165 );
166 1 $et->ProcessDirectory(\%dirInfo, GetTagTable('Image::ExifTool::BPG::Main'));
167  
168 1 ▶ return 1 unless $$et{VALUE}{Flags} & 0x0008; # all done unless extension flag is set
Branch: 1 branch
BranchTrueFalseTotal
unless $et->{"VALUE"}{"Flags"} & 8 0 1 1
169  
170 # add varSize from last entry in VarFormatData to determine
171 # the current read position in the file
172 1 my $dataPos = 9 + $dirInfo{VarFormatData}[-1][1];
173 # read extension length
174 1 ▶ unless ($raf->Seek($dataPos, 0) and $raf->Read($buff, 5) == 5) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($raf->Seek($dataPos, 0) and $raf->Read($buff, 5) == 5) 0 1 1
Condition33%1/3
$raf->Seek($dataPos, 0) and $raf->Read($buff, 5) == 5
!ll&&!rl&&r
001
MC/DC0%0/2
$raf->Seek($dataPos, 0) and $raf->Read($buff, 5) == 5
$raf->Seek($dataPos, 0) $raf->Read($buff, 5) == 5
Truth table33%1/3
A and B
A= $raf->Seek($dataPos, 0)B= $raf->Read($buff, 5) == 5
ABresult
0X0
100
111
175 0 $et->Warn('Missing BPG extension data');
176 0 return 1;
177 }
178 1 ($size, $n) = Get_ue7(\$buff);
179 1 ▶ defined $size or $et->Warn('Corrupted BPG extension length'), return 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $size or $et->Warn("Corrupted BPG extension length"), (return 1) 1 0 1
180 1 $dataPos += $n;
181 1 ▶ $size > 10000000 and $et->Warn('BPG extension is too large'), return 1;
Branch: 1 branch
BranchTrueFalseTotal
$size > 10000000 and $et->Warn("BPG extension is too large"), (return 1) 0 1 1
182 1 ▶ unless ($raf->Seek($dataPos, 0) and $raf->Read($buff, $size) == $size) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($raf->Seek($dataPos, 0) and $raf->Read($buff, $size) == $size) 0 1 1
Condition33%1/3
$raf->Seek($dataPos, 0) and $raf->Read($buff, $size) == $size
!ll&&!rl&&r
001
MC/DC0%0/2
$raf->Seek($dataPos, 0) and $raf->Read($buff, $size) == $size
$raf->Seek($dataPos, 0) $raf->Read($buff, $size) == $size
Truth table33%1/3
A and B
A= $raf->Seek($dataPos, 0)B= $raf->Read($buff, $size) == $size
ABresult
0X0
100
111
183 0 $et->Warn('Truncated BPG extension');
184 0 return 1;
185 }
186 1 my $tagTablePtr = GetTagTable('Image::ExifTool::BPG::Extensions');
187 # loop through the individual extensions
188 1 ▶ for ($pos=0; $pos<$size; $pos+=$len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < $size) 3 1 4
189 3 my $type = Get8u(\$buff, $pos);
190 # get length of this extension
191 3 ($len, $n) = Get_ue7(\$buff, ++$pos);
192 3 ▶ defined $len or $et->Warn('Corrupted BPG extension'), last;
Branch: 1 branch
BranchTrueFalseTotal
defined $len or $et->Warn("Corrupted BPG extension"), last 3 0 3
193 3 $pos += $n; # point to start of data for this extension
194 3 ▶ $pos + $len > $size and $et->Warn('Invalid BPG extension size'), last;
Branch: 1 branch
BranchTrueFalseTotal
$pos + $len > $size and $et->Warn("Invalid BPG extension size"), last 0 3 3
195 3 ▶ $$tagTablePtr{$type} or $et->Warn("Unrecognized BPG extension $type ($len bytes)", 1), next;
Branch: 1 branch
BranchTrueFalseTotal
$tagTablePtr->{$type} or $et->Warn("Unrecognized BPG extension $type ($len bytes)", 1), next 3 0 3
196 # libbpg (in my opinion) incorrectly copies the padding byte after the
197 # "EXIF\0" APP1 header to the start of the BPG EXIF extension, so issue a
198 # minor warning and ignore the padding if we find it before the TIFF header
199 3 ▶ if ($type == 1 and $len > 3 and substr($buff,$pos,3)=~/^.(II|MM)/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 1 and $len > 3 and substr($buff, $pos, 3) =~ /^.(II|MM)/s) 1 2 3
Condition66%4/6
$type == 1 and $len > 3 and substr($buff, $pos, 3) =~ /^.(II|MM)/s
!ll&&!rl&&r
201
$type == 1 and $len > 3
!ll&&!rl&&r
201
MC/DC33%1/3
$type == 1 and $len > 3 and substr($buff, $pos, 3) =~ /^.(II|MM)/s
$type == 1 $len > 3 substr($buff, $pos, 3) =~ /^.(II|MM)/s
Truth table50%2/4
A and B and C
A= $type == 1B= $len > 3C= substr($buff, $pos, 3) =~ /^.(II|MM)/s
ABCresult
0XX0
10X0
1100
1111
200 1 $et->Warn("Ignored extra byte at start of EXIF extension", 1);
201 1 ++$pos;
202 1 --$len;
203 }
204 3 $et->HandleTag($tagTablePtr, $type, undef,
205 DataPt => \$buff,
206 DataPos => $dataPos,
207 Start => $pos,
208 Size => $len,
209 Parent => 'BPG',
210 );
211 }
212 1 return 1;
213 }
214  
215 1 1; # end
216  
217 __END__
« Image/ExifTool/Minolta.pm ↑ Summary Image/ExifTool/PLUS.pm »