Skip to content

Instantly share code, notes, and snippets.

@lehins
Created December 8, 2016 00:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lehins/3876e09dfc3f111ddf644c82e47da42c to your computer and use it in GitHub Desktop.
Save lehins/3876e09dfc3f111ddf644c82e47da42c to your computer and use it in GitHub Desktop.
Image processing sample in Haskell using HIP
-- install Haskell Image Processing library using `$ cabal install hip`
import qualified Graphics.Image as I
import Graphics.Image.Processing (rotate180)
import qualified Graphics.Image.Interface as IM
import qualified Graphics.Image.Interface.Repa as R
import Graphics.Image.ColorSpace
import Graphics.Image.Types
-- | Rotate a cat image while reading it in Double precision.
rotateCat0 :: IO ()
rotateCat0 = do
cat <- I.readImageRGB "before-rotation.png"
I.writeImage "after-rotation.png" $ rotate180 cat
-- | Rotate a cat image in parallel while reading it in Double precision.
rotateCat1 :: IO ()
rotateCat1 = do
cat <- R.readImageRGB "before-rotation.png"
I.writeImage "after-rotation.png" $ R.computeP $ rotate180 cat
-- | Rotate a cat image, while using vector an reading it in native 8-bit (per
-- channel) precision
rotateCat2 :: IO ()
rotateCat2 = do
Right cat <- I.readImageExact PNG "before-rotation.png"
I.writeImageExact PNG [] "after-rotation.png" $ rotate180 (cat :: Image VU RGB Word8)
-- | Rotate a cat image in parallel, while using vector an reading it in native
-- 8-bit (per channel) precision
rotateCat3 :: IO ()
rotateCat3 = do
Right cat <- I.readImageExact PNG "before-rotation.png"
I.writeImageExact PNG [] "after-rotation.png" $ R.computeP $ rotate180 (cat :: Image RD RGB Word8)
originalFnc :: (Int, Int) -> Pixel RGB Word8
originalFnc (y, x) =
let (q, r) = x `quotRem` max 10 y
s = fromIntegral . min 0xff
in PixelRGB (s q) (s r) (s (q + r + 30))
-- | Last example of creating a cool looking gradient like image. Normally, in
-- image processing, Double precision is preferable, so that is why it is default
-- choice for pixels, but it is still possible to create and manipulate images
-- with arbitrary precision, by using `makeImage` from
-- `Graphics.Image.Interface` and specifying the type.
grad :: IO ()
grad = do
I.writeImageExact PNG [] "grad.png" (IM.makeImage (1200, 1200) originalFnc :: Image VU RGB Word8)
main :: IO ()
main = grad
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment