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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@thi.ng/pixel-convolve

v1.1.19

Published

Extensible bitmap image convolution, kernel presets, normal map & image pyramid generation

Readme

@thi.ng/pixel-convolve

npm version npm downloads Mastodon Follow

[!NOTE] This is one of 211 standalone projects, maintained as part of the @thi.ng/umbrella monorepo and anti-framework.

🚀 Please help me to work full-time on these projects by sponsoring me on GitHub. Thank you! ❤️

About

Extensible bitmap image convolution, kernel presets, normal map & image pyramid generation. This is a support package for @thi.ng/pixel.

This package contains functionality which was previously part of and has been extracted from the @thi.ng/pixel package.

  • Convolution w/ arbitrary shaped/sized kernels, pooling, striding
  • Convolution kernel & pooling kernels presets
    • Higher order kernel generators (Gaussian, Lanczos)
  • Image pooling filters (min/max, mean, adaptive threshold, custom)
  • Image pyramid generation (w/ customizable kernels)
  • Customizable normal map generation (i.e. X/Y gradients plus static Z component)

Strided convolution & pooling

Floating point buffers can be processed using arbitrary convolution kernels. The following convolution kernel presets are provided for convenience:

| Kernel | Size | |------------------|-------------| | BOX_BLUR3 | 3x3 | | BOX_BLUR5 | 5x5 | | EDGE3 | 3x3 | | EDGE5 | 5x5 | | GAUSSIAN_BLUR3 | 3x3 | | GAUSSIAN_BLUR5 | 5x5 | | GAUSSIAN(n) | 2n+1 x 2n+1 | | HIGHPASS3 | 3x3 | | LANCZOS(a,s) | as+1 x as+1 | | SHARPEN3 | 3x3 | | SOBEL_X | 3x3 | | SOBEL_Y | 3x3 | | UNSHARP_MASK5 | 5x5 |

Custom kernels can be defined (and code generated) using an array of coefficients and a given kernel size. See above presets and defKernel() for reference.

Furthermore, convolution supports striding (i.e. only processing & keeping every nth pixel column/row, aka downscaling) and pixel pooling (e.g. for ML applications). Available pooling kernel presets (kernel sizes must be configured independently):

| Kernel | Description | |------------------------|--------------------| | POOL_MEAN | Moving average | | POOL_MAX | Local maximum | | POOL_MIN | Local minimum | | POOL_NEAREST | Nearest neighbor | | POOL_THRESHOLD(bias) | Adaptive threshold |

Convolution can be applied to single, multiple or all channels of a FloatBuffer. See convolveChannel() and convolveImage().

See ConvolveOpts for config options.

import { floatBufferFromImage, FLOAT_RGB, imageFromURL } from "@thi.ng/pixel";
import { convolveImage, SOBEL_X } from "@thi.ng/pixel-convolve";

// convolutions are only available for float buffers (for now)
const src = floatBufferFromImage(await imageFromURL("test.jpg"), FLOAT_RGB);

// apply horizontal Sobel kernel preset to all channels
// downscale image by factor 2 (must be integer)
// scale kernel result values by factor 4
const dest = convolveImage(src, { kernel: SOBEL_X, stride: 2, scale: 4 });

Normal map generation

Normal maps can be created via normalMap(). This function uses an adjustable convolution kernel size to control gradient smoothness & details. Result X/Y gradients can also be scaled (uniform or anisotropic) and the Z component can be customized to (default: 1.0). The resulting image is in FLOAT_NORMAL format, using signed channel values. This channel format is auto-translating these into unsigned values when the image is converted into an integer format.

| Step | Scale = 1 | Scale = 2 | Scale = 4 | Scale = 8 | |------|------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------| | 0 | | | | | | 1 | | | | | | 2 | | | | | | 3 | | | | |

import { ARGB8888, FLOAT_GRAY, floatBufferFromImage, imageFromURL } from "@thi.ng/pixel";
import { normalMap } from "@thi.ng/pixel-convolve";

// read source image into a single channel floating point buffer
const src = floatBufferFromImage(await imageFromURL("noise.png"), FLOAT_GRAY);

// create normal map (w/ default options)
// this results in a new float pixel buffer with FLOAT_RGB format
const nmap = normalMap(src, { step: 0, scale: 1 });

// pixel lookup (vectors are stored _un_normalized)
nmap.getAt(10, 10);
// Float32Array(3) [ -0.019607841968536377, -0.04313725233078003, 1 ]

// convert to 32bit packed int format
const nmapARGB = nmap.as(ARGB8888);

Status

STABLE - used in production

Search or submit any issues for this package

Installation

yarn add @thi.ng/pixel-convolve

ESM import:

import * as pc from "@thi.ng/pixel-convolve";

Browser ESM import:

<script type="module" src="https://esm.run/@thi.ng/pixel-convolve"></script>

JSDelivr documentation

For Node.js REPL:

const pc = await import("@thi.ng/pixel-convolve");

Package sizes (brotli'd, pre-treeshake): ESM: 2.29 KB

Dependencies

Note: @thi.ng/api is in most cases a type-only import (not used at runtime)

Usage examples

Three projects in this repo's /examples directory are using this package:

| Screenshot | Description | Live demo | Source | |:--------------------------------------------------------------------------------------------------------------------------|:----------------------------------------------------------------|:---------------------------------------------------------|:--------------------------------------------------------------------------------------| | | Interactive image processing (adaptive threshold) | Demo | Source | | | 2.5D hidden line visualization of digital elevation files (DEM) | Demo | Source | | | Normal map creation/conversion basics | Demo | Source |

API

Generated API docs

Authors

If this project contributes to an academic publication, please cite it as:

@misc{thing-pixel-convolve,
  title = "@thi.ng/pixel-convolve",
  author = "Karsten Schmidt",
  note = "https://thi.ng/pixel-convolve",
  year = 2021
}

License

© 2021 - 2025 Karsten Schmidt // Apache License 2.0