module Graphics.UI.WXCore.Image
(
topLevelWindowSetIconFromFile
, imageListAddIconsFromFiles
, imageListAddIconFromFile
, withIconFromFile
, iconCreateFromFile
, iconGetSize
, withCursorFromFile
, cursorCreateFromFile
, withBitmapFromFile
, bitmapCreateFromFile
, bitmapGetSize
, bitmapSetSize
, imageTypeFromExtension
, imageTypeFromFileName
, imageGetPixels
, imageCreateFromPixels
, imageGetPixelArray
, imageCreateFromPixelArray
, imageGetSize
, withImageData
, imageCreateFromPixelBuffer
, imageGetPixelBuffer
, withPixelBuffer
, PixelBuffer
, pixelBufferCreate
, pixelBufferDelete
, pixelBufferInit
, pixelBufferSetPixel
, pixelBufferGetPixel
, pixelBufferSetPixels
, pixelBufferGetPixels
, pixelBufferGetSize
) where
import Data.Char( toLower )
import Data.Array.IArray ( IArray, listArray, bounds, elems )
import Foreign.Marshal.Array
import Graphics.UI.WXCore.WxcTypes
import Graphics.UI.WXCore.WxcDefs
import Graphics.UI.WXCore.WxcClasses
import Graphics.UI.WXCore.Types
imageListAddIconsFromFiles :: ImageList a -> Size -> [FilePath] -> IO ()
imageListAddIconsFromFiles images desiredSize fnames
= mapM_ (imageListAddIconFromFile images desiredSize) fnames
imageListAddIconFromFile :: ImageList a -> Size -> FilePath -> IO ()
imageListAddIconFromFile images desiredSize fname
= do image <- imageCreateFromFile fname
imageRescale image desiredSize
bitmap <- imageConvertToBitmap image
_ <- imageListAddBitmap images bitmap nullBitmap
bitmapDelete bitmap
imageDelete image
return ()
topLevelWindowSetIconFromFile :: TopLevelWindow a -> FilePath -> IO ()
topLevelWindowSetIconFromFile f fname
= withIconFromFile fname sizeNull (topLevelWindowSetIcon f)
withIconFromFile :: FilePath -> Size -> (Icon () -> IO a) -> IO a
withIconFromFile fname size f
= bracket (iconCreateFromFile fname size)
(iconDelete)
f
iconCreateFromFile :: FilePath -> Size -> IO (Icon ())
iconCreateFromFile fname size
= iconCreateLoad fname (imageTypeFromFileName fname) size
iconGetSize :: Icon a -> IO Size
iconGetSize icon
= do w <- iconGetWidth icon
h <- iconGetHeight icon
return (sz w h)
withCursorFromFile :: FilePath -> (Cursor () -> IO a) -> IO a
withCursorFromFile fname f
= bracket (cursorCreateFromFile fname)
(cursorDelete)
f
cursorCreateFromFile :: String -> IO (Cursor ())
cursorCreateFromFile fname = imageCreateFromFile fname >>= cursorCreateFromImage
withBitmapFromFile :: FilePath -> (Bitmap () -> IO a) -> IO a
withBitmapFromFile fname f
= bracket (bitmapCreateFromFile fname)
(bitmapDelete)
f
bitmapCreateFromFile :: FilePath -> IO (Bitmap ())
bitmapCreateFromFile fname
= bitmapCreateLoad fname (imageTypeFromFileName fname)
bitmapGetSize :: Bitmap a -> IO Size
bitmapGetSize bitmap
= do w <- bitmapGetWidth bitmap
h <- bitmapGetHeight bitmap
return (sz w h)
bitmapSetSize :: Bitmap a -> Size -> IO ()
bitmapSetSize bitmap (Size w h)
= do bitmapSetWidth bitmap w
bitmapSetHeight bitmap h
imageTypeFromFileName :: String -> BitFlag
imageTypeFromFileName fname
= imageTypeFromExtension (map toLower (reverse (takeWhile (/= '.') (reverse fname))))
imageTypeFromExtension :: String -> BitFlag
imageTypeFromExtension ext
= case ext of
"jpg" -> wxBITMAP_TYPE_JPEG
"jpeg" -> wxBITMAP_TYPE_JPEG
"gif" -> wxBITMAP_TYPE_GIF
"bmp" -> wxBITMAP_TYPE_BMP
"png" -> wxBITMAP_TYPE_PNG
"xpm" -> wxBITMAP_TYPE_XPM
"xbm" -> wxBITMAP_TYPE_XBM
"pcx" -> wxBITMAP_TYPE_PCX
"ico" -> wxBITMAP_TYPE_ICO
"tif" -> wxBITMAP_TYPE_TIF
"tiff" -> wxBITMAP_TYPE_TIF
"pnm" -> wxBITMAP_TYPE_PNM
"pict" -> wxBITMAP_TYPE_PICT
"icon" -> wxBITMAP_TYPE_ICON
"ani" -> wxBITMAP_TYPE_ANI
_other -> wxBITMAP_TYPE_ANY
data PixelBuffer = PixelBuffer Bool Size (Ptr Word8)
pixelBufferCreate :: Size -> IO PixelBuffer
pixelBufferCreate size
= do buffer <- wxcMalloc (sizeW size * sizeH size * 3)
return (PixelBuffer True size (ptrCast buffer))
pixelBufferDelete :: PixelBuffer -> IO ()
pixelBufferDelete (PixelBuffer owned _size buffer)
= when (owned && not (ptrIsNull buffer)) (wxcFree buffer)
pixelBufferGetSize :: PixelBuffer -> Size
pixelBufferGetSize (PixelBuffer _owned size _buffer)
= size
pixelBufferGetPixels :: PixelBuffer -> IO [Color]
pixelBufferGetPixels (PixelBuffer _owned (Size w h) buffer)
= do let count = w*h
rgbs <- peekArray (3*count) buffer
return (convert rgbs)
where
convert :: [Word8] -> [Color]
convert (r:g:b:xs) = colorRGB r g b : convert xs
convert [] = []
convert _ =
error $ "Graphics.UI.WXCore.Image.pixelBufferGetPixels: " ++
"Unexpected number of entries in pixelbuffer"
pixelBufferSetPixels :: PixelBuffer -> [Color] -> IO ()
pixelBufferSetPixels (PixelBuffer _owned (Size w h) buffer) colors
= do let count = w*h
pokeArray buffer (convert (take count colors))
where
convert :: [Color] -> [Word8]
convert (c:cs) = colorRed c : colorGreen c : colorBlue c : convert cs
convert [] = []
pixelBufferInit :: PixelBuffer -> Color -> IO ()
pixelBufferInit (PixelBuffer _owned size buffer) color
= wxcInitPixelsRGB buffer size (intFromColor color)
pixelBufferSetPixel :: PixelBuffer -> Point -> Color -> IO ()
pixelBufferSetPixel (PixelBuffer _owned size buffer) poynt color
=
wxcSetPixelRGB buffer (sizeW size) poynt (intFromColor color)
pixelBufferGetPixel :: PixelBuffer -> Point -> IO Color
pixelBufferGetPixel (PixelBuffer _owned size buffer) poynt
=
do colr <- wxcGetPixelRGB buffer (sizeW size) poynt
return (colorFromInt colr)
imageCreateFromPixelBuffer :: PixelBuffer -> IO (Image ())
imageCreateFromPixelBuffer (PixelBuffer _owned size buffer)
= imageCreateFromDataEx size buffer False
withImageData :: Image a -> (Ptr () -> IO b) -> IO b
withImageData image f = do
pixels <- imageGetData image
x <- f pixels
image `seq` return x
withPixelBuffer :: Image a -> (PixelBuffer -> IO b) -> IO b
withPixelBuffer image f =
withImageData image $ \ptr -> do
w <- imageGetWidth image
h <- imageGetHeight image
f $ PixelBuffer False (sz w h) (ptrCast ptr)
imageGetPixelBuffer :: Image a -> IO PixelBuffer
imageGetPixelBuffer image
= withPixelBuffer image return
imageGetPixels :: Image a -> IO [Color]
imageGetPixels image
= withPixelBuffer image pixelBufferGetPixels
imageCreateFromPixels :: Size -> [Color] -> IO (Image ())
imageCreateFromPixels size colors
= do pb <- pixelBufferCreate size
pixelBufferSetPixels pb colors
imageCreateFromPixelBuffer pb
imageGetPixelArray :: (IArray a Color) => Image b -> IO (a Point Color)
imageGetPixelArray image
= do h <- imageGetHeight image
w <- imageGetWidth image
ps <- imageGetPixels image
let bounds' = (pointZero, point (w1) (h1))
return (listArray bounds' ps)
imageCreateFromPixelArray :: (IArray a Color) => a Point Color -> IO (Image ())
imageCreateFromPixelArray pixels
= let (Point x y) = snd (bounds pixels)
in imageCreateFromPixels (sz (x+1) (y+1)) (elems pixels)
imageGetSize :: Image a -> IO Size
imageGetSize image
= do h <- imageGetHeight image
w <- imageGetWidth image
return (Size w h)