#!/usr/bin/perl
use strict;
use warnings;
use File::Temp qw(tempfile);

my ($img_fh, $img_fn) = tempfile();
print $img_fh (<>);
sub f { join ';', unpack '(C)*', $_[0] }
@_ = split /\n/, qx{ anytopnm < "$img_fn" 2>/dev/null }, 4;
exit unless @_;
my @b = ('▀', '▄'); # pixel
my ($s)= $_[1] =~ /(\d+)/; # image width
for($_[3]) {
    @_=unpack "(a".($s*3).")*",$_;
    my $e=1;
    while (my @z = splice @_, 0, 2) { # take each 2 rows
	$_=[unpack "(a3)*",$_] for @z; # r/g/b
	while (@{$z[0]}) {
	    my @y = map { shift @$_ } @z;
	    if (@y==1) {
		print "\e[38;2;@{[f($y[0])]}m$b[0]"
	    } else {
		print "\e[48;2;@{[f($y[$e])]};38;2;@{[f($y[$e^1])]}m$b[$e^=1]"
	    }
	}
	print "\e[0m\n";
    }
}
