Sorry, my mistake, it's a different bug.
Code:
cdf[0] = array1[i];
At this point, i is either 262144 or 65536 having just come out of the "if (no_of_rows == 512 && no_of_cols == 512)" block. So array1[i] is undefined, and all your values after that are critically dependent on this, so you get garbage.
metamofia like this