Perlbal/Plugin/Palimg.pm

filter: Statementstmt 7.7% 12 / 155 Branchbran 0.0% 0 / 78 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 27.7% 5 / 18 Podpod 0.0% 0 / 4 totaltotal 6.3% 17 / 270 CC 45 SCAR 47.2
CC
45
Cov
5%
modify_file
306.0
new_png_palette
90.0
__ANON__
56.0
CRAP 112.5
« Perlbal/Plugin/AccessControl.pm ↑ Summary Perlbal/Service.pm »
1 ###########################################################################
2 # Palimg plugin that allows Perlbal to serve palette altered images
3 ###########################################################################
4  
5 package Perlbal::Plugin::Palimg;
6  
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶no warnings qw(deprecated);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # called when we're being added to a service
12 0 ▶sub register {
Subroutine register: not called CC=1 SCAR=6.9
Pod undocumented
13 0 my ($class, $svc) = @_;
14  
15 # verify that an incoming request is a palimg request
16 0 ▶ $svc->register_hook('Palimg', 'start_serve_request', sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
17 0 my Perlbal::ClientHTTPBase $obj = $_[0];
18 0 ▶ return 0 unless $obj;
Branch: 1 branch
BranchTrueFalseTotal
unless $obj 0 0 0
19 0 my Perlbal::HTTPHeaders $hd = $obj->{req_headers};
20 0 my $uriref = $_[1];
21 0 ▶ return 0 unless $uriref;
Branch: 1 branch
BranchTrueFalseTotal
unless $uriref 0 0 0
22  
23 # if this is palimg, peel off the requested modifications and put in headers
24 0 ▶ return 0 unless $$uriref =~ m!^/palimg/(.+)\.(\w+)(.*)$!;
Branch: 1 branch
BranchTrueFalseTotal
unless $$uriref =~ m[^/palimg/(.+)\.(\w+)(.*)$] 0 0 0
25 0 my ($fn, $ext, $extra) = ($1, $2, $3);
26 0 ▶ return 0 unless $extra;
Branch: 1 branch
BranchTrueFalseTotal
unless $extra 0 0 0
27 0 my ($palspec) = $extra =~ m!^/p(.+)$!;
28 0 ▶ return 0 unless $fn && $palspec;
Branch: 1 branch
BranchTrueFalseTotal
unless $fn and $palspec 0 0 0
Condition0%0/3
$fn and $palspec
!ll&&!rl&&r
000
MC/DC0%0/2
$fn and $palspec
$fn $palspec
Truth table0%0/3
A and B
A= $fnB= $palspec
ABresult
0X0
100
111
29  
30 # must be ok, setup for it
31 0 $$uriref = "/palimg/$fn.$ext";
32 0 $obj->{scratch}->{palimg} = [ $ext, $palspec ];
33 0 return 0;
34 0 });
35  
36 # actually serve a palimg
37 0 ▶ $svc->register_hook('Palimg', 'start_send_file', sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
38 0 my Perlbal::ClientHTTPBase $obj = $_[0];
39 return 0 unless $obj &&
40 0 ▶ (my $palimginfo = $obj->{scratch}->{palimg});
Branch: 1 branch
BranchTrueFalseTotal
unless $obj and my $palimginfo = $obj->{"scratch"}{"palimg"} 0 0 0
Condition0%0/3
$obj and my $palimginfo = $obj->{"scratch"}{"palimg"}
!ll&&!rl&&r
000
MC/DC0%0/2
$obj and my $palimginfo = $obj->{"scratch"}{"palimg"}
$obj my $palimginfo = $obj->{"scratch"}{"palimg"}
Truth table0%0/3
A and B
A= $objB= my $palimginfo = $obj->{"scratch"}{"palimg"}
ABresult
0X0
100
111
41  
42 # turn off writes
43 0 $obj->watch_write(0);
44  
45 # create filehandle for reading
46 0 my $data = '';
47 0 ▶ Perlbal::AIO::aio_read($obj->reproxy_fh, 0, 2048, $data, sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
48 # got data? undef is error
49 0 ▶ return $obj->_simple_response(500) unless $_[0] > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] > 0 0 0 0
50  
51 # pass down to handler
52 0 my Perlbal::HTTPHeaders $hd = $obj->{req_headers};
53 0 my $res = PalImg::modify_file(\$data, $palimginfo->[0], $palimginfo->[1]);
54 0 ▶ return $obj->_simple_response(500) unless defined $res;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $res 0 0 0
55 0 ▶ return $obj->_simple_response($res) if $res;
Branch: 1 branch
BranchTrueFalseTotal
if $res 0 0 0
56  
57 # seek into the file now so sendfile starts further in
58 0 my $ld = length $data;
59 0 sysseek($obj->{reproxy_fh}, $ld, &POSIX::SEEK_SET);
60 0 $obj->{reproxy_file_offset} = $ld;
61  
62 # re-enable writes after we get data
63 0 $obj->tcp_cork(1); # by setting reproxy_file_offset above, it won't cork, so we cork it
64 0 $obj->write($data);
65 0 $obj->watch_write(1);
66 0 });
67  
68 0 return 1;
69 0 });
70  
71 0 return 1;
72 }
73  
74 # called when we're no longer active on a service
75 0 ▶sub unregister {
Subroutine unregister: not called CC=1 SCAR=6.9
Pod undocumented
76 0 my ($class, $svc) = @_;
77  
78 # clean up time
79 0 $svc->unregister_hooks('Palimg');
80 0 return 1;
81 }
82  
83 # called when we are loaded/unloaded ... someday add some stats viewing
84 # commands here?
85 0 ▶sub load { return 1; }
Subroutine load: not called CC=1 SCAR=6.9
Pod undocumented
86 0 ▶sub unload { return 1; }
Subroutine unload: not called CC=1 SCAR=6.9
Pod undocumented
87  
88 ####### PALIMG START ###########################################################################
89 package PalImg;
90  
91 sub parse_hex_color
92 0 ▶{
Subroutine parse_hex_color: not called CC=1 SCAR=6.9
93 0 my $color = shift;
94 0 return [ map { hex(substr($color, $_, 2)) } (0,2,4) ];
95 }
96  
97 sub modify_file
98 0 ▶{
Subroutine modify_file: not called CC=17 SCAR=57.2
99 0 my ($data, $type, $palspec) = @_;
100  
101 # palette altering
102 0 my %pal_colors;
103 0 ▶ if (my $pals = $palspec) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pals = $palspec) 0 0 0
104 0 my $hx = "[0-9a-f]";
105 0 ▶ if ($pals =~ /^g($hx{2,2})($hx{6,6})($hx{2,2})($hx{6,6})$/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($pals =~ /^g(${hx}{2,2})(${hx}{6,6})(${hx}{2,2})(${hx}{6,6})$/) { } 0 0 0
elsif ($pals =~ /^t(${hx}{6,6})(${hx}{6,6})?$/) { } 0 0 0
elsif (length $pals > 42 or $pals =~ /[^0-9a-f]/) { } 0 0 0
106 # gradient from index $1, color $2, to index $3, color $4
107 0 my $from = hex($1);
108 0 my $to = hex($3);
109 0 ▶ return 404 if $from == $to;
Branch: 1 branch
BranchTrueFalseTotal
if $from == $to 0 0 0
110 0 my $fcolor = parse_hex_color($2);
111 0 my $tcolor = parse_hex_color($4);
112 0 ▶ if ($to < $from) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to < $from) 0 0 0
113 0 ($from, $to, $fcolor, $tcolor) =
114 ($to, $from, $tcolor, $fcolor);
115 }
116 0 ▶ for (my $i=$from; $i<=$to; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $to) 0 0 0
117 $pal_colors{$i} = [ map {
118 0 int($fcolor->[$_] +
119 ($tcolor->[$_] - $fcolor->[$_]) *
120 ($i-$from) / ($to-$from))
121 } (0..2) ];
122 }
123 } elsif ($pals =~ /^t($hx{6,6})($hx{6,6})?$/) {
124 # tint everything towards color
125 0 my ($t, $td) = ($1, $2);
126 0 $pal_colors{'tint'} = parse_hex_color($t);
127 0 ▶ $pal_colors{'tint_dark'} = $td ? parse_hex_color($td) : [0,0,0];
Branch: 1 branch
BranchTrueFalseTotal
$td ? : 0 0 0
128 ▶ } elsif (length($pals) > 42 || $pals =~ /[^0-9a-f]/) {
Condition0%0/3
length $pals > 42 or $pals =~ /[^0-9a-f]/
l!l&&r!l&&!r
000
MC/DC0%0/2
length $pals > 42 or $pals =~ /[^0-9a-f]/
length $pals > 42 $pals =~ /[^0-9a-f]/
Truth table0%0/3
A or B
A= length $pals > 42B= $pals =~ /[^0-9a-f]/
ABresult
1X1
011
000
129 0 return 404;
130 } else {
131 0 my $len = length($pals);
132 0 ▶ return 404 if $len % 7; # must be multiple of 7 chars
Branch: 1 branch
BranchTrueFalseTotal
if $len % 7 0 0 0
133 0 ▶ for (my $i = 0; $i < $len/7; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $len / 7) 0 0 0
134 0 my $palindex = hex(substr($pals, $i*7, 1));
135 0 $pal_colors{$palindex} = [
136 hex(substr($pals, $i*7+1, 2)),
137 hex(substr($pals, $i*7+3, 2)),
138 hex(substr($pals, $i*7+5, 2)),
139 substr($pals, $i*7+1, 6),
140 ];
141 }
142 }
143 }
144  
145 0 ▶ if (%pal_colors) {
Branch: 1 branch
BranchTrueFalseTotal
if (%pal_colors) 0 0 0
146 0 ▶ if ($type eq 'gif') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'gif') { } 0 0 0
elsif ($type eq 'png') { } 0 0 0
147 0 ▶ return 404 unless PaletteModify::new_gif_palette($data, \%pal_colors);
Branch: 1 branch
BranchTrueFalseTotal
unless PaletteModify::new_gif_palette($data, \%pal_colors) 0 0 0
148 } elsif ($type eq 'png') {
149 0 ▶ return 404 unless PaletteModify::new_png_palette($data, \%pal_colors);
Branch: 1 branch
BranchTrueFalseTotal
unless PaletteModify::new_png_palette($data, \%pal_colors) 0 0 0
150 }
151 }
152  
153 # success
154 0 return 0;
155 }
156 ####### PALIMG END #############################################################################
157  
158 ####### PALETTEMODIFY START ####################################################################
159 package PaletteModify;
160  
161 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=1.2
162 0 ▶ $PaletteModify::HAVE_CRC = eval "use String::CRC32 (); 1;";
Subroutine BEGIN: called CC=1 SCAR=1.2
163 }
164  
165 sub common_alter
166 0 ▶{
Subroutine common_alter: not called CC=7 SCAR=40.3
167 0 my ($palref, $table) = @_;
168 0 my $length = length $table;
169  
170 0 my $pal_size = $length / 3;
171  
172 # tinting image? if so, we're remaking the whole palette
173 0 ▶ if (my $tint = $palref->{'tint'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tint = $palref->{"tint"}) 0 0 0
174 0 my $dark = $palref->{'tint_dark'};
175 0 my $diff = [ map { $tint->[$_] - $dark->[$_] } (0..2) ];
176 0 $palref = {};
177 0 ▶ for (my $idx=0; $idx<$pal_size; $idx++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx < $pal_size) 0 0 0
178 0 for my $c (0..2) {
179 0 my $curr = ord(substr($table, $idx*3+$c));
180 0 my $p = \$palref->{$idx}->[$c];
181 0 $$p = int($dark->[$c] + $diff->[$c] * $curr / 255);
182 }
183 }
184 }
185  
186 0 ▶ while (my ($idx, $c) = each %$palref) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$palref) 0 0 0
187 0 ▶ next if $idx >= $pal_size;
Branch: 1 branch
BranchTrueFalseTotal
if $idx >= $pal_size 0 0 0
188 0 substr($table, $idx*3+$_, 1) = chr($c->[$_]) for (0..2);
189 }
190  
191 0 return $table;
192 }
193  
194 sub new_gif_palette
195 0 ▶{
Subroutine new_gif_palette: not called CC=3 SCAR=24.8
196 0 my ($data, $palref) = @_;
197  
198 # make sure we have data to operate on, or the substrs below die
199 0 ▶ return unless $$data;
Branch: 1 branch
BranchTrueFalseTotal
unless $$data 0 0 0
200  
201 # 13 bytes for magic + image info (size, color depth, etc)
202 # and then the global palette table (3*256)
203 0 my $header = substr($$data, 0, 13+3*256);
204  
205 # figure out how big global color table is (don't want to overwrite it)
206 0 my $pf = ord substr($header, 10, 1);
207 0 my $gct = 2 ** (($pf & 7) + 1); # last 3 bits of packaged fields
208  
209 # final sanity check for size so the substr below doesn't die
210 0 ▶ return unless length $header >= 13 + 3 * $gct;
Branch: 1 branch
BranchTrueFalseTotal
unless length $header >= 13 + 3 * $gct 0 0 0
211  
212 0 substr($header, 13, 3*$gct) = common_alter($palref, substr($header, 13, 3*$gct));
213 0 $$data = $header;
214 0 return 1;
215 }
216  
217 sub new_png_palette
218 0 ▶{
Subroutine new_png_palette: not called CC=9 SCAR=45.0
219 0 my ($data, $palref) = @_;
220  
221 # subroutine for reading data
222 0 my ($curidx, $maxlen) = (0, length $$data);
223 0 ▶ my $read = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
224 # put $_[1] data into scalar reference $_[0]
225 0 ▶ return undef if $_[1] + $curidx > $maxlen;
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] + $curidx > $maxlen 0 0 0
226 0 ${$_[0]} = substr($$data, $curidx, $_[1]);
227 0 $curidx += $_[1];
228 0 return length ${$_[0]};
229 0 };
230  
231 # without this module, we can't proceed.
232 0 ▶ return 0 unless $PaletteModify::HAVE_CRC;
Branch: 1 branch
BranchTrueFalseTotal
unless $PaletteModify::HAVE_CRC 0 0 0
233  
234 0 my $imgdata;
235  
236 # Validate PNG signature
237 0 my $png_sig = pack("H16", "89504E470D0A1A0A");
238 0 my $sig;
239 0 $read->(\$sig, 8);
240 0 ▶ return 0 unless $sig eq $png_sig;
Branch: 1 branch
BranchTrueFalseTotal
unless $sig eq $png_sig 0 0 0
241 0 $imgdata .= $sig;
242  
243 # Start reading in chunks
244 0 my ($length, $type) = (0, '');
245 0 ▶ while ($read->(\$length, 4)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$read(\$length, 4)) 0 0 0
246  
247 0 $imgdata .= $length;
248 0 $length = unpack("N", $length);
249 0 ▶ return 0 unless $read->(\$type, 4) == 4;
Branch: 1 branch
BranchTrueFalseTotal
unless &$read(\$type, 4) == 4 0 0 0
250 0 $imgdata .= $type;
251  
252 0 ▶ if ($type eq 'IHDR') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'IHDR') { } 0 0 0
elsif ($type eq 'PLTE') { } 0 0 0
253 0 my $header;
254 0 $read->(\$header, $length+4);
255 0 my ($width,$height,$depth,$color,$compression,
256 $filter,$interlace, $CRC)
257 = unpack("NNCCCCCN", $header);
258 0 ▶ return 0 unless $color == 3; # unpaletted image
Branch: 1 branch
BranchTrueFalseTotal
unless $color == 3 0 0 0
259 0 $imgdata .= $header;
260 } elsif ($type eq 'PLTE') {
261 # Finally, we can go to work
262 0 my $palettedata;
263 0 $read->(\$palettedata, $length);
264 0 $palettedata = common_alter($palref, $palettedata);
265 0 $imgdata .= $palettedata;
266  
267 # Skip old CRC
268 0 my $skip;
269 0 $read->(\$skip, 4);
270  
271 # Generate new CRC
272 0 my $crc = String::CRC32::crc32($type . $palettedata);
273 0 $crc = pack("N", $crc);
274  
275 0 $imgdata .= $crc;
276 0 $$data = $imgdata;
277 0 return 1;
278 } else {
279 0 my $skip;
280 # Skip rest of chunk and add to imgdata
281 # Number of bytes is +4 because of CRC
282 #
283 0 ▶ for (my $count=0; $count < $length + 4; $count++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count < $length + 4) 0 0 0
284 0 $read->(\$skip, 1);
285 0 $imgdata .= $skip;
286 }
287 }
288 }
289  
290 0 return 0;
291 }
292 ####### PALETTEMODIFY END ######################################################################
293  
294 1 1;
295  
296 __END__
« Perlbal/Plugin/AccessControl.pm ↑ Summary Perlbal/Service.pm »