New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

@thi.ng/pixel-convolve

Package Overview
Dependencies
Maintainers
1
Versions
57
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@thi.ng/pixel-convolve

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

latest
Source
npmnpm
Version
1.1.26
Version published
Maintainers
1
Created
Source

@thi.ng/pixel-convolve

npm version npm downloads Mastodon Follow

[!NOTE] This is one of 214 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:

KernelSize
BOX_BLUR33x3
BOX_BLUR55x5
EDGE33x3
EDGE55x5
GAUSSIAN_BLUR33x3
GAUSSIAN_BLUR55x5
GAUSSIAN(n)2n+1 x 2n+1
HIGHPASS33x3
LANCZOS(a,s)as+1 x as+1
SHARPEN33x3
SOBEL_X3x3
SOBEL_Y3x3
UNSHARP_MASK55x5

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):

KernelDescription
POOL_MEANMoving average
POOL_MAXLocal maximum
POOL_MINLocal minimum
POOL_NEARESTNearest 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.

StepScale = 1Scale = 2Scale = 4Scale = 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:

ScreenshotDescriptionLive demoSource
Interactive image processing (adaptive threshold)DemoSource
2.5D hidden line visualization of digital elevation files (DEM)DemoSource
Normal map creation/conversion basicsDemoSource

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 - 2026 Karsten Schmidt // Apache License 2.0

Keywords

blur

FAQs

Package last updated on 19 Mar 2026

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts