Rasenschach 0.1.3 → 0.1.3.1
raw patch · 2 files changed
+50/−2 lines, 2 files
Files
- Rasenschach.cabal +2/−2
- Util.hs +48/−0
Rasenschach.cabal view
@@ -1,5 +1,5 @@ name: Rasenschach-version: 0.1.3+version: 0.1.3.1 cabal-version: >=1.2 build-type: Simple license: BSD3@@ -32,7 +32,7 @@ ObjectBehaviour GameFSM Message Rules Object BasicTypes Animate Helper Global Command BallFSM Main States AI Grid Parser Lineup Main Data.FSM Physics- Render AL Message Object BasicTypes Main States Render Main+ Render AL Message Object BasicTypes Main States Render Util Main ghc-prof-options: --enable-executable-profiling ghc-options: -O2
+ Util.hs view
@@ -0,0 +1,48 @@+module Util ( bitmapLoad, Image(..) ) where++import Data.Word ( Word8, Word32 )+import Data.Serialize.Get ( Get, runGet, getWord32le, getWord16le+ , skip, getByteString )+import qualified Data.ByteString as BS+import qualified Data.ByteString.Internal as BSI+import Foreign ( Ptr, pokeElemOff, peekElemOff, plusPtr+ , withForeignPtr )+ +data Image = Image !Int !Int !BS.ByteString++bitmapLoad :: FilePath -> IO (Maybe Image)+bitmapLoad f = do+ bs <- BS.readFile f+ case runGet getBitmap bs of+ Left err -> putStrLn err >> return Nothing+ Right i@(Image _ _ bytes) -> do+ let (ptr, offset, len) = BSI.toForeignPtr bytes+ withForeignPtr ptr $ bgr2rgb len offset+ return $! Just i++-- | Returns a bitmap in bgr format.+getBitmap :: Get Image+getBitmap = do+ skip 18+ width <- getWord32le+ height <- getWord32le+ _planes <- getWord16le+ bpp <- getWord16le+ let size = fromIntegral $+ width * height * (fromIntegral (bpp `div` 8) :: Word32) :: Int+ skip 24+ bgrBytes <- getByteString size+ return $! Image (fromIntegral width)+ (fromIntegral height)+ bgrBytes++bgr2rgb :: Int -> Int -> Ptr Word8 -> IO ()+bgr2rgb n o p = do+ mapM_ (\i -> do+ b <- peekElemOff p' (i+0)+ r <- peekElemOff p' (i+2)+ pokeElemOff p' (i+0) (r::Word8)+ pokeElemOff p' (i+2) (b::Word8))+ [0,3..n-3]+ where+ p' = p `plusPtr` o