Corrected bug

- Some offset propagation can lead to segmentation faults due to out of
  bounds index.
- Corrected by adding bound checking before computing patch distance.
- Segmentation fault does not appear anymore.