6.1.28 • Published 8 days ago

@thi.ng/pixel v6.1.28

Weekly downloads
35
License
Apache-2.0
Repository
github
Last release
8 days ago

pixel

npm version npm downloads Twitter Follow

This project is part of the @thi.ng/umbrella monorepo.

About

Typedarray integer & float pixel buffers w/ customizable formats, blitting, drawing, convolution.

screenshot

  • Buffer creation from HTML image elements w/ opt resize & format conversion (browser only)
  • Buffer-to-buffer blitting w/ automatic format conversion
  • Buffer-to-canvas blitting
  • Buffer-to-buffer blending w/ Porter-Duff operators
  • Pre/post-multiply alpha
  • Region / sub-image extraction
  • Single-channel manipulation / extraction / replacement / conversion
  • k-means based dominant color extraction (float buffers only)
  • Accessors for normalized channel value
  • Image sampling, resizing, pooling
    • Filters: nearest neighbor, bilinear, bicubic
    • Wrap behaviors: clamp, wrap, repeat
    • Pooling: mean/min/max
  • Invert image
  • Convolution w/ arbitrary shaped/sized kernels, pooling, striding
  • Convolution kernel & pooling kernels presets
    • Higher order kernel generators (Gaussian, Lanczos)
  • Image pyramid generation (w/ customizable kernels)
  • Customizable normal map generation (i.e. X/Y gradients plus static Z component)
  • XY full pixel & channel-only accessors
  • 12 packed integer and 6 floating point preset formats (see table below)
  • Declarative custom format & optimized code generation
  • HTML canvas creation & ImageData utilities

Integer pixel formats

All integer formats use the canvas native ABGR 32bit format as common intermediate for conversions. During conversion to ABGR, channels with sizes smaller than 8 bits will be scaled appropriately to ensure an as full-range and as linear as possible mapping. E.g. a 4 bit channel will be scaled by 255 / 15 = 17.

Format specs can freely control channel layout within current limits:

  • Channel sizes: 1 - 32 bits.
  • Storage: 8, 16 or 32 bits per pixel

New formats can be defined via defIntFormat().

Format IDBits per pixelDescription
ALPHA888 bit channel (alpha only)
GRAY888 bit single channel (grayscale conv)
GRAY_ALPHA8168 bit single channel (grayscale conv), 8 bit alpha
GRAY161616 bit single channel (grayscale conv)
GRAY_ALPHA163216 bit single channel (grayscale conv), 16 bit alpha
ARGB4444164 channels @ 4 bits each
ARGB1555165 bits each for RGB, 1 bit alpha
RGB565165 bits red, 6 bits green, 5 bits blue
RGB88832 (24 effective)3 channels @ 8 bits each
ARGB8888324 channels @ 8 bits each
BGR88832 (24 effective)3 channels @ 8 bits each
ABGR8888324 channels @ 8 bits each
  • ALPHA8 is mapped from/to ABGR alpha channel
  • GRAY8/16, GRAY_ALPHA8/16 compute grayscale/luminance when converting from ABGR and in return produce grayscale ABGR
  • In all built-in formats supporting it, the alpha channel always occupies the most-significant bits (up to format size)

Floating point pixel formats

Strided floating point format presets for use with floatBuffer(). New formats can be defined via defFloatFormat().

Format IDChannel countDescription
FLOAT_GRAY1Single channel / grayscale
FLOAT_GRAY_ALPHA2Grayscale and alpha channel
FLOAT_NORMAL3Normal map (signed values)
FLOAT_RGB3Red, Green, Blue
FLOAT_RGBA4Red, Green, Blue, Alpha
  • All color channels are unclamped (but can be clamped via buf.clamp()). For conversion to packed int formats assumed to contain normalized data (i.e. 0..1 interval, with exception of FLOAT_NORMAL which uses -1..1 range)
  • Conversion between float formats is currently unsupported

Filtered image sampling and resizing

Available (and optimized) for both integer & floating point formats, image samplers can be created with the following filters & wrap modes:

Filters

  • "nearest" - nearest neighbor
  • "linear" - bilinear interpolation
  • "cubic" - bicubic interpolation

Wrap mode

  • "clamp" - outside values return 0
  • "wrap" - infinite tiling
  • "repeat" - edge pixels are repeated
