--------------------------------------------------------------------------------
-- Module      : Data.Bitmap.Pure.File
-- Version     : 0.0.2
-- License     : BSD3
-- Copyright   : (c) 2009-2010 Balazs Komuves
-- Author      : Balazs Komuves
-- Maintainer  : bkomuves (plus) hackage (at) gmail (dot) com
-- Stability   : experimental
-- Portability : requires FFI and CPP
-- Tested with : GHC 6.10.1
--------------------------------------------------------------------------------

-- | Saving and loading uncompressed bitmaps.
-- For loading from compressed formats, see the @stb-image@ library:
-- <http://hackage.haskell.org/package/stb-image>.
--
-- The goal of this module is to provide the simplest possible interface 
-- for loading and saving bitmaps; so you can start experimenting
-- without much hassle.
-- 
-- Note: Endianness is the endianness of the host, so the resulting file is 
-- not portable across platforms with different endiannesses.
-- 
-- See the module "Data.Bitmap.IO.File" for the file format.
--

{-# LANGUAGE CPP, ForeignFunctionInterface #-}
module Data.Bitmap.Pure.File
  ( readBitmap
  , writeBitmap
  , readRawData
  , writeRawData
  ) 
  where

--------------------------------------------------------------------------------

import Control.Applicative
import Control.Monad
import Foreign
import System.IO

import Data.Bitmap.Base
import Data.Bitmap.Internal
import Data.Bitmap.IO
import qualified Data.Bitmap.IO.File as IO

--------------------------------------------------------------------------------

readBitmap :: PixelComponent t => FilePath -> IO (Bitmap t)
readBitmap :: FilePath -> IO (Bitmap t)
readBitmap FilePath
fpath = IOBitmap t -> Bitmap t
forall t. IOBitmap t -> Bitmap t
unIOBitmap (IOBitmap t -> Bitmap t) -> IO (IOBitmap t) -> IO (Bitmap t)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FilePath -> IO (IOBitmap t)
forall t. PixelComponent t => FilePath -> IO (IOBitmap t)
IO.readBitmap FilePath
fpath
  
readRawData :: PixelComponent t => FilePath -> (Size,NChn,PixelComponentType) -> IO (Bitmap t) 
readRawData :: FilePath -> (Size, NChn, PixelComponentType) -> IO (Bitmap t)
readRawData FilePath
fpath (Size, NChn, PixelComponentType)
header = IOBitmap t -> Bitmap t
forall t. IOBitmap t -> Bitmap t
unIOBitmap (IOBitmap t -> Bitmap t) -> IO (IOBitmap t) -> IO (Bitmap t)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FilePath -> (Size, NChn, PixelComponentType) -> IO (IOBitmap t)
forall t.
PixelComponent t =>
FilePath -> (Size, NChn, PixelComponentType) -> IO (IOBitmap t)
IO.readRawData FilePath
fpath (Size, NChn, PixelComponentType)
header

--------------------------------------------------------------------------------

writeBitmap :: PixelComponent t => FilePath -> Bitmap t -> IO ()
writeBitmap :: FilePath -> Bitmap t -> IO ()
writeBitmap FilePath
fpath Bitmap t
bm = FilePath -> IOBitmap t -> IO ()
forall t. PixelComponent t => FilePath -> IOBitmap t -> IO ()
IO.writeBitmap FilePath
fpath (Bitmap t -> IOBitmap t
forall t. Bitmap t -> IOBitmap t
IOBitmap Bitmap t
bm)

-- | Saves only the raw pixel data, no resolution etc.
writeRawData :: PixelComponent t => FilePath -> Bitmap t -> IO ()
writeRawData :: FilePath -> Bitmap t -> IO ()
writeRawData FilePath
fpath Bitmap t
bm = FilePath -> IOBitmap t -> IO ()
forall t. PixelComponent t => FilePath -> IOBitmap t -> IO ()
IO.writeRawData FilePath
fpath (Bitmap t -> IOBitmap t
forall t. Bitmap t -> IOBitmap t
IOBitmap Bitmap t
bm)

--------------------------------------------------------------------------------