(
baseImage: I,
src: I,
x = 0,
y = 0,
options: {
mode?: BlendMode;
opacitySource?: number;
opacityDest?: number;
} = {}
)
| 5 | import { limit255 } from "@jimp/utils"; |
| 6 | |
| 7 | export function composite<I extends JimpClass>( |
| 8 | baseImage: I, |
| 9 | src: I, |
| 10 | x = 0, |
| 11 | y = 0, |
| 12 | options: { |
| 13 | mode?: BlendMode; |
| 14 | opacitySource?: number; |
| 15 | opacityDest?: number; |
| 16 | } = {} |
| 17 | ) { |
| 18 | if (!(src instanceof baseImage.constructor)) { |
| 19 | throw new Error("The source must be a Jimp image"); |
| 20 | } |
| 21 | |
| 22 | if (typeof x !== "number" || typeof y !== "number") { |
| 23 | throw new Error("x and y must be numbers"); |
| 24 | } |
| 25 | |
| 26 | const { mode = BlendMode.SRC_OVER } = options; |
| 27 | let { opacitySource = 1.0, opacityDest = 1.0 } = options; |
| 28 | |
| 29 | if ( |
| 30 | typeof opacitySource !== "number" || |
| 31 | opacitySource < 0 || |
| 32 | opacitySource > 1 |
| 33 | ) { |
| 34 | opacitySource = 1.0; |
| 35 | } |
| 36 | |
| 37 | if (typeof opacityDest !== "number" || opacityDest < 0 || opacityDest > 1) { |
| 38 | opacityDest = 1.0; |
| 39 | } |
| 40 | |
| 41 | const blendmode = compositeModes[mode]; |
| 42 | |
| 43 | // round input |
| 44 | x = Math.round(x); |
| 45 | y = Math.round(y); |
| 46 | |
| 47 | if (opacityDest !== 1.0) { |
| 48 | baseImage.scan((_, __, idx) => { |
| 49 | const v = baseImage.bitmap.data[idx + 3]! * opacityDest; |
| 50 | baseImage.bitmap.data[idx + 3] = v; |
| 51 | }); |
| 52 | } |
| 53 | |
| 54 | src.scan((sx, sy, idx) => { |
| 55 | const dstIdx = baseImage.getPixelIndex(x + sx, y + sy, Edge.CROP); |
| 56 | |
| 57 | if (dstIdx === -1) { |
| 58 | // Skip target pixels outside of dst |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | const blended = blendmode( |
| 63 | { |
| 64 | r: src.bitmap.data[idx + 0]! / 255, |
no test coverage detected
searching dependent graphs…