That makes sense. The casting later to int (after doing a multiplication) makes me nervous though -- I can't help but wonder if that might be responsible for the artifacts you saw in the AVERAGE_3D_GRADIENT case. Maybe change that to round instead?