module Graphics.TextureContainer.PKM where
import Control.Applicative
import qualified Data.ByteString as B
import Data.Packer
import Data.Word
import Graphics.TextureContainer.KTX
data Pkm = Pkm
{ pkmName :: FilePath
, pkmContent :: B.ByteString
, pkmVersion :: Word16
, pkmType :: Word16
, pkmWidth :: Word16
, pkmHeight :: Word16
, pkmActiveW :: Word16
, pkmActiveH :: Word16
, pkmImage :: B.ByteString
} deriving Show
unpackPkm :: FilePath -> B.ByteString -> Unpacking Pkm
unpackPkm name orig = do
0x204d4b50 <- getWord32
let w = getWord16BE
pkm <- Pkm name orig <$> w <*> w <*> w <*> w <*> w <*> w
let size = pkmActiveW (pkm "") * pkmActiveH (pkm "") `div` 2
pkm <$> getBytes (fromIntegral size)
pkmToKtx :: Pkm -> Ktx
pkmToKtx Pkm{..} = Ktx
{ ktxName = pkmName
, ktxContent = pkmContent
, ktxGlType = 0
, ktxGlTypeSize = 1
, ktxGlFormat = 0
, ktxGlInternalFormat = fromPkmType pkmVersion pkmType
, ktxGlBaseInternalFormat = 0
, ktxPixelWidth = fromIntegral pkmWidth
, ktxPixelHeight = fromIntegral pkmHeight
, ktxPixelDepth = 0
, ktxNumElems = 0
, ktxNumFaces = 1
, ktxNumMipLevels = 1
, ktxMap = []
, ktxImage = [[pkmImage]]
}
where
fromPkmType 12592 0 = 36196
fromPkmType 12592 1 = undefined
fromPkmType 12592 2 = undefined
fromPkmType 12592 3 = undefined
fromPkmType 12848 0 = undefined
fromPkmType 12848 1 = undefined
fromPkmType 12848 2 = undefined
fromPkmType 12848 3 = undefined
fromPkmType 12848 4 = undefined
fromPkmType 12848 5 = undefined
fromPkmType 12848 6 = undefined
fromPkmType 12848 7 = undefined
fromPkmType 12848 8 = undefined
pkmFromFile :: FilePath -> IO Pkm
pkmFromFile path = B.readFile path >>= return . readPkm path
readPkm :: FilePath
-> B.ByteString -> Pkm
readPkm path bs = runUnpacking (unpackPkm path bs) bs