packages feed

babl-0.0.0.1: src/BABL/FFI/Format.hs

-- FFI module for handling pixel formats
module BABL.FFI.Format
  ( BablFormatPtr(..)
  , BablFormatDummy
  , ComponentModel(..)
  , ComponentFormat(..)
  , PixelFormat(..)
  , c_babl_format
  , c_babl_get_bpp
  , c_babl_format_get_n_components
  ) where

import Foreign
import Foreign.C.Types (CInt(..))
import Foreign.C.String
import Foreign.Ptr (Ptr)

-- | The dummy Pointer behind 'BablFormatPtr'. Do not 'peek' or 'poke'
type BablFormatDummy = Ptr ()
data BablFormatPtr = BablFormatPtr
  { unwrap :: BablFormatDummy }

-- | List of component models af babl formats.
data ComponentModel
  = RGB
  | RGBA
  | RaGaBaA
  | R'G'B'
  | R'G'B'A
  | R'aG'aB'aA
  | Y
  | YA
  | YaA
  | Y'
  | Y'A
  | Y'aA
  | Y'CbCr
  | Y'CbCrA
  | HSVA
  | HSV
  | CMYK
  | CMY
  | CIELab
  | CIELabAlpha
  | CIELCHab
  | CIELCHabAlpha
  | HSLA
  | HSL
  | Y'CbCr709
  | Y'CbCrA709
  | Cairo

instance Show ComponentModel where
  show RGB           = "RGB"
  show RGBA          = "RGBA"
  show RaGaBaA       = "RaGaBaA"
  show R'G'B'        = "R'G'B'"
  show R'G'B'A       = "R'G'B'A"
  show R'aG'aB'aA    = "R'aG'aB'aA"
  show Y             = "Y"
  show YA            = "YA"
  show YaA           = "YaA"
  show Y'            = "Y'"
  show Y'A           = "Y'A"
  show Y'aA          = "Y'aA"
  show Y'CbCr        = "Y'CbCr"
  show Y'CbCrA       = "Y'CbCrA"
  show HSVA          = "HSVA"
  show HSV           = "HSV"
  show CMYK          = "CMYK"
  show CMY           = "CMY"
  show CIELab        = "CIE Lab"
  show CIELabAlpha   = "CIE Lab alpha"
  show CIELCHab      = "CIE LCH(ab)"
  show CIELCHabAlpha = "CIE LCH(ab) alpha"
  show HSLA          = "HSLA"
  show HSL           = "HSL"
  show Y'CbCr709     = "Y'CbCr709"
  show Y'CbCrA709    = "Y'CbCrA709"
  show Cairo         = error "no show for Cairo model"

-- | Byte format of a colour component.
data ComponentFormat
  = CFhalf
  | CFfloat
  | CFdouble
  | CFu8
  | CFu15
  | CFu16
  | CFu32
  | A8
  | RGB24
  | ARGB32

instance Show ComponentFormat where
  show CFhalf   = "half"
  show CFfloat  = "float"
  show CFdouble = "double"
  show CFu8     = "u8"
  show CFu15    = "u15"
  show CFu16    = "u16"
  show CFu32    = "u32"
  show _        = error "no show here"

data PixelFormat
  = PixelFormat
    { pfComponents :: ComponentModel
    , pfType       :: ComponentFormat
    }

-- | create a new predefined BABL format
foreign import ccall unsafe "babl.h babl_format"
  c_babl_format
    :: CString -- ^ Format name
    -> IO BablFormatDummy

-- | create and define your own BABL format
-- foreign import ccall unsafe "babl.h babl_format_new"

-- | Get the number of bytes per pixel from a format.
foreign import ccall unsafe "babl.h babl_format_get_bytes_per_pixel"
  c_babl_get_bpp
    :: BablFormatDummy -- ^ Format to be drawn from
    -> IO CInt

foreign import ccall unsafe "babl.h babl_format_get_n_components"
  c_babl_format_get_n_components
    :: BablFormatDummy
    -> IO CInt