module Graphics.Image.ColorSpace.Binary (
Binary(..), Bit(..), on, off, isOn, isOff, fromBool, complement
) where
import Prelude hiding (map)
import Data.Word (Word8)
import Graphics.Image.Interface
import Data.Typeable (Typeable)
import Foreign.Ptr
import Foreign.Storable
data Binary = Binary deriving (Eq, Enum, Show, Typeable)
newtype Bit = Bit Word8 deriving (Ord, Eq, Typeable)
data instance Pixel Binary e = PixelBinary !e deriving (Ord, Eq)
instance Show (Pixel Binary Bit) where
show (PixelBinary (Bit 0)) = "<Binary:(0)>"
show _ = "<Binary:(1)>"
on :: Pixel Binary Bit
on = PixelBinary (Bit 1)
off :: Pixel Binary Bit
off = PixelBinary (Bit 0)
fromBool :: Bool -> Pixel Binary Bit
fromBool False = off
fromBool True = on
isOn :: Pixel Binary Bit -> Bool
isOn (PixelBinary (Bit 0)) = False
isOn _ = True
isOff :: Pixel Binary Bit -> Bool
isOff = not . isOn
complement :: Pixel Binary Bit -> Pixel Binary Bit
complement = fromBool . isOff
instance ColorSpace Binary Bit where
type Components Binary Bit = Bit
broadcastC = PixelBinary
fromComponents = PixelBinary
toComponents (PixelBinary b) = b
getPxC (PixelBinary b) _ = b
setPxC (PixelBinary _) _ b = PixelBinary b
mapPxC f (PixelBinary b) = PixelBinary (f Binary b)
mapPx f (PixelBinary b) = PixelBinary (f b)
zipWithPx f (PixelBinary b1) (PixelBinary b2) = PixelBinary (f b1 b2)
foldrPx f z (PixelBinary b) = f b z
instance Elevator Bit where
toWord8 (Bit 0) = 0
toWord8 _ = maxBound
toWord16 (Bit 0) = 0
toWord16 _ = maxBound
toWord32 (Bit 0) = 0
toWord32 _ = maxBound
toWord64 (Bit 0) = 0
toWord64 _ = maxBound
toFloat (Bit 0) = 0
toFloat _ = 1
toDouble (Bit 0) = 0
toDouble _ = 1
fromDouble 0 = Bit 0
fromDouble _ = Bit 1
instance Num Bit where
(Bit 0) + (Bit 0) = Bit 0
_ + _ = Bit 1
_ (Bit 1) = Bit 0
_ _ = Bit 1
_ * (Bit 0) = Bit 0
(Bit 0) * _ = Bit 0
_ * _ = Bit 1
abs = id
signum = id
fromInteger 0 = Bit 0
fromInteger _ = Bit 1
instance Num (Pixel Binary Bit) where
(+) = zipWithPx (+)
() = zipWithPx ()
(*) = zipWithPx (*)
abs = mapPx abs
signum = mapPx signum
fromInteger = broadcastC . fromInteger
instance Storable Bit where
sizeOf _ = sizeOf (undefined :: Word8)
alignment _ = alignment (undefined :: Word8)
peek p = do
q <- return $ castPtr p
b <- peek q
return (Bit b)
poke p (Bit b) = do
q <- return $ castPtr p
poke q b
instance Storable (Pixel Binary Bit) where
sizeOf _ = sizeOf (undefined :: Bit)
alignment _ = alignment (undefined :: Bit)
peek p = do
q <- return $ castPtr p
b <- peek q
return (PixelBinary b)
poke p (PixelBinary b) = do
q <- return $ castPtr p
poke q b