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

Function blit

plugins/plugin-blit/src/index.ts:43–113  ·  view source on GitHub ↗

* Short for "bit-block transfer". * It involves the transfer of a block of pixel data from one area of a computer's memory to another area, typically for the purpose of rendering images on the screen or manipulating them in various ways. * It's a fundamental operation in computer graphics util

(image: I, options: BlitOptions)

Source from the content-addressed store, hash-verified

41 * ```
42 */
43 blit<I extends JimpClass>(image: I, options: BlitOptions) {
44 const parsed = BlitOptionsSchema.parse(options);
45 let {
46 // eslint-disable-next-line prefer-const
47 src,
48 x = 0,
49 y = 0,
50 srcX = 0,
51 srcY = 0,
52 srcW = src.bitmap.width,
53 srcH = src.bitmap.height,
54 } = "bitmap" in parsed ? ({ src: parsed } as BlitOptionsComplex) : parsed;
55
56 if (!("bitmap" in src)) {
57 throw new Error("The source must be a Jimp image");
58 }
59
60 if (typeof x !== "number" || typeof y !== "number") {
61 throw new Error("x and y must be numbers");
62 }
63
64 // round input
65 x = Math.round(x);
66 y = Math.round(y);
67
68 // round input
69 srcX = Math.round(srcX);
70 srcY = Math.round(srcY);
71 srcW = Math.round(srcW);
72 srcH = Math.round(srcH);
73
74 const maxWidth = image.bitmap.width;
75 const maxHeight = image.bitmap.height;
76
77 scan(src, srcX, srcY, srcW, srcH, function (sx, sy, idx) {
78 const xOffset = x + sx - srcX;
79 const yOffset = y + sy - srcY;
80
81 if (
82 xOffset >= 0 &&
83 yOffset >= 0 &&
84 maxWidth - xOffset > 0 &&
85 maxHeight - yOffset > 0
86 ) {
87 const dstIdx = image.getPixelIndex(xOffset, yOffset);
88 const srcColor = {
89 r: src.bitmap.data[idx] || 0,
90 g: src.bitmap.data[idx + 1] || 0,
91 b: src.bitmap.data[idx + 2] || 0,
92 a: src.bitmap.data[idx + 3] || 0,
93 };
94
95 const dst = {
96 r: image.bitmap.data[dstIdx] || 0,
97 g: image.bitmap.data[dstIdx + 1] || 0,
98 b: image.bitmap.data[dstIdx + 2] || 0,
99 a: image.bitmap.data[dstIdx + 3] || 0,
100 };

Callers

nothing calls this directly

Calls 3

scanFunction · 0.90
limit255Function · 0.90
getPixelIndexMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…