| 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
MC/DC0%0/1
$opts{"rom_size"} ||= 8192
$opts{"rom_size"}
Truth table0%0/2
A
A= $opts{"rom_size"}
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 0 | my $image = Image::PNM->new($data); | |||||||||||||||||||||||||||
| 23 | 0 | ▶ | if ($image->width % 8 || $image->height % 8) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$image->width % 8 or $image->height % 8
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | ▶ | if ($idx > 4) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||