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.
This commit is contained in:
Jonathan Daugherty 2017-05-20 08:13:47 -07:00
parent 60c0468f91
commit 887a8a1e10

View File

@ -242,6 +242,7 @@ translate x y i = translateX x (translateY y i)
-- | Translates an image by padding or cropping its left side. -- | Translates an image by padding or cropping its left side.
translateX :: Int -> Image -> Image translateX :: Int -> Image -> Image
translateX x i translateX x i
| x < 0 && (abs x > imageWidth i) = emptyImage
| x < 0 = let s = abs x in CropLeft i s (imageWidth i - s) (imageHeight i) | x < 0 = let s = abs x in CropLeft i s (imageWidth i - s) (imageHeight i)
| x == 0 = i | x == 0 = i
| otherwise = let h = imageHeight i in HorizJoin (BGFill x h) i (imageWidth i + x) h | otherwise = let h = imageHeight i in HorizJoin (BGFill x h) i (imageWidth i + x) h
@ -249,6 +250,7 @@ translateX x i
-- | Translates an image by padding or cropping its top. -- | Translates an image by padding or cropping its top.
translateY :: Int -> Image -> Image translateY :: Int -> Image -> Image
translateY y i translateY y i
| y < 0 && (abs y > imageHeight i) = emptyImage
| y < 0 = let s = abs y in CropTop i s (imageWidth i) (imageHeight i - s) | y < 0 = let s = abs y in CropTop i s (imageWidth i) (imageHeight i - s)
| y == 0 = i | y == 0 = i
| otherwise = let w = imageWidth i in VertJoin (BGFill w y) i w (imageHeight i + y) | otherwise = let w = imageWidth i in VertJoin (BGFill w y) i w (imageHeight i + y)