Safe Haskell | None |
---|
Reading and writing uncompressed BMP files.
Reading works for both uncompressed 24bit RGB and 32bit RGBA WindowsV3, WindowsV4 and WindowsV5 formats.
Writing is limited to the uncompressed 24bit RGB WindowsV3 format.
We don't support the plain OS/2 BitmapCoreHeader and BitmapCoreHeader2 image headers, but I haven't yet seen one of these in the wild.
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 ...
Release Notes:
* bmp 1.2.3 Add pure parseBMP / renderBMP API. * bmp 1.2.2 Allow the physical image buffer to be larger than the image size stated in the header, to accept output of foolish Win7 codec. * bmp 1.2.1 Fix slow ByteString conversion via lists. * bmp 1.2.0 Accept files with zero padding on the end of the file. Accept RGBA files with V3 headers. * bmp 1.1.2 Accept files with the image size field set to zero.
- data BMP = BMP {}
- data FileHeader = FileHeader {}
- data BitmapInfo
- data BitmapInfoV3 = BitmapInfoV3 {}
- data BitmapInfoV4 = BitmapInfoV4 {}
- data BitmapInfoV5 = BitmapInfoV5 {}
- data Compression
- data CIEXYZ = CIEXYZ Word32 Word32 Word32
- data Error
- = ErrorBadMagic {
- errorMagic :: Word16
- | ErrorFileHeaderTruncated
- | ErrorImageHeaderTruncated
- | ErrorImageDataTruncated { }
- | ErrorReservedFieldNotZero
- | ErrorDodgyFileHeaderFieldOffset { }
- | ErrorUnhandledBitmapHeaderSize { }
- | ErrorUnhandledPlanesCount { }
- | ErrorUnhandledColorDepth { }
- | ErrorUnhandledCompressionMode { }
- | ErrorImagePhysicalSizeMismatch { }
- | ErrorInternalErrorPleaseReport
- = ErrorBadMagic {
- readBMP :: FilePath -> IO (Either Error BMP)
- hGetBMP :: Handle -> IO (Either Error BMP)
- parseBMP :: ByteString -> Either Error BMP
- writeBMP :: FilePath -> BMP -> IO ()
- hPutBMP :: Handle -> BMP -> IO ()
- renderBMP :: BMP -> ByteString
- packRGBA32ToBMP :: Int -> Int -> ByteString -> BMP
- unpackBMPToRGBA32 :: BMP -> ByteString
- bmpDimensions :: BMP -> (Int, Int)
Data Structures
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 various image header types.
data BitmapInfoV3 Source
Device Independent Bitmap (DIB) header for Windows V3.
BitmapInfoV3 | |
|
data BitmapInfoV4 Source
Device Independent Bitmap (DIB) header for Windows V4 (95 and newer)
BitmapInfoV4 | |
|
data BitmapInfoV5 Source
Device Independent Bitmap (DIB) header for Windows V5 (98/2000 and newer)
BitmapInfoV5 | |
|
data Compression Source
The Compression mode says how the image data is encoded in the file.
Contains the XYZ coordinates of a specific color in a specified color space.
Things that can go wrong when loading a BMP file.
ErrorBadMagic | Magic number was not at the start of the file, so this probably isn't a BMP file. |
| |
ErrorFileHeaderTruncated | File is too short to contain a file header. |
ErrorImageHeaderTruncated | File is too short to contain an image header. |
ErrorImageDataTruncated | File is too short to contain the image data. |
ErrorReservedFieldNotZero | Reserved fields should be zero. |
ErrorDodgyFileHeaderFieldOffset | The offset to the image data from the file header doesn't point anywhere sensible. |
ErrorUnhandledBitmapHeaderSize | We handle V3 V4 and V5 image headers, but the size of the header indicates it has some other format. |
ErrorUnhandledPlanesCount | We only handle single color planes. |
ErrorUnhandledColorDepth | We only handle 24 and 32 bit images. |
ErrorUnhandledCompressionMode | We only handle uncompressed images. |
ErrorImagePhysicalSizeMismatch | Mismatch between the image size stated in the header and that which is calculuated from the other fields. |
ErrorInternalErrorPleaseReport | Something went wrong in the library. |
Reading
readBMP :: FilePath -> IO (Either Error BMP)Source
Read a BMP from a file.
The file is checked for problems and unsupported features when read.
If there is anything wrong this gives an Error
instead.
parseBMP :: ByteString -> Either Error BMPSource
Parse a BMP image from a lazy ByteString
Writing
renderBMP :: BMP -> ByteStringSource
Render a BMP image to a lazy ByteString
.
Pack and Unpack
:: Int | Width of image (must be positive). |
-> Int | Height of image (must be positive). |
-> ByteString | A string of RGBA component values.
Must have length ( |
-> BMP |
unpackBMPToRGBA32 :: BMP -> ByteStringSource
Unpack a BMP image to a string of RGBA component values.
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.