fixed bug in image gradient calculation