blib/lib/Games/NES/SpriteMaker.pm

filter: Statementstmt 29.6% 16 / 54 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 62.5% 5 / 8 Podpod 100% 1 / 1 totaltotal 27.1% 22 / 81 CC 16 SCAR 44.8
CC
16
Cov
23%
image_to_sprite
156.0
_get_palette_colors
30.0
_color_key
2.0
CRAP 88.2
↑ Summary
1 package Games::NES::SpriteMaker;
2 BEGIN {
3 1 ▶ $Games::NES::SpriteMaker::AUTHORITY = 'cpan:DOY';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 }
5 1 $Games::NES::SpriteMaker::VERSION = '0.01';
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 # ABSTRACT: manipulate PNM files and generate NES sprite data from them
9  
10 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 our @EXPORT_OK = ('image_to_sprite');
12  
13 1 ▶use Image::PNM;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16  
17 sub image_to_sprite {
18 0 ▶ my ($data, %opts) = @_;
Subroutine image_to_sprite: not called CC=12 SCAR=50.5
Pod documented
19  
20 0 ▶ $opts{rom_size} ||= 8192;
Condition0%0/2
$opts{"rom_size"} ||= 8192
l!l
00
MC/DC0%0/1
$opts{"rom_size"} ||= 8192
$opts{"rom_size"}
Truth table0%0/2
A
A= $opts{"rom_size"}
Aresult
11
00
21  
22 0 my $image = Image::PNM->new($data);
23 0 ▶ if ($image->width % 8 || $image->height % 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($image->width % 8 or $image->height % 8) 0 0 0
Condition0%0/3
$image->width % 8 or $image->height % 8
l!l&&r!l&&!r
000
MC/DC0%0/2
$image->width % 8 or $image->height % 8
$image->width % 8 $image->height % 8
Truth table0%0/3
A or B
A= $image->width % 8B= $image->height % 8
ABresult
1X1
011
000
24 0 die "Sprite collections must be tiles of 8x8 sprites (not "
25 . $image->width . "x" . $image->height . ")";
26 }
27 0 my %colors = _get_palette_colors($image);
28  
29 0 my $sprite_x = $image->width / 8;
30 0 my $sprite_y = $image->height / 8;
31  
32 0 my $bytes = '';
33 0 for my $base_y (0..$sprite_y-1) {
34 0 for my $base_x (0..$sprite_x-1) {
35 0 for my $pixel_y ($base_y*8..$base_y*8 + 7) {
36 0 my $bits;
37 0 for my $pixel_x ($base_x*8..$base_x*8 + 7) {
38 0 my $pixel = $image->raw_pixel($pixel_y, $pixel_x);
39 0 my $pixel_value = $colors{_color_key($pixel)};
40 0 ▶ $bits .= $pixel_value & 0x01 ? "1" : "0";
Branch: 1 branch
BranchTrueFalseTotal
$pixel_value & 1 ? : 0 0 0
41 }
42 0 $bytes .= pack("C", oct("0b$bits"));
43 }
44 0 for my $pixel_y ($base_y*8..$base_y*8 + 7) {
45 0 my $bits;
46 0 for my $pixel_x ($base_x*8..$base_x*8 + 7) {
47 0 my $pixel = $image->raw_pixel($pixel_y, $pixel_x);
48 0 my $pixel_value = $colors{_color_key($pixel)};
49 0 ▶ $bits .= $pixel_value & 0x02 ? "1" : "0";
Branch: 1 branch
BranchTrueFalseTotal
$pixel_value & 2 ? : 0 0 0
50 }
51 0 $bytes .= pack("C", oct("0b$bits"));
52 }
53 }
54 }
55  
56 0 return $bytes . ("\x00" x ($opts{rom_size} - length($bytes)));
57 }
58  
59 sub _get_palette_colors {
60 0 ▶ my ($image) = @_;
Subroutine _get_palette_colors: not called CC=5 SCAR=34.0
61  
62 0 my %unique_values = ("1;1;1" => 0);
63 0 my $idx = 1;
64 0 for my $row (0..$image->height - 1) {
65 0 for my $col (0..$image->width - 1) {
66 0 my $pixel = $image->raw_pixel($row, $col);
67 $unique_values{_color_key($pixel)} = $idx++
68 0 ▶ unless defined $unique_values{_color_key($pixel)};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $unique_values{_color_key($pixel)} 0 0 0
69 }
70 }
71  
72 0 ▶ if ($idx > 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx > 4) 0 0 0
73 0 die "Sprites can only use four colors";
74 }
75  
76 0 return %unique_values;
77 }
78  
79 sub _color_key {
80 0 ▶ my ($pixel) = @_;
Subroutine _color_key: not called CC=1 SCAR=6.9
81 0 return "$pixel->[0];$pixel->[1];$pixel->[2]";
82 }
83  
84  
85 1 1;
86  
87 __END__
↑ Summary