MCPcopy
hub / github.com/jimp-dev/jimp / pixelate

Function pixelate

plugins/plugin-color/src/index.ts:693–726  ·  view source on GitHub ↗

* Pixelates the image or a region * @example * ```ts * import { Jimp } from "jimp"; * * const image = await Jimp.read("test/image.png"); * * // pixelate the whole image * image.pixelate(10); * * // pixelate a region * image.pixelate(10, 10, 10, 20, 20); * ```

(image: I, options: PixelateOptions)

Source from the content-addressed store, hash-verified

691 * ```
692 */
693 pixelate<I extends JimpClass>(image: I, options: PixelateOptions) {
694 const parsed = PixelateOptionsSchema.parse(options);
695 const {
696 size,
697 x = 0,
698 y = 0,
699 w = image.bitmap.width - x,
700 h = image.bitmap.height - y,
701 } = typeof parsed === "number"
702 ? ({ size: parsed } as PixelateComplexOptions)
703 : parsed;
704
705 const kernel = [
706 [1 / 16, 2 / 16, 1 / 16],
707 [2 / 16, 4 / 16, 2 / 16],
708 [1 / 16, 2 / 16, 1 / 16],
709 ];
710
711 const source = clone(image);
712
713 scan(source, x, y, w, h, (xx, yx, idx) => {
714 xx = size * Math.floor(xx / size);
715 yx = size * Math.floor(yx / size);
716
717 const value = applyKernel(source, kernel, xx, yx);
718
719 image.bitmap.data[idx] = value[0]!;
720 image.bitmap.data[idx + 1] = value[1]!;
721 image.bitmap.data[idx + 2] = value[2]!;
722 image.bitmap.data[idx + 3] = value[3]!;
723 });
724
725 return image;
726 },
727
728 /**
729 * Applies a convolution kernel to the image or a region

Callers

nothing calls this directly

Calls 3

cloneFunction · 0.90
scanFunction · 0.90
applyKernelFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…