Skip to content

Commit

Permalink
translateX/Y: fix negative translations
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jtdaugherty committed May 20, 2017
1 parent 60c0468 commit 887a8a1
Showing 1 changed file with 2 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Graphics/Vty/Image.hs
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,15 @@ translate x y i = translateX x (translateY y i)
-- | Translates an image by padding or cropping its left side.
translateX :: Int -> Image -> Image
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 = i
| otherwise = let h = imageHeight i in HorizJoin (BGFill x h) i (imageWidth i + x) h

-- | Translates an image by padding or cropping its top.
translateY :: Int -> Image -> Image
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 = i
| otherwise = let w = imageWidth i in VertJoin (BGFill w y) i w (imageHeight i + y)
Expand Down

0 comments on commit 887a8a1

Please sign in to comment.