bmp-1.2.6.3: Read and write uncompressed BMP image files.
Safe HaskellNone
LanguageHaskell98

Codec.BMP

Description

Reading and writing uncompressed BMP files.

Supports uncompressed 24bit RGB and 32bit RGBA WindowsV3, WindowsV4 and WindowsV5 formats.

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.5
   Add support for writing uncompressed 32-bit files.

 * bmp 1.2.4
   Update to use binary 0.6.

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

Data Structures

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.

Constructors

BMP 

Instances

Instances details
Show BMP Source # 
Instance details

Defined in Codec.BMP.Base

Methods

showsPrec :: Int -> BMP -> ShowS

show :: BMP -> String

showList :: [BMP] -> ShowS

data FileHeader Source #

BMP file header.

Constructors

FileHeader 

Fields

Instances

Instances details
Show FileHeader Source # 
Instance details

Defined in Codec.BMP.FileHeader

Methods

showsPrec :: Int -> FileHeader -> ShowS

show :: FileHeader -> String

showList :: [FileHeader] -> ShowS

Binary FileHeader Source # 
Instance details

Defined in Codec.BMP.FileHeader

Methods

put :: FileHeader -> Put

get :: Get FileHeader

putList :: [FileHeader] -> Put

data BitmapInfo Source #

A wrapper for the various image header types.

Instances

Instances details
Show BitmapInfo Source # 
Instance details

Defined in Codec.BMP.BitmapInfo

Methods

showsPrec :: Int -> BitmapInfo -> ShowS

show :: BitmapInfo -> String

showList :: [BitmapInfo] -> ShowS

Binary BitmapInfo Source # 
Instance details

Defined in Codec.BMP.BitmapInfo

Methods

put :: BitmapInfo -> Put

get :: Get BitmapInfo

putList :: [BitmapInfo] -> Put

data BitmapInfoV3 Source #

Device Independent Bitmap (DIB) header for Windows V3.

Constructors

BitmapInfoV3 

Fields

  • dib3Size :: Word32

    (+0) Size of the image header, in bytes.

  • dib3Width :: Word32

    (+4) Width of the image, in pixels.

  • dib3Height :: Word32

    (+8) Height of the image, in pixels.

  • dib3HeightFlipped :: Bool

    If the height field in the file is negative then this is interpreted as an image with the rows flipped.

  • dib3Planes :: Word16

    (+12) Number of color planes.

  • dib3BitCount :: Word16

    (+14) Number of bits per pixel.

  • dib3Compression :: Compression

    (+16) Image compression mode.

  • dib3ImageSize :: Word32

    (+20) Size of raw image data. Some encoders set this to zero, so we need to calculate it based on the overall file size.

    If it is non-zero then we check it matches the file size - header size.

  • dib3PelsPerMeterX :: Word32

    (+24) Prefered resolution in pixels per meter, along the X axis.

  • dib3PelsPerMeterY :: Word32

    (+28) Prefered resolution in pixels per meter, along the Y axis.

  • dib3ColorsUsed :: Word32

    (+32) Number of color entries that are used.

  • dib3ColorsImportant :: Word32

    (+36) Number of significant colors.

Instances

Instances details
Show BitmapInfoV3 Source # 
Instance details

Defined in Codec.BMP.BitmapInfoV3

Methods

showsPrec :: Int -> BitmapInfoV3 -> ShowS

show :: BitmapInfoV3 -> String

showList :: [BitmapInfoV3] -> ShowS

Binary BitmapInfoV3 Source # 
Instance details

Defined in Codec.BMP.BitmapInfoV3

Methods

put :: BitmapInfoV3 -> Put

get :: Get BitmapInfoV3

putList :: [BitmapInfoV3] -> Put

data BitmapInfoV4 Source #

Device Independent Bitmap (DIB) header for Windows V4 (95 and newer)

Constructors

BitmapInfoV4 

Fields

Instances

Instances details
Show BitmapInfoV4 Source # 
Instance details

Defined in Codec.BMP.BitmapInfoV4

Methods

showsPrec :: Int -> BitmapInfoV4 -> ShowS

show :: BitmapInfoV4 -> String

showList :: [BitmapInfoV4] -> ShowS

Binary BitmapInfoV4 Source # 
Instance details

Defined in Codec.BMP.BitmapInfoV4

Methods

put :: BitmapInfoV4 -> Put

get :: Get BitmapInfoV4

putList :: [BitmapInfoV4] -> Put

data BitmapInfoV5 Source #

Device Independent Bitmap (DIB) header for Windows V5 (98/2000 and newer)

Constructors

BitmapInfoV5 

Fields

Instances

