-
Notifications
You must be signed in to change notification settings - Fork 34
/
visualize_binary.pl
52 lines (39 loc) · 1.02 KB
/
visualize_binary.pl
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
#!/usr/bin/perl
# Visualize a given input stream of bytes, as a PGM (P5) image.
use 5.014;
use strict;
use warnings;
use Getopt::Long qw(GetOptions);
my $width = 0;
my $height = 0;
my $colors = 255;
sub print_usage {
print <<"EOT";
usage: $0 [options] [<input.bin] [>output.pgm]
options:
--width=i : width of the image (default: $width)
--height=i : height of the image (default: $height)
--colors=i : number of colors (default: $colors)
--help : display this message and exit
EOT
exit;
}
GetOptions(
"w|width=i" => \$width,
"h|height=i" => \$height,
"c|colors=i" => \$colors,
"help" => \&print_usage,
)
or die "Error in arguments";
binmode(STDIN, ':raw');
binmode(STDOUT, ':raw');
my $data = do {
local $/;
<>;
};
if (!$width or !$height) {
$width ||= ($height ? int(length($data) / $height) : int(sqrt(length($data))));
$height ||= int(length($data) / $width);
}
print "P5 $width $height $colors\n";
print $data;