Reading and writing uncompressed 24 bit BMP files. We only handle Windows V3 file headers, but this is the most common.
To write a file do something like:
do let rgba = Data.ByteString.pack [some list of Word8s] let bmp = packRGBA32ToBMP width height rgba writeBMP fileName bmp
To read a file do something like:
do Right bmp <- readBMP fileName let rgba = unpackBMPToRGBA32 bmp let (width, height) = bmpDimensions bmp ...
- data BMP = BMP {}
- data FileHeader = FileHeader {}
- data BitmapInfo = InfoV3 BitmapInfoV3
- data BitmapInfoV3 = BitmapInfoV3 {}
- data Error
- = ErrorReadOfFileHeaderFailed
- | ErrorReadOfImageHeaderFailed
- | ErrorReadOfImageDataFailed
- | ErrorBadMagic Word16
- | ErrorReservedFieldNotZero
- | ErrorDodgyFileHeaderFieldOffset Int
- | ErrorDodgyFileHeaderFieldFileSize Int
- | ErrorFileIsTruncated
- | ErrorUnhandledBitmapHeaderSize Int
- | ErrorUnhandledPlanesCount Int
- | ErrorUnhandledColorDepth Int
- | ErrorUnhandledCompressionMode Int
- | ErrorZeroImageSize
- | ErrorLacksWholeNumberOfLines
- readBMP :: FilePath -> IO (Either Error BMP)
- writeBMP :: FilePath -> BMP -> IO ()
- hGetBMP :: Handle -> IO (Either Error BMP)
- hPutBMP :: Handle -> BMP -> IO ()
- packRGBA32ToBMP :: Int -> Int -> ByteString -> BMP
- unpackBMPToRGBA32 :: BMP -> ByteString
- bmpDimensions :: BMP -> (Int, Int)
Documentation
A BMP image. For an uncompressed image, the image data contains triples of BGR component values. Each line may also have zero pad values on the end, to bring them up to a multiple of 4 bytes in length.
data FileHeader Source
BMP file header.
FileHeader | |
|
data BitmapInfo Source
A wrapper for the bitmap info, in case we want to support other header types in the future.
data BitmapInfoV3 Source
Device Independent Bitmap (DIB) header for Windows V3.
BitmapInfoV3 | |
|
Things that can go wrong when loading a BMP file.
hGetBMP :: Handle -> IO (Either Error BMP)Source
Get a BMP image from a file handle.
The file is checked for problems and unsupported features when read.
If there is anything wrong this gives an Error
instead.
hPutBMP :: Handle -> BMP -> IO ()Source
Put a BMP image to a file handle.
The size of the provided image data is checked against the given dimensions.
If these don't match then error
.
:: Int | Width of image. |
-> Int | Height of image. |
-> ByteString | A string of RGBA component values. Must have length ( |
-> BMP |
Pack a string of RGBA component values into a BMP image.
The alpha component is ignored.
If the given dimensions don't match the input string then error
.
unpackBMPToRGBA32 :: BMP -> ByteStringSource
Unpack a BMP image to a string of RGBA component values. The alpha component is set to 255 for every pixel.
bmpDimensions :: BMP -> (Int, Int)Source
Get the width and height of an image. It's better to use this function than to access the headers directly.