@@ -34,6 +34,8 @@ module Graphics.Vty.Image ( DisplayText
3434 , crop_top
3535 , pad
3636 , resize
37+ , resize_width
38+ , resize_height
3739 , translate
3840 -- | The possible display attributes used in constructing an `Image`.
3941 , module Graphics.Vty.Attributes
@@ -56,7 +58,7 @@ import Data.Word
5658infixr 5 <|>
5759infixr 4 <->
5860
59−-- | Currently append in the Monoid instance is equivalent to <->.
61+-- | Append in the Monoid instance is equivalent to <->.
6062instance Monoid Image where
6163 mempty = EmptyImage
6264 mappend = (<->)
@@ -229,6 +231,7 @@ empty_image :: Image
229231empty_image = EmptyImage
230232
231233-- | pad the given image. This adds background character fills to the left, top, right, bottom.
234+-- The pad values are how many display columns or rows to add.
232235pad :: Int -> Int -> Int -> Int -> Image -> Image
233236pad 0 0 0 0 i = i
234237pad in_l in_t in_r in_b in_image
@@ -273,12 +276,15 @@ crop_bottom h in_i
273276 | otherwise = go in_i
274277 where
275278 go EmptyImage = EmptyImage
276− go (CropBottom {cropped_image, output_width, output_height}) =
277− CropBottom cropped_image output_width (min h output_height)
279+ go i@(CropBottom {cropped_image, output_width, output_height})
280+ | output_height <= h = i
281+ | otherwise = CropBottom cropped_image output_width (min h output_height)
278282 go i
279283 | h >= image_height i = i
280284 | otherwise = CropBottom i (image_width i) h
281285
286+-- | ensure the image is no wider than the given width. If the image is wider then crop the right
287+-- side.
282288crop_right :: Int -> Image -> Image
283289crop_right 0 _ = EmptyImage
284290crop_right w in_i
@@ -286,20 +292,67 @@ crop_right w in_i
286292 | otherwise = go in_i
287293 where
288294 go EmptyImage = EmptyImage
289− go (CropRight {cropped_image, output_width, output_height}) =
290− CropRight cropped_image (min w output_width) output_height
295+ go i@(CropRight {cropped_image, output_width, output_height})
296+ | output_width <= w = i
297+ | otherwise = CropRight cropped_image (min w output_width) output_height
291298 go i
292299 | w >= image_width i = i
293300 | otherwise = CropRight i w (image_height i)
294301
302+-- | ensure the image is no wider than the given width. If the image is wider then crop the left
303+-- side.
295304crop_left :: Int -> Image -> Image
296−crop_left _ _ = error "not implemented"
305+crop_left 0 _ = EmptyImage
306+crop_left w in_i
307+ | w < 0 = error "cannot crop the width to less than zero"
308+ | otherwise = go in_i
309+ where
310+ go EmptyImage = EmptyImage
311+ go i@(CropLeft {cropped_image, left_skip, output_width, output_height})
312+ | output_width <= w = i
313+ | otherwise =
314+ let left_skip' = left_skip + w - output_width
315+ in CropLeft cropped_image left_skip' w output_height
316+ go i
317+ | image_width i <= w = i
318+ | otherwise = CropLeft i (image_width i - w) w (image_height i)
297319
320+-- | crop the display height. If the image is less than or equal in height then this operation has
321+-- no effect. Otherwise the image is cropped from the top.
298322crop_top :: Int -> Image -> Image
299−crop_top _ _ = error "not implemented"
323+crop_top 0 _ = EmptyImage
324+crop_top h in_i
325+ | h < 0 = error "cannot crop the height to less than zero"
326+ | otherwise = go in_i
327+ where
328+ go EmptyImage = EmptyImage
329+ go i@(CropTop {cropped_image, top_skip, output_width, output_height})
330+ | output_height <= h = i
331+ | otherwise =
332+ let top_skip' = top_skip + h - output_height
333+ in CropTop cropped_image top_skip' output_width h
334+ go i
335+ | image_height i <= h = i
336+ | otherwise = CropTop i (image_height i - h) (image_width i) h
300337
301338-- | Generic resize. Pads and crops as required to assure the given display width and height.
302−-- This is biased to pad the right and bottom.
339+-- This is biased to pad/crop the right and bottom.
303340resize :: Int -> Int -> Image -> Image
304−resize _w _h _i = error "not implemented yet"
341+resize w h i = resize_height h (resize_width w i)
342+
343+-- | Resize the width. Pads and crops as required to assure the given display width.
344+-- This is biased to pad/crop the right.
345+resize_width :: Int -> Image -> Image
346+resize_width w i = case w `compare` image_width i of
347+ LT -> crop_right w i
348+ EQ -> i
349+ GT -> i <|> BGFill (w - image_width i) (image_height i)
350+
351+-- | Resize the height. Pads and crops as required to assure the given display height.
352+-- This is biased to pad/crop the bottom.
353+resize_height :: Int -> Image -> Image
354+resize_height h i = case h `compare` image_height i of
355+ LT -> crop_bottom h i
356+ EQ -> i
357+ GT -> i <-> BGFill (image_width i) (h - image_height i)
305358