fixed possible overflow when calculating size of gradients array