#!/usr/local/bin/gawk -f
#
#	GFB -> PPM ascii
#	(C) Hitoshi Yamauchi, 1 February 1997
#
function filtcolor(color)
{
    # ret = color * 500.0;
    ret = color / 2.0;
    # ret = color;
    if(ret > 255)
    {
	return 255;
    }else if(ret < 0)
    {
	return 0;
    }else
    {
	return ret;
    }
}


BEGIN {
    line = 0;
    sizeX = 0;
    sizeY = 0;
}

{
    line++;
    if(line == 1)
    {
	if($1 != "GFB")
	{
	    print "This is not GFB file. exit" > "/dev/stderr";
	    exit;
	}
    }else if(line == 2)
    {
	if($1 != "size")
	{
	    print "This is not GFB file. exit" > "/dev/stderr";
	    exit;
	}else
	{
	    print "P3";
	    print "# CREATOR: mpi2 GFB -> PPM: 1 Feb 1997 (C)Hitoshi Yamauchi";
	    sizeX = $2;
	    sizeY = $3;
	    printf("%d %d\n", sizeX, sizeY);
	    printf("255\n");	# color ¤Ï 255 RGB
	}
    }else
    {
	r = filtcolor($1);
	g = filtcolor($2);
	b = filtcolor($3);
	printf("%3d %3d %3d ", r, g, b);
	if((line % 5) == 0)
	{
	    printf("\n");
	}
    }
}	    

END{
    printf("\n");		
}
