#!/usr/bin/perl

#
# Emit a PIC asm snippet for a nonlinear brightness
# transfer function.
#
# showValues(16);
# showValues(32);

# I find 64 levels to be just about right.
#
showValues(64);


sub showValues($)
{
    my $res = shift;
    print "\n\n";
    my $expo = 1.7;
    print "; +------------------------------------\n";
    print "; | exp-curve table for $res steps --> 0..255\n";
    print "; | transfer function based on x ^ $expo\n";
    print "; | poly@omino.com 2007\n";
    print "; | \n";
    print "; | define EXP_ENTRY as needed. RETLW is short,\n";
    print "; | but uses a precious stack spot. MOVLW/GOTO is\n";
    print "; | sometimes preferable.\n";
    print "; | \n";

    for(my $i = 0; $i < $res; $i++)
    {
        my $i2 = 255  * $i / ($res - 1);
        my $x2 = $i2 ** $expo / (255 ** $expo) * 255;

        if($x2 <= $i) #($res / 2)) 
        {
            $x2 = $i; 
        }
        if($x2 < 4 & $x2 != 0) 
        {
            $x2 = 4; 
        }
#        printf("i: %4d    adj: %4d\n",$i,$x2);
    # integer values... and pretty strings
        my $x2i = int($x2); # the transfer value
        my $i2i = int($i2); # the source value

        my $shrunk = int($x2i / 8 + 0.5);
        my $bar1 = "." x ($shrunk + 0);
        my $bar2 = "." x (32 - $shrunk);

	my $blanks = "";
	$blanks = " " if $x2i < 100;
	$blanks = "  " if $x2i < 10;


        printf "\tEXP_ENTRY d'%d' $blanks; %3d --> %3d (%2d of %2d) %s\n",
            $x2i,$i2i,$x2i,$i + 1,$res,
            $bar1 . "*" . $bar2;
    }
    #print "; got the log value\n";
    #print "gotExp:\n";
    #print "\n";



}