const src = intBuffer(4, 4, ABGR8888);

// fill w/ random colors
src.forEach((_,i) => 0xff000000 | Math.random() * 0xffffff);

// create bilinear sampler w/ repeated edge pixels
const sampler = defSampler(src, "linear", "repeat");

// sample at fractional positions (even outside image)
sampler(-1.1, 0.5).toString(16)
// 'ff79643a'

// resize image to 1024x256 using bicubic sampling
const img = src.resize(1024, 256, "cubic");
Filter
"nearest"resized image w/ nearest neighbor sampling
"linear"resized image w/ bilinear sampling
"cubic"resized image w/ bicubic sampling

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
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.

// convolutions are only available for float buffers (for now)
src = floatBuffer(read("test.ppm"), 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
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
0npm.ionpm.ionpm.ionpm.io
1npm.ionpm.ionpm.ionpm.io
2npm.ionpm.ionpm.ionpm.io
3npm.ionpm.ionpm.ionpm.io
import { floatBuffer, normalMap, FLOAT_GRAY, RGB888 } from "@thi.ng/pixel";
import { asPPM, read } from "@thi.ng/pixel-io-netpbm";

// read source image into a single channel floating point buffer
const src = floatBuffer(read(readFileSync("noise.pgm")), FLOAT_GRAY);

// create normal map (w/ default options)
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 ]

// save as 24bit PBM, conversion to RGB int format first
writeFileSync("noise-normal.ppm", asPPM(nmap.as(RGB888)));

Dominant color extraction

The dominantColors() function applies k-means clustering to extract the dominant colors from the given image. The clustering can be configured. The function returns an array of { color, area } objects (sorted by descending area), where color is a cluster's dominant color (in the format of the source image) and area the normalized cluster size (number of selected pixels over total number of pixels in the image).

Example image & extracted dominant colors

Picture credit: /u/kristophershinn

// read test PPM image and convert into float RGB format
const img = floatBuffer(read(readFileSync(`test.ppm`)), FLOAT_RGB);

// extract 5 dominant color clusters
const clusters = dominantColors(img, 5);

console.log(clusters);
// [
//   {
//     color: [ 0.4000000059604645, 0.30980393290519714, 0.21176470816135406 ],
//     area: 0.3141084558823529
//   },
//   {
//     color: [ 0.21960784494876862, 0.19607843458652496, 0.1411764770746231 ],
//     area: 0.2780330882352941
//   },
//   {
//     color: [ 0.4156862795352936, 0.4745098054409027, 0.5647059082984924 ],
//     area: 0.16620710784313725
//   },
//   {
//     color: [ 0.6666666865348816, 0.7568627595901489, 0.9254902005195618 ],
//     area: 0.12385110294117647
//   },
//   {
//     color: [ 0.7176470756530762, 0.4745098054409027, 0.12941177189350128 ],
//     area: 0.11780024509803921
//   }
// ]

Status

STABLE - used in production

Search or submit any issues for this package

Support packages

Related packages

  • @thi.ng/color - Array-based color types, CSS parsing, conversions, transformations, declarative theme generation, gradients, presets
  • @thi.ng/porter-duff - Porter-Duff operators for packed ints & float-array alpha compositing
  • @thi.ng/rasterize - 2D shape drawing & rasterization
  • @thi.ng/shader-ast - DSL to define shader code in TypeScript and cross-compile to GLSL, JS and other targets
  • @thi.ng/webgl - WebGL & GLSL abstraction layer

Installation

yarn add @thi.ng/pixel

ES module import:

<script type="module" src="https://cdn.skypack.dev/@thi.ng/pixel"></script>

Skypack documentation

For Node.js REPL:

# with flag only for < v16
node --experimental-repl-await

> const pixel = await import("@thi.ng/pixel");

Package sizes (gzipped, pre-treeshake): ESM: 8.91 KB

Dependencies

Usage examples

Several demos in this repo's /examples directory are using this package.

A selection:

