#include <v_lib.h>

void rgb_maximize(vec3_t rgb)
{
	vec_t scale = rgb[vec3MaxInd(rgb)];
	if(scale > 0)
	{
		scale = 1 / scale;
		vec3Scale(rgb,rgb,scale);
	}
}


int main()
{
	u32_t i,j,k;
	vec3_t rgb, out;
	framebuf_p fb, ref;
	
	logStartConsoleLog();
	logUpperLevel(LOG_TRACE);

	ref = fbLoad("scene100.jpg");
    if(!ref)
    	return -1;
    fb = fbDuplicate(ref);

    for(k=0;k<12;k++)
    {
    	char name[256];
    	vec_t angle = M_2PI * k / 12;
    	matrix3x4_t transform, next;

    	vec3Set(out,-0.5,-0.5,-0.5);
    	mat3x4Translate(transform,out);
    	mat3x4RotateX(next,angle);
    	mat3x4ConcatBefore(transform,next);
    	vec3Set(out,-1,-1,-1);
    	mat3x4Scale(transform,out);
    	mat3x4ConcatBefore(transform,next);
    	vec3Set(out,0.5,0.5,0.5);
    	mat3x4Translate(next,out);
    	mat3x4ConcatBefore(transform,next);
        
        for(i=0;i<fb->height;i++)
        {
        	for(j=0;j<fb->width;j++)
        	{
        		fbReadPixel3v(ref,j,i,rgb);
        		mat3x4VecMul(out,transform,rgb);
        		fbDrawPixel3v(fb,j,i,out);
        	}
        }
        sprintf(name,"out%02d.bmp",k);
        fbSave(fb,name);
	}
	memFree(fb);
	memFree(ref);

	return 0;
}
