#!/usr/bin/perl -w use strict; use POSIX qw(ceil); use List::Util qw(reduce sum); use Image::Magick; use constant STARTCHAR => 32; use constant ENDCHAR => 127; use constant CHARLIST => STARTCHAR .. ENDCHAR; my @charmap = map { chomp; [split] } ; my @binary = (0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15); my ($rows, $cols, @blob) = do { my $image = Image::Magick->new; $image->Read($ARGV[0]); $image->Quantize(colors => 2) if $image->Get('colors') > 2; $image->Set(magick => 'MONO'); ceil($image->Get('height') / 16), ceil($image->Get('width') / 8), split //, $image->ImageToBlob(); }; for my $y (0 .. $rows - 1) { for my $x (0 .. $cols - 1) { my @score = map { my $char = $_; 128 - sum map { my $byte = ord($blob[ ($y * 16 + $_) * $cols + $x ] || chr 0); my $bitmap = $binary[ $byte / 16 ] + $binary[ $byte % 16 ] * 16; unpack("B*", chr($bitmap^$charmap[$char][$_])) =~ tr/1/1/ } 0 .. 15; } CHARLIST; print chr(reduce { $score[$a - STARTCHAR] >= $score[$b - STARTCHAR] ? $a : $b } CHARLIST); } print "\n"; }