on master help sign in
Add operation for cropping an image to size
commit9b5dd97a3ff79c20b5d513d25a635e3ba3149375 authorBen Boeckel <MathStuf@gmail.com> dateWed 30 Mar 2011, 22:53 (16 years ago) parent4fae8bf
3 files changed+15
src/Graphics/Vty/Image.hs+10 src/Graphics/Vty/Picture.hs+1 src/Graphics/Vty/Span.hs+4
src/Graphics/Vty/Image.hs+10
@@ -19,6 +19,7 @@ module Graphics.Vty.Image ( Image(..)
1919 , char_fill
2020 , empty_image
2121 , translate
22+ , crop
2223 -- | The possible display attributes used in constructing an `Image`.
2324 , module Graphics.Vty.Attributes
2425 )
@@ -99,6 +100,8 @@ data Image =
99100 -- Any image of zero size equals the empty image.
100101 | EmptyImage
101102 | Translation (Int, Int) Image
103+ -- Crop an image to a size
104+ | ImageCrop (Int, Int) Image
102105 deriving Eq
103106
104107instance Show Image where
@@ -112,6 +115,8 @@ instance Show Image where
112115 = "VertJoin (" ++ show c ++ ", " ++ show r ++ ") ( " ++ show t ++ " ) <-> ( " ++ show b ++ " )"
113116 show ( Translation offset i )
114117 = "Translation ( " ++ show offset ++ ", " ++ show i ++ " )"
118+ show ( ImageCrop size i )
119+ = "ImageCrop ( " ++ show size ++ ", " ++ show i ++ " )"
115120 show ( EmptyImage ) = "EmptyImage"
116121
117122-- | 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
203208image_width BGFill { output_width = w } = w
204209image_width EmptyImage = 0
205210image_width ( Translation _v i ) = image_width i
211+image_width ( ImageCrop _v i ) = min (image_width i) (toEnum $ fst _v)
206212
207213-- | The height of an Image. This is the number of display rows the image will occupy.
208214image_height :: Image -> Word
@@ -212,6 +218,7 @@ image_height VertJoin { output_height = r } = r
212218image_height BGFill { output_height = r } = r
213219image_height EmptyImage = 0
214220image_height ( Translation _v i ) = image_height i
221+image_height ( ImageCrop _v i ) = min (image_height i) (toEnum $ fst _v)
215222
216223-- | Combines two images side by side.
217224--
@@ -328,3 +335,6 @@ empty_image = EmptyImage
328335translate :: (Int, Int) -> Image -> Image
329336translate v i = Translation v i
330337
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
1616 , char_fill
1717 , empty_image
1818 , translate
19+ , crop
1920 -- | The possible display attributes used in constructing an `Image`.
2021 , module Graphics.Vty.Attributes
2122 )
src/Graphics/Vty/Span.hs+4
@@ -152,6 +152,10 @@ ops_for_row mrow_ops bg region image y remaining_columns
152152 else width
153153 forM_ [y .. y + actual_height - 1] $ \y' -> snoc_bg_fill mrow_ops bg actual_width y'
154154 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)
155159
156160snoc_text_span :: (Foldable.Foldable t)
157161 => Attr