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

Function threshold

plugins/plugin-threshold/src/index.ts:30–55  ·  view source on GitHub ↗

* Applies a minimum color threshold to a grayscale image. * Converts image to grayscale by default. * @example * ```ts * import { Jimp } from "jimp"; * * const image = await Jimp.read("test/image.png"); * * image.threshold({ max: 150 }); * ```

(image: I, options: ThresholdOptions)

Source from the content-addressed store, hash-verified

28 * ```
29 */
30 threshold<I extends JimpClass>(image: I, options: ThresholdOptions) {
31 let {
32 max,
33 replace = 255,
34 // eslint-disable-next-line prefer-const
35 autoGreyscale = true,
36 } = ThresholdOptionsSchema.parse(options);
37
38 max = limit255(max);
39 replace = limit255(replace);
40
41 if (autoGreyscale) {
42 color.greyscale(image);
43 }
44
45 image.scan((_, __, idx) => {
46 const grey =
47 image.bitmap.data[idx]! < max ? image.bitmap.data[idx]! : replace;
48
49 image.bitmap.data[idx] = grey;
50 image.bitmap.data[idx + 1] = grey;
51 image.bitmap.data[idx + 2] = grey;
52 });
53
54 return image;
55 },
56};

Callers

nothing calls this directly

Calls 2

limit255Function · 0.90
scanMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…