blib/lib/PCX/Loader.pm

filter: Statementstmt 15.9% 11 / 69 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 30.0% 3 / 10 Podpod 71.4% 5 / 7 totaltotal 18.8% 19 / 101 CC 10 SCAR 31.3
CC
10
Cov
13%
load_pcx
56.0
get_block
7.1
intr
6.0
CRAP 23.0
↑ Summary
1 package PCX::Loader;
2
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5
6 1 our $VERSION = "0.51";
7
8 =head1 NAME
9
10 PCX::Loader - Loads 320x200 8-bit PCX-format graphics.
11
12 =head1 SYNOPSIS
13
14 my $pcx = PCX::Loader->new('face.pcx');
15
16 =head1 METHODS
17
18 =head2 new($filename);
19
20 Constructor. Loades PCX file into class.
21
22 This is a treat... this routine will load a PCX-format file (yah, I know ... ancient
23 format ... but it is the only one I could find specs for to write it in Perl. If
24 anyone can get specs for any other formats, or could write a loader for them, I
25 would be very grateful!) Anyways, a PCX-format file that is exactly 320x200 with 8 bits
26 per pixel, with pure Perl. It returns a blessed refrence to a PCX::Loader object.
27
28 The object will have the following attributes:
29
30 =over
31
32 =item $pcx->{image}
33
34 This is an array refrence to the entire image. The array containes exactly 64000 elements, each
35 element contains a number corresponding into an index of the palette array, details below.
36
37
38
39 =item $pcx->{palette}
40
41 This is an array ref to an AoH (array of hashes). Each element has the following three keys:
42
43 $pcx->{palette}->[0]->{red};
44 $pcx->{palette}->[0]->{green};
45 $pcx->{palette}->[0]->{blue};
46
47 Each is in the range of 0..63, corresponding to their named color component.
48
49 =back
50
51 =cut
52
53 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
54 0 my $type = shift;
55 0 my $self = {
56 file => $_[1]
57 };
58 0 my (@a,@b)=load_pcx($_[1]);
59 0 $self->{image}=\@a;
60 0 $self->{palette}=\@b;
61 0 bless \%{$self}, $type;
62 }
63
64
65
66
67 =head2 $pcx->get_block($array_ref);
68
69 Returns a rectangular block defined by an array ref in the form of:
70
71 [$left,$top,$right,$bottom]
72
73 The return value is an array ref.
74
75 These must be in the range of 0..319 for $left and $right, and the range of 0..199 for
76 $top and $bottom. The block is returned as an array ref with horizontal lines in sequental order.
77 I.e. to get a pixel from [2,5] in the block, and $left-$right was 20, then the element in
78 the array ref containing the contents of coordinates [2,5] would be found by [5*20+2] ($y*$width+$x).
79
80 print $pcx->get_block(0,0,20,50)->[5*20+2];
81
82 This would print the contents of the element at block coords [2,5].
83
84 =cut
85
86 0 ▶sub get_block {
Subroutine get_block: not called CC=3 SCAR=19.6
Pod documented
87 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
88 0 my $self = shift;
89 0 my $ref = shift;
90 0 my ($x1,$y1,$x2,$y2) = @{$ref};
91 0 my @block = ();
92 0 my $count = 0;
93 0 for my $x ($x1..$x2-1) {
94 0 for my $y ($y1..$y2-1) {
95 0 $block[$count++] = $self->get($x,$y);
96 }
97 }
98 0 return \@block;
99 }
100
101
102
103
104 =head2 $pcx->get($x,$y);
105
106 Returns the value of pixel at image coordinates $x,$y.
107 $x must be in the range of 0..319 and $y must be in the range of 0..199.
108
109 =cut
110
111 0 ▶sub get {
Subroutine get: not called CC=1 SCAR=6.9
Pod documented
112 0 my $self = shift;
113 0 my ($x,$y) = (shift,shift);
114 0 return $self->{image}->[$y*320+$x];
115 }
116
117
118
119
120 =head2 $pcx->rgb($index);
121
122 Returns a 3-element array (not array ref) with each element corresponding to the red, green, or
123 blue color components, respecitvely.
124
125 =cut
126
127 0 ▶sub rgb {
Subroutine rgb: not called CC=1 SCAR=6.9
Pod documented
128 0 my $self = shift;
129 0 my $color = shift;
130
131 # Returns array of (r,g,b) value from palette index passed
132 0 return ($self->{palette}->[$color]->{red},$self->{palette}->[$color]->{green},$self->{palette}->[$color]->{blue});
133 }
134
135
136
137
138 =head2 $pcx->avg($index);
139
140 Returns the mean value of the red, green, and blue values at the palette index in C<$index>.
141
142 =cut
143
144 # Returns mean of (rgb) value of palette index passed
145 0 ▶sub avg {
Subroutine avg: not called CC=1 SCAR=6.9
Pod documented
146 0 my $self = shift;
147 0 my $color = shift;
148 0 return intr(($self->{palette}->[$color]->{red}+$self->{palette}->[$color]->{green}+$self->{palette}->[$color]->{blue})/3);
149 }
150
151
152
153
154 # Loads and decompresses a PCX-format 320x200, 8-bit image file and returns
155 # two arrays, first is a 64000-byte long array, each element contains a palette
156 # index, and the second array is a 255-byte long array, each element is a hash
157 # ref with the keys 'red', 'green', and 'blue', each key contains the respective color
158 # component for that color index in the palette.
159 0 ▶sub load_pcx {
Subroutine load_pcx: not called CC=7 SCAR=40.3
Pod undocumented
160 0 ▶ shift if(substr($_[0],0,4) eq 'AI::');
Branch: 1 branch
BranchTrueFalseTotal
if substr($_[0], 0, 4) eq "AI::" 0 0 0
161
162 # open the file
163 0 open(FILE, "$_[0]");
164 0 binmode(FILE);
165
166 0 my $tmp;
167 my @image;
168 0 my @palette;
169 0 my $data;
170
171 # Read header
172 0 read(FILE,$tmp,128);
173
174 # load the data and decompress into buffer
175 0 my $count=0;
176
177 0 ▶ while($count<320*200) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count < 64000) 0 0 0
178 # get the first piece of data
179 0 read(FILE,$data,1);
180 0 $data=ord($data);
181
182 # is this a rle?
183 0 ▶ if ($data>=192 && $data<=255) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data >= 192 and $data <= 255) { } 0 0 0
Condition0%0/3
$data >= 192 and $data <= 255
!ll&&!rl&&r
000
MC/DC0%0/2
$data >= 192 and $data <= 255
$data >= 192 $data <= 255
Truth table0%0/3
A and B
A= $data >= 192B= $data <= 255
ABresult
0X0
100
111
184 # how many bytes in run?
185 0 my $num_bytes = $data-192;
186
187 # get the actual $data for the run
188 0 read(FILE, $data, 1);
189 0 $data=ord($data);
190 # replicate $data in buffer num_bytes times
191 0 ▶ while($num_bytes-->0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num_bytes-- > 0) 0 0 0
192 0 $image[$count++] = $data;
193 } # end while
194 } else {
195 # actual $data, just copy it into buffer at next location
196 0 $image[$count++] = $data;
197 } # end else not rle
198 }
199
200 # move to end of file then back up 768 bytes i.e. to begining of palette
201 0 seek(FILE,-768,2);
202
203 # load the pallete into the palette
204 0 for my $index (0..255) {
205 # get the red component
206 0 read(FILE,$tmp,1);
207 0 $palette[$index]->{red} = ($tmp>>2);
208
209 # get the green component
210 0 read(FILE,$tmp,1);
211 0 $palette[$index]->{green} = ($tmp>>2);
212
213 # get the blue component
214 0 read(FILE,$tmp,1);
215 0 $palette[$index]->{blue} = ($tmp>>2);
216
217 }
218
219 0 close(FILE);
220
221 0 return @image,@palette;
222 }
223
224
225
226
227 # Rounds a floating-point to an integer with int() and sprintf()
228 0 ▶sub intr {
Subroutine intr: not called CC=2 SCAR=17.9
Pod undocumented
229 0 ▶ shift if(substr($_[0],0,4) eq 'AI::');
Branch: 1 branch
BranchTrueFalseTotal
if substr($_[0], 0, 4) eq "AI::" 0 0 0
230 0 try { return int(sprintf("%.0f",shift)) }
231 0 catch { return 0 }
232 }
233
234
235
236
237 =head1 BUGS
238
239 Please submit bugs to the CPAN bug tracker or the L<Github|https://github.com/asb-capfan/PCX-Loader/issues> repository.
240
241
242 =head1 AUTHOR
243
244 Josiah Bryan F<E<lt>jdb@wcoil.comE<gt>>, Alexander Becker F<E<lt>asb@cpan.orgE<gt>>
245
246 Copyright (c) 2000 Josiah Bryan, 2017 Alexander Becker. All rights reserved. This program is free software;
247 you can redistribute it and/or modify it under the same terms as Perl itself.
248
249 The C<PCX::Loader> module is free software. IT COMES WITHOUT WARRANTY OF ANY KIND.
250
251 =cut
252
253 1 1; # /PCX::Loader
↑ Summary