Instances details
Show BitmapInfoV5 Source # 
Instance details

Defined in Codec.BMP.BitmapInfoV5

Methods

showsPrec :: Int -> BitmapInfoV5 -> ShowS

show :: BitmapInfoV5 -> String

showList :: [BitmapInfoV5] -> ShowS

Binary BitmapInfoV5 Source # 
Instance details

Defined in Codec.BMP.BitmapInfoV5

Methods

put :: BitmapInfoV5 -> Put

get :: Get BitmapInfoV5

putList :: [BitmapInfoV5] -> Put

data Compression Source #

The Compression mode says how the image data is encoded in the file.

Instances

Instances details
Eq Compression Source # 
Instance details

Defined in Codec.BMP.Compression

Methods

(==) :: Compression -> Compression -> Bool

(/=) :: Compression -> Compression -> Bool

Show Compression Source # 
Instance details

Defined in Codec.BMP.Compression

Methods

showsPrec :: Int -> Compression -> ShowS

show :: Compression -> String

showList :: [Compression] -> ShowS

Binary Compression Source # 
Instance details

Defined in Codec.BMP.Compression

Methods

put :: Compression -> Put

get :: Get Compression

putList :: [Compression] -> Put

data CIEXYZ Source #

Contains the XYZ coordinates of a specific color in a specified color space.

Constructors

CIEXYZ Word32 Word32 Word32 

Instances

Instances details
Show CIEXYZ Source # 
Instance details

Defined in Codec.BMP.CIEXYZ

Methods

showsPrec :: Int -> CIEXYZ -> ShowS

show :: CIEXYZ -> String

showList :: [CIEXYZ] -> ShowS

Binary CIEXYZ Source # 
Instance details

Defined in Codec.BMP.CIEXYZ

Methods

put :: CIEXYZ -> Put

get :: Get CIEXYZ

putList :: [CIEXYZ] -> Put

data Error Source #

Things that can go wrong when loading a BMP file.

Constructors

ErrorBadMagic

Magic number was not at the start of the file, so this probably isn't a BMP file.

Fields

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.

Fields

ErrorReservedFieldNotZero

Reserved fields should be zero.

ErrorDodgyFileHeaderFieldOffset

The offset to the image data from the file header doesn't point anywhere sensible.

Fields

ErrorUnhandledBitmapHeaderSize

We handle V3 V4 and V5 image headers, but the size of the header indicates it has some other format.

Fields

ErrorUnhandledPlanesCount

We only handle single color planes.

Fields

ErrorUnhandledColorDepth

We only handle 24 and 32 bit images.

Fields

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.

Instances

Instances details
Eq Error Source # 
Instance details

Defined in Codec.BMP.Error

Methods

(==) :: Error -> Error -> Bool

(/=) :: Error -> Error -> Bool

Show Error Source # 
Instance details

Defined in Codec.BMP.Error

Methods

showsPrec :: Int -> Error -> ShowS

show :: Error -> String

showList :: [Error] -> ShowS

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.

hGetBMP :: Handle -> IO (Either Error BMP) Source #

Get a BMP image from a file handle.

parseBMP :: ByteString -> Either Error BMP Source #

Parse a BMP image from a lazy ByteString

Writing

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

Wrapper for hPutBMP

hPutBMP :: Handle -> BMP -> IO () Source #

Put a BMP image to a file handle.

renderBMP :: BMP -> ByteString Source #

Render a BMP image to a lazy ByteString.

Pack and Unpack

packRGBA32ToBMP Source #

Arguments

:: Int

Width of image (must be positive).

-> Int

Height of image (must be positive).

-> ByteString

A string of RGBA component values. Must have length (width * height * 4)

-> BMP 

Pack a string of RGBA component values into a 32-bit BMP image.

Alias for packRGBA32ToBMP32.

packRGBA32ToBMP32 Source #

Arguments

:: Int

Width of image (must be positive).

-> Int

Height of image (must be positive).

-> ByteString

A string of RGBA component values. Must have length (width * height * 4)

-> BMP 

Pack a string of RGBA component values into a 32-bit BMP image.

  • If the given dimensions don't match the input string then error.
  • If the width or height fields are negative then error.

packRGBA32ToBMP24 Source #

Arguments

:: Int

Width of image (must be positive).

-> Int

Height of image (must be positive).

-> ByteString

A string of RGBA component values. Must have length (width * height * 4)

-> BMP 

Pack a string of RGBA component values into a 24-bit BMP image, discarding the alpha channel of the source data.

  • If the given dimensions don't match the input string then error.
  • If the width or height fields are negative then error.

unpackBMPToRGBA32 :: BMP -> ByteString Source #

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.