visualize_binary.pl 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/perl
  2. # Visualize a given input stream of bytes, as a PGM (P5) image.
  3. use 5.014;
  4. use strict;
  5. use warnings;
  6. use Getopt::Long qw(GetOptions);
  7. my $width = 0;
  8. my $height = 0;
  9. my $colors = 255;
  10. sub print_usage {
  11. print <<"EOT";
  12. usage: $0 [options] [<input.bin] [>output.pgm]
  13. options:
  14. --width=i : width of the image (default: $width)
  15. --height=i : height of the image (default: $height)
  16. --colors=i : number of colors (default: $colors)
  17. --help : display this message and exit
  18. EOT
  19. exit;
  20. }
  21. GetOptions(
  22. "w|width=i" => \$width,
  23. "h|height=i" => \$height,
  24. "c|colors=i" => \$colors,
  25. "help" => \&print_usage,
  26. )
  27. or die "Error in arguments";
  28. binmode(STDIN, ':raw');
  29. binmode(STDOUT, ':raw');
  30. my $data = do {
  31. local $/;
  32. <>;
  33. };
  34. if (!$width or !$height) {
  35. $width ||= ($height ? int(length($data) / $height) : int(sqrt(length($data))));
  36. $height ||= int(length($data) / $width);
  37. }
  38. print "P5 $width $height $colors\n";
  39. print $data;