Source

gauge / hex2png

#!/usr/bin/perl
# 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;
my $offset = 0;

GetOptions(
	"w|width=i"		=> \$cols,
	"b|bit-offset=i"	=> \$bit_offset,
	"o|offset=i"		=> \$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 = <>;

# Skip offset bytes into the image
$buf = substr $buf, $offset;

$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(($max_size + $px_height - 1) / $px_height);
$px_width = int(($px_width + $cols - 1) / $cols) * $cols;

warn "$max_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 + $bit_offset;
		my $bit = $offset < $max_size ? $bits[$offset] : '0';
		$bit = !$bit if $use_negative;

		push @row, $bit;
	}

	print join(' ', @row), "\n";
}


__END__

buf = sys.stdin.read()

# rearrange buf into parallel columns
bw=8*6
cols=128
w=bw*cols
h=256*256/cols
img = Image.new("1",(w,h))

for c in range(cols):
    colimg = Image.fromstring("1",(bw,h),buf[c*h:(c+int(bw/8))*h])
    img.paste(colimg,(bw*c,0))

img.save(sys.argv[1])
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.