blib/lib/Image/XWD.pm

filter: Statementstmt 13.6% 26 / 190 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 36 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 53.3% 8 / 15 Podpod 85.7% 6 / 7 totaltotal 12.9% 40 / 308 CC 34 SCAR 58.0
CC
34
Cov
10%
read_file
600.0
xy_rgb
132.0
new
2.0
CRAP 330.6
↑ Summary
1 package Image::XWD;
2  
3 #
4 # Based on /usr/include/X11/XWDFile.h
5 #
6  
7 1 ▶use 5.008008;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 require Exporter;
12  
13 1 our @ISA = qw(Exporter);
14 1 our @EXPORT_OK = ();
15 1 our @EXPORT = ();
16  
17 1 our $VERSION = '0.01';
18  
19 1 ▶use constant XWD_FILE_VERSION => 7;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use constant SIZEOF_XWD_HEADER => 25*4;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 ▶use constant XWD_PIXMAP_FORMAT_XYPIXMAP => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use constant XWD_PIXMAP_FORMAT_ZPIXMAP => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
26 0 my $class = shift;
27 0 my $self = bless {}, $class;
28  
29 0 return $self->init(@_);
30 }
31  
32 0 ▶sub init {
Subroutine init: not called CC=1 SCAR=6.9
Pod undocumented
33 0 my $self = shift;
34 0 my %args = @_;
35  
36 0 return $self;
37 }
38  
39 0 ▶sub read_file($$) {
Subroutine read_file: not called CC=24 SCAR=64.0
Pod documented
40 0 my $self = shift;
41 0 my ($fname) = @_;
42  
43 0 my ($raw_data);
44  
45 my ($header_size);
46 0 my ($file_version);
47 0 my ($pixmap_format);
48 0 my ($pixmap_depth);
49 0 my ($pixmap_width);
50 0 my ($pixmap_heigth);
51 0 my ($xoffset);
52 0 my ($byte_order);
53 0 my ($bitmap_unit);
54 0 my ($bitmap_bit_order);
55 0 my ($bitmap_pad);
56 0 my ($bits_per_pixel);
57 0 my ($bytes_per_line);
58 0 my ($visual_class);
59 0 my ($red_mask);
60 0 my ($green_mask);
61 0 my ($blue_mask);
62 0 my ($bits_per_rgb);
63 0 my ($colormap_entries);
64 0 my ($ncolors);
65 0 my ($window_width);
66 0 my ($window_heigth);
67 0 my ($window_x);
68 0 my ($window_y);
69 0 my ($window_bdrwidth);
70 0 my ($window_name);
71  
72 0 ▶ open(F, "<$fname") or return undef;
Branch: 1 branch
BranchTrueFalseTotal
open F, "<$fname" or return undef 0 0 0
73  
74 0 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
75 $atime,$mtime,$ctime,$blksize,$blocks)
76 = stat(F);
77  
78 0 print("file size is: $size bytes\n");
79  
80 # read the first element of the XWDHeader
81 0 read(F, $header_size, 4);
82 0 $header_size = unpack('N', $header_size);
83 0 print("XWDFileHeader.header_size=$header_size\n");
84  
85 0 ▶ if ($header_size < SIZEOF_XWD_HEADER) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header_size < 100) 0 0 0
86
87 }
88  
89 # read the rest of the XWDHeader
90 0 read(F, $raw_data, SIZEOF_XWD_HEADER-4);
91
92 0 ($file_version,
93 $pixmap_format,
94 $pixmap_depth,
95 $pixmap_width,
96 $pixmap_heigth,
97 $xoffset,
98 $byte_order,
99 $bitmap_unit,
100 $bitmap_bit_order,
101 $bitmap_pad,
102 $bits_per_pixel,
103 $bytes_per_line,
104 $visual_class,
105 $red_mask,
106 $green_mask,
107 $blue_mask,
108 $bits_per_rgb,
109 $colormap_entries,
110 $ncolors,
111 $window_width,
112 $window_heigth,
113 $window_x,
114 $window_y,
115 $window_bdrwidth) = unpack('N'.SIZEOF_XWD_HEADER, $raw_data);
116  
117 0 ▶ if ($file_version != XWD_FILE_VERSION) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_version != 7) 0 0 0
118 0 printf(STDERR __PACKAGE__.": Unknown file_version: 0x%8.8X\n", $file_version);
119 0 close(F);
120 0 return undef;
121 }
122 0 ▶ if ($pixmap_format == XWD_PIXMAP_FORMAT_XYPIXMAP) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pixmap_format == 1) { } 0 0 0
elsif ($pixmap_format == 2) { } 0 0 0
123 0 printf(STDERR __PACKAGE__.": pixmap_format=XYPixmap is not supported, yet. Sorry.\n");
124 0 close(F);
125 0 return undef;
126  
127 } elsif ($pixmap_format == XWD_PIXMAP_FORMAT_ZPIXMAP) {
128 0 ▶ if ($pixmap_depth != 24) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pixmap_depth != 24) 0 0 0
129 0 printf(STDERR __PACKAGE__.": Only pixmap_depth=24 is supported, sorry. (pixmap_depth=$pixmap_depth)\n");
130 0 close(F);
131 0 return undef;
132 }
133 0 ▶ if ($byte_order != 0 && $byte_order != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($byte_order != 0 and $byte_order != 1) 0 0 0
Condition0%0/3
$byte_order != 0 and $byte_order != 1
!ll&&!rl&&r
000
MC/DC0%0/2
$byte_order != 0 and $byte_order != 1
$byte_order != 0 $byte_order != 1
Truth table0%0/3
A and B
A= $byte_order != 0B= $byte_order != 1
ABresult
0X0
100
111
134 0 printf(STDERR __PACKAGE__.": Only byte_order=0 is supported, sorry. (byte_order=$byte_order)\n");
135 0 close(F);
136 0 return undef;
137 }
138 0 ▶ if ($bitmap_unit != 32) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bitmap_unit != 32) 0 0 0
139 0 printf(STDERR __PACKAGE__.": Only bitmap_unit=32 is supported, sorry. (bitmap_unit=$bitmap_unit)\n");
140 0 close(F);
141 0 return undef;
142 }
143 0 ▶ if ($bitmap_bit_order != 0 && $bitmap_bit_order != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bitmap_bit_order != 0 and $bitmap_bit_order != 1) 0 0 0
Condition0%0/3
$bitmap_bit_order != 0 and $bitmap_bit_order != 1
!ll&&!rl&&r
000
MC/DC0%0/2
$bitmap_bit_order != 0 and $bitmap_bit_order != 1
$bitmap_bit_order != 0 $bitmap_bit_order != 1
Truth table0%0/3
A and B
A= $bitmap_bit_order != 0B= $bitmap_bit_order != 1
ABresult
0X0
100
111
144 0 printf(STDERR __PACKAGE__.": Only bitmap_bit_order={0,1} is supported, sorry. (bitmap_bit_order=$bitmap_bit_order)\n");
145 0 close(F);
146 0 return undef;
147 }
148 0 ▶ if ($bitmap_pad != 32) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bitmap_pad != 32) 0 0 0
149 0 printf(STDERR __PACKAGE__.": Only bitmap_pad=32 is supported, sorry. (bitmap_pad=$bitmap_pad)\n");
150 0 close(F);
151 0 return undef;
152 }
153 0 ▶ if ($bits_per_pixel != 24 && $bits_per_pixel != 32) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bits_per_pixel != 24 and $bits_per_pixel != 32) 0 0 0
Condition0%0/3
$bits_per_pixel != 24 and $bits_per_pixel != 32
!ll&&!rl&&r
000
MC/DC0%0/2
$bits_per_pixel != 24 and $bits_per_pixel != 32
$bits_per_pixel != 24 $bits_per_pixel != 32
Truth table0%0/3
A and B
A= $bits_per_pixel != 24B= $bits_per_pixel != 32
ABresult
0X0
100
111
154 0 printf(STDERR __PACKAGE__.": Only bits_per_pixel={24,32} is supported, sorry. (bits_per_pixel=$bits_per_pixel)\n");
155 0 close(F);
156 0 return undef;
157 }
158 0 ▶ if ($visual_class != 4 && $visual_class != 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($visual_class != 4 and $visual_class != 5) 0 0 0
Condition0%0/3
$visual_class != 4 and $visual_class != 5
!ll&&!rl&&r
000
MC/DC0%0/2
$visual_class != 4 and $visual_class != 5
$visual_class != 4 $visual_class != 5
Truth table0%0/3
A and B
A= $visual_class != 4B= $visual_class != 5
ABresult
0X0
100
111
159 0 printf(STDERR __PACKAGE__.": Only visual_class={4,5} is supported, sorry. (visual_class=$visual_class)\n");
160 0 close(F);
161 0 return undef;
162 }
163 0 ▶ if ($red_mask != 0x00FF0000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($red_mask != 16711680) 0 0 0
164 0 printf(STDERR __PACKAGE__.": Only red_mask=0x00FF0000 is supported, sorry. (red_mask=%8.8X)\n", $red_mask);
165 0 close(F);
166 0 return undef;
167 }
168 0 ▶ if ($green_mask != 0x0000FF00) {
Branch: 1 branch
BranchTrueFalseTotal
if ($green_mask != 65280) 0 0 0
169 0 printf(STDERR __PACKAGE__.": Only green_mask=0x0000FF00 is supported, sorry. (green_mask=%8.8X)\n", $green_mask);
170 0 close(F);
171 0 return undef;
172 }
173 0 ▶ if ($blue_mask != 0x000000FF) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blue_mask != 255) 0 0 0
174 0 printf(STDERR __PACKAGE__.": Only blue_mask=0x000000FF is supported, sorry. (blue_mask=%8.8X)\n", $blue_mask);
175 0 close(F);
176 0 return undef;
177 }
178 0 ▶ if ($bits_per_rgb != 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bits_per_rgb != 8) 0 0 0
179 0 printf(STDERR __PACKAGE__.": Only bits_per_rgb=8 is supported, sorry. (bits_per_rgb=$bits_per_rgb)\n");
180 0 close(F);
181 0 return undef;
182 }
183  
184 } else {
185 0 printf(STDERR __PACKAGE__.": Unknown pixmap_format: 0x%8.8X\n", $pixmap_format);
186 0 close(F);
187 0 return undef;
188 }
189  
190 0 $self->{'file_version'} = $file_version;
191 0 $self->{'pixmap_format'} = $pixmap_format;
192 0 $self->{'pixmap_depth'} = $pixmap_depth;
193 0 $self->{'pixmap_width'} = $pixmap_width;
194 0 $self->{'pixmap_heigth'} = $pixmap_heigth;
195 0 $self->{'xoffset'} = $xoffset;
196 0 $self->{'byte_order'} = $byte_order;
197 0 $self->{'bitmap_unit'} = $bitmap_unit;
198 0 $self->{'bitmap_bit_order'} = $bitmap_bit_order;
199 0 $self->{'bitmap_pad'} = $bitmap_pad;
200 0 $self->{'bits_per_pixel'} = $bits_per_pixel;
201 0 $self->{'bytes_per_line'} = $bytes_per_line;
202 0 $self->{'visual_class'} = $visual_class;
203 0 $self->{'red_mask'} = $red_mask;
204 0 $self->{'green_mask'} = $green_mask;
205 0 $self->{'blue_mask'} = $blue_mask;
206 0 $self->{'bits_per_rgb'} = $bits_per_rgb;
207 0 $self->{'colormap_entries'} = $colormap_entries;
208 0 $self->{'ncolors'} = $ncolors;
209 0 $self->{'window_width'} = $window_width;
210 0 $self->{'window_heigth'} = $window_heigth;
211 0 $self->{'window_x'} = $window_x;
212 0 $self->{'window_y'} = $window_y;
213 0 $self->{'window_bdrwidth'} = $window_bdrwidth;
214  
215 0 ▶ if (0 < $header_size - SIZEOF_XWD_HEADER) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 < $header_size - 100) { } 0 0 0
216 # read window_name
217 0 read(F, $raw_data, $header_size - SIZEOF_XWD_HEADER);
218 0 $window_name = unpack('Z*', $raw_data);
219 } else {
220 # No window_name
221 0 $window_name = undef;
222 }
223  
224 0 $self->{'window_name'} = $window_name;
225  
226 0 my (@colors);
227 my ($pixel);
228 0 my ($red);
229 0 my ($green);
230 0 my ($blue);
231 0 my ($flags);
232 0 my ($pad);
233 0 my ($i);
234  
235 0 ▶ for ($i=0; $i<$ncolors; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $ncolors) 0 0 0
236 # read one XWDColor structure
237 0 read(F, $raw_data, 12);
238 0 ($pixel,
239 $red,
240 $green,
241 $blue,
242 $flags,
243 $pad) = unpack("NnnnCC", $raw_data);
244  
245 0 $self->{"colors[$i]"} = { 'pixel' => $pixel,
246 'red' => $red,
247 'green' => $green,
248 'blue' => $blue,
249 'flags' => $flags,
250 'pad' => $pad
251 };
252
253 }
254  
255 0 my (@row);
256 0 my ($x, $y);
257 0 my ($r, $g, $b);
258 0 my (%pixel_code);
259 0 ▶ for ($y=0; $y<$pixmap_heigth; $y++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y < $pixmap_heigth) 0 0 0
260 # read one complete line at once
261 0 read(F, $raw_data, $bytes_per_line);
262  
263 # store the raw data to save memory
264 0 push(@{$self->{"pixel"}}, $raw_data);
265 }
266  
267 0 close(F);
268  
269 0 return 1;
270 }
271  
272 0 ▶sub get_width($) {
Subroutine get_width: not called CC=1 SCAR=6.9
Pod documented
273 0 my ($self) = shift;
274  
275 0 return $self->{'pixmap_width'};
276 }
277  
278 0 ▶sub get_heigth($) {
Subroutine get_heigth: not called CC=1 SCAR=6.9
Pod documented
279 0 my ($self) = shift;
280
281 0 return $self->{'pixmap_heigth'};
282 }
283  
284 0 ▶sub get_window_name($) {
Subroutine get_window_name: not called CC=1 SCAR=6.9
Pod documented
285 0 my ($self) = shift;
286  
287 0 return $self->{'window_name'};
288 }
289  
290 0 ▶sub xy_rgb($$$) {
Subroutine xy_rgb: not called CC=11 SCAR=48.8
Pod documented
291 0 my ($self) = shift;
292 0 my ($x, $y) = @_;
293 0 my ($pixel);
294 0 my ($r, $g, $b);
295  
296 0 ▶ if ($self->{'bitmap_bit_order'} == 1 &&
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'bitmap_bit_order'} == 1 and $self->{'bits_per_pixel'} == 24 and $self->{'red_mask'} == 16711680 and $self->{'green_mask'} == 65280 and $self->{'blue_mask'} == 255) { } 0 0 0
elsif ($self->{'bitmap_bit_order'} == 0 and $self->{'bits_per_pixel'} == 32 and $self->{'red_mask'} == 16711680 and $self->{'green_mask'} == 65280 and $self->{'blue_mask'} == 255) { } 0 0 0
Condition0%0/12
$self->{"bitmap_bit_order"} == 1 and $self->{"bits_per_pixel"} == 24 and $self->{"red_mask"} == 16711680 and $self->{"green_mask"} == 65280 and $self->{"blue_mask"} == 255
!ll&&!rl&&r
000
$self->{"bitmap_bit_order"} == 1 and $self->{"bits_per_pixel"} == 24 and $self->{"red_mask"} == 16711680 and $self->{"green_mask"} == 65280
!ll&&!rl&&r
000
$self->{"bitmap_bit_order"} == 1 and $self->{"bits_per_pixel"} == 24 and $self->{"red_mask"} == 16711680
!ll&&!rl&&r
000
$self->{"bitmap_bit_order"} == 1 and $self->{"bits_per_pixel"} == 24
!ll&&!rl&&r
000
MC/DC0%0/5
$self->{"bitmap_bit_order"} == 1 and $self->{"bits_per_pixel"} == 24 and $self->{"red_mask"} == 16711680 and $self->{"green_mask"} == 65280 and $self->{"blue_mask"} == 255
$self->{"bitmap_bit_order"} == 1 $self->{"bits_per_pixel"} == 24 $self->{"red_mask"} == 16711680 $self->{"green_mask"} == 65280 $self->{"blue_mask"} == 255
Truth table0%0/6
A and B and C and D and E
A= $self->{"bitmap_bit_order"} == 1B= $self->{"bits_per_pixel"} == 24C= $self->{"red_mask"} == 16711680D= $self->{"green_mask"} == 65280E= $self->{"blue_mask"} == 255
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
297 $self->{'bits_per_pixel'} == 24 &&
298 $self->{'red_mask'} == 0x00FF0000 &&
299 $self->{'green_mask'} == 0x0000FF00 &&
300 $self->{'blue_mask'} == 0x000000FF
301 ) {
302 ($r, $g, $b) = unpack('C*',
303 substr($self->{"pixel"}->[$y], $x * ($self->{'bits_per_pixel'}/8),
304 0 $self->{'bits_per_pixel'}/8))
305  
306 ▶ } elsif ($self->{'bitmap_bit_order'} == 0 &&
Condition0%0/12
$self->{"bitmap_bit_order"} == 0 and $self->{"bits_per_pixel"} == 32 and $self->{"red_mask"} == 16711680 and $self->{"green_mask"} == 65280 and $self->{"blue_mask"} == 255
!ll&&!rl&&r
000
$self->{"bitmap_bit_order"} == 0 and $self->{"bits_per_pixel"} == 32 and $self->{"red_mask"} == 16711680 and $self->{"green_mask"} == 65280
!ll&&!rl&&r
000
$self->{"bitmap_bit_order"} == 0 and $self->{"bits_per_pixel"} == 32 and $self->{"red_mask"} == 16711680
!ll&&!rl&&r
000
$self->{"bitmap_bit_order"} == 0 and $self->{"bits_per_pixel"} == 32
!ll&&!rl&&r
000
MC/DC0%0/5
$self->{"bitmap_bit_order"} == 0 and $self->{"bits_per_pixel"} == 32 and $self->{"red_mask"} == 16711680 and $self->{"green_mask"} == 65280 and $self->{"blue_mask"} == 255
$self->{"bitmap_bit_order"} == 0 $self->{"bits_per_pixel"} == 32 $self->{"red_mask"} == 16711680 $self->{"green_mask"} == 65280 $self->{"blue_mask"} == 255
Truth table0%0/6
A and B and C and D and E
A= $self->{"bitmap_bit_order"} == 0B= $self->{"bits_per_pixel"} == 32C= $self->{"red_mask"} == 16711680D= $self->{"green_mask"} == 65280E= $self->{"blue_mask"} == 255
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
307 $self->{'bits_per_pixel'} == 32 &&
308 $self->{'red_mask'} == 0x00FF0000 &&
309 $self->{'green_mask'} == 0x0000FF00 &&
310 $self->{'blue_mask'} == 0x000000FF) {
311  
312 0 my ($dummy);
313  
314 ($r, $g, $b, $dummy) = unpack('C*',
315 substr($self->{"pixel"}->[$y], $x * ($self->{'bits_per_pixel'}/8),
316 0 $self->{'bits_per_pixel'}/8))
317  
318 } else {
319 0 print(STDERR __PACKAGE__.": Image format not supported, sorry.\n");
320 }
321  
322 0 return ($r, $g, $b);
323 }
324  
325 1 ▶END {
Subroutine END: called CC=1 SCAR=0.0
326 # Cleanup code
327 }
328  
329 1 1;
330 __END__
↑ Summary