Image/ExifTool/WPG.pm

filter: Statementstmt 70.4% 62 / 88 Branchbran 45.1% 28 / 62 Conditioncond 39.2% 11 / 28 MC/DCmcdc 18.7% 3 / 16 Subroutinesub 100% 6 / 6 Podpod 0.0% 0 / 3 totaltotal 54.1% 110 / 203 CC 42 SCAR 47.2
CC
42
Cov
57%
ProcessWPG
148.0
ReadVarInt
14.5
PrintRecord
3.2
CRAP 112.3
« Image/ExifTool/Microsoft.pm ↑ Summary Image/ExifTool/PPM.pm »
1 #------------------------------------------------------------------------------
2 # File: WPG.pm
3 #
4 # Description: Read WordPerfect Graphics meta information
5 #
6 # Revisions: 2023-05-01 - P. Harvey Created
7 #
8 # References: 1) https://www.fileformat.info/format/wpg/egff.htm
9 # 2) https://archive.org/details/mac_Graphics_File_Formats_Second_Edition_1996/page/n991/mode/2up
10 # 3) http://libwpg.sourceforge.net/
11 #------------------------------------------------------------------------------
12  
13 package Image::ExifTool::WPG;
14  
15 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Image::ExifTool qw(:DataAccess :Utils);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 $VERSION = '1.01';
20  
21 sub PrintRecord($$$);
22  
23 # WPG metadata
24 1 %Image::ExifTool::WPG::Main = (
25 GROUPS => { 0 => 'File', 1 => 'File', 2 => 'Image' },
26 VARS => { ID_FMT => 'none' },
27 NOTES => 'Tags extracted from WordPerfect Graphics (WPG) images.',
28 WPGVersion => { },
29 ImageWidthInches => { PrintConv => 'sprintf("%.2f",$val)' },
30 ImageHeightInches => { PrintConv => 'sprintf("%.2f",$val)' },
31 Records => {
32 Notes => 'records for version 1.0 files',
33 List => 1,
34 PrintHex => 2,
35 PrintConvColumns => 2,
36 PrintConv => {
37 OTHER => \&PrintRecord,
38 0x01 => 'Fill Attributes',
39 0x02 => 'Line Attributes',
40 0x03 => 'Marker Attributes',
41 0x04 => 'Polymarker',
42 0x05 => 'Line',
43 0x06 => 'Polyline',
44 0x07 => 'Rectangle',
45 0x08 => 'Polygon',
46 0x09 => 'Ellipse',
47 0x0a => 'Reserved',
48 0x0b => 'Bitmap (Type 1)',
49 0x0c => 'Graphics Text (Type 1)',
50 0x0d => 'Graphics Text Attributes',
51 0x0e => 'Color Map',
52 0x0f => 'Start WPG (Type 1)',
53 0x10 => 'End WPG',
54 0x11 => 'PostScript Data (Type 1)',
55 0x12 => 'Output Attributes',
56 0x13 => 'Curved Polyline',
57 0x14 => 'Bitmap (Type 2)',
58 0x15 => 'Start Figure',
59 0x16 => 'Start Chart',
60 0x17 => 'PlanPerfect Data',
61 0x18 => 'Graphics Text (Type 2)',
62 0x19 => 'Start WPG (Type 2)',
63 0x1a => 'Graphics Text (Type 3)',
64 0x1b => 'PostScript Data (Type 2)',
65 },
66 },
67 RecordsV2 => {
68 Notes => 'records for version 2.0 files',
69 List => 1,
70 PrintHex => 2,
71 PrintConvColumns => 2,
72 PrintConv => {
73 OTHER => \&PrintRecord,
74 0x00 => 'End Marker',
75 0x01 => 'Start WPG',
76 0x02 => 'End WPG',
77 0x03 => 'Form Settings',
78 0x04 => 'Ruler Settings',
79 0x05 => 'Grid Settings',
80 0x06 => 'Layer',
81 0x08 => 'Pen Style Definition',
82 0x09 => 'Pattern Definition',
83 0x0a => 'Comment',
84 0x0b => 'Color Transfer',
85 0x0c => 'Color Palette',
86 0x0d => 'DP Color Palette',
87 0x0e => 'Bitmap Data',
88 0x0f => 'Text Data',
89 0x10 => 'Chart Style',
90 0x11 => 'Chart Data',
91 0x12 => 'Object Image',
92 0x15 => 'Polyline',
93 0x16 => 'Polyspline',
94 0x17 => 'Polycurve',
95 0x18 => 'Rectangle',
96 0x19 => 'Arc',
97 0x1a => 'Compound Polygon',
98 0x1b => 'Bitmap',
99 0x1c => 'Text Line',
100 0x1d => 'Text Block',
101 0x1e => 'Text Path',
102 0x1f => 'Chart',
103 0x20 => 'Group',
104 0x21 => 'Object Capsule',
105 0x22 => 'Font Settings',
106 0x25 => 'Pen Fore Color',
107 0x26 => 'DP Pen Fore Color',
108 0x27 => 'Pen Back Color',
109 0x28 => 'DP Pen Back Color',
110 0x29 => 'Pen Style',
111 0x2a => 'Pen Pattern',
112 0x2b => 'Pen Size',
113 0x2c => 'DP Pen Size',
114 0x2d => 'Line Cap',
115 0x2e => 'Line Join',
116 0x2f => 'Brush Gradient',
117 0x30 => 'DP Brush Gradient',
118 0x31 => 'Brush Fore Color',
119 0x32 => 'DP Brush Fore Color',
120 0x33 => 'Brush Back Color',
121 0x34 => 'DP Brush Back Color',
122 0x35 => 'Brush Pattern',
123 0x36 => 'Horizontal Line',
124 0x37 => 'Vertical Line',
125 0x38 => 'Poster Settings',
126 0x39 => 'Image State',
127 0x3a => 'Envelope Definition',
128 0x3b => 'Envelope',
129 0x3c => 'Texture Definition',
130 0x3d => 'Brush Texture',
131 0x3e => 'Texture Alignment',
132 0x3f => 'Pen Texture ',
133 }
134 },
135 );
136  
137 #------------------------------------------------------------------------------
138 # Print record type
139 # Inputs: 0) record type and count, 1) inverse flag, 2) PrintConv hash ref
140 # Returns: converted record name
141 sub PrintRecord($$$)
142 1 ▶{
Subroutine PrintRecord: called CC=3 SCAR=11.8
Pod undocumented
143 1 my ($val, $inv, $printConv) = @_;
144 1 my ($type, $count) = split 'x', $val;
145 1 ▶ my $prt = $$printConv{$type} || sprintf('Unknown (0x%.2x)', $type);
Condition33%1/3
$printConv->{$type} || sprintf('Unknown (0x%.2x)', $type)
l!l&&r!l&&!r
100
MC/DC0%0/2
$printConv->{$type} || sprintf('Unknown (0x%.2x)', $type)
$printConv->{$type} sprintf('Unknown (0x%.2x)', $type)
Truth table33%1/3
A || B
A= $printConv->{$type}B= sprintf('Unknown (0x%.2x)', $type)
ABresult
1X1
011
000
146 1 ▶ $prt .= " x $count" if $count;
Branch: 1 branch
BranchTrueFalseTotal
if $count 1 0 1
147 1 return $prt;
148 }
149  
150 #------------------------------------------------------------------------------
151 # Read variable-length integer
152 # Inputs: 0) RAF ref
153 # Returns: integer value
154 sub ReadVarInt($)
155 12 ▶{
Subroutine ReadVarInt: called CC=6 SCAR=26.8
Pod undocumented
156 12 my $raf = shift;
157 12 my $buff;
158 12 ▶ $raf->Read($buff, 1) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$raf->Read($buff, 1) or return 0 12 0 12
159 12 my $val = ord($buff);
160 12 ▶ if ($val == 0xff) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val == 255) 0 12 12
161 0 ▶ $raf->Read($buff, 2) == 2 or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$raf->Read($buff, 2) == 2 or return 0 0 0 0
162 0 $val = unpack('v', $buff);
163 0 ▶ if ($val & 0x8000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val & 32768) 0 0 0
164 0 ▶ $raf->Read($buff, 2) == 2 or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$raf->Read($buff, 2) == 2 or return 0 0 0 0
165 0 $val = (($val & 0x7fff) << 16) | unpack('v', $buff);
166 }
167 }
168 12 return $val;
169 }
170  
171 #------------------------------------------------------------------------------
172 # Read WPG version 1 or 2 image
173 # Inputs: 0) ExifTool object reference, 1) dirInfo reference
174 # Returns: 1 on success, 0 if this wasn't a valid WPG file
175 sub ProcessWPG($$)
176 1 ▶{
Subroutine ProcessWPG: called CC=35 SCAR=50.0
Pod undocumented
177 1 my ($et, $dirInfo) = @_;
178 1 my $raf = $$dirInfo{RAF};
179 1 my ($buff, $lastType, $count);
180  
181 # verify this is a valid WPG file
182 1 ▶ return 0 unless $raf->Read($buff, 16) == 16;
Branch: 1 branch
BranchTrueFalseTotal
unless $raf->Read($buff, 16) == 16 0 1 1
183 1 ▶ return 0 unless $buff =~ /^\xff\x57\x50\x43/;
Branch: 1 branch
BranchTrueFalseTotal
unless $buff =~ /^\xff\x57\x50\x43/ 0 1 1
184 1 $et->SetFileType();
185 1 SetByteOrder('II');
186 1 my $tagTablePtr = GetTagTable('Image::ExifTool::WPG::Main');
187 1 my $offset = Get32u(\$buff, 4);
188 1 my ($ver, $rev) = unpack('x10CC', $buff);
189 1 $et->HandleTag($tagTablePtr, WPGVersion => "$ver.$rev");
190 1 ▶ if ($ver < 1 or $ver > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ver < 1 or $ver > 2) 0 1 1
Condition33%1/3
$ver < 1 or $ver > 2
l!l&&r!l&&!r
001
MC/DC0%0/2
$ver < 1 or $ver > 2
$ver < 1 $ver > 2
Truth table33%1/3
A or B
A= $ver < 1B= $ver > 2
ABresult
1X1
011
000
191 # currently support only version 1 and 2 formats
192 0 $et->Warn('Unsupported WPG version');
193 0 return 1;
194 }
195 1 ▶ my $tag = $ver == 1 ? 'Records' : 'RecordsV2';
Branch: 1 branch
BranchTrueFalseTotal
$ver == 1 ? : 1 0 1
196 1 ▶ $raf->Seek($offset - 16, 1) or return 1 if $offset > 16;
Branch: 1 branch
BranchTrueFalseTotal
if $offset > 16 0 1 1
Condition25%1/4
$offset > 16 && ($raf->Seek($offset - 16, 1) || (return 1))
!ll
10
$raf->Seek($offset - 16, 1) or return 1
l!l
00
MC/DC0%0/2
$offset > 16 && ($raf->Seek($offset - 16, 1) || (return 1))
$offset > 16 $raf->Seek($offset - 16, 1)
Truth table33%1/3
A && B
A= $offset > 16B= $raf->Seek($offset - 16, 1)
ABresult
0X0
100
111
197 # loop through records
198 1 for (;;) {
199 13 my ($type, $len, $getSize);
200 13 ▶ if ($raf->Read($buff, $ver) == $ver) { # read 1 or 2 bytes, based on version
Branch: 1 branch
BranchTrueFalseTotal
if ($raf->Read($buff, $ver) == $ver) 12 1 13
201 12 ▶ if ($ver == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ver == 1) { } 12 0 12
202 # read version 1 record header
203 12 $type = ord($buff);
204 12 $len = ReadVarInt($raf);
205 12 ▶ $getSize = 1 if $type == 0x0f; # Start WPG (Type 1)
Branch: 1 branch
BranchTrueFalseTotal
if $type == 15 1 11 12
206 } else {
207 # read version 2 record header
208 0 $type = unpack('xC', $buff);
209 0 ReadVarInt($raf); # skip extensions
210 0 $len = ReadVarInt($raf);
211 0 ▶ $getSize = 1 if $type == 0x01; # Start WPG
Branch: 1 branch
BranchTrueFalseTotal
if $type == 1 0 0 0
212 0 ▶ undef $type if $type > 0x3f;
Branch: 1 branch
BranchTrueFalseTotal
if $type > 63 0 0 0
213 }
214 12 ▶ if ($getSize) {
Branch: 1 branch
BranchTrueFalseTotal
if ($getSize) { } 1 11 12
215 # read Start record to obtain image size
216 1 ▶ $raf->Read($buff, $len) == $len or $et->Warn('File format error'), last;
Branch: 1 branch
BranchTrueFalseTotal
$raf->Read($buff, $len) == $len or $et->Warn("File format error"), last 1 0 1
217 1 my ($w, $h, $xres, $yres, $err);
218 1 ▶ if ($ver == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ver == 1) { } 1 0 1
219 1 ▶ if ($len < 6) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len < 6) { } 0 1 1
220 0 $err = '';
221 } else {
222 1 ($w, $h) = unpack('x2vv', $buff);
223 }
224 } else {
225 0 my ($precision, $format);
226 0 ($xres, $yres, $precision) = unpack('vvC', $buff);
227 0 ▶ if ($precision == 0 and $len >= 21) {
Branch: 2 branches
BranchTrueFalseTotal
if ($precision == 0 and $len >= 21) { } 0 0 0
elsif ($precision == 1 and $len >= 29) { } 0 0 0
Condition0%0/3
$precision == 0 and $len >= 21
!ll&&!rl&&r
000
MC/DC0%0/2
$precision == 0 and $len >= 21
$precision == 0 $len >= 21
Truth table0%0/3
A and B
A= $precision == 0B= $len >= 21
ABresult
0X0
100
111
228 0 ▶ $len >= 13+2*4 or $err = '';
Branch: 1 branch
BranchTrueFalseTotal
$len >= 21 or $err = "" 0 0 0
229 0 $format = 'int16s';
230 ▶ } elsif ($precision == 1 and $len >= 29) {
Condition0%0/3
$precision == 1 and $len >= 29
!ll&&!rl&&r
000
MC/DC0%0/2
$precision == 1 and $len >= 29
$precision == 1 $len >= 29
Truth table0%0/3
A and B
A= $precision == 1B= $len >= 29
ABresult
0X0
100
111
231 0 ▶ $len >= 13+4*4 or $err = '';
Branch: 1 branch
BranchTrueFalseTotal
$len >= 29 or $err = "" 0 0 0
232 0 $format = 'int32s';
233 } else {
234 0 $err = 'Invalid integer precision';
235 }
236 0 ▶ unless (defined $err) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $err) 0 0 0
237 0 my ($x1,$y1,$x2,$y2) = ReadValue(\$buff, 13, $format, 4, $len-13);
238 0 $w = abs($x2 - $x1);
239 0 $h = abs($y2 - $y1);
240 }
241 }
242 1 ▶ if (defined $err) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $err) { } 0 1 1
243 0 ▶ $et->Warn($err || 'Truncated start record');
Condition0%0/2
$err || 'Truncated start record'
l!l
00
MC/DC0%0/1
$err || 'Truncated start record'
$err
Truth table0%0/2
A
A= $err
Aresult
11
00
244 } else {
245 1 ▶ $et->HandleTag($tagTablePtr, ImageWidthInches => $w / ($xres || 1200));
Condition50%1/2
$xres || 1200
l!l
01
MC/DC0%0/1
$xres || 1200
$xres
Truth table50%1/2
A
A= $xres
Aresult
11
00
246 1 ▶ $et->HandleTag($tagTablePtr, ImageHeightInches => $h / ($yres || 1200));
Condition50%1/2
$yres || 1200
l!l
01
MC/DC0%0/1
$yres || 1200
$yres
Truth table50%1/2
A
A= $yres
Aresult
11
00
247 }
248 } else {
249 # skip to the next record
250 11 ▶ $raf->Seek($len, 1) or $et->Warn('Truncated record'), last;
Branch: 1 branch
BranchTrueFalseTotal
$raf->Seek($len, 1) or $et->Warn("Truncated record"), last 11 0 11
251 }
252 }
253 # go to some trouble to collapse identical sequential entries in record list
254 # (trying to keep the length of the list managable for complex images)
255 13 ▶ $lastType and $type and $type == $lastType and ++$count, next;
Branch: 1 branch
BranchTrueFalseTotal
$lastType and $type and $type == $lastType and ++$count, next 4 9 13
Condition100%6/6
$lastType and $type and $type == $lastType
!ll&&!rl&&r
274
$lastType and $type
!ll&&!rl&&r
1111
MC/DC100%3/3
$lastType and $type and $type == $lastType
$lastType $type $type == $lastType
Truth table100%4/4
A and B and C
A= $lastTypeB= $typeC= $type == $lastType
ABCresult
0XX0
10X0
1100
1111
256 9 ▶ if ($lastType) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lastType) 8 1 9
257 8 ▶ my $val = $count > 1 ? "${lastType}x$count" : $lastType;
Branch: 1 branch
BranchTrueFalseTotal
$count > 1 ? : 1 7 8
258 8 $et->HandleTag($tagTablePtr, $tag => $val);
259 }
260 9 ▶ last unless $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 1 8 9
261 8 $lastType = $type;
262 8 $count = 1;
263 }
264 1 return 1;
265 }
266  
267 1 1; # end
268  
269 __END__
« Image/ExifTool/Microsoft.pm ↑ Summary Image/ExifTool/PPM.pm »