testBit :: a -> Int -> Bool checks whether a value has a one at a specified bit offset. For binary bitmap parsing, read a Word8 and test its eight offsets individually. section 12.2.3
Extracting eight bitmap pixels
A bitmap parser defines parseBitAtIndex n = Single $ if testBit w n then 1 else 0, then tests offsets 7 through 0. This returns eight pixel values in most-significant-to-least-significant order. section 12.2.3
Listing 12.27 A parser for binary-encoded bitmaps
p4PixelP ::
Parser
( RawPixel,
RawPixel,
RawPixel,
RawPixel,
RawPixel,
RawPixel,
RawPixel,
RawPixel
)
p4PixelP = do
w <- anyWord8 #1
let parseBitAtIndex n =
Single $ if testBit w n then 1 else 0 #2
return
( parseBitAtIndex 7, #3
parseBitAtIndex 6, #3
parseBitAtIndex 5, #3
parseBitAtIndex 4, #3
parseBitAtIndex 3, #3
parseBitAtIndex 2, #3
parseBitAtIndex 1, #3
parseBitAtIndex 0 #3
) #3
Inspect the complete binary bitmap parser, including the local testBit helper and its eight returned pixel positions. section 12.2.3
