Assuming that the windows progress from left to right and then top to bottom, the pseudo code may be as follow:
consider four attributes of each window, namely, bg_top
, bg_left
, bg_width
, bg_hight
, grd_top
... etc.
also considering a inner window will never go outside crossing the outer window,
set all windows left and top to image's left and top, that may be (0,0).
now the loop
while(bg_top+bg_hight <= image_top+image_hight)
{
while(bg_laft+bg_width <= image_left+image_width)
{
while(grd_top+gdr_hight <= bg_top+bg_hight)
{
while(grd_left+gdr_width <= bg_left+bg_width)
//some pixels may be left out if the inner and outer window sizes are not divisible, it will not change the window size to fit in the last case.
{
while(target_top+target_hight <= grd_top+grd_hight)
{
while(target_left+target_width <= grd_left+grd_width)
// the condition will move till end but never goes outside nor changes the inner window size to fit
{
//DO THE PROCESSING
//target_left+=target_width; //if they do not overlap
target_left+=1; //if they overlap
}
target_top+=target_hight// don't overlap. use 1 for overlaping
// use 1 if it goes down 1 pixel
}
grd_left+=grd_width; //or 1
}
grd_top+=grd_hight; //or 1
}
bg_left+=bg_width; //or 1
}
bg_top+=bg_hight; //or 1
}