1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
{-# OPTIONS_HADDOCK hide #-}
module Codec.BMP.Unpack
(unpackBMPToRGBA32)
where
import Codec.BMP.Base
import Codec.BMP.BitmapInfo
import Codec.BMP.BitmapInfoV3
import Foreign.Ptr
import Foreign.Marshal.Alloc
import Foreign.Storable
import System.IO.Unsafe
import Data.Word
import Data.ByteString as BS
import Data.ByteString.Unsafe as BS
import Prelude as P
-- | Unpack a BMP image to a string of RGBA component values.
unpackBMPToRGBA32 :: BMP -> ByteString
unpackBMPToRGBA32 bmp
= let info = getBitmapInfoV3 $ bmpBitmapInfo bmp
width = fromIntegral $ dib3Width info
height = fromIntegral $ dib3Height info
flipX = dib3HeightFlipped info
bitCount = dib3BitCount info
in case bitCount of
24 -> packRGB24ToRGBA32 width height flipX (bmpRawImageData bmp)
32 -> packRGB32ToRGBA32 width height flipX (bmpRawImageData bmp)
_ -> error "Codec.BMP: Unhandled bitcount."
-- | Unpack raw, uncompressed 24 bit BMP image data to a string of
-- RGBA component values.
--
-- The alpha component is set to 255 for every pixel.
packRGB24ToRGBA32
:: Int -- ^ Width of image.
-> Int -- ^ Height of image.
-> Bool -- ^ Image data is flipped along the X axis.
-> ByteString -- ^ Input string.
-> ByteString
packRGB24ToRGBA32 width height flipX str
= let -- Number of bytes per line in the source file,
-- including padding bytes.
srcBytesPerLine = BS.length str `div` height
sizeDest = width * height * 4
-- We allow padding bytes on the end of the image data.
in if BS.length str < height * srcBytesPerLine
then error "Codec.BMP: Image data is truncated."
else unsafePerformIO
$ allocaBytes sizeDest $ \bufDest ->
BS.unsafeUseAsCString str $ \bufSrc ->
do packRGB24ToRGBA32'
width height flipX
srcBytesPerLine
(castPtr bufSrc) (castPtr bufDest)
packCStringLen (bufDest, sizeDest)
-- We're doing this via Ptrs because we don't want to take the
-- overhead of doing the bounds checks in ByteString.index.
packRGB24ToRGBA32' width height flipX srcBytesPerLine ptrSrc ptrDst
= go 0
where
go posY
-- we've finished the image.
| posY == height
= return ()
-- Image source data is flipped along the X axis.
| flipX
= let !oSrc = srcBytesPerLine * (height - 1 - posY)
!oDst = width * 4 * posY
in go_line 0 posY oSrc oDst
-- Image source data is in the natural order.
| otherwise
= let !oSrc = srcBytesPerLine * posY
!oDst = width * 4 * posY
in go_line 0 posY oSrc oDst
go_line posX posY oSrc oDst
-- move to the next line.
| posX == width
= go (posY + 1)
-- process a pixel.
| otherwise
= do blue :: Word8 <- peekByteOff ptrSrc (oSrc + 0)
green :: Word8 <- peekByteOff ptrSrc (oSrc + 1)
red :: Word8 <- peekByteOff ptrSrc (oSrc + 2)
pokeByteOff ptrDst (oDst + 0) red
pokeByteOff ptrDst (oDst + 1) green
pokeByteOff ptrDst (oDst + 2) blue
pokeByteOff ptrDst (oDst + 3) (255 :: Word8)
go_line (posX + 1) posY (oSrc + 3) (oDst + 4)
-- | Unpack raw, uncompressed 32 bit BMP image data to a string of
-- RGBA component values.
-- Note in the BMP file the components are arse-around ABGR instead of RGBA.
-- The 'unpacking' here is really just flipping the components around.
packRGB32ToRGBA32
:: Int -- ^ Width of image.
-> Int -- ^ Height of image.
-> Bool -- ^ Image data is flipped along the X axis.
-> ByteString -- ^ Input string.
-> ByteString
packRGB32ToRGBA32 width height flipX str
= let sizeDest = height * width * 4
in if BS.length str < sizeDest
then error "Codec.BMP: Image data is truncated."
else unsafePerformIO
$ allocaBytes sizeDest $ \bufDest ->
BS.unsafeUseAsCString str $ \bufSrc ->
do packRGB32ToRGBA32' width height
flipX
(castPtr bufSrc) (castPtr bufDest)
packCStringLen (bufDest, sizeDest)
-- We're doing this via Ptrs because we don't want to take the
-- overhead of doing the bounds checks in ByteString.index.
packRGB32ToRGBA32' width height flipX ptrSrc ptrDst
= go 0
where
go posY
-- we've finished the image.
| posY == height
= return ()
-- Image source data is flipped along the X axis.
| flipX
= let !oSrc = width * 4 * (height - 1 - posY)
!oDst = width * 4 * posY
in go_line 0 posY oSrc oDst
-- Image source data is in the natural order.
| otherwise
= let !oSrc = width * 4 * posY
!oDst = width * 4 * posY
in go_line 0 posY oSrc oDst
go_line posX posY oSrc oDst
-- move to the next line.
| posX == width
= go (posY + 1)
-- process a pixel.
| otherwise
= do blue :: Word8 <- peekByteOff ptrSrc (oSrc + 0)
green :: Word8 <- peekByteOff ptrSrc (oSrc + 1)
red :: Word8 <- peekByteOff ptrSrc (oSrc + 2)
alpha :: Word8 <- peekByteOff ptrSrc (oSrc + 3)
pokeByteOff ptrDst (oDst + 0) red
pokeByteOff ptrDst (oDst + 1) green
pokeByteOff ptrDst (oDst + 2) blue
pokeByteOff ptrDst (oDst + 3) alpha
go_line (posX + 1) posY (oSrc + 4) (oDst + 4)
|