Skip to content

Instantly share code, notes, and snippets.

@bryanhunter
Created January 20, 2015 21:18
Show Gist options
  • Save bryanhunter/a3a905ba890a21eb345f to your computer and use it in GitHub Desktop.
Save bryanhunter/a3a905ba890a21eb345f to your computer and use it in GitHub Desktop.
Example of Elixir and the bit-syntax to read a Bitmap image
defmodule Bumper do
@doc ~S"""
Reads a Bitmap (24-bit) and displays width, height, and the RGB of each pixel
"""
def show(filename) do
{:ok, bindata} = File.read(filename)
<< "BM",
_::size(64),
offset_to_pixels::size(32)-little,
_::size(32),
width::size(32)-little,
height::size(32)-little,
_::size(16),
24::size(16)-little,
_rest::binary>> = bindata
<<_::size(offset_to_pixels)-bytes, pixels::binary>> = bindata
IO.puts "Offset:#{offset_to_pixels} Width:#{width} Height:#{height}"
for <<b::size(8), g::size(8), r::size(8) <- pixels >>, do: {r,g,b}
end
end
Bumper.show("krgbw.bmp")
|> IO.inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment