repa-devil 0.3.2 → 0.3.2.1
raw patch · 3 files changed
+52/−33 lines, 3 filesdep ~basesetup-changed
Dependency ranges changed: base
Files
- Data/Array/Repa/IO/DevIL.hsc +42/−24
- Setup.hs +1/−0
- repa-devil.cabal +9/−9
Data/Array/Repa/IO/DevIL.hsc view
@@ -19,7 +19,7 @@ -- -- * Image format parsing is determined by the filepath extension type. ----- * Only RGB, RGBA and Greyscale images are supported.+-- * Only RGB, RGBA, BGR, BGRA and Greyscale images are supported. -- -- Example: read a .png file into a repa array, and write it out as a .jpg --@@ -36,7 +36,7 @@ module Data.Array.Repa.IO.DevIL ( -- * The Image array type Image (..)- + -- * The IL monad , IL, runIL @@ -76,12 +76,14 @@ -- ---------------------------------------------------------------------- --- | RGBA and RGB images are 3D repa arrays where indices are +-- | RGBA, RGB, BGRA and BGR images are 3D repa arrays where indices are -- /Z :. row :. column :. color channel/. Grey images are 2D repa arrays. -- -- The origin (/Z :. 0 :. 0/) is on the lower left point of the image. data Image = RGBA (Array F DIM3 Word8) | RGB (Array F DIM3 Word8)+ | BGRA (Array F DIM3 Word8)+ | BGR (Array F DIM3 Word8) | Grey (Array F DIM2 Word8) -- | The IL monad. Provides statically-guaranteed access to an initialized IL@@ -105,7 +107,7 @@ -- > x <- runIL $ readImage "/tmp/x.png" -- > .. operations on x .. -- --- /Note:/ The image input type is determined by the filename extension. +-- /Note:/ The image input type is determined by the filename extension. readImage :: FilePath -> IL Image readImage f = liftIO $ do name <- ilGenImageName@@ -120,7 +122,7 @@ -- | Writes an 'Image' to a file. The image array must be represented as foreign -- buffers. You can use 'copyS' or 'copyP' to convert the array. -- --- /Note:/ The image output type is determined by the filename extension. +-- /Note:/ The image output type is determined by the filename extension. writeImage :: FilePath -> Image -> IL () writeImage f i = liftIO $ do name <- ilGenImageName@@ -138,9 +140,9 @@ -- ---------------------------------------------------------------------- -foreign import ccall "ilInit" ilInitC :: IO ()-foreign import ccall "ilOriginFunc" ilOriginFuncC :: ILenum -> IO ILboolean-foreign import ccall "ilEnable" ilEnableC :: ILenum -> IO ILboolean+foreign import ccall unsafe "ilInit" ilInitC :: IO ()+foreign import ccall unsafe "ilOriginFunc" ilOriginFuncC :: ILenum -> IO ILboolean+foreign import ccall unsafe "ilEnable" ilEnableC :: ILenum -> IO ILboolean -- | Initialize the library. ilInit :: IO ()@@ -152,7 +154,7 @@ return () {-# INLINE ilInit #-} -foreign import ccall "ilGenImages" ilGenImagesC+foreign import ccall unsafe "ilGenImages" ilGenImagesC :: ILsizei -> Ptr ILuint -> IO () -- | Allocates a new image name.@@ -164,25 +166,27 @@ return $! ImageName name {-# INLINE ilGenImageName #-} -foreign import ccall "ilBindImage" ilBindImageC :: ILuint -> IO ()+foreign import ccall unsafe "ilBindImage" ilBindImageC :: ILuint -> IO () -- | Sets the image name as the current image for processing. ilBindImage :: ImageName -> IO () ilBindImage (ImageName name) = ilBindImageC name {-# INLINE ilBindImage #-} -foreign import ccall "ilLoadImage" ilLoadImageC :: CString -> IO ILboolean+foreign import ccall unsafe "ilLoadImage" ilLoadImageC :: CString -> IO ILboolean -- | Loads the image as the current DevIL image name. ilLoadImage :: FilePath -> IO Bool ilLoadImage f = (0 /=) <$> withCString f ilLoadImageC {-# INLINE ilLoadImage #-} -foreign import ccall "ilGetInteger" ilGetIntegerC :: ILenum -> IO ILint+foreign import ccall unsafe "ilGetInteger" ilGetIntegerC :: ILenum -> IO ILint -il_RGB, il_RGBA, il_LUMINANCE :: ILint+il_RGB, il_RGBA, il_BGR, il_BGRA, il_LUMINANCE :: ILint il_RGB = (#const IL_RGB) il_RGBA = (#const IL_RGBA)+il_BGR = (#const IL_BGR)+il_BGRA = (#const IL_BGRA) il_LUMINANCE = (#const IL_LUMINANCE) il_IMAGE_HEIGHT, il_IMAGE_WIDTH, il_IMAGE_FORMAT, il_UNSIGNED_BYTE :: ILenum@@ -191,7 +195,7 @@ il_IMAGE_FORMAT = (#const IL_IMAGE_FORMAT) il_UNSIGNED_BYTE = (#const IL_UNSIGNED_BYTE) -foreign import ccall "ilGetData" ilGetDataC :: IO (Ptr ILubyte)+foreign import ccall unsafe "ilGetData" ilGetDataC :: IO (Ptr ILubyte) -- | Puts the current image inside a repa array. toRepa :: ImageName -> IO Image@@ -201,10 +205,10 @@ let (width, height) = (fromIntegral width', fromIntegral height') format <- ilGetIntegerC il_IMAGE_FORMAT pixels <- ilGetDataC- + -- Destroys the image when the array will be garbage collected managedPixels <- newForeignPtr pixels (ilDeleteImage name) - + return $! imageFromFormat format width height managedPixels where -- Create an 'Image' object with the right format.@@ -213,13 +217,17 @@ RGB $! fromForeignPtr (Z :. height :. width :. 3) managedPixels | format == il_RGBA = RGBA $! fromForeignPtr (Z :. height :. width :. 4) managedPixels+ | format == il_BGR =+ BGR $! fromForeignPtr (Z :. height :. width :. 3) managedPixels+ | format == il_BGRA =+ BGRA $! fromForeignPtr (Z :. height :. width :. 4) managedPixels | format == il_LUMINANCE = Grey $! fromForeignPtr (Z :. height :. width) managedPixels | otherwise = error "Unsupported image format." {-# INLINE imageFromFormat #-} -foreign import ccall "ilTexImage" ilTexImageC+foreign import ccall unsafe "ilTexImage" ilTexImageC :: ILuint -> ILuint -> ILuint -- w h depth -> ILubyte -> ILenum -> ILenum -- numberOfChannels format type -> Ptr () -- data (copy from this pointer)@@ -228,23 +236,33 @@ -- | Copies the repa array to the current image buffer. fromRepa :: Image -> IO Bool fromRepa (RGB i) =- let Z :. h :. w :. _ = extent i + let Z :. h :. w :. _ = extent i in (0 /=) <$> (withForeignPtr (toForeignPtr i) $ \p ->- ilTexImageC (fromIntegral w) (fromIntegral h) 1 3 + ilTexImageC (fromIntegral w) (fromIntegral h) 1 3 (fromIntegral il_RGB) il_UNSIGNED_BYTE (castPtr p)) fromRepa (RGBA i) =- let Z :. h :. w :. _ = extent i + let Z :. h :. w :. _ = extent i in (0 /=) <$> (withForeignPtr (toForeignPtr i) $ \p ->- ilTexImageC (fromIntegral w) (fromIntegral h) 1 4 + ilTexImageC (fromIntegral w) (fromIntegral h) 1 4 (fromIntegral il_RGBA) il_UNSIGNED_BYTE (castPtr p))+fromRepa (BGR i) =+ let Z :. h :. w :. _ = extent i+ in (0 /=) <$> (withForeignPtr (toForeignPtr i) $ \p ->+ ilTexImageC (fromIntegral w) (fromIntegral h) 1 3 + (fromIntegral il_BGR) il_UNSIGNED_BYTE (castPtr p))+fromRepa (BGRA i) =+ let Z :. h :. w :. _ = extent i+ in (0 /=) <$> (withForeignPtr (toForeignPtr i) $ \p ->+ ilTexImageC (fromIntegral w) (fromIntegral h) 1 4 + (fromIntegral il_BGRA) il_UNSIGNED_BYTE (castPtr p)) fromRepa (Grey i) =- let Z :. h :. w = extent i + let Z :. h :. w = extent i in (0 /=) <$> (withForeignPtr (toForeignPtr i) $ \p -> ilTexImageC (fromIntegral w) (fromIntegral h) 1 1 (fromIntegral il_LUMINANCE) il_UNSIGNED_BYTE (castPtr p)) -foreign import ccall "ilSaveImage" ilSaveImageC :: CString -> IO ILboolean+foreign import ccall unsafe "ilSaveImage" ilSaveImageC :: CString -> IO ILboolean -- | Saves the current image. ilSaveImage :: FilePath -> IO Bool@@ -252,7 +270,7 @@ (0 /=) <$> withCString file ilSaveImageC {-# INLINE ilSaveImage #-} -foreign import ccall "ilDeleteImages" ilDeleteImagesC+foreign import ccall unsafe "ilDeleteImages" ilDeleteImagesC :: ILsizei -> Ptr ILuint -> IO () -- | Releases an image with its name.
Setup.hs view
@@ -1,2 +1,3 @@ import Distribution.Simple+ main = defaultMain
repa-devil.cabal view
@@ -1,5 +1,5 @@ Name: repa-devil-Version: 0.3.2+Version: 0.3.2.1 Synopsis: Support for image reading and writing of Repa arrays using in-place FFI calls Description: The repa-image library adds support for reading and@@ -13,7 +13,7 @@ interface. DevIL can load, save, convert, manipulate, filter and display a wide variety of image formats, including:- . + . * BMP, ICO, JPG, PNG, PNM, TGA, TIF, GIF, EXIF and many more. . /References:/@@ -38,22 +38,22 @@ Library Exposed-modules: Data.Array.Repa.IO.DevIL- - Build-depends: - base >= 4.5 && < 5.0,++ Build-depends:+ base == 4.*, repa >= 3.2.0.0 && < 4.0, transformers >= 0.2 && < 0.4- + Build-tools: hsc2hs- + GHC-Options: -O2 -Wall- + Extensions: CPP, ForeignFunctionInterface - Extra-Libraries: + Extra-Libraries: IL source-repository head