npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

blender-compiler

v0.1.1

Published

Compiles per-pixel blend

Downloads

2

Readme

blender-compiler

build status dependency status coverage report stability index

npm stats

browser support

Compile per-pixel blend functions into a high performance shader.

Motivation

When layering images on top of each other, we often wish to apply a blend function to combine the images in some way. The functions that define how these images should be blended together are easier to think about in the form of a source pixel a destination pixel and a result pixel.

For example, an "average" blend function might be of the form

result.r = (source.r + destination.r) / 2
result.g = (source.g + destination.g) / 2
result.b = (source.b + destination.b) / 2
result.a = (source.a + destination.a) / 2

So we can quite easily define a per pixel "blend function", but if we are going to blend two images on top of each other, we also have to do the following

  • Loop over and read each pixel in the source and destination pixels
  • Scale the pixels from their source scale [0,255] to their blend scale [0,1]
  • Call the blend function for the matching pixels
  • Upscale the pixels back from their blend scale to their destination scale

The blender compiler will do all of these things for us, whilst also making agressive performance optimisations, so that we just have to provide a blend function.

blender-compiler was used to generate blender-compiler, an implementation of the blend modes used in PDF and Adobe® Photoshop®

Use

Per pixel blend functions are written in the form

function blendFunction(src, dst, out) {}

Where

  • src is the pixel object to be blended on top
  • dst is the pixel object to blend over
  • out is the pixel result of the blend function
  • { r: 0, g: 0, b: 0, a: 0 } are pixels, with values between 0 and 1

Precaution - Eval is used to compile the shader. The shader is compiled in the folling way:

The per pixel blend function is inlined into a per-pixel loop. All references to src.r dst.r and out.r (and for g, b, a) are replaced by local variables src_r, dst_r and out_r - This is achieved by naive string replace for efficiency (instead of parsing the AST), so you need to be careful about how you name any other tokens in your blend function.

The function is tested after being compiled, and if it fails, a working but less efficient version is returned, using the same pixel loop function but without the blend function inlining.

The resulting blender is a function in the form

function blender(src, dst, out) {}

Where

  • src is the image on top
  • dst is the image underneath
  • out is the array in which to write the result of the operation

The functions are designed to work with the pixel format of an ImageData object, where pixel values are stored as

[R, G, B, A, R, G, B, A, R, G, B, A, ... ]

The RGB values in the images range from 0 - 255. They are scaled to 0 - 1 for pixel shading and then automatically upscaled for you efficienly back to 0 - 255

Note that the values are not rounded to integers when they are upscaled. If you use a Uint8ClampedArray as the out array, then setting this value will efficiently round it for you anyway, so we can avoid the extra rounding.

Example

var compile = require("blender-compiler")

var blender = compile(function (src, dst, out)) {
    // An averaging blend function
    out.r = (src.r + dst.r) / 2
    out.g = (src.g + dst.g) / 2
    out.b = (src.b + dst.b) / 2
    out.a = (src.a + dst.a) / 2
})

Installation

npm install blender-compiler

Contributors

  • Matt-Esch

MIT Licenced