Trammell Hudson avatar Trammell Hudson committed c53db18 Draft

Converted to perl and output pbm instead of png

Comments (0)

Files changed (1)

-#!/usr/bin/python
-import sys
-import Image
+#!/usr/bin/perl
+# Convert a ROM dump into a monochrome PNG
+# Either LSB or MSB first
+use warnings;
+use strict;
+
+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;
+
+# 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);
+$px_width = int(($px_width + $cols - 1) / $cols) * $cols;
+
+warn "$size bits = $px_width x $px_height\n";
+
+# Now generate a PBM file with those dimensions
+print <<"";
+P1
+$px_width $px_height
+
+for (my $y = 0 ; $y < $px_height ; $y++)
+{
+	my @row;
+
+	for (my $x = 0 ; $x < $px_width ; $x++)
+	{
+		my $col = $x % $cols;
+		my $group = int($x / $cols);
+		my $offset = $col + $y * $cols + $group * $cols * $px_height;
+
+		push @row, $offset < $size ? $bits[$offset] : '0';
+	}
+
+	print join(' ', @row), "\n";
+}
+
+
+__END__
 
 buf = sys.stdin.read()
 
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.