I have this double for-loop, where I have both row-order and column-order array indexing, which should be bad for performance.
for (int row = 0; row < h
Isn't this the cause of your data skew? All the negative values are being zeroed out:
if (d < 0) { d = 0; }