ScreenshotDescriptionLive demoSource
Interactive image processing (adaptive threshold)DemoSource
Color palette generation via dominant color extraction from uploaded imagesDemoSource
Pixel buffer manipulationsDemoSource
Showcase of various dithering algorithmsDemoSource
Image dithering and remapping using indexed palettesDemoSource
Interactive pixel sorting tool using thi.ng/color & thi.ng/pixelDemoSource
Port-Duff image compositing / alpha blendingDemoSource
2D scenegraph & image map based geometry manipulationDemoSource
WebGL & Canvas2D textured tunnel shaderDemoSource
Fork-join worker-based raymarch renderer (JS/CPU only)DemoSource
Textmode image warping w/ 16bit color outputDemoSource
Minimal multi-pass / GPGPU exampleDemoSource

API

Generated API docs

import * as pix from "@thi.ng/pixel";
import { SRC_OVER_I } from "@thi.ng/porter-duff";

import IMG from "../assets/haystack.jpg";
import LOGO from "../assets/logo-64.png";

Promise
    .all([IMG, LOGO].map(pix.imagePromise))
    .then(([img, logo]) => {

        // init 16 bit packed RGB pixel buffer from image (resized to 256x256)
        const buf = intBufferFromImage(img, pix.RGB565, 256, 256);

        // create grayscale buffer for logo and use Porter-Duff operator to
        // composite with main image. Since the logo has transparency, we need
        // to premultiply alpha first...
        pix.intBufferFromImage(logo, pix.GRAY_ALPHA88)
            .premultiply()
            .blend(SRC_OVER_I, buf, {
                dx: 10,
                dy: 10
            });

        // extract sub-image
        const region = buf.getRegion(32, 96, 128, 64);
        // copy region back at new position
        region.blit(buf, { dx: 96, dy: 32 });

        // or alternatively blit buf into itself
        // buf.blit(buf, { dx: 96, dy: 32, sx: 32, sy: 96, w: 128, h: 64 });

        // create html canvas
        // (returns obj of canvas & 2d context)
        const ctx = pix.canvas2d(buf.width, buf.height * 3, document.body);

        // write pixel buffer to canvas
        buf.blitCanvas(ctx.canvas);

        // manipulate single color channel (here red)
        const id = 0;
        // obtain channel & invert
        const ch = buf.getChannel(id).invert();
        // create dot pattern
        for (let y = 0; y < ch.height; y += 2) {
            for (let x = (y >> 1) & 1; x < ch.width; x += 2) {
                ch.setAt(x, y, 0xff);
            }
        }
        // replace original channel
        buf.setChannel(id, ch);

        // write pixel buffer to new position
        buf.blitCanvas(ctx.canvas, 0, buf.height);

        // create & write grayscale version
        buf.as(GRAY8).blitCanvas(ctx.canvas, 0, buf.height * 2);
});

TODO see examples & source comments for now

Authors

Karsten Schmidt

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

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

License

© 2019 - 2021 Karsten Schmidt // Apache Software License 2.0

6.1.28

8 days ago

6.1.27

11 days ago

6.1.26

21 days ago

6.1.25

22 days ago

6.1.24

23 days ago

6.1.23

27 days ago

6.1.22

29 days ago

6.1.21

1 month ago

6.1.20

1 month ago

6.1.19

1 month ago

6.1.18

1 month ago

6.1.17

1 month ago

6.1.16

1 month ago

6.1.15

2 months ago

6.1.14

2 months ago

6.1.13

2 months ago

6.1.12

2 months ago

6.1.11

2 months ago

6.1.10

2 months ago

6.1.8

2 months ago

6.1.9

2 months ago

6.1.7

2 months ago

6.1.6

2 months ago

6.1.5

2 months ago

6.1.2

3 months ago

6.1.1

3 months ago

6.1.4

3 months ago

6.1.3

3 months ago

6.1.0

3 months ago

6.0.4

3 months ago

6.0.3

4 months ago

6.0.2

4 months ago

5.0.6

4 months ago

6.0.1

4 months ago

6.0.0

4 months ago

5.0.5

4 months ago

5.0.4

4 months ago

5.0.3

4 months ago

5.0.2

5 months ago

5.0.1

5 months ago

5.0.0

5 months ago

4.2.20

8 months ago

4.2.21

7 months ago

4.2.22

7 months ago

4.2.23

7 months ago

4.2.28

6 months ago

4.2.29

6 months ago

