Code Monkey home page Code Monkey logo

ex_png's Introduction

ExPng

Release Elixir UNLICENSE CI Tests Credo Dialyzer

ExPng is a pure Elixir implementation of the PNG image format. It can read and write PNG files.

Installation

Add :ex_png as a dependency in your project's mix.exs:

def deps do
  [
    {:ex_png, "~> 1.0.0"}
  ]
end

And run:

~$ mix deps.get

Features

  • Decodes any image the PNG standard allows. This includes all standard bit depths, color modes, filtering and iterlacing options
  • Encodes using any options the PNG standard allows. See Saving Images below for the defaults ExPng uses, and how to override them
  • Read/write access to the image's pixels
  • Implements Xiaolin Wu's algorithm for drawing antialiased lines
  • Works with all Elixir versions >= 1.7

Creating images

Images can be created by decoding an existing PNG file

{:ok, image} = ExPng.Image.from_file("adorable_kittens.png")

by passing in a list of rows of ExPng.Pixel structs

image = ExPng.Image.new([
    [ExPng.Pixel.rgb(100, 200, 200), ExPng.Pixel.rgb(0, 100, 100)],
    [ExPng.Pixel.rgb(50, 100, 200), ExPng.Pixel.rgb(0, 75, 43)],
])

or by creating a blank image with a given width and height

image = ExPng.Image.new(200, 100)

Modifying images

Images can be edited by painting/clearing individual pixels, drawing lines, or erasing the entire canvas. These editing actions do not occur in layers, and change the underlying pixels, so be careful as changes cannot be undone.

image = ExPng.Image.new(32, 32)

# Draws a pixel with the given color at the given coordinate
image = ExPng.Image.draw(image, {5, 8}, ExPng.Pixel.rgb(0, 100, 200))

# Draws a line between two points.
image = ExPng.Image.line(image, {0, 0}, {15, 8}, ExPng.Pixel.black())

# Sets a single pixel to opaque white
image = ExPng.Image.clear(image, {0, 0})

# Colors the entire canvas opaque white
image = ExPng.Image.erase(image)

Saving images

Images can be saved via

ExPng.Image.to_file(image, filename)

When encoding images, ExPng will attempt to find the optimal bit depth and color mode for the image in the following priority order:

  1. pure black and white images
  2. grayscale images, with or without transparency
  3. color images where the number of unique colors is indexable
  4. truecolor images with too many colors to index, with or without transparency

Filter type, compression level, and whether to encode the image with Adam7 interlacing can be set when saving an image:

ExPng.Image.to_file(
    image,
    filename,
    filter: ExPng.Image.Filtering.sub,
    compression: 9,
    interlace: true
)

Valid values for compression are integers 0 (no compression) through 9 (max compression).

Valid filter values are .none, .up, .sub, .average, and .paeth, which can also be represented, respectively, by integers 0-5.

By default, ExPng uses the up filter, the default zlib compression level 6, and no interlacing.

Documentation

Complete documentation can be found on hexdocs.pm: ExPng documentation

About

This library is written by Michael Berkowitz and released under the UNLICENSE.

The suite of test images in test/png_suite comes from Willem van Schaik's PngSuite, with some additions made by Willem van Bergen as part of his work on ChunkyPNG, and some added by the author of this library.

ex_png's People

Contributors

mikowitz avatar pmarreck avatar

Stargazers

 avatar Matt Harris avatar Aslak Johansen avatar  avatar Yos avatar Daniel Sczepansky avatar Jindřich K. Smitka avatar Dorian Karter avatar Frank Hunleth avatar Jason Axelson avatar Tiemen Waterreus avatar Max Wardeh avatar peter madsen-mygdal avatar Philip Brown avatar Camelo avatar Dennis Beatty avatar

Watchers

 avatar James Cloos avatar Matt Harris avatar  avatar

Forkers

pmarreck mad42

ex_png's Issues

Remove `ExPng.Image.Line` struct

Right now this acts as a temporary data structure between the raw image data binary and the pixels field on an Image. With the changes in #19 these are no longer persisted anywhere, and it should be possible to simplify the workflow by removing this struct entirely and dealing with binaries.

Implement support for encoding with different settings

Right now encoding is done with 8 bit depth, truecolor_alpha color, default zlib compression, and no interlacing.

It would be nice to provide options to do differently, either user provided or determined based on the image contents.

Performance improvements?

  • Would using erlang :arrays be faster for storing pixels than 2d Lists?
  • Storing pixel data as 4byte integers instead of structs?

Should add some benchmark data for reading and writing current formats

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.