Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Floodfill example implemented

  • Loading branch information...
commit af18cc6d0c17b4e45c723c2d57c38ef5d2d8e9f5 1 parent b142542
@qrilka qrilka authored
View
3  .gitignore
@@ -0,0 +1,3 @@
+cabal-dev
+dist
+TAGS
View
4 Graphics/ImageMagick/MagickCore.hs
@@ -0,0 +1,4 @@
+module Graphics.ImageMagick.MagickCore
+ ( module G ) where
+
+import Graphics.ImageMagick.MagickCore.Option as G
View
18 Graphics/ImageMagick/MagickCore/FFI/Option.hsc
@@ -0,0 +1,18 @@
+{-# LANGUAGE CPP #-}
+{-# LANGUAGE ForeignFunctionInterface #-}
+
+module Graphics.ImageMagick.MagickCore.FFI.Option
+ where
+
+import Foreign.C.String
+import Foreign.C.Types
+
+import Graphics.ImageMagick.MagickWand.FFI.Types
+
+#include <magick/MagickCore.h>
+
+-- | ParseChannelOption() parses channel type string representation
+
+foreign import ccall "ParseChannelOption" parseChannelOption
+ :: CString -> IO ChannelType
+
View
14 Graphics/ImageMagick/MagickCore/Option.hs
@@ -0,0 +1,14 @@
+module Graphics.ImageMagick.MagickCore.Option
+ ( parseChannelOption
+ ) where
+
+import Control.Monad.IO.Class
+import Control.Monad.Trans.Resource
+import Data.ByteString (ByteString)
+
+import qualified Graphics.ImageMagick.MagickCore.FFI.Option as F
+import Graphics.ImageMagick.MagickWand.FFI.Types
+
+
+parseChannelOption :: (MonadResource m) => ByteString -> m ChannelType
+parseChannelOption s = liftIO $ useAsCString s F.parseChannelOption
View
1  Graphics/ImageMagick/MagickWand.hs
@@ -1,6 +1,7 @@
module Graphics.ImageMagick.MagickWand
( module G ) where
+import Graphics.ImageMagick.MagickCore as G
import Graphics.ImageMagick.MagickWand.MagickWand as G
import Graphics.ImageMagick.MagickWand.PixelIterator as G
import Graphics.ImageMagick.MagickWand.PixelPacket as G
View
5 Graphics/ImageMagick/MagickWand/FFI/PixelWand.hsc
@@ -1,4 +1,5 @@
{-# LANGUAGE CPP, ForeignFunctionInterface #-}
+
module Graphics.ImageMagick.MagickWand.FFI.PixelWand
where
@@ -11,14 +12,18 @@ import Graphics.ImageMagick.MagickWand.FFI.Types
#include <wand/MagickWand.h>
-- | DestroyPixelWand() deallocates resources associated with a PixelWand.
+
foreign import ccall "DestroyPixelWand" destroyPixelWand
:: Ptr PixelWand -> IO (Ptr PixelWand)
+
-- | PixelGetMagickColor() gets the magick color of the pixel wand.
+
foreign import ccall "PixelGetMagickColor" pixelGetMagickColor
:: Ptr PixelWand -> Ptr (MagickPixelPacket) -> IO ()
-- | PixelSetMagickColor() sets the color of the pixel wand.
+
foreign import ccall "PixelSetMagickColor" pixelSetMagickColor
:: Ptr PixelWand -> Ptr (MagickPixelPacket) -> IO ()
View
30 Graphics/ImageMagick/MagickWand/FFI/WandImage.hsc
@@ -1,15 +1,16 @@
-{-# LANGUAGE CPP, ForeignFunctionInterface #-}
+{-# LANGUAGE CPP #-}
+{-# LANGUAGE ForeignFunctionInterface #-}
module Graphics.ImageMagick.MagickWand.FFI.WandImage
where
-import Foreign
-import Foreign.C.Types
+import Foreign
+import Foreign.C.Types
-import Graphics.ImageMagick.MagickWand.FFI.Types
+import Graphics.ImageMagick.MagickWand.FFI.Types
#include <wand/MagickWand.h>
--- | MagickGetImageHeight() returns the image height.
+-- | MagickGetImageHeight() returns the image height.
foreign import ccall "MagickGetImageHeight" magickGetImageHeight
:: Ptr MagickWand -> IO (CSize)
@@ -34,8 +35,8 @@ foreign import ccall "MagickGetImageBackgroundColor" magickGetImageBackgroundCol
foreign import ccall "MagickSetImageBackgroundColor" magickSetImageBackgroundColor
:: Ptr MagickWand -> Ptr PixelWand -> IO (MagickBooleanType)
--- | MagickExtentImage() extends the image as defined by the geometry, gravity,
--- and wand background color. Set the (x,y) offset of the geometry to move the
+-- | MagickExtentImage() extends the image as defined by the geometry, gravity,
+-- and wand background color. Set the (x,y) offset of the geometry to move the
-- original wand relative to the extended wand.
foreign import ccall "MagickExtentImage" magickExtentImage
:: Ptr MagickWand -- ^ wand
@@ -45,3 +46,18 @@ foreign import ccall "MagickExtentImage" magickExtentImage
-> CSize -- ^ y offset
-> IO ()
+-- | MagickFloodfillPaintImage() changes the color value of any pixel
+-- that matches target and is an immediate neighbor. If the method FillToBorderMethod
+-- is specified, the color value is changed for any neighbor pixel that does
+-- not match the bordercolor member of image.
+foreign import ccall "MagickFloodfillPaintImage" magickFloodfillPaintImage
+ :: Ptr MagickWand -- ^ wand
+ -> ChannelType -- ^ channel
+ -> Ptr PixelWand -- ^ fill
+ -> CDouble -- ^ fuzz
+ -> Ptr PixelWand -- ^ bordercolor
+ -> CSize -- ^ x offset
+ -> CSize -- ^ y offset
+ -> MagickBooleanType -- ^ invert
+ -> IO (MagickBooleanType)
+
View
4 Graphics/ImageMagick/MagickWand/Utils.hs
@@ -1,5 +1,6 @@
module Graphics.ImageMagick.MagickWand.Utils
( fromMBool
+ , toMBool
)
where
@@ -10,3 +11,6 @@ import Graphics.ImageMagick.MagickWand.FFI.Types
fromMBool :: (MonadResource m) => IO MagickBooleanType -> m Bool
fromMBool = liftM (==mTrue) . liftIO
+
+toMBool :: Bool -> MagickBooleanType
+toMBool v = if v then mTrue else mFalse
View
5 Graphics/ImageMagick/MagickWand/WandImage.hs
@@ -7,6 +7,7 @@ module Graphics.ImageMagick.MagickWand.WandImage
, getImageBackgroundColor
, setImageBackgroundColor
, extentImage
+ , floodfillPaintImage
) where
import Control.Monad.IO.Class
@@ -47,3 +48,7 @@ setImageBackgroundColor w p = fromMBool $! F.magickSetImageBackgroundColor w p
extentImage :: (MonadResource m) => PMagickWand -> Int -> Int -> Int -> Int -> m ()
extentImage w width height offsetX offsetY =
liftIO $ F.magickExtentImage w (fromIntegral width) (fromIntegral height) (fromIntegral offsetX) (fromIntegral offsetY)
+
+floodfillPaintImage :: (MonadResource m) => PMagickWand -> ChannelType -> PPixelWand -> Double -> PPixelWand -> Int -> Int -> Bool -> m Bool
+floodfillPaintImage w channel fill fuzz border x y invert =
+ fromMBool $! F.magickFloodfillPaintImage w channel fill (realToFrac fuzz) border (fromIntegral x) (fromIntegral y) (toMBool invert)
View
28 examples/floodfill.hs
@@ -0,0 +1,28 @@
+{-# LANGUAGE OverloadedStrings #-}
+-- | Example taken from: http://members.shaw.ca/el.supremo/MagickWand/floodfill.htm
+--
+-- Replace the white background area of 1st argument with transparent and don't forget
+-- that for this the channel must be "rgba" and the output image must be PNG
+-- or other format which supports transparency
+
+import Filesystem.Path.CurrentOS (decodeString)
+import Graphics.ImageMagick.MagickWand
+import System.Environment (getArgs)
+
+main = do
+ [img,img'] <- getArgs
+ withMagickWandGenesis $ do
+ (_,w) <- magickWand
+ readImage w (decodeString img)
+
+ fc <- pixelWand
+ bc <- pixelWand
+
+ fc `setColor` "none"
+ bc `setColor` "white"
+
+ channel <- parseChannelOption "rgba"
+
+ floodfillPaintImage w channel fc 20 bc 0 0 False
+
+ writeImages w (decodeString img') True
View
28 imagemagick.cabal
@@ -18,7 +18,8 @@ Cabal-version: >=1.2
Library
- Exposed-modules: Graphics.ImageMagick.MagickWand
+ Exposed-modules: Graphics.ImageMagick.MagickCore
+ , Graphics.ImageMagick.MagickWand
Build-depends: base
, resourcet == 0.3.*
@@ -29,7 +30,8 @@ Library
extensions: EmptyDataDecls
Ghc-options: -Wall
pkgconfig-depends: ImageMagick
- Other-modules: Graphics.ImageMagick.MagickWand.FFI.ImageDrawing
+ Other-modules: Graphics.ImageMagick.MagickCore.FFI.Option
+ , Graphics.ImageMagick.MagickWand.FFI.ImageDrawing
, Graphics.ImageMagick.MagickWand.FFI.MagickWand
, Graphics.ImageMagick.MagickWand.FFI.PixelIterator
, Graphics.ImageMagick.MagickWand.FFI.PixelWand
@@ -38,3 +40,25 @@ Library
, Graphics.ImageMagick.MagickWand.FFI.WandProperties
Build-tools: hsc2hs
+
+executable floodfill
+ Main-is: examples/floodfill.hs
+ Build-depends: base
+ , resourcet == 0.3.*
+ , transformers == 0.3.*
+ , vector == 0.9.*
+ , bytestring == 0.9.*
+ , system-filepath == 0.4.*
+
+ extensions: EmptyDataDecls
+ Ghc-options: -Wall
+ pkgconfig-depends: ImageMagick, MagickWand
+ Other-modules: Graphics.ImageMagick.MagickCore.FFI.Option
+ , Graphics.ImageMagick.MagickWand.FFI.ImageDrawing
+ , Graphics.ImageMagick.MagickWand.FFI.MagickWand
+ , Graphics.ImageMagick.MagickWand.FFI.PixelIterator
+ , Graphics.ImageMagick.MagickWand.FFI.PixelWand
+ , Graphics.ImageMagick.MagickWand.FFI.Types
+ , Graphics.ImageMagick.MagickWand.FFI.WandImage
+ , Graphics.ImageMagick.MagickWand.FFI.WandProperties
+ Build-tools: hsc2hs
Please sign in to comment.
Something went wrong with that request. Please try again.