4.2.24

6 months ago

4.2.25

6 months ago

4.2.26

6 months ago

4.2.27

6 months ago

4.3.2

6 months ago

4.3.1

6 months ago

4.2.10

8 months ago

4.3.4

5 months ago

4.2.11

8 months ago

4.3.3

5 months ago

4.2.12

8 months ago

4.3.0

6 months ago

4.2.17

8 months ago

4.2.18

8 months ago

4.2.19

8 months ago

4.2.13

8 months ago

4.2.14

8 months ago

4.2.15

8 months ago

4.2.16

8 months ago

4.2.9

9 months ago

4.2.8

9 months ago

4.2.5

10 months ago

4.2.7

10 months ago

4.2.6

10 months ago

4.2.3

11 months ago

4.2.4

11 months ago

4.2.2

12 months ago

4.2.1

1 year ago

4.1.8

1 year ago

4.1.7

1 year ago

4.1.9

1 year ago

4.2.0

1 year ago

4.1.10

1 year ago

4.1.11

1 year ago

4.1.6

1 year ago

4.1.5

1 year ago

4.1.4

1 year ago

4.1.3

1 year ago

4.1.2

1 year ago

4.1.1

1 year ago

4.0.12

1 year ago

4.0.11

1 year ago

4.0.14

1 year ago

4.0.13

1 year ago

4.1.0

1 year ago

4.0.5

1 year ago

4.0.4

1 year ago

4.0.7

1 year ago

4.0.6

1 year ago

4.0.1

2 years ago

4.0.3

2 years ago

4.0.2

2 years ago

4.0.9

1 year ago

4.0.8

1 year ago

4.0.10

1 year ago

4.0.0

2 years ago

3.4.14

2 years ago

3.4.13

2 years ago

3.4.10

2 years ago

3.4.11

2 years ago

3.4.12

2 years ago

3.4.8

2 years ago

3.4.7

2 years ago

3.4.6

2 years ago

3.4.9

2 years ago

3.4.4

2 years ago

3.4.3

2 years ago

3.4.2

2 years ago

3.4.1

2 years ago

3.4.5

2 years ago

3.4.0

2 years ago

3.3.2

2 years ago

3.2.2

2 years ago

3.2.1

2 years ago

3.2.0

2 years ago

3.2.5

2 years ago

3.2.4

2 years ago

3.2.3

2 years ago

3.3.1

2 years ago

3.3.0

2 years ago

3.1.0

2 years ago

2.2.0

2 years ago

3.0.0

2 years ago

2.1.3

2 years ago

2.1.5

2 years ago

2.0.1

3 years ago

2.0.0

3 years ago

2.1.2

3 years ago

2.1.0

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

0.11.2

3 years ago

0.11.0

3 years ago

0.11.1

3 years ago

0.10.5

3 years ago

0.10.4

3 years ago

0.10.3

3 years ago

0.10.2

3 years ago

0.10.1

3 years ago

0.10.0

3 years ago

0.9.0

3 years ago

0.8.1

3 years ago

0.8.0

3 years ago

0.7.4

3 years ago

0.7.2

3 years ago

0.7.1

3 years ago

0.7.3

3 years ago

0.6.1

3 years ago

0.6.0

3 years ago

0.5.1

3 years ago

0.5.0

3 years ago

0.4.10

3 years ago

0.4.9

3 years ago

0.4.8

3 years ago

0.4.7

3 years ago

0.4.6

4 years ago

0.4.5

4 years ago

0.4.4

4 years ago

0.4.3

4 years ago

0.4.2

4 years ago

0.4.1

4 years ago

0.4.0

4 years ago

0.3.6

4 years ago

0.3.5

4 years ago

0.3.4

4 years ago

0.3.3

4 years ago

0.3.2

4 years ago

0.3.1

4 years ago

0.3.0

4 years ago

0.2.0

4 years ago

0.1.20

4 years ago

0.1.19

4 years ago

0.1.18

4 years ago

0.1.17

4 years ago

0.1.16

4 years ago

0.1.15

4 years ago

0.1.14

4 years ago

0.1.13

4 years ago

0.1.12

4 years ago

0.1.11

4 years ago

0.1.8

4 years ago

0.1.7

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago