module Vision.Image.HSV.Type (
HSV, HSVPixel (..), HSVDelayed
) where
import Control.Applicative ((<$>), (<*>))
import Data.Word
import Foreign.Storable (Storable (..))
import Foreign.Ptr (castPtr, plusPtr)
import Vision.Image.Interpolate (Interpolable (..))
import Vision.Image.Type (Pixel (..), Manifest, Delayed)
data HSVPixel = HSVPixel {
hsvHue :: !Word8, hsvSat :: !Word8
, hsvValue :: !Word8
} deriving (Eq, Show)
type HSV = Manifest HSVPixel
type HSVDelayed = Delayed HSVPixel
instance Storable HSVPixel where
sizeOf _ = 3 * sizeOf (undefined :: Word8)
alignment _ = alignment (undefined :: Word8)
peek !ptr =
let !ptr' = castPtr ptr
in HSVPixel <$> peek ptr' <*> peek (ptr' `plusPtr` 1)
<*> peek (ptr' `plusPtr` 2)
poke !ptr HSVPixel { .. } =
let !ptr' = castPtr ptr
in poke ptr' hsvHue >>
poke (ptr' `plusPtr` 1) hsvSat >>
poke (ptr' `plusPtr` 2) hsvValue
instance Pixel HSVPixel where
type PixelChannel HSVPixel = Word8
pixNChannels _ = 3
pixIndex !(HSVPixel h _ _) 0 = h
pixIndex !(HSVPixel _ s _) 1 = s
pixIndex !(HSVPixel _ _ v) _ = v
instance Interpolable HSVPixel where
interpol f a b =
let HSVPixel aHue aSat aVal = a
HSVPixel bHue bSat bVal = b
in HSVPixel {
hsvHue = f aHue bHue, hsvSat = f aSat bSat
, hsvValue = f aVal bVal
}