View Source BMP (BMP v0.1.0)

Elixir library implementing BMP struct allowing to interact with bitmap images.

This library adds new type called bmp/0 and several functions to work with BMP files.

examples

Examples

iex(1)> img = BMP.new({1920, 1080}, 24, "#F5ABB9")
file header:
  signature:          BM              | 42 4D                   | 2
  file size:          5.93 MiB        | 36 EC 5E 00             | 4
  reserved                            | 00 00 00 00             | 4
  data offset:        54 B            | 36 00 00 00             | 4

info header:
  header size:        40 B            | 28 00 00 00             | 4
  image size:         1920x1080       | 80 07 00 00 38 04 00 00 | 8
  planes:             1               | 01 00                   | 2
  color depth:        24 bit          | 18 00                   | 2
  compression:        type 0          | 00 00 00 00             | 4
  compressed size:    5.93 MiB        | 00 EC 5E 00             | 4
  x resolution:       255 px/m        | FF 00 00 00             | 4
  y resolution:       255 px/m        | FF 00 00 00             | 4
  used colors:        0               | 00 00 00 00             | 4
  important colors:   0               | 00 00 00 00             | 4

color table:          0 B

raster data:          B9 AB F5 B9 AB F5 B9 AB F5 B9 AB F5 ...   | 6220800

iex(2)> BMP.write_file!(img, "cat.bmp")
:ok

iex(3)> BMP.read_file!("cat.bmp") == img
true

Link to this section Summary

Types

Bitmap image type.

Color depth in bits.

BMP compression type.

Functions

Creates new BMP image filled with specified color.

Reads specified file to bmp/0.

Reads specified file to bmp/0.

Writes bmp to the file path.

Writes bmp to the file path.

Link to this section Types

@type bmp() :: %BMP{
  color_table: term(),
  header: term(),
  info_header: term(),
  name: term(),
  raster_data: term()
}

Bitmap image type.

@type color_depth() :: 1 | 4 | 8 | 16 | 24

Color depth in bits.

@type compression() :: bi_rgb() | bi_rle8() | bi_rle4()

BMP compression type.

  • BI_RGB - no compression
  • BI_RLE8 - 8 bit RLE encoding
  • BI_RLE4 - 8 bit RLE encoding

Link to this section Functions

@spec new({non_neg_integer(), non_neg_integer()}, color_depth(), String.t()) :: bmp()

Creates new BMP image filled with specified color.

Takes width and height, color depth and fill color - either hex string or <<r, g, b>> binary, returns a bmp/0.

examples

Examples

iex(1)> BMP.new {1600, 900}, 24, "#F5ABB9"
file header:
  signature:          BM              | 42 4D                   | 2
  file size:          4.12 MiB        | 36 EB 41 00             | 4
  reserved                            | 00 00 00 00             | 4
  data offset:        54 B            | 36 00 00 00             | 4

info header:
  header size:        40 B            | 28 00 00 00             | 4
  image size:         1600x900        | 40 06 00 00 84 03 00 00 | 8
  planes:             1               | 01 00                   | 2
  color depth:        24 bit          | 18 00                   | 2
  compression:        type 0          | 00 00 00 00             | 4
  compressed size:    4.12 MiB        | 00 EB 41 00             | 4
  x resolution:       255 px/m        | FF 00 00 00             | 4
  y resolution:       255 px/m        | FF 00 00 00             | 4
  used colors:        0               | 00 00 00 00             | 4
  important colors:   0               | 00 00 00 00             | 4

color table:          0 B

raster data:          B9 AB F5 B9 AB F5 B9 AB F5 B9 AB F5 ...   | 4320000

iex(2)> BMP.new({1600, 900}, 24, "#F5ABB9") == BMP.new({1600, 900}, 24, <<245, 171, 185>>)
true
@spec read_file(Path.t()) :: {:ok, bmp()} | {:error, atom()}

Reads specified file to bmp/0.

Takes path to file as its only argument and returns {:ok, bmp} on success, {:error, reason} otherwise.

examples

Examples

iex(1)> BMP.read_file("mew.bmp")
{:ok,
 mew.bmp
-------
file header:
  signature:          BM              | 42 4D                   | 2
  file size:          3.05 KiB        | 36 0C 00 00             | 4
  reserved                            | 00 00 00 00             | 4
  data offset:        54 B            | 36 00 00 00             | 4

info header:
  header size:        40 B            | 28 00 00 00             | 4
  image size:         32x32           | 20 00 00 00 20 00 00 00 | 8
  planes:             1               | 01 00                   | 2
  color depth:        24 bit          | 18 00                   | 2
  compression:        type 0          | 00 00 00 00             | 4
  compressed size:    3.0 KiB         | 00 0C 00 00             | 4
  x resolution:       3780 px/m       | C4 0E 00 00             | 4
  y resolution:       3780 px/m       | C4 0E 00 00             | 4
  used colors:        0               | 00 00 00 00             | 4
  important colors:   0               | 00 00 00 00             | 4

color table:          0 B

raster data:          FF FF FF FF FF FF FF FF FF FF FF FF ...   | 3072
}
iex(2)> BMP.read_file("xeon.jpg")
{:error, :not_a_bmp}
@spec read_file!(Path.t()) :: bmp()

Reads specified file to bmp/0.

Takes path to file as its only argument and returns bmp/0 on success, raises Exceptions.FileReadError otherwise.

examples

Examples

iex(1)> BMP.read_file("mew.bmp")
mew.bmp
-------
file header:
  signature:          BM              | 42 4D                   | 2
  file size:          3.05 KiB        | 36 0C 00 00             | 4
  reserved                            | 00 00 00 00             | 4
  data offset:        54 B            | 36 00 00 00             | 4

info header:
  header size:        40 B            | 28 00 00 00             | 4
  image size:         32x32           | 20 00 00 00 20 00 00 00 | 8
  planes:             1               | 01 00                   | 2
  color depth:        24 bit          | 18 00                   | 2
  compression:        type 0          | 00 00 00 00             | 4
  compressed size:    3.0 KiB         | 00 0C 00 00             | 4
  x resolution:       3780 px/m       | C4 0E 00 00             | 4
  y resolution:       3780 px/m       | C4 0E 00 00             | 4
  used colors:        0               | 00 00 00 00             | 4
  important colors:   0               | 00 00 00 00             | 4

color table:          0 B

raster data:          FF FF FF FF FF FF FF FF FF FF FF FF ...   | 3072

iex(2)> BMP.read_file("xeon.jpg")
** (Exceptions.FileReadError) error reading file "xeon.jpg": not a BMP file
    (bmp 0.1.0) lib/bmp.ex:101: BMP.read_file!/1
    iex:2: (file)
@spec write_file(bmp(), Path.t()) :: :ok | {:error, File.posix()}

Writes bmp to the file path.

Returns :ok if successful, {:error, reason} otherwise.

@spec write_file!(bmp(), Path.t()) :: :ok

Writes bmp to the file path.

Returns :ok if successful, raises File.Error exception otherwise.