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

Function contain

plugins/plugin-contain/src/index.ts:37–81  ·  view source on GitHub ↗

* Scale the image to the given width and height keeping the aspect ratio. Some parts of the image may be letter boxed. * @param w the width to resize the image to * @param h the height to resize the image to * @param align A bitmask for horizontal and vertical alignment * @param mode a s

(image: I, options: ContainOptions)

Source from the content-addressed store, hash-verified

35 * ```
36 */
37 contain<I extends JimpClass>(image: I, options: ContainOptions) {
38 const {
39 w,
40 h,
41 align = HorizontalAlign.CENTER | VerticalAlign.MIDDLE,
42 mode,
43 } = ContainOptionsSchema.parse(options);
44
45 const hbits = align & ((1 << 3) - 1);
46 const vbits = align >> 3;
47
48 // check if more flags than one is in the bit sets
49 if (
50 !(
51 (hbits !== 0 && !(hbits & (hbits - 1))) ||
52 (vbits !== 0 && !(vbits & (vbits - 1)))
53 )
54 ) {
55 throw new Error("only use one flag per alignment direction");
56 }
57
58 const alignH = hbits >> 1; // 0, 1, 2
59 const alignV = vbits >> 1; // 0, 1, 2
60
61 const f =
62 w / h > image.bitmap.width / image.bitmap.height
63 ? h / image.bitmap.height
64 : w / image.bitmap.width;
65
66 const c = resizeMethods.scale(clone(image), { f, mode });
67
68 image = resizeMethods.resize(image, { w, h, mode });
69
70 image.scan((_, __, idx) => {
71 image.bitmap.data.writeUInt32BE(image.background, idx);
72 });
73
74 image = blitMethods.blit(image, {
75 src: c,
76 x: ((image.bitmap.width - c.bitmap.width) / 2) * alignH,
77 y: ((image.bitmap.height - c.bitmap.height) / 2) * alignV,
78 });
79
80 return image;
81 },
82};

Callers

nothing calls this directly

Calls 3

cloneFunction · 0.90
resizeMethod · 0.80
scanMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…