#!/bin/env perl

$lim = $ARGV[0] || 512;
$n = 0;
$left_residue = 0;

@story_b = split //, "kathleen";
@story_w = split //, " ";

while( ++$n and read STDIN, $pixel, 3 ) {
	next if ($n/$lim)%2;

	$col = $n % $lim;
	
	(undef, $byte, undef) = unpack "CCC", $pixel;

	$byte *= $byte/255; # nonlinear truncating intensity ramp!
	
	$byte += $error;

	$byte = 255 if $byte > 255;

	$idx = int ((2*$byte+1)/128);
	$idx = 1 if $idx > 1;

	if($idx) {
		print $story_w[$n%@story_w];
	} else {
		print $story_b[$n%@story_b];
	}

	$displayed = $idx*128;

	$error = $byte-$displayed;
	
	if( $col == 0) {
		print "\n";
	}

}

print "\n\nvim: nowrap\n";
