Show a basic storage type to handle a simple RGB raster graphics image,
and some primitive associated functions.

If possible provide a function to allocate an uninitialised image,
given its width and height, and provide 3 additional functions:

   one to fill an image with a plain RGB color,
   one to set a given pixel with a color,
   one to get the color of a pixel.


(If there are specificities about the storage or the allocation, explain those.)

These functions are used as a base for the articles in the category raster graphics operations, 
and a basic output function to check the results
is available in the article write ppm file.




 
defmodule RosBitmap do
  defrecord Bitmap, pixels: nil, shape: {0, 0}
 
  defp new(width, height, {:rgb, r, g, b}) do
    Bitmap[
      pixels: :array.new(width * height,
        {:default, <<r::size(8), g::size(8), b::size(8)>>}),
      shape: {width, height}]
  end
 
  def new(width, height), do: new(width, height, {:rgb, 0, 0, 0})
 
  def fill(Bitmap[shape: {width, height}], {:rgb, _r, _g, _b}=color) do
    new(width, height, color)
  end
 
  def set_pixel(Bitmap[pixels: pixels, shape: {width, _height}]=bitmap,
      {:at, x, y}, {:rgb, r, g, b}) do
    index = x + y * width
    bitmap.pixels(:array.set(index, <<r::size(8), g::size(8), b::size(8)>>, pixels))
  end
 
  def get_pixel(Bitmap[pixels: pixels, shape: {width, _height}], {:at, x, y}) do
    index = x + y * width
    <<r::size(8), g::size(8), b::size(8)>> = :array.get(index, pixels)
    {:rgb, r, g, b}
  end
end
 