arjan / pixels

Elixir library to read pixels from a PNG file, using libpng (using a NIF)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Pixels

Elixir CI Hex pm

Elixir NIF to read and write image data from/to PNG and JPEG files.

For PNG images, it uses the lodepng C library; for JPEG images, it uses the ujpeg C library.

Progressive or lossless JPEG files, as well as JPEG encoding are not supported by the underlying uJpeg.

Getting Pixel data

Pixels.read and Pixels.read_file allow you to decode a PNG / JPEG file into a %Pixels{} struct. The data component of this struct contains the RGBA data of the image, in row order. To get an individual pixel's value, you can calculate an offset into this data based on the x/y coordinate. For example:

{:ok, pixels} = Pixels.read_file("test.png")

# the pixel value you want to get:
x = 10
y = 3

# calculate offset into `pixels.data`
offset = (y * pixels.width + x) * 4

# binary-pattern-match the pixel value in the data:
<<_::binary-size(offset), r, g, b, alpha, _::binary>> = pixels.data

I need feature X!

Pull requests welcome! For a more feature-complete library you could also use image, however its license is LGPL so a bit more restrictive than the MIT license of Pixels.

Installation

If available in Hex, the package can be installed by adding pixels to your list of dependencies in mix.exs:

def deps do
  [
    {:pixels, "~> 0.1.0"}
  ]
end

Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/pixels.

About

Elixir library to read pixels from a PNG file, using libpng (using a NIF)

License:MIT License


Languages

Language:C 71.9%Language:C++ 26.3%Language:Elixir 1.6%Language:Makefile 0.3%