|
| 1 | +import { JimpClass } from "@jimp/types"; |
| 2 | +import { limit255, scan } from "@jimp/utils"; |
| 3 | + |
| 4 | +/** |
| 5 | + * Blits a source image on to this image |
| 6 | + */ |
| 7 | +function blit<I extends JimpClass>( |
| 8 | + image: I, |
| 9 | + { |
| 10 | + src, |
| 11 | + x = 0, |
| 12 | + y = 0, |
| 13 | + srcX = 0, |
| 14 | + srcY = 0, |
| 15 | + srcW = src.bitmap.width, |
| 16 | + srcH = src.bitmap.height, |
| 17 | + }: { |
| 18 | + /** This image to blit on to the current image */ |
| 19 | + src: I; |
| 20 | + /** the x position to blit the image */ |
| 21 | + x: number; |
| 22 | + /** the y position to blit the image */ |
| 23 | + y: number; |
| 24 | + /** the x position from which to crop the source image */ |
| 25 | + srcX?: number; |
| 26 | + /** the y position from which to crop the source image */ |
| 27 | + srcY?: number; |
| 28 | + /** the width to which to crop the source image */ |
| 29 | + srcW?: number; |
| 30 | + /** the height to which to crop the source image */ |
| 31 | + srcH?: number; |
| 32 | + }, |
| 33 | +) { |
| 34 | + if (!("bitmap" in src)) { |
| 35 | + throw new Error("The source must be a Jimp image"); |
| 36 | + } |
| 37 | + |
| 38 | + if (typeof x !== "number" || typeof y !== "number") { |
| 39 | + throw new Error("x and y must be numbers"); |
| 40 | + } |
| 41 | + |
| 42 | + // round input |
| 43 | + x = Math.round(x); |
| 44 | + y = Math.round(y); |
| 45 | + |
| 46 | + // round input |
| 47 | + srcX = Math.round(srcX); |
| 48 | + srcY = Math.round(srcY); |
| 49 | + srcW = Math.round(srcW); |
| 50 | + srcH = Math.round(srcH); |
| 51 | + |
| 52 | + const maxWidth = image.bitmap.width; |
| 53 | + const maxHeight = image.bitmap.height; |
| 54 | + |
| 55 | + scan(src, srcX, srcY, srcW, srcH, function (sx, sy, idx) { |
| 56 | + const xOffset = x + sx - srcX; |
| 57 | + const yOffset = y + sy - srcY; |
| 58 | + |
| 59 | + if ( |
| 60 | + xOffset >= 0 && |
| 61 | + yOffset >= 0 && |
| 62 | + maxWidth - xOffset > 0 && |
| 63 | + maxHeight - yOffset > 0 |
| 64 | + ) { |
| 65 | + const dstIdx = image.getPixelIndex(xOffset, yOffset); |
| 66 | + const srcColor = { |
| 67 | + r: src.bitmap.data[idx] || 0, |
| 68 | + g: src.bitmap.data[idx + 1] || 0, |
| 69 | + b: src.bitmap.data[idx + 2] || 0, |
| 70 | + a: src.bitmap.data[idx + 3] || 0, |
| 71 | + }; |
| 72 | + |
| 73 | + const dst = { |
| 74 | + r: image.bitmap.data[dstIdx] || 0, |
| 75 | + g: image.bitmap.data[dstIdx + 1] || 0, |
| 76 | + b: image.bitmap.data[dstIdx + 2] || 0, |
| 77 | + a: image.bitmap.data[dstIdx + 3] || 0, |
| 78 | + }; |
| 79 | + |
| 80 | + image.bitmap.data[dstIdx] = |
| 81 | + ((srcColor.a * (srcColor.r - dst.r) - dst.r + 255) >> 8) + dst.r; |
| 82 | + image.bitmap.data[dstIdx + 1] = |
| 83 | + ((srcColor.a * (srcColor.g - dst.g) - dst.g + 255) >> 8) + dst.g; |
| 84 | + image.bitmap.data[dstIdx + 2] = |
| 85 | + ((srcColor.a * (srcColor.b - dst.b) - dst.b + 255) >> 8) + dst.b; |
| 86 | + image.bitmap.data[dstIdx + 3] = limit255(dst.a + srcColor.a); |
| 87 | + } |
| 88 | + }); |
| 89 | + |
| 90 | + return image; |
| 91 | +} |
| 92 | + |
| 93 | +export default function blitPlugin() { |
| 94 | + return { |
| 95 | + blit, |
| 96 | + }; |
| 97 | +} |
0 commit comments