Seems like you have problem with value of i
Your loop actually, reduced from 320/240 and each time you increment i by 3! which start from 54. so looks like it will soon go out of bound of img
for (int y = hi-1; y > 0; y--)
{
for (int x = wi-1; x > 0; x--)
{
i = i + 3;
}
}
Also, img allocation change to below.
img = new pix[wi, hi];