Trammell Hudson avatar Trammell Hudson committed 7094f77

getoptified hex2png

Comments (0)

Files changed (1)

 #!/usr/bin/perl
-# Convert a ROM dump into a monochrome PNG
-# Either LSB or MSB first
+# Convert a ROM dump into a monochrome PBM
 use warnings;
 use strict;
+use Getopt::Long;
+
+my $usage = <<__END_OF_USAGE__;
+Usage:
+    $0 [options] < file.bin | convert - file.png
+
+Options:
+    -w | --width 16          Column width (in bits) to stride
+    -b | --offset N          Offset (in bits) into the stride
+    -y | --height N          Height of the image (in scan lines)
+    -m | --msb               Use most significant bit first (default LSB)
+    -s | --size N            Limit size to the first N bytes
+    -n | --negative          Invert the colors
+
+__END_OF_USAGE__
+
+my $max_size;
+my $use_msb;
+my $cols = 8;
+my $bit_offset = 0;
+my $px_height = 512;
+my $use_negative;
+
+GetOptions(
+	"w|width=i"		=> \$cols,
+	"b|offset=i"		=> \$bit_offset,
+	"m|msb+"		=> \$use_msb,
+	"s|size=i"		=> \$max_size,
+	"n|negative+"		=> \$use_negative,
+	"y|height=i"		=> \$px_height,
+) or die $usage;
+
 
 undef $/;
 my $buf = <>;
 
-# Stride across the ROM image, in bits
-my $cols = 16;
-
-# Maximum height of the strip, in bits
-my $px_height = 512;
-
-# Ascending order in each byte == "B"
-# Descending order == "b"
-my $bitstring_fmt = "B*";
-
-my @bits = split //, unpack($bitstring_fmt, $buf);
-my $size = @bits;
+$max_size ||= length $buf;
+$max_size *= 8;
+my @bits = split //, unpack($use_msb ? "b*" : "B*", $buf), $max_size;
 
 # The width of the image will be a function of the number of columns
 # and the maximum height.  This must be rounded to be an even multiple
 # of the column size
-my $px_width = int(($size + $px_height - 1) / $px_height);
+my $px_width = int(($max_size + $px_height - 1) / $px_height);
 $px_width = int(($px_width + $cols - 1) / $cols) * $cols;
 
-warn "$size bits = $px_width x $px_height\n";
+warn "$max_size bits = $px_width x $px_height\n";
 
 # Now generate a PBM file with those dimensions
 print <<"";
 	{
 		my $col = $x % $cols;
 		my $group = int($x / $cols);
-		my $offset = $col + $y * $cols + $group * $cols * $px_height;
+		my $offset = $col + $y * $cols + $group * $cols * $px_height + $bit_offset;
+		my $bit = $offset < $max_size ? $bits[$offset] : '0';
+		$bit = !$bit if $use_negative;
 
-		push @row, $offset < $size ? $bits[$offset] : '0';
+		push @row, $bit;
 	}
 
 	print join(' ', @row), "\n";
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.