// (c) 2005-2006 Criterion Software Limited and its licensors. All Rights Reserved
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

bool isP2(int val)
{
	return !(val & (val-1));
}

int intlog2(int val)
{
	return (int)(log10((float)val)/log10(2.0f)+0.0000001);
}

int p2(int count, int x, int y, int width, int height)
{
	if (isP2(height))
	{
		if (isP2(width))
		{
			printf("-crop%d,%d,%d,%d -x%d -y%d\n",x,y,width,height,x,y);
			return count+1;
		}
		else
		{
			int p2width = 1<<intlog2(width);
			count = p2(count, x,y, p2width, height);
			count = p2(count, x+p2width,y, width-p2width, height);
		}			
	}
	else
	{
		int p2height = 1<<intlog2(height);
		count = p2(count, x,y, width, p2height);
		count = p2(count, x,y+p2height, width, height-p2height);
	}			
	return count;
}

int main(int argc, char *argv[])
{
	if (argc<3)
		printf("p2er width height\n");
	int width = atoi(argv[1]);
	int height = atoi(argv[2]);
	int count = p2(0,0,0,width,height);
	return count;
}
