jPix is a new, very promising, open source bitmap editor. As the project is in a very alpha stage of development, currently jPix supports only 3 colors: white, grey and black. Other features of jPix include drawing straight lines with grey color and floodfill with black color. However, the most promising and coolest feature of jPix is the ability to work with images of vast sizes.
To draw lines jPix uses the following advanced algorithm:
void draw_line(int x1, int y1, int x2, int y2)
{
int x_middle = (x1+x2)/2;
int y_middle = (y1+y2)/2;
set_color(GREY);
put_pixel(x_middle, y_middle);
if((x1!=x2) || (y1!=y2))
{
if(abs(x1-x2) > abs(y1-y2))
{
if(x1<x2)
{
draw_line(x1, y1, x_middle, y_middle);
draw_line(x_middle+1, y_middle, x2, y2);
}
else // x1>x2
{
draw_line(x1, y1, x_middle+1, y_middle);
draw_line(x_middle, y_middle, x2, y2);
}
}
else
{
if(y1<y2)
{
draw_line(x1, y1, x_middle, y_middle);
draw_line(x_middle, y_middle+1, x2, y2);
}
else // y1>y2
{
draw_line(x1, y1, x_middle, y_middle+1);
draw_line(x_middle, y_middle, x2, y2);
}
}
}
}
One of the features that is urgently needed among its users is the ability to report the number of pixels of each color in the image. The developers of jPix, unable to come up with an efficient solution, asked us, the IPSC team, for help. Since we are lost too, we ask you to help us.
A jPix image is X pixels wide and Y pixels high. The pixel (0,0) is the upper left corner, and the pixel (X-1,Y-1) is the lower right corner. When jPix starts, the image is completely white.
The user is allowed to draw several line segments using grey color. The segments, however, must form a simple polygon. (See the input specification for a more precise definition.) Then the user must issue the flood fill command, starting from pixel (0,0); the fill color is black. (Since jPix is quite buggy, any other sequence of commands crashes jPix.)
Given the list of the commands issued by the user, your task is to implement the requested feature which will count the number of pixels of each color.