Skip to content

Commit 44254f9

Browse files
committed
rewrite png2ansi by Perl
1 parent 590685b commit 44254f9

File tree

5 files changed

+271
-9
lines changed

5 files changed

+271
-9
lines changed

convert_gif2ansi.sh

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,17 @@ main () {
88
fi
99
F_FILENAME=$(basename $F_NAME)
1010
F_BASENAME=$(basename $F_FILENAME .gif)
11-
CMD=png2ansi2.py
11+
PALETTE=palette.default.json
1212
case $F_BASENAME in
13-
"monk_stand"| "maken_stand")
14-
CMD=png2ansi3.py
15-
;;
16-
"viking_stand" )
17-
CMD=png2ansi4.py
13+
"monk_stand"| "maken_stand"|"viking_stand" )
14+
PALETTE="palette.type1.json"
1815
;;
1916
*)
2017
;;
2118
esac
22-
python $CMD $F_NAME | perl -nle 'next if $i++ % 3; s/(.){3}/\1/g; print' > dat/$F_BASENAME.dat
23-
perl ./ansi_escape.pl dat/$F_BASENAME.dat
24-
perl ./ansi_escape.pl dat/$F_BASENAME.dat > motd/$F_BASENAME.txt
19+
./png2ansi.pl -p $PALETTE $F_NAME | ./resize.pl 3 > dat/$F_BASENAME.dat
20+
./ansi_escape.pl dat/$F_BASENAME.dat
21+
./ansi_escape.pl dat/$F_BASENAME.dat > motd/$F_BASENAME.txt
2522
done
2623
}
2724
main "$@"

palette.default.json

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{
2+
"green" : {
3+
"index" : 2,
4+
"rgb" : [
5+
0,
6+
255,
7+
0
8+
]
9+
},
10+
"white" : {
11+
"index" : 7,
12+
"rgb" : [
13+
255,
14+
255,
15+
255
16+
]
17+
},
18+
"blue" : {
19+
"index" : 4,
20+
"rgb" : [
21+
0,
22+
0,
23+
127
24+
]
25+
},
26+
"black" : {
27+
"index" : 0,
28+
"rgb" : [
29+
0,
30+
0,
31+
0
32+
]
33+
},
34+
"red" : {
35+
"index" : 1,
36+
"rgb" : [
37+
255,
38+
0,
39+
0
40+
]
41+
},
42+
"cyan" : {
43+
"index" : 6,
44+
"rgb" : [
45+
0,
46+
255,
47+
255
48+
]
49+
},
50+
"magenta" : {
51+
"index" : 5,
52+
"rgb" : [
53+
255,
54+
0,
55+
255
56+
]
57+
},
58+
"yellow" : {
59+
"index" : 3,
60+
"rgb" : [
61+
255,
62+
255,
63+
0
64+
]
65+
}
66+
}

palette.type1.json

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{
2+
"green" : {
3+
"index" : 2,
4+
"rgb" : [
5+
0,
6+
127,
7+
0
8+
]
9+
},
10+
"white" : {
11+
"index" : 7,
12+
"rgb" : [
13+
127,
14+
127,
15+
127
16+
]
17+
},
18+
"blue" : {
19+
"index" : 4,
20+
"rgb" : [
21+
0,
22+
0,
23+
127
24+
]
25+
},
26+
"black" : {
27+
"index" : 0,
28+
"rgb" : [
29+
0,
30+
0,
31+
0
32+
]
33+
},
34+
"red" : {
35+
"index" : 1,
36+
"rgb" : [
37+
127,
38+
0,
39+
0
40+
]
41+
},
42+
"cyan" : {
43+
"index" : 6,
44+
"rgb" : [
45+
0,
46+
127,
47+
127
48+
]
49+
},
50+
"magenta" : {
51+
"index" : 5,
52+
"rgb" : [
53+
127,
54+
0,
55+
127
56+
]
57+
},
58+
"yellow" : {
59+
"index" : 3,
60+
"rgb" : [
61+
127,
62+
127,
63+
0
64+
]
65+
}
66+
}

