* Masks a source image on to this image using average pixel colour. A completely black pixel on the mask will turn a pixel in the image completely transparent. * @param src the source Jimp instance * @param x the horizontal position to blit the image * @param y the vertical position to blit
(image: I, options: MaskOptions)
| 30 | * ``` |
| 31 | */ |
| 32 | mask<I extends JimpClass>(image: I, options: MaskOptions) { |
| 33 | MaskOptionsSchema.parse(options); |
| 34 | |
| 35 | let src: JimpClass; |
| 36 | let x: number; |
| 37 | let y: number; |
| 38 | |
| 39 | if ("bitmap" in options) { |
| 40 | src = options as unknown as JimpClass; |
| 41 | x = 0; |
| 42 | y = 0; |
| 43 | } else { |
| 44 | src = options.src as unknown as JimpClass; |
| 45 | x = options.x ?? 0; |
| 46 | y = options.y ?? 0; |
| 47 | } |
| 48 | |
| 49 | // round input |
| 50 | x = Math.round(x); |
| 51 | y = Math.round(y); |
| 52 | |
| 53 | const w = image.bitmap.width; |
| 54 | const h = image.bitmap.height; |
| 55 | |
| 56 | src.scan(function (sx, sy, idx) { |
| 57 | const destX = x + sx; |
| 58 | const destY = y + sy; |
| 59 | |
| 60 | if (destX >= 0 && destY >= 0 && destX < w && destY < h) { |
| 61 | const dstIdx = image.getPixelIndex(destX, destY); |
| 62 | const { data } = src.bitmap; |
| 63 | const avg = (data[idx + 0]! + data[idx + 1]! + data[idx + 2]!) / 3; |
| 64 | |
| 65 | image.bitmap.data[dstIdx + 3] *= avg / 255; |
| 66 | } |
| 67 | }); |
| 68 | |
| 69 | return image; |
| 70 | }, |
| 71 | }; |
nothing calls this directly
no test coverage detected
searching dependent graphs…