UNPKG

@thi.ng/pixel

Version:

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

23 lines (22 loc) 907 B
import { assert } from "@thi.ng/errors/assert"; const ensureSize = (data, width, height, stride = 1) => assert(data.length >= width * height * stride, "pixel buffer too small"); const ensureImageData = (data, width, height) => data ? (ensureImageDataSize(data, width, height), data) : new ImageData(width, height); const ensureImageDataSize = (data, width, height) => assert( data.width === width && data.height === height, "imagedata has wrong dimensions" ); const ensureChannel = (fmt, id) => { const chan = fmt.channels[id]; assert(chan != null, `invalid channel ID: ${id}`); return chan; }; const ensureSingleChannel = (fmt) => assert(fmt.channels.length === 1, `require single channel buffer`); const ensureAlpha = (fmt) => assert(!!fmt.alpha, "missing alpha channel"); export { ensureAlpha, ensureChannel, ensureImageData, ensureImageDataSize, ensureSingleChannel, ensureSize };