Image.pm

filter: Statementstmt 31.0% 32 / 103 Branchbran 32.3% 11 / 34 Conditioncond 13.5% 10 / 74 MC/DCmcdc 8.1% 3 / 37 Subroutinesub 23.8% 5 / 21 Podpod 82.3% 14 / 17 totaltotal 26.2% 75 / 286 CC 55 SCAR 41.4
CC
55
Cov
25%
change_from_player_colors
182.0
change_to_player_colors
110.0
guess_tilesize
56.0
CRAP 62.6
↑ Summary Pak.pm »
1 package Games::Simutrans::Image;
2  
3 # An abstraction of a Simutrans PNG image file, as might be attached to a .dat file.
4 # Note, this is at a lower level than the .dat attchment information -- this is just the
5 # image alone.
6  
7 2 ▶use Mojo::Base -base, -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 ################
10 # COLORS
11 ################
12  
13 0 ▶sub _player_colors ($type = 'std') {
Subroutine _player_colors: not called CC=2 SCAR=17.9
14 0 state $player_colors = {std => ['#244b67', '#395e7c', '#4c7191', '#6084a7', '#7497bd', '#88abd3', '#9cbee9', '#b0d2ff'],
15 alt => ['#7b5803', '#8e6f04', '#a18605', '#b49d07', '#c6b408', '#d9cb0a', '#ece20b', '#fff90d'],
16 menu => ['#6b6b6b', '#9b9b9b', '#b3b3b3', '#c9c9c9', '#dfdfdf'],
17 day => ['#7f9bf1', '#ffff53', '#ff211d', '#01dd01', '#4d4d4d', '#57656f', '#c1b1d1', '#e3e3ff', '#ff017f', '#0101ff'],
18 night => ['#80c3d3', '#ffff53', '#ff211d', '#01dd01', '#d3c380', '#d3c380', '#d3c380', '#ffffe3', '#ff017f', '#0101ff'],
19 };
20 0 return $player_colors->{$type};
21 }
22  
23 0 ▶sub _player_color ($type = 'std', $index = 0) {
Subroutine _player_color: not called CC=3 SCAR=24.8
24 0 return _player_colors($type)->[$index];
25 }
26  
27 0 ▶sub player_color ($type, $index) {
Subroutine player_color: not called CC=1 SCAR=6.9
Pod documented
28 0 return Imager::Color->new(web => _player_color($type, $index));
29 }
30  
31 ################
32  
33 2 my @_mapcolor_rgb = ( # from simgraph16.cc
34 36, 75, 103,
35 57, 94, 124,
36 76, 113, 145,
37 96, 132, 167,
38 116, 151, 189,
39 136, 171, 211,
40 156, 190, 233,
41 176, 210, 255,
42 88, 88, 88,
43 107, 107, 107,
44 125, 125, 125,
45 144, 144, 144,
46 162, 162, 162,
47 181, 181, 181,
48 200, 200, 200,
49 219, 219, 219,
50 17, 55, 133,
51 27, 71, 150,
52 37, 86, 167,
53 48, 102, 185,
54 58, 117, 202,
55 69, 133, 220,
56 79, 149, 237,
57 90, 165, 255,
58 123, 88, 3,
59 142, 111, 4,
60 161, 134, 5,
61 180, 157, 7,
62 198, 180, 8,
63 217, 203, 10,
64 236, 226, 11,
65 255, 249, 13,
66 86, 32, 14,
67 110, 40, 16,
68 134, 48, 18,
69 158, 57, 20,
70 182, 65, 22,
71 206, 74, 24,
72 230, 82, 26,
73 255, 91, 28,
74 34, 59, 10,
75 44, 80, 14,
76 53, 101, 18,
77 63, 122, 22,
78 77, 143, 29,
79 92, 164, 37,
80 106, 185, 44,
81 121, 207, 52,
82 0, 86, 78,
83 0, 108, 98,
84 0, 130, 118,
85 0, 152, 138,
86 0, 174, 158,
87 0, 196, 178,
88 0, 218, 198,
89 0, 241, 219,
90 74, 7, 122,
91 95, 21, 139,
92 116, 37, 156,
93 138, 53, 173,
94 160, 69, 191,
95 181, 85, 208,
96 203, 101, 225,
97 225, 117, 243,
98 59, 41, 0,
99 83, 55, 0,
100 107, 69, 0,
101 131, 84, 0,
102 155, 98, 0,
103 179, 113, 0,
104 203, 128, 0,
105 227, 143, 0,
106 87, 0, 43,
107 111, 11, 69,
108 135, 28, 92,
109 159, 45, 115,
110 183, 62, 138,
111 230, 74, 174,
112 245, 121, 194,
113 255, 156, 209,
114 20, 48, 10,
115 44, 74, 28,
116 68, 99, 45,
117 93, 124, 62,
118 118, 149, 79,
119 143, 174, 96,
120 168, 199, 113,
121 193, 225, 130,
122 54, 19, 29,
123 82, 44, 44,
124 110, 69, 58,
125 139, 95, 72,
126 168, 121, 86,
127 197, 147, 101,
128 226, 173, 115,
129 255, 199, 130,
130 8, 11, 100,
131 14, 22, 116,
132 20, 33, 139,
133 26, 44, 162,
134 41, 74, 185,
135 57, 104, 208,
136 76, 132, 231,
137 96, 160, 255,
138 43, 30, 46,
139 68, 50, 85,
140 93, 70, 110,
141 118, 91, 130,
142 143, 111, 170,
143 168, 132, 190,
144 193, 153, 210,
145 219, 174, 230,
146 63, 18, 12,
147 90, 38, 30,
148 117, 58, 42,
149 145, 78, 55,
150 172, 98, 67,
151 200, 118, 80,
152 227, 138, 92,
153 255, 159, 105,
154 11, 68, 30,
155 33, 94, 56,
156 54, 120, 81,
157 76, 147, 106,
158 98, 174, 131,
159 120, 201, 156,
160 142, 228, 181,
161 164, 255, 207,
162 64, 0, 0,
163 96, 0, 0,
164 128, 0, 0,
165 192, 0, 0,
166 255, 0, 0,
167 255, 64, 64,
168 255, 96, 96,
169 255, 128, 128,
170 0, 128, 0,
171 0, 196, 0,
172 0, 225, 0,
173 0, 240, 0,
174 0, 255, 0,
175 64, 255, 64,
176 94, 255, 94,
177 128, 255, 128,
178 0, 0, 128,
179 0, 0, 192,
180 0, 0, 224,
181 0, 0, 255,
182 0, 64, 255,
183 0, 94, 255,
184 0, 106, 255,
185 0, 128, 255,
186 128, 64, 0,
187 193, 97, 0,
188 215, 107, 0,
189 255, 128, 0,
190 255, 128, 0,
191 255, 149, 43,
192 255, 170, 85,
193 255, 193, 132,
194 8, 52, 0,
195 16, 64, 0,
196 32, 80, 4,
197 48, 96, 4,
198 64, 112, 12,
199 84, 132, 20,
200 104, 148, 28,
201 128, 168, 44,
202 164, 164, 0,
203 193, 193, 0,
204 215, 215, 0,
205 255, 255, 0,
206 255, 255, 32,
207 255, 255, 64,
208 255, 255, 128,
209 255, 255, 172,
210 32, 4, 0,
211 64, 20, 8,
212 84, 28, 16,
213 108, 44, 28,
214 128, 56, 40,
215 148, 72, 56,
216 168, 92, 76,
217 184, 108, 88,
218 64, 0, 0,
219 96, 8, 0,
220 112, 16, 0,
221 120, 32, 8,
222 138, 64, 16,
223 156, 72, 32,
224 174, 96, 48,
225 192, 128, 64,
226 32, 32, 0,
227 64, 64, 0,
228 96, 96, 0,
229 128, 128, 0,
230 144, 144, 0,
231 172, 172, 0,
232 192, 192, 0,
233 224, 224, 0,
234 64, 96, 8,
235 80, 108, 32,
236 96, 120, 48,
237 112, 144, 56,
238 128, 172, 64,
239 150, 210, 68,
240 172, 238, 80,
241 192, 255, 96,
242 32, 32, 32,
243 48, 48, 48,
244 64, 64, 64,
245 80, 80, 80,
246 96, 96, 96,
247 172, 172, 172,
248 236, 236, 236,
249 255, 255, 255,
250 41, 41, 54,
251 60, 45, 70,
252 75, 62, 108,
253 95, 77, 136,
254 113, 105, 150,
255 135, 120, 176,
256 165, 145, 218,
257 198, 191, 232,
258 );
259  
260 2 my @_mapcolor;
261  
262 2 ▶while (scalar @_mapcolor_rgb) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_mapcolor_rgb) 448 2 450
263 448 push @_mapcolor, Imager::Color->new(shift @_mapcolor_rgb, shift @_mapcolor_rgb, shift @_mapcolor_rgb);
264 }
265  
266 1 ▶sub mapcolor ($index) {
Subroutine mapcolor: called CC=1 SCAR=0.0
Pod documented
267 1 return $_mapcolor[$index];
268 }
269  
270 ################
271 # COLOR PIXEL MANIPULATION
272 #
273 # These use Imager::transform2 (see Imager::Engines manpage) and are
274 # inspired by a little program by Tony Cook (tonyc),
275 # http://www.perlmonks.org/?node_id=497355
276 ################
277  
278 0 ▶sub replace_rgb ($self, $constants) {
Subroutine replace_rgb: not called CC=1 SCAR=6.9
Pod documented
279 # Replace one (r,g,b) with another
280  
281 0 my $rpnexpr = <<'EOS';
282 x y getp1 !pix
283 @pix red !pred @pred from_red eq
284 @pix green !pgreen @pgreen from_green eq and
285 @pix blue !pblue @pblue from_blue eq and !match
286 @match rr @pred if
287 @match gg @pgreen if
288 @match bb @pblue if
289 @pix alpha
290 rgba
291 EOS
292  
293 0 $constants->@{qw(from_red from_green from_blue from_alpha)}=$constants->{from_color}->rgba;
294 0 $constants->@{qw(rr gg bb aa)}=$constants->{to_color}->rgba;
295  
296 0 $self->image( Imager::transform2({ rpnexpr => $rpnexpr,
297 constants => $constants,
298 channels => 4},
299 $self->image));
300 }
301  
302 0 ▶sub replace_hue ($self, $constants) {
Subroutine replace_hue: not called CC=1 SCAR=6.9
Pod documented
303  
304 0 my $rpnexpr = <<'EOS';
305 x y getp1 !pix
306 @pix hue !phue @phue from_hue from_hue_thresh + lt @phue from_hue from_hue_thresh - gt and new_hue @phue if
307 @pix sat
308 @pix value
309 @pix alpha
310 hsva
311 EOS
312  
313 0 $self->image( Imager::transform2({ rpnexpr => $rpnexpr,
314 constants => $constants,
315 channels => 4},
316 $self->image));
317 }
318  
319 0 ▶sub replace_hue_sat ($self, $constants) {
Subroutine replace_hue_sat: not called CC=1 SCAR=6.9
Pod documented
320  
321 0 my $rpnexpr = <<'EOS';
322 x y getp1 !pix
323 @pix hue !phue @phue from_hue from_hue_thresh + lt @phue from_hue from_hue_thresh - gt and !match
324 @match new_hue @phue if
325 @match new_sat @pix sat if
326 @pix value
327 @pix alpha
328 hsva
329 EOS
330  
331 0 $self->image( Imager::transform2({ rpnexpr => $rpnexpr,
332 constants => $constants,
333 channels => 4},
334 $self->image));
335 }
336  
337 0 ▶sub replace_color_range ($self, $constants) {
Subroutine replace_color_range: not called CC=1 SCAR=6.9
Pod documented
338  
339 0 my $rpnexpr = <<'EOS';
340 x y getp1 !pix
341 @pix hue !phue
342 @pix value !pval
343  
344 @phue from_hue from_hue_thresh + le @phue from_hue from_hue_thresh - ge and
345 @pval from_value from_value_thresh + le @pval from_value from_value_thresh - ge and
346 @pix sat 0.15 gt @pix value 0.15 gt and
347 and and
348 rr gg bb rgb @pix if
349  
350 EOS
351  
352 0 $constants->@{qw(rr gg bb aa)} = $constants->{to_color}->rgba;
353  
354 0 $self->image( Imager::transform2({ rpnexpr => $rpnexpr,
355 constants => $constants,
356 channels => 4},
357 $self->image));
358 }
359  
360 ################
361 # Change an object's image to/from a single player/alternate color.
362 # These modify the object's image (unlike the above 'replace_' subs)
363 ################
364  
365 0 ▶sub change_to_player_color ($self, $colortype, $value, $opts) {
Subroutine change_to_player_color: not called CC=1 SCAR=6.9
Pod undocumented
366 # $colortype should be 'std' or 'alt' for standard / alternate player color
367 # $value should be 0..7 for the level
368  
369 0 $self->replace_color_range( {%{$opts},
370 to_color => player_color($colortype, $value)
371 });
372 }
373  
374 0 ▶sub change_from_player_color ($self, $colortype, $value, $opts) {
Subroutine change_from_player_color: not called CC=1 SCAR=6.9
Pod undocumented
375 # $colortype should be 'std' or 'alt' for standard / alternate player color
376 # $value should be 0..7 for the level
377  
378 0 $self->replace_rgb( {%{$opts},
379 from_color => player_color($colortype, $value),
380 });
381 }
382  
383 ################
384 # Change a color range to/from player/alternate colors
385 ################
386  
387 0 ▶sub change_to_player_colors ($self, $opts = {}) {
Subroutine change_to_player_colors: not called CC=10 SCAR=47.0
Pod documented
388  
389 my ($colortype,
390 $hue, $hue_threshold,
391 $levels, $offset, $level_offset) =
392 ( $opts->{type} // $opts->{colortype} ,
393 $opts->{hue},
394 $opts->{hue_threshold} // $opts->{hue_thresh} // $opts->{hue_t},
395 $opts->{levels} // 8, # Normally, use all eight player/alternate colors
396 $opts->{offset} // 0, # NOTE: offset+level must be ≤ 8
397 0 ▶ $opts->{level_offset} // $opts->{level_o} // 0.1, # By default, do not modify values very near black
Condition0%0/18
$opts->{"type"} // $opts->{"colortype"}
l!l&&r!l&&!r
000
($opts->{"hue_threshold"} // $opts->{"hue_thresh"}) // $opts->{"hue_t"}
l!l&&r!l&&!r
000
$opts->{"hue_threshold"} // $opts->{"hue_thresh"}
l!l&&r!l&&!r
000
$opts->{"levels"} // 8
l!l
00
$opts->{"offset"} // 0
l!l
00
($opts->{"level_offset"} // $opts->{"level_o"}) // "0.1"
l!l
00
$opts->{"level_offset"} // $opts->{"level_o"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts->{"type"} // $opts->{"colortype"}
$opts->{"type"} $opts->{"colortype"}
MC/DC0%0/3
($opts->{"hue_threshold"} // $opts->{"hue_thresh"}) // $opts->{"hue_t"}
$opts->{"hue_threshold"} $opts->{"hue_thresh"} $opts->{"hue_t"}
MC/DC0%0/1
$opts->{"levels"} // 8
$opts->{"levels"}
MC/DC0%0/1
$opts->{"offset"} // 0
$opts->{"offset"}
MC/DC0%0/2
($opts->{"level_offset"} // $opts->{"level_o"}) // "0.1"
$opts->{"level_offset"} $opts->{"level_o"}
Truth table0%0/3
A // B
A= $opts->{"type"}B= $opts->{"colortype"}
ABresult
1X1
011
000
Truth table0%0/4
A // B // C
A= $opts->{"hue_threshold"}B= $opts->{"hue_thresh"}C= $opts->{"hue_t"}
ABCresult
1XX1
01X1
0011
0000
Truth table0%0/2
A
A= $opts->{"levels"}
Aresult
11
00
Truth table0%0/2
A
A= $opts->{"offset"}
Aresult
11
00
Truth table0%0/3
A // B
A= $opts->{"level_offset"}B= $opts->{"level_o"}
ABresult
1X1
011
000
398 # NOTE: Could also introduce a gamma parameter
399 );
400  
401 0 my $v_threshold = (1-$level_offset)/($levels*2); # for levels=2, this is 1/4
402 0 foreach my $value ( $offset .. ($offset + $levels) - 1 ) { # skipping 0 so black does not get modified!
403 # Replace gradations of values of the given hue, with special player colors
404 0 my $v = ($value * (1 - $level_offset) / $levels) + $level_offset + $v_threshold; # for levels=2, this will be 1/4 and then 3/4.
405 0 $self->change_to_player_color($colortype, $value, {
406 from_hue => $hue,
407 from_hue_thresh => $hue_threshold,
408 from_value => $v,
409 from_value_thresh => $v_threshold,
410 });
411 }
412  
413 }
414  
415 0 ▶sub change_from_player_colors ($self, $opts = {}) {
Subroutine change_from_player_colors: not called CC=13 SCAR=52.0
Pod undocumented
416 # Replace a set of eight player colors to gradations of the given hue.
417 # Replacement could be a hue or a mapcolor.
418 my ($colortype,
419 $hue, $saturation, $offset, $levels,
420 $mapcolor) =
421 ( $opts->{type} // $opts->{colortype} ,
422 $opts->{hue},
423 $opts->{sat} // $opts->{saturation} // 1,
424 $opts->{offset} // 0,
425 $opts->{levels} // 8,
426 $opts->{map} // $opts->{mapcolor},
427 0 ▶ );
Condition0%0/15
$opts->{"type"} // $opts->{"colortype"}
l!l&&r!l&&!r
000
($opts->{"sat"} // $opts->{"saturation"}) // 1
l!l
00
$opts->{"sat"} // $opts->{"saturation"}
l!l&&r!l&&!r
000
$opts->{"offset"} // 0
l!l
00
$opts->{"levels"} // 8
l!l
00
$opts->{"map"} // $opts->{"mapcolor"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts->{"type"} // $opts->{"colortype"}
$opts->{"type"} $opts->{"colortype"}
MC/DC0%0/2
($opts->{"sat"} // $opts->{"saturation"}) // 1
$opts->{"sat"} $opts->{"saturation"}
MC/DC0%0/1
$opts->{"offset"} // 0
$opts->{"offset"}
MC/DC0%0/1
$opts->{"levels"} // 8
$opts->{"levels"}
MC/DC0%0/2
$opts->{"map"} // $opts->{"mapcolor"}
$opts->{"map"} $opts->{"mapcolor"}
Truth table0%0/3
A // B
A= $opts->{"type"}B= $opts->{"colortype"}
ABresult
1X1
011
000
Truth table0%0/3
A // B
A= $opts->{"sat"}B= $opts->{"saturation"}
ABresult
1X1
011
000
Truth table0%0/2
A
A= $opts->{"offset"}
Aresult
11
00
Truth table0%0/2
A
A= $opts->{"levels"}
Aresult
11
00
Truth table0%0/3
A // B
A= $opts->{"map"}B= $opts->{"mapcolor"}
ABresult
1X1
011
000
428 0 my @to_colors;
429 0 ▶ if (defined $mapcolor) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mapcolor) { } 0 0 0
430 # change to a mapcolor scale
431 0 my $start_mapcolor = $mapcolor & 0xf8; # mask off bottom 3 bits
432 0 for my $i ( $offset .. ($offset + $levels) - 1) {
433 0 push @to_colors, $_mapcolor[$start_mapcolor + $i];
434 }
435 } else {
436 # TODO: calculate an array of output colors based on fixed hue
437 # and varying lightness/saturation ...(how to improve this
438 # basic algorithm?)
439 0 ▶ $saturation //= 1; # Saturation given as 0..1
Condition0%0/2
$saturation //= 1
l!l
00
MC/DC0%0/1
$saturation //= 1
$saturation
Truth table0%0/2
A
A= $saturation
Aresult
11
00
440 0 for my $i ( $offset .. ($offset + $levels) - 1 ) {
441 0 push @to_colors, Imager::Color->new( hue => $hue, s => $saturation , v => ($i+1) / 8 );
442 }
443 }
444 0 for my $i ( 0 .. scalar @to_colors - 1) {
445 0 $self->change_from_player_color( $colortype, $offset + $i, { to_color => $to_colors[$i] } );
446 }
447  
448 }
449  
450 ################
451 # IMAGE FILE HANDLING
452 ################
453  
454 # Note that the .dat files do not specify the tilesize. Rather, that
455 # is done in the makefiles by calling makeobj with the tilesize as a
456 # parameter. Furthermore, many paksets have multiple tilesizes, and
457 # although many give the tilesize in a subdirectory or filename, there
458 # is no regularity nor requirement to do so.
459 #
460 # However, we guess the tilesize by evaluating all the datfiles that
461 # reference each image file, and then making the assumption that an
462 # imagefile will be less than twice the maximum x,y tile reference to
463 # it. Given that tiles are always square (32x32, 128x128) and that we
464 # look at both x and y dimensions in this calculation (even if a
465 # particular image has extra width or height, that is almost always in
466 # one direction, not both, for otherwise 3/4 of the image would be
467 # unused), this should result in a high success rate.
468  
469 2 ▶use Imager;
Subroutine BEGIN: called CC=1 SCAR=0.0
470  
471 2 has 'file'; # Filename
472 2 has 'image'; # Imager object
473 2 has 'modified'; # File modification time (perl's -M)
474 2 has 'width'; # from image
475 2 has 'height';
476 2 has 'xmax'; # Retained while reading the collection of .dat files
477 2 has 'ymax';
478 2 has 'tilesize'; # Imputed square grid size (calculated from aggregate of .dat files); computed at read() if xmax, ymax are known.
479 2 has 'is_transparent'; # set when we convert heritage transparent-equivalent color to actual transparency
480  
481 16 ▶sub record_grid_coordinate ($self, $x, $y) {
Subroutine record_grid_coordinate: called CC=5 SCAR=16.1
Pod documented
482 # As each dat file is scanned, we make a record of each grid coordinate that was used.
483  
484 16 ▶ $self->xmax($x) if $x > ($self->xmax // -1);
Branch: 1 branch
BranchTrueFalseTotal
if $x > ($self->xmax // -1) 6 10 16
Condition100%2/2
$self->xmax // -1
l!l
124
MC/DC100%1/1
$self->xmax // -1
$self->xmax
Truth table100%2/2
A
A= $self->xmax
Aresult
11
00
485 16 ▶ $self->ymax($y) if $y > ($self->ymax // -1);
Branch: 1 branch
BranchTrueFalseTotal
if $y > ($self->ymax // -1) 4 12 16
Condition100%2/2
$self->ymax // -1
l!l
124
MC/DC100%1/1
$self->ymax // -1
$self->ymax
Truth table100%2/2
A
A= $self->ymax
Aresult
11
00
486  
487 }
488  
489 0 ▶sub guess_tilesize ($self) {
Subroutine guess_tilesize: not called CC=7 SCAR=40.3
Pod documented
490 0 ▶ if (!defined $self->tilesize && defined $self->width && defined $self->height && defined $self->xmax && defined $self->ymax) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->tilesize and defined $self->width and defined $self->height and defined $self->xmax and defined $self->ymax) 0 0 0
Condition0%0/12
not defined $self->tilesize and defined $self->width and defined $self->height and defined $self->xmax and defined $self->ymax
!ll&&!rl&&r
000
not defined $self->tilesize and defined $self->width and defined $self->height and defined $self->xmax
!ll&&!rl&&r
000
not defined $self->tilesize and defined $self->width and defined $self->height
!ll&&!rl&&r
000
not defined $self->tilesize and defined $self->width
!ll&&!rl&&r
000
MC/DC0%0/5
not defined $self->tilesize and defined $self->width and defined $self->height and defined $self->xmax and defined $self->ymax
not defined $self->tilesize defined $self->width defined $self->height defined $self->xmax defined $self->ymax
Truth table0%0/6
not(A) and B and C and D and E
A= not defined $self->tilesizeB= defined $self->widthC= defined $self->heightD= defined $self->xmaxE= defined $self->ymax
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
491 0 my @guess_tile_size = (
492 ($self->width() / ($self->xmax + 1)) & ~31,
493 ($self->height() / ($self->ymax + 1)) & ~31 );
494 # It's almost certainly the smaller of the two.
495 0 my $tile_size = $guess_tile_size[0];
496 0 ▶ $tile_size = $guess_tile_size[1] if $tile_size > $guess_tile_size[1]; # Choose smaller
Branch: 1 branch
BranchTrueFalseTotal
if $tile_size > $guess_tile_size[1] 0 0 0
497 0 $self->tilesize ( $tile_size );
498 }
499 0 return $self->tilesize;
500 }
501  
502 5 ▶sub read ($self, $params = {}) {
Subroutine read: called CC=12 SCAR=33.6
Pod documented
503 5 ▶ my $file = defined $params->{file} ? $params->{file} : $self->file();
Branch: 1 branch
BranchTrueFalseTotal
defined $params->{'file'} ? : 0 5 5
504 5 ▶ return undef unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 5 5
505  
506 5 ▶ if ( (!defined $self->modified) || ( -M $file != $self->modified ) || ($params->{save}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->modified or -M $file != $self->modified or $params->{"save"}) 5 0 5
Condition33%2/6
not defined $self->modified or -M $file != $self->modified or $params->{"save"}
l!l&&r!l&&!r
500
not defined $self->modified or -M $file != $self->modified
l!l&&r!l&&!r
500
MC/DC0%0/3
not defined $self->modified or -M $file != $self->modified or $params->{"save"}
not defined $self->modified -M $file != $self->modified $params->{"save"}
Truth table25%1/4
not(A) or B or C
A= not defined $self->modifiedB= -M $file != $self->modifiedC= $params->{"save"}
ABCresult
1XX1
01X1
0011
0000
507 # Haven't read yet, or was modified
508  
509 5 ▶ my $image = $self->image // Imager->new();
Condition33%1/3
$self->image // "Imager"->new
l!l&&r!l&&!r
050
MC/DC0%0/2
$self->image // "Imager"->new
$self->image "Imager"->new
Truth table33%1/3
A // B
A= $self->imageB= "Imager"->new
ABresult
1X1
011
000
510 # NOTE: Older Simutrans PNG are afflicted with 'pHYs out of place' errors,
511 # which may be safely ignored, thus the flag below.
512 5 $image->read(file => $file, png_ignore_benign_errors => 1);
513  
514 # For each found image file,
515 # If the file exists, open it with Imager
516 # We know that Simutrans image objects are always square, and always have a size a multiple of 32
517 # Some images may have extra graphical bits (explanatory text) to one side or the bottom,
518 # but we assume an image file will be more than one half used, so we can compute the
519 # tile size…
520 #
521 # This must be done for each image individually, as many paksets have icons, hull/hold
522 # images, and others of varying sizes. A 128 pakset might have airplanes or ships at
523 # 256 size and icons at 64 or 32 size.
524  
525 5 ▶ if (defined $image && $image->getwidth()) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $image and $image->getwidth) 0 5 5
Condition33%1/3
defined $image and $image->getwidth
!ll&&!rl&&r
050
MC/DC0%0/2
defined $image and $image->getwidth
defined $image $image->getwidth
Truth table33%1/3
A and B
A= defined $imageB= $image->getwidth
ABresult
0X0
100
111
526 0 $self->file($file);
527 0 $self->modified(-M $file);
528 0 $self->width ($image->getwidth() );
529 0 $self->height($image->getheight());
530 # Check for heritage pseudo-transparency by examining farthest lower-left pixel, which
531 # due to Simutrans's standard masking should be either transparent, or the heritage pseudo-transparent color.
532 0 $self->is_transparent( ($image->getpixel( x => 0, y => $image->getheight() - 1)->rgba)[3] == 0);
533 0 $self->guess_tilesize;
534 }
535  
536 5 ▶ $self->image($image) if $params->{save} // 1; # can set save=0 to discard the image
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"save"} // 1 5 0 5
Condition100%2/2
$params->{"save"} // 1
l!l
14
MC/DC100%1/1
$params->{"save"} // 1
$params->{"save"}
Truth table100%2/2
A
A= $params->{"save"}
Aresult
11
00
537 }
538 5 1;
539 }
540  
541 0 ▶sub write ($self, $filename) {
Subroutine write: not called CC=1 SCAR=6.9
Pod documented
542 0 $self->image->write( file => $filename );
543 }
544  
545 0 ▶sub flush ($self) {
Subroutine flush: not called CC=1 SCAR=6.9
Pod documented
546 # Free the memory associated with the image
547 0 $self->modified(undef);
548 0 undef $self->{image};
549 0 $self;
550 }
551  
552 0 ▶sub make_transparent ($self) {
Subroutine make_transparent: not called CC=6 SCAR=37.4
Pod documented
553 0 ▶ return if $self->is_transparent;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_transparent 0 0 0
554 0 ▶ $self->read({save => 1}) unless defined $self->image;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->image 0 0 0
555 0 ▶ if (defined $self->image) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->image) 0 0 0
556 0 my $temp_img = Imager->new(xsize=>$self->width, ysize=>$self->height, channels=>4);
557 0 ▶ return unless defined $temp_img;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $temp_img 0 0 0
558 0 $temp_img->box(filled=>1, color=>Imager::Color->new(231,255,255));
559 0 my $new_img = $temp_img->difference(other=>$self->image);
560 0 ▶ return unless $new_img;
Branch: 1 branch
BranchTrueFalseTotal
unless $new_img 0 0 0
561 0 undef $self->{image};
562 0 $self->image($new_img);
563 0 $self->is_transparent(1);
564 }
565 0 $self;
566 }
567  
568 0 ▶sub subimage ($self, $x, $y) {
Subroutine subimage: not called CC=5 SCAR=34.0
Pod documented
569 # Once the tilesize of an image is known, we can slice a subimage
570 # from it, given its (x,y). Returns an Imager object.
571 0 $self->guess_tilesize; # no-op if already known
572 0 ▶ return undef unless defined $x && defined $y && defined $self->image && defined $self->tilesize;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $x and defined $y and defined $self->image and defined $self->tilesize 0 0 0
Condition0%0/9
defined $x and defined $y and defined $self->image and defined $self->tilesize
!ll&&!rl&&r
000
defined $x and defined $y and defined $self->image
!ll&&!rl&&r
000
defined $x and defined $y
!ll&&!rl&&r
000
MC/DC0%0/4
defined $x and defined $y and defined $self->image and defined $self->tilesize
defined $x defined $y defined $self->image defined $self->tilesize
Truth table0%0/5
A and B and C and D
A= defined $xB= defined $yC= defined $self->imageD= defined $self->tilesize
ABCDresult
0XXX0
10XX0
110X0
11100
11111
573 0 return $self->image->copy->crop(left=>$x*$self->tilesize, top=>$y*$self->tilesize,width=>$self->tilesize,height=>$self->tilesize);
574 }
575  
576 2 1;
577  
578 __END__
↑ Summary Pak.pm »