Fixed bug related with image's edges.

Coordinates of top left corner of the image are (0, 0).  Coordinates of bottom right corner are (img.width-1, img.height-1), not (img.width, img.height). So there was not quite right cropping and extending of line.