summaryrefslogtreecommitdiffstats
path: root/bin/pnm2chr
blob: a0754ab043feb3e21ec63b4617f0959d2295173c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env perl
use strict;
use warnings;
# PODNAME: pnm2chr
# ABSTRACT: converts PNM files to NES sprite data (.chr files)

use Games::NES::SpriteMaker 'image_to_sprite';

=head1 SYNOPSIS

  pnm2chr input.pgm # generates input.chr
  pnm2chr < input.pgm > output.chr

=head1 DESCRIPTION

This is a simple wrapper around L<Games::NES::SpriteMaker> to convert existing
files from PNM format to CHR-ROM format. If you pass a filename as an argument,
it will create a new file in the same directory with the same name except
replacing the extension with C<.chr>, otherwise it will read PNM data from
stdin and write CHR data to stdout.

=cut

my $data;
if (@ARGV) {
    $data = $ARGV[0];
}
else {
    $data = \(do { local $/; <> });
}

my $sprite_data = image_to_sprite($data);

if (@ARGV) {
    (my $outfile = $data) =~ s/\..*$/.chr/;
    open my $fh, '>', $outfile
        or die "Couldn't open $outfile for writing: $!";
    $fh->print($sprite_data);
    $fh->close;
}
else {
    print $sprite_data;
}