on master help sign in
Add operation for cropping an image to size
9b5dd97Ben Boeckel
src/Graphics/Vty/Image.hs+10
@@ -19,6 +19,7 @@ module Graphics.Vty.Image ( Image(..)
19 , char_fill
19 , char_fill
20 , empty_image
20 , empty_image
21 , translate
21 , translate
22+ , crop
22 -- | The possible display attributes used in constructing an `Image`.
23 -- | The possible display attributes used in constructing an `Image`.
23 , module Graphics.Vty.Attributes
24 , module Graphics.Vty.Attributes
24 )
25 )
@@ -99,6 +100,8 @@ data Image =
99 -- Any image of zero size equals the empty image.
100 -- Any image of zero size equals the empty image.
100 | EmptyImage
101 | EmptyImage
101 | Translation (Int, Int) Image
102 | Translation (Int, Int) Image
103+ -- Crop an image to a size
104+ | ImageCrop (Int, Int) Image
102 deriving Eq
105 deriving Eq
103
106
104instance Show Image where
107instance Show Image where
@@ -112,6 +115,8 @@ instance Show Image where
112 = "VertJoin (" ++ show c ++ ", " ++ show r ++ ") ( " ++ show t ++ " ) <-> ( " ++ show b ++ " )"
115 = "VertJoin (" ++ show c ++ ", " ++ show r ++ ") ( " ++ show t ++ " ) <-> ( " ++ show b ++ " )"
113 show ( Translation offset i )
116 show ( Translation offset i )
114 = "Translation ( " ++ show offset ++ ", " ++ show i ++ " )"
117 = "Translation ( " ++ show offset ++ ", " ++ show i ++ " )"
118+ show ( ImageCrop size i )
119+ = "ImageCrop ( " ++ show size ++ ", " ++ show i ++ " )"
115 show ( EmptyImage ) = "EmptyImage"
120 show ( EmptyImage ) = "EmptyImage"
116
121
117-- | Currently append in the Monoid instance is equivalent to <->. Future versions will just stack
122-- | Currently append in the Monoid instance is equivalent to <->. Future versions will just stack
@@ -203,6 +208,7 @@ image_width VertJoin { output_width = w } = w
203image_width BGFill { output_width = w } = w
208image_width BGFill { output_width = w } = w
204image_width EmptyImage = 0
209image_width EmptyImage = 0
205image_width ( Translation _v i ) = image_width i
210image_width ( Translation _v i ) = image_width i
211+image_width ( ImageCrop _v i ) = min (image_width i) (toEnum $ fst _v)
206
212
207-- | The height of an Image. This is the number of display rows the image will occupy.
213-- | The height of an Image. This is the number of display rows the image will occupy.
208image_height :: Image -> Word
214image_height :: Image -> Word
@@ -212,6 +218,7 @@ image_height VertJoin { output_height = r } = r
212image_height BGFill { output_height = r } = r
218image_height BGFill { output_height = r } = r
213image_height EmptyImage = 0
219image_height EmptyImage = 0
214image_height ( Translation _v i ) = image_height i
220image_height ( Translation _v i ) = image_height i
221+image_height ( ImageCrop _v i ) = min (image_height i) (toEnum $ fst _v)
215
222
216-- | Combines two images side by side.
223-- | Combines two images side by side.
217--
224--
@@ -328,3 +335,6 @@ empty_image = EmptyImage
328translate :: (Int, Int) -> Image -> Image
335translate :: (Int, Int) -> Image -> Image
329translate v i = Translation v i
336translate v i = Translation v i
330
337
338+crop :: (Int, Int) -> Image -> Image
339+crop v i = ImageCrop v i
340+
src/Graphics/Vty/Picture.hs+1
@@ -16,6 +16,7 @@ module Graphics.Vty.Picture ( module Graphics.Vty.Picture
16 , char_fill
16 , char_fill
17 , empty_image
17 , empty_image
18 , translate
18 , translate
19+ , crop
19 -- | The possible display attributes used in constructing an `Image`.
20 -- | The possible display attributes used in constructing an `Image`.
20 , module Graphics.Vty.Attributes
21 , module Graphics.Vty.Attributes
21 )
22 )
src/Graphics/Vty/Span.hs+4
@@ -152,6 +152,10 @@ ops_for_row mrow_ops bg region image y remaining_columns
152 else width
152 else width
153 forM_ [y .. y + actual_height - 1] $ \y' -> snoc_bg_fill mrow_ops bg actual_width y'
153 forM_ [y .. y + actual_height - 1] $ \y' -> snoc_bg_fill mrow_ops bg actual_width y'
154 Translation _offset i -> ops_for_row mrow_ops bg region i y remaining_columns
154 Translation _offset i -> ops_for_row mrow_ops bg region i y remaining_columns
155+ ImageCrop _size i ->
156+ if y >= (toEnum $ snd _size)
157+ then return ()
158+ else ops_for_row mrow_ops bg region i y (min remaining_columns $ toEnum $ fst _size)
155
159
156snoc_text_span :: (Foldable.Foldable t)
160snoc_text_span :: (Foldable.Foldable t)
157 => Attr
161 => Attr