[][src]Crate photon

An image processing crate that provides a set of functions for image filtering, convolution, colour manipulation, and more.

Functions

96 functions are available, including:

Example

extern crate photon;
fn main() {
    let img = photon::helpers::open_image("valley.PNG");
    photon::effects::solarize(&mut img);
    // Write the contents of this image in PNG format.
    photon::helpers::save_image(img, "new_image.PNG");
}

This crate contains built-in preset functions, which provide default image processing functionality, as well as functions that allow for direct, low-level access to channel manipulation. To view a full demo of filtered imagery, visit the official website.

WebAssembly Use

To allow for universal communication between the core Rust library and WebAssembly, the functions have been generalised to allow for both native and in-browser use. Due to this, image data from the browser must first be converted to a PhotonImage before being passed to the image processing functions. The PhotonImage can then be converted back to JS-compatible ImageData so that it can be displayed in-browser. See the code snippet below:

function filterImage() {
    // Create a canvas and get a 2D context from the canvas
    var canvas = document.getElementById("canvas");
    var ctx = canvas.getContext("2d"); 
    
    // Draw the image element onto the canvas
    ctx.drawImage(newimg, 0, 0);
    
    // Convert the ImageData found in the canvas to a PhotonImage (so that it can communicate with the core Rust library)
    let rust_image = module.open_image(canvas, ctx);
    
    // Filter the image, the PhotonImage's raw pixels are modified
    module.filter(rust_image, "radio");
    
    // Place the PhotonImage back on the canvas
    ctx.putImageData(rust_image, 0, 0)
}

Live Demo

View the official demo of WASM in action. Not all functions available in the core Rust library are available in WebAssembly (currently investigating this). Only WASM-friendly functions have been annotated with #[wasm_bindgen]. All supported WASM functions are displayed in the starter demo.

Modules

channels

Channel manipulation.

colour_spaces

Image manipulation effects in HSL, LCh and HSV.

conv

Convolution effects such as sharpening, blurs, sobel filters, etc.,

effects

Special effects.

filters

Preset color filters.

helpers

Helper functions for converting between various formats

monochrome

Monochrome-related effects and greyscaling/duotoning.

multiple

Image manipulation with multiple images, including adding watermarks, changing backgrounds, etc.,

native

Native-only functions. Includes functions that open images from the file-system, etc.,

noise

Add noise to images.

text

Draw text onto an image. For extended graphic design/text-drawing functionality, see GDL, which is a graphic design library, compatible with Photon.

transform

Image transformations, ie: scale, crop, resize, etc.,

Structs

PhotonImage

Provides the image's height, width, and contains the image's raw pixels. For use when communicating between JS and WASM, and also natively.

Rgb

RGB color type.

Functions

base64_to_image

Convert a base64 string to a PhotonImage.

base64_to_vec

Convert a base64 string to a Vec of u8s.

get_image_data

Get the ImageData from a 2D canvas context

open_image

Convert a HTML5 Canvas Element to a PhotonImage.

putImageData

Place a PhotonImage onto a 2D canvas.

run

Create a PhotonImage from a byte slice. [temp] Check if WASM is supported.

to_image_data

Convert a PhotonImage to JS-compatible ImageData.

to_raw_pixels

Convert ImageData to a raw pixel vec of u8s.