on master help sign in
translateX/Y: fix negative translations

This change makes it so that translating an image negatively will return an empty image if the negative translation completely hides the input image. Previously we'd just return an image with a negative height, which not only violates the requirement that all images have height no less than zero, but it caused downstream bugs in mutable vector accesses due to negative vector indices.

commit887a8a1e1008c9d1cec22c88d010f89badc2523c authorJonathan Daugherty <cygnus@foobox.com> dateSat 20 May 2017, 08:13 (9 years ago) parent60c0468
1 file changed+2
src/Graphics/Vty/Image.hs+2
src/Graphics/Vty/Image.hs+2
@@ -242,6 +242,7 @@ translate x y i = translateX x (translateY y i)
242242-- | Translates an image by padding or cropping its left side.
243243translateX :: Int -> Image -> Image
244244translateX x i
245+ | x < 0 && (abs x > imageWidth i) = emptyImage
245246 | x < 0 = let s = abs x in CropLeft i s (imageWidth i - s) (imageHeight i)
246247 | x == 0 = i
247248 | otherwise = let h = imageHeight i in HorizJoin (BGFill x h) i (imageWidth i + x) h
@@ -249,6 +250,7 @@ translateX x i
249250-- | Translates an image by padding or cropping its top.
250251translateY :: Int -> Image -> Image
251252translateY y i
253+ | y < 0 && (abs y > imageHeight i) = emptyImage
252254 | y < 0 = let s = abs y in CropTop i s (imageWidth i) (imageHeight i - s)
253255 | y == 0 = i
254256 | otherwise = let w = imageWidth i in VertJoin (BGFill w y) i w (imageHeight i + y)