-
Notifications
You must be signed in to change notification settings - Fork 1
/
pgmtomosaic
executable file
·209 lines (177 loc) · 4.16 KB
/
pgmtomosaic
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/usr/bin/perl -w
# Based on my PGM to ASCII art tool, makes a mosaic from some toast images
# (not provided).
# Benjamin Elijah Griffin 27 Dec 1999
use strict;
use integer;
use vars qw( $in $type $x $y $max $id %map $custommap $i $last );
$id = $0;
$id =~ s:.*/::;
$custommap = 0;
%map = (
# The keys are the letter 'd' followed by a decimal number of a character.
# Alternatively the raw character can be used, but the 'd' form will
# override if both are defined.
d000 => 'toast.pgm',
d010 => 'toast.pgm',
d053 => 'toast-20.pgm',
d105 => 'toast-40.pgm',
d156 => 'toast-60.pgm',
d207 => 'toast-80.pgm',
d255 => 'toast-800.pgm',
# d000 => ' ',
# d016 => '.',
# d032 => ',',
# d048 => ';',
# d064 => '+',
# d080 => 'x',
# d096 => 'z',
# d112 => 'm',
# d128 => 'X',
# d144 => 'Y',
# d160 => 'U',
# d186 => 'C',
# d202 => 'O',
# d218 => 'M',
# d234 => 'W',
# d256 => '%',
);
# Fill in holes.
$last = ' ';
for ($i = 0; $i < 256; $i++) {
$i = sprintf("%03d", $i);
if (defined($map{"d$i"})) {
$last = $map{"d$i"};
} else {
$map{"d$i"} = $last;
}
}
while (defined($ARGV[0]) and $ARGV[0] =~ /^-(.*)/) {
my $arg = $1; shift;
if ($arg eq '-help' or $arg eq '-version' or $arg eq 'v') {
&usage(0);
} elsif ($arg eq 'm' or $arg eq '-map') {
&readmap($ARGV[1]);
}
}
# Add to map the character versions of the numerical mappings.
my @map = keys %map;
foreach (@map) {
s/^d//;
$map{chr($_)} = $map{"d$_"};
}
if ($ARGV[0]) {
open(STDIN,"< $ARGV[0]") or die "$id: Can't open '$ARGV[0]': $!\n";
}
$in = '#';
while($in =~ /^#/) { $in = <STDIN> }
if ($in =~ s/^P([25])\s*//) {
$type = $1;
} else {
warn "$id: no portable graymap image file found\n";
exit(1);
}
read(STDIN,$in,1024,length($in));
# Eat comments, if the version allows comments
if ($type < 4 and $in =~ tr:#:#:) {
while ($in =~ s/(^|\cm|\cj)#[^\cj\cm]*([\cm\cj])/$1$2/g) {
read(STDIN,$in,256,length($in));
}
}
if ($in =~ s/^\s*(\d+)\s+(\d+)\s+//) {
$x = $1;
$y = $2;
} else {
warn "$id: no dimensions found in image\n";
exit(1);
}
if ($in =~ s/^\s*(\d+)\s+//) {
$max = $1;
} else {
warn "$id: no max value found in image\n";
exit(1);
}
if ($max != 255 and !$custommap) {
warn "$id: Internal map is for 255 grays, this has $max grays\n";
}
my $pieces = 0;
my $done = 0;
my $col = 0;
my $pixel = 0;
my $row = 0;
my $all = '';
my $chr;
print "pnmcat -lr ";
while($done < 2) {
if (length($in) < 4) {
if (!read(STDIN,$in,1024,length($in))) {
$done = 1;
} elsif ($type == 2) {
# Nuke any leading whitespace if this is a text based PGM file.
$in =~ s/^\s+//;
}
}
if ($type == 5) {
$in =~ s/^(.)//s;
$chr = $1;
} else {
if ($done) {
($chr) = $in =~ s/^(\d+)\s*//s;
} else {
($chr) = $in =~ s/^(\d+)\s+//s;
}
if (!defined($chr)) {
warn "$id: Missing gray value!\n";
next;
}
$chr = sprintf("d%03d", $chr);
}
if (!defined($map{$chr})) {
my $new = $map{d000};
warn "$id: Gray $chr missing from map using <$new>\n";
$map{$chr} = $new;
}
print " " . $map{$chr};
if($map{$chr} ne $map{'d255'}) { $pieces ++; }
$pixel ++;
$col++;
if ($col == $x) {
print "> temp.$row.pgm\n";
$all = "$all temp.$row.pgm";
print "pnmcat -lr ";
$row ++;
$col = 0;
}
if($done and $in eq '') {
$done = 2;
}
if ($pixel > ($x * $y)) {
die "Too many pixels\nin=$in\n" unless $done == 2;
}
}
print "/dev/null > /dev/null 2>/dev/null\n";
print "pnmcat -tb $all > done.pgm\n";
print "echo $pieces pieces of toast used. Yum.\n";
exit(0);
print STDERR "\$in = '$in';\n";
print STDERR "\$x = '$x';\n";
print STDERR "\$y = '$y';\n";
print STDERR "\$x*\$y = '" . ($x*$y) ."';\n";
print STDERR "\$col = '$col';\n";
print STDERR "\$pixel = '$pixel';\n";
print STDERR "\$chr = '$chr';\n";
sub usage ($) {
my $exit = shift;
print "$id: usage\n", <<USAGEnotes;
pgmtoascii [option] [file]
Options:
-h --help print this message
-m --map MAPFILE use MAPFILE for gray to ascii map
USAGEnotes
exit($exit);
} # end &usage
# A really crude way to read in the graymapping file.
sub readmap ($) {
my $file = shift;
do $file or die "$id: Can't read map: $!\n"
} # end &readmap