-
Notifications
You must be signed in to change notification settings - Fork 39
/
ppmascii
executable file
·234 lines (208 loc) · 5.33 KB
/
ppmascii
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/usr/bin/perl -s
#
# ppmascii
# --------
# Useful with ttytter, a commandline twitter client, for displaying users'
# avatars as ascii art.
#
# Taken from http://www.floodgap.com/software/ppmascii/
#
# @authors: Cameron Kaiser <[email protected]>
# @license: Floodgap Free Software License
# http://www.floodgap.com/software/ffsl/license.txt
#
BEGIN { $^H |= 0x000000008 unless ($] < 5.006); } # "use bytes"
if ($h || $help) {
die <<"EOF";
ppmascii v1.2 by cameron kaiser
options:
-showheader show ppm height, width, etc.
distortions and transformations:
-x2 double pixels horizontally (averaged unless -nosmooth)
-y2 double pixels vertically (simple reduplication)
-winx=n scroll window to horizonal pixel n
-winy=n scroll window to vertical pixel n
-width=n set width to n columns (default 79)
-height=n stop after n rows
input-stage adjustments:
-bright=n set bright to n (1.0=no change), 0<n<infinite (NOT gamma)
-contrast=n set contrast to n (0=no change), -infinite<n<infinite
-photo preset bright/contrast for continuous-tone and photo images
-lineart preset bright/contrast for line art
rendering controls (by default RGB perceptual luminance with Floyd-Steinberg):
-invert invert display to dark on light, not light on dark
-norgb use arithmetic sum for luminance, not RGB perceptual coeffs
-nodither do not use Floyd-Steinberg dithering (faster)
-nosmooth do not use antialiasing (right now just -x2) (faster)
(c)2000, 2008, 2010 cameron kaiser all rights reserved word to your mother
*** http://www.floodgap.com/software/ppmascii/ ***
licensed under FFSL: www.floodgap.com/software/ffsl/
EOF
}
select(STDOUT); $|++;
@elements = (' ', qw(
.
,
:
;
+
=
o
a
e
O
$
@
A
#
M
));
(@elements = reverse(@elements)) if ($invert);
$width ||= $ENV{'COLUMNS'} || 79;
$width += $winx;
$width += $winx if ($x2);
$bright ||= 1;
$bright = abs($bright);
$contrast ||= 0;
if ($photo) { $bright = 0.8; $contrast = -0.6; }
if ($lineart) { $bright = 1.0; $contrast = -300; }
binmode ARGV;
binmode STDIN;
&loadppm(\*ARGV);
&initppm;
print "$ppm_width $ppm_height $ppm_ccv $ppm_type\n" if ($showheader);
$pix = $ppm_width * $ppm_height;
$pix += $pix if ($x2);
$pix += $pix if ($y2);
$scf = (($ppm_ccv * 3) / scalar(@elements));
$pixx = $rows = $dwcc = $drows = 0;
$wcc = -1; # to start with
$ppm_ccv *= 1.5;
$this_row = '';
$diffscale = 768/scalar(@elements);
$lastlum = 0;
@empty = (); $#empty = $width;
@thisline = @empty; # for the floyd-steinberg algorithm
@nextline = @empty;
$lap = $llap = 0;
for(;;) {
&widthfix;
($r, $g, $b) = &getnextpixel;
#print "$r $g $b -- $wcc -- ";
last if ($r == -1 || ($height && $drows >= $height));
next if ($dwcc >= $width || $wcc < $winx || $rows < $winy);
# normalized from 0.3R+0.59G+0.11B to the 3x we use here
$olum = ($norgb) ? ($r + $g + $b) : (0.9*$r + 1.77*$g + 0.33*$b);
unless ($nodither) {
$lum = $olum+(7/16)*$lastlum+shift(@thisline);
} else {
$lum = $olum;
}
$cc = ($ppm_ccv - $lum) * $contrast;
$cc += $lum; $cc=0 if ($cc<0);
$llap = $lap;
$lap = (!$scf) ? 0 : &clip($cc * $bright / $scf);
unless ($nodither) {
# setup matrix for f-s dither
$lastlum = $olum-($diffscale*$lap);
# remember that wcc
$nextline[$wcc] += (5/16)*$lastlum;
$nextline[$wcc+1] += (1/16)*$lastlum if ($wcc < $ppm_width);
$nextline[$wcc-1] += (3/16)*$lastlum if ($wcc);
}
if ($x2 && $dwcc > 1 && !$nosmooth) {
$elap = &clip(($lap + $llap) / 2);
} else {
$elap = $lap;
}
$j = ($elements[$elap]);
$this_row .= $j;
print $j;
if ($x2) {
$j = ($elements[$lap]);
$this_row .= $j;
print $j;
}
}
exit;
sub widthfix {
if (++$wcc == $ppm_width) {
if ($rows >= $winy) {
print "\n";
print "$this_row\n" if ($y2);
$drows++;
$llap = 0;
}
$rows += 1 + $y2;
$this_row = '';
$dwcc = $wcc = $lastlum = 0;
@thisline = @nextline;
@nextline = @empty;
}
$dwcc += 1 + $x2;
}
sub clip {
local $g = shift;
$g = $#elements if ($g > $#elements);
return int($g);
}
sub loadppm {
local ($p, $w) = ($/, @_);
undef $/;
$ppm_buf = scalar(<$w>);
return 1;
}
sub initppm {
$ppm_buf =~ s/^\s*//s;
($ppm_type, $ppm_buf) = split(/\s+/s, $ppm_buf, 2);
$ppm_buf =~ s/#[^\r\l\n]*[\r\l\n]+/\n/gs
if ($ppm_type ne 'P6');
$ppm_buf =~ s/^\s*//s;
if ($ppm_type eq 'P1') { #pbm
($ppm_width, $ppm_height, $ppm_buf) =
split(/\s+/s, $ppm_buf, 3);
$ppm_ccv = 1;
} else {
($ppm_width, $ppm_height, $ppm_ccv, $ppm_buf) =
split(/\s+/s, $ppm_buf, 4);
}
$ppm_width += 0;
$ppm_height += 0;
$ppm_ccv += 0;
$ppm_filepos = 0;
return 1;
}
sub getnextcolour {
local $nc;
return -1 if (!length($ppm_buf) || $ppm_filepos >= length($ppm_buf));
if ($ppm_type eq 'P3' || $ppm_type eq 'P2') {
$ppm_buf =~ s/^\s*//s;
($nc, $ppm_buf) = split(/\s+/s, $ppm_buf, 2);
return (0+$nc);
} elsif ($ppm_type eq 'P6' || $ppm_type eq 'P5') {
$nc = substr($ppm_buf, $ppm_filepos, 1);
$ppm_filepos++;
return 0+unpack("C", $nc);
} elsif ($ppm_type eq 'P1') {
$nc = substr($ppm_buf, 0, 1);
$ppm_buf = substr($ppm_buf, 1);
$ppm_buf =~ s/^[\r\l\n\s]*//s;
return ($nc+0);
} else {
die("unsupported format $ppm_type");
}
}
sub getnextpixel {
local $r, $g, $b;
if ($ppm_type eq 'P3' || $ppm_type eq 'P6') {
$r = &getnextcolour;
$g = &getnextcolour;
$b = &getnextcolour;
} else { # must be P1, P2 or P5
$b = &getnextcolour;
$r = $g = $b;
}
return (($r == -1 || $g == -1 || $b == -1) ? (-1, -1, -1) :
($r, $g, $b));
}
1;