bmp- Read and write uncompressed 24bit BMP image files.



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 Source

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.




fileHeaderType :: Word16

Magic numbers 0x42 0x4d

fileHeaderFileSize :: Word32

Size of the file, in bytes.

fileHeaderReserved1 :: Word16

Reserved, must be zero.

fileHeaderReserved2 :: Word16

Reserved, must be zero.

fileHeaderOffset :: Word32

Offset in bytes to the start of the pixel data.

data BitmapInfo Source

A wrapper for the bitmap info, in case we want to support other header types in the future.


InfoV3 BitmapInfoV3 

data BitmapInfoV3 Source

Device Independent Bitmap (DIB) header for Windows V3.




dib3Size :: Word32

Size of the image header, in bytes.

dib3Width :: Word32

Width of the image, in pixels.

dib3Height :: Word32

Height of the image, in pixels.

dib3Planes :: Word16

Number of color planes.

dib3BitCount :: Word16

Number of bits per pixel.

dib3Compression :: Word32

Image compression mode. 0 = uncompressed.

dib3ImageSize :: Word32

Size of raw image data.

dib3PelsPerMeterX :: Word32

Prefered resolution in pixels per meter, along the X axis.

dib3PelsPerMeterY :: Word32

Prefered resolution in pixels per meter, along the Y axis.

dib3ColorsUsed :: Word32

Number of color entries that are used.

dib3ColorsImportant :: Word32

Number of significant colors.

writeBMP :: FilePath -> BMP -> IO ()Source

Wrapper for hPutBMP

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 (width * height * 4)

-> 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.