-
Notifications
You must be signed in to change notification settings - Fork 1
/
ledpixelize
executable file
·329 lines (271 loc) · 7.83 KB
/
ledpixelize
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#!/usr/bin/perl -w
# Take a PPM image and blow up the pixels to resemble those on an LED
# monitor.
#
# 18 Jun 2012
use strict;
use Image::PBMlib;
use vars qw( %cfv %stackmemo @order $mv
$filein $fileout %info @pixels %options $rc $id
);
sub mk_cfv();
%options = (
verbose => 0,
boxsize => 18, # designed for ( ( odd number + 1 ) * 3)
);
$mv = 5; # global max value (for output)
@order = qw( b g r ); # global color bar order
mk_cfv(); # set %cfv global from $mv and @order
$id = $0;
$id =~ s,.*/([^/]+),$1,;
while( defined($_ = shift(@ARGV) ) ) {
if(/^-b$|^--boxsize/) {
$options{boxsize} = shift;
if(!defined($options{boxsize})) {
usage("Need a boxsize with $_");
}
next;
}
if(/^-v$|^--verbose/) {
$options{verbose} = 1;
next;
}
if(!defined($filein)) {
$filein = $_;
next;
} elsif (!defined($fileout)) {
$fileout = $_;
next;
} else {
usage("What to do with $_?");
# never returns
}
}
if(defined($filein)) {
if(!open(STDIN, "<:raw", $filein)) {
usage("Cannot open $filein: $!", 1);
}
}
readpnmfile( \*STDIN, \%info, \@pixels, 'float' );
if(length($info{error})) {
die "$0: input image: $info{error}\n";
}
if($info{bgp} ne 'p') {
die "$0: input is not pixmap, cannot pixelize\n";
}
if(defined($fileout)) {
if(!open(STDOUT, ">", $fileout)) {
usage("Cannot open $fileout: $!", 1);
}
}
print STDOUT makepnmheader(3, ($info{width} * $options{boxsize}),
($info{height} * $options{boxsize}),
$mv);
# writes to STDOUT as it goes
process(\%options, \%info, \@pixels);
# writepnmfile( \*STDOUT, \%info, \@pixels );
exit;
sub process {
my $o = shift; # hash ref
my $i = shift; # hash ref
my $p = shift; # array ref to 2d array
my $bsize = $$o{boxsize};
my $itall = $$i{height};
my $j;
for($j = 0; $j < $itall; $j ++) {
# writes to STDOUT as it goes
process_line($bsize, $$p[$j]);
}
} # end &process
sub process_line {
my $size = shift;
my $r = shift; # array ref to 1d array
my $v;
my $l;
my @rgb;
for($l = 0; $l < $size; $l ++) {
for $v (@{$r}) {
@rgb = explodetriple($v);
chop(@rgb); # remove ,
print drawline($l, $size, @rgb);
}
}
} # end &process_line
sub usage {
my $message = shift;
my $exit = shift;
my $id = $0;
$id =~ s,^/.*/,,;
if(!defined($exit)) {
$exit = 0;
}
if(defined($message)) {
warn "$id: $message\n";
$exit = 2;
}
print <<"END_usage";
$id: usage
ledpixelize [ OPTIONS ] [ INPUT [ OUTPUT ] ]
Options:
-b SIZE --boxsize SIZE use output "pixels" SIZE large (def:18)
-v --verbose be verbose
Takes a PPM file and styles it like it was displayed on an LED
monitor. Each pixel is blown up to nine squares (in three single
color columns). The brightness of each color is concentrated to
the top of the column. Columns are not in RGB order.
SIZE is intended to be ((odd number) + 1 ) x 3
END_usage
exit($exit);
} # end &usage
# what a single pixel looks like:
#
# .BBBBB.GGGGG.RRRRR each pixel is three stacks of three squares
# .BBBBB.GGGGG.RRRRR key: R-red, G-green, B-blue .-black
# .BBBBB.GG.GG RR.RR s = size of whole pixel
# .BBBBB.GGGGG.RRRRR S = (s/3) - 1 size of inner square
# .BBBBB.GGGGG.RRRRR M = round(s/2) middle of inner square
# ..................
# .BB.BB.gg.gg.rr.rr 5: very brightest is SxS of one color
# .BB.BB.gg.gg.rr.rr 4: one down from very brightest has black center and dimmer
# .................. 3: two down from very brightest has black plus and dimmer
# .BB.BB.gg.gg.rr.rr 2: three down from very brightest has black plus and dimmer
# .BB.BB.gg.gg.rr.rr 1: four down from very brightest has black plus and dimmer
# .................. 0: five down from very brightest is all black
# .bb.bb.gg.gg......
# .bb.bb.gg.gg...... full color $RGB[n] is stack of three full brights
# .................. dimmer colors use full bright at top and less below
# .bb.bb.gg.gg......
# .bb.bb.gg.gg......
# ..................
# returns pixels to one line of one of those expanded pixel boxes
# $pixstring = drawline( which_line_in_whole_pixel, size_of_whole_pixel,
# red_value, green_value, blue_value )
sub drawline {
my $l = shift; # which line within the box
my $s = shift; # size of the whole box
my %p = ( r => shift, g => shift, b => shift); # values for single pixel
my $s3 = int( $s / 3 ); # 1/3rd of whole box
my $s23 = 2 * $s3; # 2/3rds of whole box
my $S = $s3 - 1; # colored area with each third
my $M = int($S / 2); # middle of $S, counting from 0
my $c;
my $buff = '';
my $black = $cfv{'r0'}; # r0 eq b0 eq g0, so any will do
if(($l == 0) or ($l == $s3) or ($l == $s23)) {
# all black row
# R G B x box size
$buff = $black x $s;
return $buff . "\n"; # \n for ascii mode
}
for $c (@order) {
my @stack = stack_from_value($p{$c});
my $value;
my $color;
my $pl;
if($l < $s3) {
$value = $stack[0];
$pl = $l;
# debug print STDERR "line $l, first in stack\n";
} elsif ($l < $s23) {
$value = $stack[1];
# debug print STDERR "line $l, second in stack\n";
$pl = $l - $s3;
} else {
# debug print STDERR "line $l, third in stack\n";
$value = $stack[2];
$pl = $l - $s23;
}
# correct for black lines between blocks
$pl --;
$color = color_from_value($value, $c);
# no line by line diff for these
if($value == 0 or $value == $mv) {
$buff .= $black . ( $color x $S );
# debug print STDERR "line $l: black + val $value x $S, color $c\n";
} elsif($value == ($mv - 1)) {
# dim center
if($pl == $M) {
# debug print STDERR "line $l: black + val $value x $M, black, $value x $M, color $c (center)\n";
$buff .= $black . ( $color x $M ) . $black . ( $color x $M );
} else {
# debug print STDERR "line $l: black + val $value x $S, color $c\n";
$buff .= $black . ( $color x $S );
}
} else {
# dim cross
if($pl == $M) {
$buff .= $black x (1+$S);
# debug print STDERR "line $l: black (cross)\n";
} else {
# debug print STDERR "line $l: black + val $value x $M, black, $value x $M, color $c (cross)\n";
$buff .= $black . ( $color x $M ) . $black . ( $color x $M );
}
}
} # for each color
$buff .= "\n"; # for ascii mode output
return $buff;
} # drawline
# from a brightness value and a single channel,
# make an RGB color that represents that.
# all possible colors have been worked out in advance
sub color_from_value {
my $value = shift;
my $c = shift;
$value = int($value);
return $cfv{"$c$value"};
} # color_from_value
# work out all possible colors for color_from_value()
sub mk_cfv() {
my $black = " 0";
my $bright;
my $pix;
for my $v (0..$mv) {
$bright = " $v";
for my $c (@order) {
if($c eq 'r') {
$pix = "$bright$black$black";
} elsif($c eq 'g') {
$pix = "$black$bright$black";
} else {
$pix = "$black$black$bright";
}
$cfv{"$c$v"} = $pix;
}
}
} # mk_cfv
# from a 0.0 to 1.0 value, assign first third of brightness (0 to $mv)
# to the top of a stack, the second third to the middle, and the last
# third to the bottom.
# This is called often, so results are cached in a global memo.
sub stack_from_value {
my $v = shift;
my $value = $v * $mv * 3;
my $round;
my @ret;
# check cache
if(defined($stackmemo{$value})) {
@ret = @{$stackmemo{$value}};
return @ret;
}
# nope, need to calculate
if($value < $mv) {
$round = sprintf("%0.f", $value);
@ret = ($round, 0, 0);
} else {
$value -= $mv;
if($value < $mv) {
$round = sprintf("%0.f", $value);
@ret = ($mv, $round, 0);
} else {
if($value < $mv) {
$round = sprintf("%0.f", $value);
@ret = ($mv, $mv, $round);
} else {
@ret = ($mv, $mv, $mv);
}
}
}
# save in cache
$stackmemo{$value} = \@ret;
return @ret;
} # stack_from_value