png2ansi.pl

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
#!/usr/bin/env perl
2+
use utf8;
3+
use strict;
4+
use warnings;
5+
use v5.16;
6+
use GD;
7+
use Carp;
8+
use Pod::Usage;
9+
use JSON::XS ();
10+
use Getopt::Long qw(:config posix_default no_ignore_case gnu_compat bundling auto_version);
11+
12+
our $VERSION = "0.01";
13+
my %args;
14+
15+
my $palette = {
16+
black => { rgb => [ 0, 0, 0 ], index => 0, },
17+
red => { rgb => [ 255, 0, 0 ], index => 1, },
18+
green => { rgb => [ 0, 255, 0 ], index => 2, },
19+
yellow => { rgb => [ 255, 255, 0 ], index => 3, },
20+
blue => { rgb => [ 0, 0, 127 ], index => 4, },
21+
magenta => { rgb => [ 255, 0, 255 ], index => 5, },
22+
cyan => { rgb => [ 0, 255, 255 ], index => 6, },
23+
white => { rgb => [ 255, 255, 255 ], index => 7, },
24+
};
25+
26+
main();
27+
28+
sub main {
29+
init();
30+
foreach my $file (@ARGV) {
31+
png2ansi($file);
32+
}
33+
}
34+
35+
sub png2ansi {
36+
my ($file) = @_;
37+
Carp::croak "$file is not found" unless -e $file;
38+
my $im = GD::Image->new($file);
39+
my $pal = mk_palette();
40+
my ( $width, $height ) = $im->getBounds();
41+
for ( my $y = 0; $y < $height; $y++ ) {
42+
my @row;
43+
for ( my $x = 0; $x < $width; $x++ ) {
44+
my $color = $pal->colorClosest( $im->rgb( $im->getPixel( $x, $y ) ) );
45+
push @row, $color;
46+
}
47+
print join q{}, @row, "\n";
48+
}
49+
}
50+
51+
sub mk_palette {
52+
my $pal = GD::Image->new(1,1);
53+
foreach my $color (sort {$a->{index} <=> $b->{index} } values %{$args{palette_table}}){
54+
$pal->colorAllocate(@{$color->{rgb}});
55+
}
56+
return $pal;
57+
}
58+
59+
60+
61+
sub init {
62+
my @args_pattern = ('palette|p=s', 'help|h', 'manual', 'verbose' );
63+
if ( not GetOptions( \%args, @args_pattern ) ) { pod2usage(2) }
64+
if ( exists $args{help} ) { pod2usage(0) }
65+
elsif ( exists $args{manual} ) { pod2usage( -exitstatus => 0, -verbose => 2 ) }
66+
if (exists $args{palette} and -e $args{palette} ) {
67+
my $json_text;
68+
{ local $/ = undef; local *FILE; open FILE, '<', $args{palette}; $json_text = <FILE>; close FILE }
69+
$args{palette_table} = JSON::XS::decode_json($json_text);
70+
}
71+
$args{palette_table} ||= $palette;
72+
73+
return \%args;
74+
}
75+
76+
1;
77+
78+
__END__
79+
80+
=pod
81+
82+
=head1 NAME
83+
84+
png2ansi.pl - PNG to ANSI
85+
86+
=head1 SYNOPSIS
87+
88+
png2ansi.pl [options] file[, file2, ..., fileN]
89+
90+
convert png file to ansi dat
91+
92+
=head1 OPTIONS
93+
94+
=over 8
95+
96+
=item B<--palette|-p> pallet.json
97+
98+
set color pallet
99+
100+
=item B<--help|?>
101+
102+
show help
103+
104+
=item B<--verbose>
105+
106+
verbose mode
107+
108+
=item B<--manual>
109+
110+
show manual
111+
112+
=item B<--version>
113+
114+
show version
115+
116+
=back
117+
118+
=head1 DESCRIPTION
119+
120+
This is skelton of perl script.
121+
122+
=head1 PREREQUISITES
123+
124+
This script requires the C<Getopt::Long>, C<Pod::Usage>
125+
126+
=head1 Author
127+
128+
Yusuke Wtase <[email protected]>
129+
130+
=cut

resize.pl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env perl
2+
my $resize = shift @ARGV;
3+
while(<>){ next if $i++ % $resize; s/(.){$resize}/\1/g; print}

0 commit comments

Comments
 (0)