4

Haskell を使用してバイナリ形式 (PES) を解析しようとしています。

import qualified Data.ByteString.Lazy as BL
import Data.Word
import Data.Word.Word24
import qualified Data.ByteString.Lazy.Char8 as L8

data Stitch = MyCoord Int Int deriving (Eq, Show)

data PESFile = PESFile {
      pecstart :: Word24
    , width :: Int
    , height :: Int
    , numColors :: Int
    , header :: String
    , stitches :: [Stitch]
    } deriving (Eq, Show)


readPES :: BL.ByteString -> Maybe PESFile
readPES bs =
        let s = L8.drop 7 bs
            pecstart = L8.readInt s in
            case pecstart of
        Nothing -> Nothing
        Just (offset,rest) ->   Just (PESFile offset 1 1 1 "#PES" [])

main = do
  input <- BL.getContents
  print $ readPES input

他のデータ (幅、高さ、ステッチ) のオフセットを取得するために pecstart を読み取る必要があります。ビット版。

別のアプローチを使用する必要がありますか? Data.Binary パッケージは単純な形式に適しているようですが、ファイル内の他のデータのオフセットを見つけるために値を読み取る必要があるため、このようなものでどのように機能するかはわかりません。私が行方不明の何か?

4

1 に答える 1

6

さて、3バイトのインデックスを作成することで24ビット値を解析できます(ここではネットワーク順に):

import qualified Data.ByteString as B
import Data.ByteString (ByteString, index)
import Data.Bits
import Data.Int
import Data.Word

type Int24 = Int32

readInt24 :: ByteString -> (Int24, ByteString)
readInt24 bs = (roll [a,b,c], B.drop 3 bs)
   where a = bs `index` 0
         b = bs `index` 1
         c = bs `index` 2

roll :: [Word8] -> Int24
roll   = foldr unstep 0
  where
    unstep b a = a `shiftL` 8 .|. fromIntegral b
于 2011-06-04T20:12:17.080 に答える