#!/bin/env perl

## rgbview.pl
## display the green channel of .rgb file in ascii, skipping every other line
## pass line width as only argument, data should be in standard in

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

@p = split //, ' X';


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

	$col = $n % $lim;
	
	if( $col == 0) {
		$left_residue = 0;
		print "\n";
	}
	(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;

	print $p[$idx];

	$displayed = $idx*128;

	$error = $byte-$displayed;

}

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