Initial commit
This commit is contained in:
commit
e49726988c
5445 files changed
+1089914
No files matched your search
+8
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"extends": "../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"rootDir": "../src",
|
||||
"module": "nodenext",
|
||||
"moduleResolution": "nodenext"
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"extends": "./build.json",
|
||||
"include": [
|
||||
"../src/**/*.ts",
|
||||
"../src/**/*.cts",
|
||||
"../src/**/*.tsx",
|
||||
"../src/**/*.json"
|
||||
],
|
||||
"exclude": [
|
||||
"../**/*.test.ts",
|
||||
"../src/**/*.mts",
|
||||
"../src/package.json"
|
||||
],
|
||||
"compilerOptions": {
|
||||
"outDir": "../.tshy-build/commonjs"
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"extends": "./build.json",
|
||||
"include": [
|
||||
"../src/**/*.ts",
|
||||
"../src/**/*.mts",
|
||||
"../src/**/*.tsx",
|
||||
"../src/**/*.json"
|
||||
],
|
||||
"exclude": [
|
||||
"../**/*.test.ts",
|
||||
"../src/package.json"
|
||||
],
|
||||
"compilerOptions": {
|
||||
"outDir": "../.tshy-build/esm"
|
||||
}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
|
||||
|
||||
> @jimp/plugin-mask@1.5.0 build /Users/andrewlisowski/Documents/jimp/plugins/plugin-mask
|
||||
> tshy
|
||||
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
|
||||
|
||||
> @jimp/plugin-mask@1.1.2 lint /Users/andrewlisowski/Documents/jimp/plugins/plugin-mask
|
||||
> eslint .
|
||||
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
|
||||
|
||||
> @jimp/plugin-mask@1.1.2 test:browser /Users/andrewlisowski/Documents/jimp/plugins/plugin-mask
|
||||
> vitest --config vitest.config.browser.mjs "--watch=false"
|
||||
|
||||
Re-optimizing dependencies because lockfile has changed
|
||||
Port 5173 is in use, trying another one...
|
||||
Port 5174 is in use, trying another one...
|
||||
Port 5175 is in use, trying another one...
|
||||
Port 5176 is in use, trying another one...
|
||||
Port 5177 is in use, trying another one...
|
||||
|
||||
[7m[1m[36m RUN [39m[22m[27m [36mv2.0.5[39m [90m/Users/andrewlisowski/Documents/jimp/plugins/plugin-mask[39m
|
||||
[2m[32m Browser runner started at http://localhost:5178/[39m[22m
|
||||
|
||||
|
||||
[31m⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1m[7m Unhandled Rejection [27m[22m⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[39m
|
||||
[31m[1mError[22m: The service was stopped: write EPIPE[39m
|
||||
[90m [2m❯[22m ../../node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:[2m993:26[22m[39m
|
||||
[90m [2m❯[22m responseCallbacks.<computed> ../../node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:[2m622:9[22m[39m
|
||||
[90m [2m❯[22m afterClose ../../node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:[2m613:28[22m[39m
|
||||
[90m [2m❯[22m ../../node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:[2m1983:18[22m[39m
|
||||
[90m [2m❯[22m onwriteError node:internal/streams/writable:[2m418:3[22m[39m
|
||||
[90m [2m❯[22m process.processTicksAndRejections node:internal/process/task_queues:[2m84:21[22m[39m
|
||||
|
||||
|
||||
|
||||
|
||||
[41m[30m ELIFECYCLE [39m[49m [31mCommand failed with exit code 1.[39m
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
|
||||
|
||||
> @jimp/plugin-mask@1.1.2 test /Users/andrewlisowski/Documents/jimp/plugins/plugin-mask
|
||||
> vitest "--watch=false"
|
||||
|
||||
|
||||
[7m[1m[36m RUN [39m[22m[27m [36mv2.0.5[39m [90m/Users/andrewlisowski/Documents/jimp/plugins/plugin-mask[39m
|
||||
|
||||
[?25l [90m·[39m [2msrc/[22mindex[2m.test.ts[22m[2m (8)[22m
|
||||
[90m·[39m Mask[2m (8)[22m
|
||||
[90m·[39m Affect opaque image with a gray mask with the same size
|
||||
[90m·[39m Affect opaque image with a gray mask with the same size, blited
|
||||
[90m·[39m Affect opaque image with a gray mask with the same size, blited negative
|
||||
[90m·[39m Affect opaque image with a smaller gray mask
|
||||
[90m·[39m Affect opaque image with a smaller gray mask, blited
|
||||
[90m·[39m Affect alpha image with a bigger gray mask
|
||||
[90m·[39m Affect alpha image with a bigger gray mask, blited
|
||||
[90m·[39m Affect opaque image with a colored mask
|
||||
[?25l[2K[1A[2K[1A[2K[1A[2K[1A[2K[1A[2K[1A[2K[1A[2K[1A[2K[1A[2K[1A[2K[G [32m✓[39m [2msrc/[22mindex[2m.test.ts[22m[2m (8)[22m
|
||||
[32m✓[39m Mask[2m (8)[22m
|
||||
[32m✓[39m Affect opaque image with a gray mask with the same size
|
||||
[32m✓[39m Affect opaque image with a gray mask with the same size, blited
|
||||
[32m✓[39m Affect opaque image with a gray mask with the same size, blited negative
|
||||
[32m✓[39m Affect opaque image with a smaller gray mask
|
||||
[32m✓[39m Affect opaque image with a smaller gray mask, blited
|
||||
[32m✓[39m Affect alpha image with a bigger gray mask
|
||||
[32m✓[39m Affect alpha image with a bigger gray mask, blited
|
||||
[32m✓[39m Affect opaque image with a colored mask
|
||||
[?25l[2K[1A[2K[1A[2K[1A[2K[1A[2K[1A[2K[1A[2K[1A[2K[1A[2K[1A[2K[1A[2K[G [32m✓[39m [2msrc/[22mindex[2m.test.ts[22m[2m (8)[22m
|
||||
[32m✓[39m Mask[2m (8)[22m
|
||||
[32m✓[39m Affect opaque image with a gray mask with the same size
|
||||
[32m✓[39m Affect opaque image with a gray mask with the same size, blited
|
||||
[32m✓[39m Affect opaque image with a gray mask with the same size, blited negative
|
||||
[32m✓[39m Affect opaque image with a smaller gray mask
|
||||
[32m✓[39m Affect opaque image with a smaller gray mask, blited
|
||||
[32m✓[39m Affect alpha image with a bigger gray mask
|
||||
[32m✓[39m Affect alpha image with a bigger gray mask, blited
|
||||
[32m✓[39m Affect opaque image with a colored mask
|
||||
|
||||
[2m Test Files [22m [1m[32m1 passed[39m[22m[90m (1)[39m
|
||||
[2m Tests [22m [1m[32m8 passed[39m[22m[90m (8)[39m
|
||||
[2m Start at [22m 01:33:48
|
||||
[2m Duration [22m 1.46s[2m (transform 350ms, setup 0ms, collect 598ms, tests 6ms, environment 3ms, prepare 241ms)[22m
|
||||
|
||||
[?25h[?25h
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
# v1.1.3 (Mon Sep 02 2024)
|
||||
|
||||
#### 🐛 Bug Fix
|
||||
|
||||
- Update deps [#1319](https://github.com/jimp-dev/jimp/pull/1319) ([@hipstersmoothie](https://github.com/hipstersmoothie))
|
||||
|
||||
#### Authors: 1
|
||||
|
||||
- Andrew Lisowski ([@hipstersmoothie](https://github.com/hipstersmoothie))
|
||||
|
||||
---
|
||||
|
||||
# v1.1.0 (Sun Sep 01 2024)
|
||||
|
||||
#### ⚠️ Pushed to `main`
|
||||
|
||||
- upgrade tshy ([@hipstersmoothie](https://github.com/hipstersmoothie))
|
||||
- update more docs ([@hipstersmoothie](https://github.com/hipstersmoothie))
|
||||
|
||||
#### Authors: 1
|
||||
|
||||
- Andrew Lisowski ([@hipstersmoothie](https://github.com/hipstersmoothie))
|
||||
|
||||
---
|
||||
|
||||
# v1.0.3 (Sat Aug 31 2024)
|
||||
|
||||
#### 🐛 Bug Fix
|
||||
|
||||
- Fix build [#1303](https://github.com/jimp-dev/jimp/pull/1303) ([@hipstersmoothie](https://github.com/hipstersmoothie))
|
||||
|
||||
#### ⚠️ Pushed to `main`
|
||||
|
||||
- add clean script ([@hipstersmoothie](https://github.com/hipstersmoothie))
|
||||
|
||||
#### Authors: 1
|
||||
|
||||
- Andrew Lisowski ([@hipstersmoothie](https://github.com/hipstersmoothie))
|
||||
|
||||
---
|
||||
|
||||
# v1.0.2 (Sat Aug 31 2024)
|
||||
|
||||
#### ⚠️ Pushed to `main`
|
||||
|
||||
- set side effects ([@hipstersmoothie](https://github.com/hipstersmoothie))
|
||||
- fix versions ([@hipstersmoothie](https://github.com/hipstersmoothie))
|
||||
- add publish config ([@hipstersmoothie](https://github.com/hipstersmoothie))
|
||||
|
||||
#### Authors: 1
|
||||
|
||||
- Andrew Lisowski ([@hipstersmoothie](https://github.com/hipstersmoothie))
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2018 Oliver Moran
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
<div align="center">
|
||||
<img width="200" height="200"
|
||||
src="https://s3.amazonaws.com/pix.iemoji.com/images/emoji/apple/ios-11/256/crayon.png">
|
||||
<h1>@jimp/plugin-mask</h1>
|
||||
<p>mask an image with another image.</p>
|
||||
</div>
|
||||
|
||||
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.
|
||||
|
||||
- [mask](http://jimp-dev.github.io/jimp/api/jimp/classes/jimp#mask)
|
||||
+102
@@ -0,0 +1,102 @@
|
||||
import { JimpClass } from "@jimp/types";
|
||||
import { z } from "zod";
|
||||
declare const MaskOptionsSchema: z.ZodUnion<[z.ZodObject<{
|
||||
bitmap: z.ZodObject<{
|
||||
data: z.ZodUnion<[z.ZodType<Buffer, z.ZodTypeDef, Buffer>, z.ZodType<Uint8Array, z.ZodTypeDef, Uint8Array>]>;
|
||||
width: z.ZodNumber;
|
||||
height: z.ZodNumber;
|
||||
}, "strip", z.ZodTypeAny, {
|
||||
data: Buffer | Uint8Array;
|
||||
width: number;
|
||||
height: number;
|
||||
}, {
|
||||
data: Buffer | Uint8Array;
|
||||
width: number;
|
||||
height: number;
|
||||
}>;
|
||||
}, "strip", z.ZodTypeAny, {
|
||||
bitmap: {
|
||||
data: Buffer | Uint8Array;
|
||||
width: number;
|
||||
height: number;
|
||||
};
|
||||
}, {
|
||||
bitmap: {
|
||||
data: Buffer | Uint8Array;
|
||||
width: number;
|
||||
height: number;
|
||||
};
|
||||
}>, z.ZodObject<{
|
||||
src: z.ZodObject<{
|
||||
bitmap: z.ZodObject<{
|
||||
data: z.ZodUnion<[z.ZodType<Buffer, z.ZodTypeDef, Buffer>, z.ZodType<Uint8Array, z.ZodTypeDef, Uint8Array>]>;
|
||||
width: z.ZodNumber;
|
||||
height: z.ZodNumber;
|
||||
}, "strip", z.ZodTypeAny, {
|
||||
data: Buffer | Uint8Array;
|
||||
width: number;
|
||||
height: number;
|
||||
}, {
|
||||
data: Buffer | Uint8Array;
|
||||
width: number;
|
||||
height: number;
|
||||
}>;
|
||||
}, "strip", z.ZodTypeAny, {
|
||||
bitmap: {
|
||||
data: Buffer | Uint8Array;
|
||||
width: number;
|
||||
height: number;
|
||||
};
|
||||
}, {
|
||||
bitmap: {
|
||||
data: Buffer | Uint8Array;
|
||||
width: number;
|
||||
height: number;
|
||||
};
|
||||
}>;
|
||||
/** the x position to draw the image */
|
||||
x: z.ZodOptional<z.ZodNumber>;
|
||||
/** the y position to draw the image */
|
||||
y: z.ZodOptional<z.ZodNumber>;
|
||||
}, "strip", z.ZodTypeAny, {
|
||||
src: {
|
||||
bitmap: {
|
||||
data: Buffer | Uint8Array;
|
||||
width: number;
|
||||
height: number;
|
||||
};
|
||||
};
|
||||
x?: number | undefined;
|
||||
y?: number | undefined;
|
||||
}, {
|
||||
src: {
|
||||
bitmap: {
|
||||
data: Buffer | Uint8Array;
|
||||
width: number;
|
||||
height: number;
|
||||
};
|
||||
};
|
||||
x?: number | undefined;
|
||||
y?: number | undefined;
|
||||
}>]>;
|
||||
export type MaskOptions = z.infer<typeof MaskOptionsSchema>;
|
||||
export declare const methods: {
|
||||
/**
|
||||
* 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 the image
|
||||
* @example
|
||||
* ```ts
|
||||
* import { Jimp } from "jimp";
|
||||
*
|
||||
* const image = await Jimp.read("test/image.png");
|
||||
* const mask = await Jimp.read("test/mask.png");
|
||||
*
|
||||
* image.mask(mask);
|
||||
* ```
|
||||
*/
|
||||
mask<I extends JimpClass>(image: I, options: MaskOptions): I;
|
||||
};
|
||||
export {};
|
||||
//# sourceMappingURL=index.d.ts.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAmB,MAAM,aAAa,CAAC;AACzD,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAUxB,QAAA,MAAM,iBAAiB;;oCAcZ,MAAK,gBAAe,MAAO;;;;cAMO,MAAO;;;;cAIzC,MAAO;;;;;;cAOd,MAAK;;;;;;cAGc,MACpB;;;;;;;wCArBQ,MAAK,gBAAe,MAAO;;;;kBAMO,MAAO;;;;kBAIzC,MAAO;;;;;;kBAOd,MAAK;;;;;;kBAGc,MACpB;;;;;IAzCD,uCAAuC;;IAEvC,uCAAuC;;;;;kBAmCrC,MAAK;;;;;;;;;;kBAGc,MACpB;;;;;;;IAnC0E,CAAC;AAE9E,MAAM,MAAM,WAAW,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,iBAAiB,CAAC,CAAC;AAE5D,eAAO,MAAM,OAAO;IAClB;;;;;;;;;;;;;;OAcG;SACE,CAAC,SAAS,SAAS,SAAS,CAAC,WAAW,WAAW;CAuCzD,CAAC"}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.methods = void 0;
|
||||
const types_1 = require("@jimp/types");
|
||||
const zod_1 = require("zod");
|
||||
const MaskOptionsObjectSchema = zod_1.z.object({
|
||||
src: types_1.JimpClassSchema,
|
||||
/** the x position to draw the image */
|
||||
x: zod_1.z.number().optional(),
|
||||
/** the y position to draw the image */
|
||||
y: zod_1.z.number().optional(),
|
||||
});
|
||||
const MaskOptionsSchema = zod_1.z.union([types_1.JimpClassSchema, MaskOptionsObjectSchema]);
|
||||
exports.methods = {
|
||||
/**
|
||||
* 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 the image
|
||||
* @example
|
||||
* ```ts
|
||||
* import { Jimp } from "jimp";
|
||||
*
|
||||
* const image = await Jimp.read("test/image.png");
|
||||
* const mask = await Jimp.read("test/mask.png");
|
||||
*
|
||||
* image.mask(mask);
|
||||
* ```
|
||||
*/
|
||||
mask(image, options) {
|
||||
MaskOptionsSchema.parse(options);
|
||||
let src;
|
||||
let x;
|
||||
let y;
|
||||
if ("bitmap" in options) {
|
||||
src = options;
|
||||
x = 0;
|
||||
y = 0;
|
||||
}
|
||||
else {
|
||||
src = options.src;
|
||||
x = options.x ?? 0;
|
||||
y = options.y ?? 0;
|
||||
}
|
||||
// round input
|
||||
x = Math.round(x);
|
||||
y = Math.round(y);
|
||||
const w = image.bitmap.width;
|
||||
const h = image.bitmap.height;
|
||||
src.scan(function (sx, sy, idx) {
|
||||
const destX = x + sx;
|
||||
const destY = y + sy;
|
||||
if (destX >= 0 && destY >= 0 && destX < w && destY < h) {
|
||||
const dstIdx = image.getPixelIndex(destX, destY);
|
||||
const { data } = src.bitmap;
|
||||
const avg = (data[idx + 0] + data[idx + 1] + data[idx + 2]) / 3;
|
||||
image.bitmap.data[dstIdx + 3] *= avg / 255;
|
||||
}
|
||||
});
|
||||
return image;
|
||||
},
|
||||
};
|
||||
//# sourceMappingURL=index.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,uCAAyD;AACzD,6BAAwB;AAExB,MAAM,uBAAuB,GAAG,OAAC,CAAC,MAAM,CAAC;IACvC,GAAG,EAAE,uBAAe;IACpB,uCAAuC;IACvC,CAAC,EAAE,OAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;IACxB,uCAAuC;IACvC,CAAC,EAAE,OAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;CACzB,CAAC,CAAC;AAEH,MAAM,iBAAiB,GAAG,OAAC,CAAC,KAAK,CAAC,CAAC,uBAAe,EAAE,uBAAuB,CAAC,CAAC,CAAC;AAIjE,QAAA,OAAO,GAAG;IACrB;;;;;;;;;;;;;;OAcG;IACH,IAAI,CAAsB,KAAQ,EAAE,OAAoB;QACtD,iBAAiB,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;QAEjC,IAAI,GAAc,CAAC;QACnB,IAAI,CAAS,CAAC;QACd,IAAI,CAAS,CAAC;QAEd,IAAI,QAAQ,IAAI,OAAO,EAAE,CAAC;YACxB,GAAG,GAAG,OAA+B,CAAC;YACtC,CAAC,GAAG,CAAC,CAAC;YACN,CAAC,GAAG,CAAC,CAAC;QACR,CAAC;aAAM,CAAC;YACN,GAAG,GAAG,OAAO,CAAC,GAA2B,CAAC;YAC1C,CAAC,GAAG,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC;YACnB,CAAC,GAAG,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC;QACrB,CAAC;QAED,cAAc;QACd,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QAClB,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QAElB,MAAM,CAAC,GAAG,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC;QAC7B,MAAM,CAAC,GAAG,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC;QAE9B,GAAG,CAAC,IAAI,CAAC,UAAU,EAAE,EAAE,EAAE,EAAE,GAAG;YAC5B,MAAM,KAAK,GAAG,CAAC,GAAG,EAAE,CAAC;YACrB,MAAM,KAAK,GAAG,CAAC,GAAG,EAAE,CAAC;YAErB,IAAI,KAAK,IAAI,CAAC,IAAI,KAAK,IAAI,CAAC,IAAI,KAAK,GAAG,CAAC,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;gBACvD,MAAM,MAAM,GAAG,KAAK,CAAC,aAAa,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;gBACjD,MAAM,EAAE,IAAI,EAAE,GAAG,GAAG,CAAC,MAAM,CAAC;gBAC5B,MAAM,GAAG,GAAG,CAAC,IAAI,CAAC,GAAG,GAAG,CAAC,CAAE,GAAG,IAAI,CAAC,GAAG,GAAG,CAAC,CAAE,GAAG,IAAI,CAAC,GAAG,GAAG,CAAC,CAAE,CAAC,GAAG,CAAC,CAAC;gBAEnE,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,IAAI,GAAG,GAAG,GAAG,CAAC;YAC7C,CAAC;QACH,CAAC,CAAC,CAAC;QAEH,OAAO,KAAK,CAAC;IACf,CAAC;CACF,CAAC"}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"type": "commonjs"
|
||||
}
|
||||
+102
@@ -0,0 +1,102 @@
|
||||
import { JimpClass } from "@jimp/types";
|
||||
import { z } from "zod";
|
||||
declare const MaskOptionsSchema: z.ZodUnion<[z.ZodObject<{
|
||||
bitmap: z.ZodObject<{
|
||||
data: z.ZodUnion<[z.ZodType<Buffer, z.ZodTypeDef, Buffer>, z.ZodType<Uint8Array, z.ZodTypeDef, Uint8Array>]>;
|
||||
width: z.ZodNumber;
|
||||
height: z.ZodNumber;
|
||||
}, "strip", z.ZodTypeAny, {
|
||||
data: Buffer | Uint8Array;
|
||||
width: number;
|
||||
height: number;
|
||||
}, {
|
||||
data: Buffer | Uint8Array;
|
||||
width: number;
|
||||
height: number;
|
||||
}>;
|
||||
}, "strip", z.ZodTypeAny, {
|
||||
bitmap: {
|
||||
data: Buffer | Uint8Array;
|
||||
width: number;
|
||||
height: number;
|
||||
};
|
||||
}, {
|
||||
bitmap: {
|
||||
data: Buffer | Uint8Array;
|
||||
width: number;
|
||||
height: number;
|
||||
};
|
||||
}>, z.ZodObject<{
|
||||
src: z.ZodObject<{
|
||||
bitmap: z.ZodObject<{
|
||||
data: z.ZodUnion<[z.ZodType<Buffer, z.ZodTypeDef, Buffer>, z.ZodType<Uint8Array, z.ZodTypeDef, Uint8Array>]>;
|
||||
width: z.ZodNumber;
|
||||
height: z.ZodNumber;
|
||||
}, "strip", z.ZodTypeAny, {
|
||||
data: Buffer | Uint8Array;
|
||||
width: number;
|
||||
height: number;
|
||||
}, {
|
||||
data: Buffer | Uint8Array;
|
||||
width: number;
|
||||
height: number;
|
||||
}>;
|
||||
}, "strip", z.ZodTypeAny, {
|
||||
bitmap: {
|
||||
data: Buffer | Uint8Array;
|
||||
width: number;
|
||||
height: number;
|
||||
};
|
||||
}, {
|
||||
bitmap: {
|
||||
data: Buffer | Uint8Array;
|
||||
width: number;
|
||||
height: number;
|
||||
};
|
||||
}>;
|
||||
/** the x position to draw the image */
|
||||
x: z.ZodOptional<z.ZodNumber>;
|
||||
/** the y position to draw the image */
|
||||
y: z.ZodOptional<z.ZodNumber>;
|
||||
}, "strip", z.ZodTypeAny, {
|
||||
src: {
|
||||
bitmap: {
|
||||
data: Buffer | Uint8Array;
|
||||
width: number;
|
||||
height: number;
|
||||
};
|
||||
};
|
||||
x?: number | undefined;
|
||||
y?: number | undefined;
|
||||
}, {
|
||||
src: {
|
||||
bitmap: {
|
||||
data: Buffer | Uint8Array;
|
||||
width: number;
|
||||
height: number;
|
||||
};
|
||||
};
|
||||
x?: number | undefined;
|
||||
y?: number | undefined;
|
||||
}>]>;
|
||||
export type MaskOptions = z.infer<typeof MaskOptionsSchema>;
|
||||
export declare const methods: {
|
||||
/**
|
||||
* 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 the image
|
||||
* @example
|
||||
* ```ts
|
||||
* import { Jimp } from "jimp";
|
||||
*
|
||||
* const image = await Jimp.read("test/image.png");
|
||||
* const mask = await Jimp.read("test/mask.png");
|
||||
*
|
||||
* image.mask(mask);
|
||||
* ```
|
||||
*/
|
||||
mask<I extends JimpClass>(image: I, options: MaskOptions): I;
|
||||
};
|
||||
export {};
|
||||
//# sourceMappingURL=index.d.ts.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAmB,MAAM,aAAa,CAAC;AACzD,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAUxB,QAAA,MAAM,iBAAiB;;oCAcZ,MAAK,gBAAe,MAAO;;;;cAMO,MAAO;;;;cAIzC,MAAO;;;;;;cAOd,MAAK;;;;;;cAGc,MACpB;;;;;;;wCArBQ,MAAK,gBAAe,MAAO;;;;kBAMO,MAAO;;;;kBAIzC,MAAO;;;;;;kBAOd,MAAK;;;;;;kBAGc,MACpB;;;;;IAzCD,uCAAuC;;IAEvC,uCAAuC;;;;;kBAmCrC,MAAK;;;;;;;;;;kBAGc,MACpB;;;;;;;IAnC0E,CAAC;AAE9E,MAAM,MAAM,WAAW,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,iBAAiB,CAAC,CAAC;AAE5D,eAAO,MAAM,OAAO;IAClB;;;;;;;;;;;;;;OAcG;SACE,CAAC,SAAS,SAAS,SAAS,CAAC,WAAW,WAAW;CAuCzD,CAAC"}
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
import { JimpClassSchema } from "@jimp/types";
|
||||
import { z } from "zod";
|
||||
const MaskOptionsObjectSchema = z.object({
|
||||
src: JimpClassSchema,
|
||||
/** the x position to draw the image */
|
||||
x: z.number().optional(),
|
||||
/** the y position to draw the image */
|
||||
y: z.number().optional(),
|
||||
});
|
||||
const MaskOptionsSchema = z.union([JimpClassSchema, MaskOptionsObjectSchema]);
|
||||
export const methods = {
|
||||
/**
|
||||
* 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 the image
|
||||
* @example
|
||||
* ```ts
|
||||
* import { Jimp } from "jimp";
|
||||
*
|
||||
* const image = await Jimp.read("test/image.png");
|
||||
* const mask = await Jimp.read("test/mask.png");
|
||||
*
|
||||
* image.mask(mask);
|
||||
* ```
|
||||
*/
|
||||
mask(image, options) {
|
||||
MaskOptionsSchema.parse(options);
|
||||
let src;
|
||||
let x;
|
||||
let y;
|
||||
if ("bitmap" in options) {
|
||||
src = options;
|
||||
x = 0;
|
||||
y = 0;
|
||||
}
|
||||
else {
|
||||
src = options.src;
|
||||
x = options.x ?? 0;
|
||||
y = options.y ?? 0;
|
||||
}
|
||||
// round input
|
||||
x = Math.round(x);
|
||||
y = Math.round(y);
|
||||
const w = image.bitmap.width;
|
||||
const h = image.bitmap.height;
|
||||
src.scan(function (sx, sy, idx) {
|
||||
const destX = x + sx;
|
||||
const destY = y + sy;
|
||||
if (destX >= 0 && destY >= 0 && destX < w && destY < h) {
|
||||
const dstIdx = image.getPixelIndex(destX, destY);
|
||||
const { data } = src.bitmap;
|
||||
const avg = (data[idx + 0] + data[idx + 1] + data[idx + 2]) / 3;
|
||||
image.bitmap.data[dstIdx + 3] *= avg / 255;
|
||||
}
|
||||
});
|
||||
return image;
|
||||
},
|
||||
};
|
||||
//# sourceMappingURL=index.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAa,eAAe,EAAE,MAAM,aAAa,CAAC;AACzD,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,MAAM,uBAAuB,GAAG,CAAC,CAAC,MAAM,CAAC;IACvC,GAAG,EAAE,eAAe;IACpB,uCAAuC;IACvC,CAAC,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;IACxB,uCAAuC;IACvC,CAAC,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;CACzB,CAAC,CAAC;AAEH,MAAM,iBAAiB,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,eAAe,EAAE,uBAAuB,CAAC,CAAC,CAAC;AAI9E,MAAM,CAAC,MAAM,OAAO,GAAG;IACrB;;;;;;;;;;;;;;OAcG;IACH,IAAI,CAAsB,KAAQ,EAAE,OAAoB;QACtD,iBAAiB,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;QAEjC,IAAI,GAAc,CAAC;QACnB,IAAI,CAAS,CAAC;QACd,IAAI,CAAS,CAAC;QAEd,IAAI,QAAQ,IAAI,OAAO,EAAE,CAAC;YACxB,GAAG,GAAG,OAA+B,CAAC;YACtC,CAAC,GAAG,CAAC,CAAC;YACN,CAAC,GAAG,CAAC,CAAC;QACR,CAAC;aAAM,CAAC;YACN,GAAG,GAAG,OAAO,CAAC,GAA2B,CAAC;YAC1C,CAAC,GAAG,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC;YACnB,CAAC,GAAG,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC;QACrB,CAAC;QAED,cAAc;QACd,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QAClB,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QAElB,MAAM,CAAC,GAAG,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC;QAC7B,MAAM,CAAC,GAAG,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC;QAE9B,GAAG,CAAC,IAAI,CAAC,UAAU,EAAE,EAAE,EAAE,EAAE,GAAG;YAC5B,MAAM,KAAK,GAAG,CAAC,GAAG,EAAE,CAAC;YACrB,MAAM,KAAK,GAAG,CAAC,GAAG,EAAE,CAAC;YAErB,IAAI,KAAK,IAAI,CAAC,IAAI,KAAK,IAAI,CAAC,IAAI,KAAK,GAAG,CAAC,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;gBACvD,MAAM,MAAM,GAAG,KAAK,CAAC,aAAa,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;gBACjD,MAAM,EAAE,IAAI,EAAE,GAAG,GAAG,CAAC,MAAM,CAAC;gBAC5B,MAAM,GAAG,GAAG,CAAC,IAAI,CAAC,GAAG,GAAG,CAAC,CAAE,GAAG,IAAI,CAAC,GAAG,GAAG,CAAC,CAAE,GAAG,IAAI,CAAC,GAAG,GAAG,CAAC,CAAE,CAAC,GAAG,CAAC,CAAC;gBAEnE,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,IAAI,GAAG,GAAG,GAAG,CAAC;YAC7C,CAAC;QACH,CAAC,CAAC,CAAC;QAEH,OAAO,KAAK,CAAC;IACf,CAAC;CACF,CAAC"}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"type": "module"
|
||||
}
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
import shared from "@jimp/config-eslint/base.js";
|
||||
export default [...shared];
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
{
|
||||
"name": "@jimp/plugin-mask",
|
||||
"version": "1.6.0",
|
||||
"repository": "jimp-dev/jimp",
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
},
|
||||
"scripts": {
|
||||
"lint": "eslint .",
|
||||
"test": "vitest",
|
||||
"test:browser": "vitest --config vitest.config.browser.mjs",
|
||||
"build": "tshy",
|
||||
"dev": "tshy --watch",
|
||||
"clean": "rm -rf node_modules .tshy .tshy-build dist .turbo"
|
||||
},
|
||||
"author": "Andrew Lisowski <lisowski54@gmail.com>",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@jimp/types": "1.6.0",
|
||||
"zod": "^3.23.8"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@jimp/config-eslint": "1.6.0",
|
||||
"@jimp/config-typescript": "1.6.0",
|
||||
"@jimp/config-vitest": "1.6.0",
|
||||
"@jimp/core": "1.6.0",
|
||||
"@jimp/test-utils": "1.6.0",
|
||||
"@vitest/browser": "^2.0.5",
|
||||
"eslint": "^9.9.1",
|
||||
"tshy": "^3.0.2",
|
||||
"typescript": "^5.5.4",
|
||||
"vite-plugin-node-polyfills": "^0.22.0",
|
||||
"vitest": "^2.0.5"
|
||||
},
|
||||
"tshy": {
|
||||
"exclude": [
|
||||
"**/*.test.ts"
|
||||
],
|
||||
"exports": {
|
||||
"./package.json": "./package.json",
|
||||
".": "./src/index.ts"
|
||||
}
|
||||
},
|
||||
"exports": {
|
||||
"./package.json": "./package.json",
|
||||
".": {
|
||||
"import": {
|
||||
"types": "./dist/esm/index.d.ts",
|
||||
"default": "./dist/esm/index.js"
|
||||
},
|
||||
"require": {
|
||||
"types": "./dist/commonjs/index.d.ts",
|
||||
"default": "./dist/commonjs/index.js"
|
||||
}
|
||||
}
|
||||
},
|
||||
"main": "./dist/commonjs/index.js",
|
||||
"types": "./dist/commonjs/index.d.ts",
|
||||
"type": "module",
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
},
|
||||
"sideEffects": false,
|
||||
"module": "./dist/esm/index.js",
|
||||
"gitHead": "c88abe6046dccbdb6e4f5f00c3dd403c81d83515"
|
||||
}
|
||||
+125
@@ -0,0 +1,125 @@
|
||||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
|
||||
|
||||
exports[`Mask > Affect alpha image with a bigger gray mask 1`] = `
|
||||
Visualization:
|
||||
|
||||
▴812
|
||||
66▾▿
|
||||
66▾▿
|
||||
|
||||
Data:
|
||||
|
||||
FF-00-00⁰⁰ FF-00-00²¹ 00-00-FF⁸⁸ 00-00-FF⁴³
|
||||
FF-00-00⁸⁸ FF-00-00⁶⁵ 00-00-FFᶠᶠ 00-00-FF⁷ᶠ
|
||||
FF-00-00⁸⁸ FF-00-00⁶⁵ 00-00-FFᶠᶠ 00-00-FF⁷ᶠ
|
||||
`;
|
||||
|
||||
exports[`Mask > Affect alpha image with a bigger gray mask, blited 1`] = `
|
||||
Visualization:
|
||||
|
||||
4▵▾1
|
||||
4▵▾1
|
||||
8713
|
||||
|
||||
Data:
|
||||
|
||||
FF-00-00ᶜᶜ FF-00-00⁷ᶠ 00-00-FFᶠᶠ 00-00-FF⁶⁵
|
||||
FF-00-00ᶜᶜ FF-00-00⁷ᶠ 00-00-FFᶠᶠ 00-00-FF⁶⁵
|
||||
FF-00-00⁴⁴ FF-00-00⁴³ 00-00-FF⁸⁸ 00-00-FF²¹
|
||||
`;
|
||||
|
||||
exports[`Mask > Affect opaque image with a colored mask 1`] = `
|
||||
Visualization:
|
||||
|
||||
▴□▾□■□
|
||||
■!▿▰▿□
|
||||
■C5C0□
|
||||
■□■▴■▾
|
||||
|
||||
Data:
|
||||
|
||||
FF-00-00ᶠᶠ FF-FF-FFᶠᶠ 00-00-FFᶠᶠ FF-FF-FFᶠᶠ 00-00-00ᶠᶠ FF-FF-FFᶠᶠ
|
||||
00-00-00ᶠᶠ FF-00-00⁵⁵ 00-00-00⁵⁵ 00-00-FF⁵⁵ 00-00-00⁵⁵ FF-FF-FFᶠᶠ
|
||||
00-00-00ᶠᶠ FF-FF-FFᵃᵃ FF-00-00ᵃᵃ FF-FF-FFᵃᵃ 00-00-FFᵃᵃ FF-FF-FFᶠᶠ
|
||||
00-00-00ᶠᶠ FF-FF-FFᶠᶠ 00-00-00ᶠᶠ FF-00-00ᶠᶠ 00-00-00ᶠᶠ 00-00-FFᶠᶠ
|
||||
`;
|
||||
|
||||
exports[`Mask > Affect opaque image with a gray mask with the same size 1`] = `
|
||||
Visualization:
|
||||
|
||||
▴F1DF0
|
||||
D4■▾▥D
|
||||
D▥▴□□D
|
||||
FD6F5
|
||||
|
||||
Data:
|
||||
|
||||
FF-00-00⁰⁰ FF-FF-FF⁴⁴ 00-00-FF⁸⁸ FF-FF-FF⁸⁸ 00-00-00⁴⁴ FF-FF-FF⁰⁰
|
||||
00-00-00⁸⁸ FF-00-00ᶜᶜ 00-00-00ᶠᶠ 00-00-FFᶠᶠ 00-00-00ᶜᶜ FF-FF-FF⁸⁸
|
||||
00-00-00⁸⁸ FF-FF-FFᶜᶜ FF-00-00ᶠᶠ FF-FF-FFᶠᶠ 00-00-FFᶜᶜ FF-FF-FF⁸⁸
|
||||
00-00-00⁰⁰ FF-FF-FF⁴⁴ 00-00-00⁸⁸ FF-00-00⁸⁸ 00-00-00⁴⁴ 00-00-FF⁰⁰
|
||||
`;
|
||||
|
||||
exports[`Mask > Affect opaque image with a gray mask with the same size, blited 1`] = `
|
||||
Visualization:
|
||||
|
||||
▴□▾□■□
|
||||
■▴F1DF
|
||||
■D4□▾▥
|
||||
■D▥▴■□
|
||||
|
||||
Data:
|
||||
|
||||
FF-00-00ᶠᶠ FF-FF-FFᶠᶠ 00-00-FFᶠᶠ FF-FF-FFᶠᶠ 00-00-00ᶠᶠ FF-FF-FFᶠᶠ
|
||||
00-00-00ᶠᶠ FF-00-00⁰⁰ 00-00-00⁴⁴ 00-00-FF⁸⁸ 00-00-00⁸⁸ FF-FF-FF⁴⁴
|
||||
00-00-00ᶠᶠ FF-FF-FF⁸⁸ FF-00-00ᶜᶜ FF-FF-FFᶠᶠ 00-00-FFᶠᶠ FF-FF-FFᶜᶜ
|
||||
00-00-00ᶠᶠ FF-FF-FF⁸⁸ 00-00-00ᶜᶜ FF-00-00ᶠᶠ 00-00-00ᶠᶠ 00-00-FFᶜᶜ
|
||||
`;
|
||||
|
||||
exports[`Mask > Affect opaque image with a gray mask with the same size, blited negative 1`] = `
|
||||
Visualization:
|
||||
|
||||
4□▾▥D□
|
||||
▥▴■□D□
|
||||
FD6F5□
|
||||
■□■▴■▾
|
||||
|
||||
Data:
|
||||
|
||||
FF-00-00ᶜᶜ FF-FF-FFᶠᶠ 00-00-FFᶠᶠ FF-FF-FFᶜᶜ 00-00-00⁸⁸ FF-FF-FFᶠᶠ
|
||||
00-00-00ᶜᶜ FF-00-00ᶠᶠ 00-00-00ᶠᶠ 00-00-FFᶜᶜ 00-00-00⁸⁸ FF-FF-FFᶠᶠ
|
||||
00-00-00⁴⁴ FF-FF-FF⁸⁸ FF-00-00⁸⁸ FF-FF-FF⁴⁴ 00-00-FF⁰⁰ FF-FF-FFᶠᶠ
|
||||
00-00-00ᶠᶠ FF-FF-FFᶠᶠ 00-00-00ᶠᶠ FF-00-00ᶠᶠ 00-00-00ᶠᶠ 00-00-FFᶠᶠ
|
||||
`;
|
||||
|
||||
exports[`Mask > Affect opaque image with a smaller gray mask 1`] = `
|
||||
Visualization:
|
||||
|
||||
▴◆2▹■□
|
||||
◆7▹□■□
|
||||
E▹4□▾□
|
||||
■□■▴■▾
|
||||
|
||||
Data:
|
||||
|
||||
FF-00-00⁰⁰ FF-FF-FF³³ 00-00-FF⁶⁶ FF-FF-FF⁹⁹ 00-00-00ᶠᶠ FF-FF-FFᶠᶠ
|
||||
00-00-00³³ FF-00-00⁶⁶ 00-00-00⁹⁹ 00-00-FFᶜᶜ 00-00-00ᶠᶠ FF-FF-FFᶠᶠ
|
||||
00-00-00⁶⁶ FF-FF-FF⁹⁹ FF-00-00ᶜᶜ FF-FF-FFᶠᶠ 00-00-FFᶠᶠ FF-FF-FFᶠᶠ
|
||||
00-00-00ᶠᶠ FF-FF-FFᶠᶠ 00-00-00ᶠᶠ FF-00-00ᶠᶠ 00-00-00ᶠᶠ 00-00-FFᶠᶠ
|
||||
`;
|
||||
|
||||
exports[`Mask > Affect opaque image with a smaller gray mask, blited 1`] = `
|
||||
Visualization:
|
||||
|
||||
▴□▾□■□
|
||||
■▴◆2▹□
|
||||
■◆7▹□□
|
||||
■E▹4■▾
|
||||
|
||||
Data:
|
||||
|
||||
FF-00-00ᶠᶠ FF-FF-FFᶠᶠ 00-00-FFᶠᶠ FF-FF-FFᶠᶠ 00-00-00ᶠᶠ FF-FF-FFᶠᶠ
|
||||
00-00-00ᶠᶠ FF-00-00⁰⁰ 00-00-00³³ 00-00-FF⁶⁶ 00-00-00⁹⁹ FF-FF-FFᶠᶠ
|
||||
00-00-00ᶠᶠ FF-FF-FF³³ FF-00-00⁶⁶ FF-FF-FF⁹⁹ 00-00-FFᶜᶜ FF-FF-FFᶠᶠ
|
||||
00-00-00ᶠᶠ FF-FF-FF⁶⁶ 00-00-00⁹⁹ FF-00-00ᶜᶜ 00-00-00ᶠᶠ 00-00-FFᶠᶠ
|
||||
`;
|
||||
+95
@@ -0,0 +1,95 @@
|
||||
import { expect, test, describe } from "vitest";
|
||||
|
||||
import { makeTestImage } from "@jimp/test-utils";
|
||||
import { createJimp } from "@jimp/core";
|
||||
|
||||
import { methods } from "./index.js";
|
||||
|
||||
const jimp = createJimp({ plugins: [methods] });
|
||||
|
||||
describe("Mask", () => {
|
||||
const imgSrcOpaq = jimp.fromBitmap(
|
||||
// prettier-ignore
|
||||
makeTestImage(
|
||||
"▴□▾□■□",
|
||||
"■▴■▾■□",
|
||||
"■□▴□▾□",
|
||||
"■□■▴■▾"
|
||||
),
|
||||
);
|
||||
const imgSrcAlpa = jimp.fromBitmap(
|
||||
// prettier-ignore
|
||||
makeTestImage(
|
||||
"▴▵▾▿",
|
||||
"▴▵▾▿",
|
||||
"▴▵▾▿"
|
||||
),
|
||||
);
|
||||
const maskGrayBig = jimp.fromBitmap(
|
||||
// prettier-ignore
|
||||
makeTestImage(
|
||||
"048840",
|
||||
"8CFFC8",
|
||||
"8CFFC8",
|
||||
"048840"
|
||||
),
|
||||
);
|
||||
const maskGraySmall = jimp.fromBitmap(
|
||||
// prettier-ignore
|
||||
makeTestImage(
|
||||
"0369",
|
||||
"369C",
|
||||
"69CF"
|
||||
),
|
||||
);
|
||||
const maskColor = jimp.fromBitmap(
|
||||
// prettier-ignore
|
||||
makeTestImage(
|
||||
"▴▴▾▾",
|
||||
"▪▪▰▰",
|
||||
"□□□□"
|
||||
),
|
||||
);
|
||||
|
||||
test("Affect opaque image with a gray mask with the same size", () => {
|
||||
expect(imgSrcOpaq.clone().mask(maskGrayBig)).toMatchSnapshot();
|
||||
});
|
||||
|
||||
test("Affect opaque image with a gray mask with the same size, blited", () => {
|
||||
expect(
|
||||
imgSrcOpaq.clone().mask({ src: maskGrayBig, x: 1, y: 1 }),
|
||||
).toMatchSnapshot();
|
||||
});
|
||||
|
||||
test("Affect opaque image with a gray mask with the same size, blited negative", () => {
|
||||
expect(
|
||||
imgSrcOpaq.clone().mask({ src: maskGrayBig, x: -1, y: -1 }),
|
||||
).toMatchSnapshot();
|
||||
});
|
||||
|
||||
test("Affect opaque image with a smaller gray mask", () => {
|
||||
expect(imgSrcOpaq.clone().mask(maskGraySmall)).toMatchSnapshot();
|
||||
});
|
||||
|
||||
test("Affect opaque image with a smaller gray mask, blited", () => {
|
||||
expect(
|
||||
imgSrcOpaq.clone().mask({ src: maskGraySmall, x: 1, y: 1 }),
|
||||
).toMatchSnapshot();
|
||||
});
|
||||
|
||||
test("Affect alpha image with a bigger gray mask", () => {
|
||||
expect(imgSrcAlpa.clone().mask(maskGrayBig)).toMatchSnapshot();
|
||||
});
|
||||
|
||||
test("Affect alpha image with a bigger gray mask, blited", () => {
|
||||
expect(
|
||||
imgSrcAlpa.clone().mask({ src: maskGrayBig, x: -1, y: -1 }),
|
||||
).toMatchSnapshot();
|
||||
});
|
||||
|
||||
test("Affect opaque image with a colored mask", () => {
|
||||
expect(
|
||||
imgSrcOpaq.clone().mask({ src: maskColor, x: 1, y: 1 }),
|
||||
).toMatchSnapshot();
|
||||
});
|
||||
});
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
import { JimpClass, JimpClassSchema } from "@jimp/types";
|
||||
import { z } from "zod";
|
||||
|
||||
const MaskOptionsObjectSchema = z.object({
|
||||
src: JimpClassSchema,
|
||||
/** the x position to draw the image */
|
||||
x: z.number().optional(),
|
||||
/** the y position to draw the image */
|
||||
y: z.number().optional(),
|
||||
});
|
||||
|
||||
const MaskOptionsSchema = z.union([JimpClassSchema, MaskOptionsObjectSchema]);
|
||||
|
||||
export type MaskOptions = z.infer<typeof MaskOptionsSchema>;
|
||||
|
||||
export const methods = {
|
||||
/**
|
||||
* 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 the image
|
||||
* @example
|
||||
* ```ts
|
||||
* import { Jimp } from "jimp";
|
||||
*
|
||||
* const image = await Jimp.read("test/image.png");
|
||||
* const mask = await Jimp.read("test/mask.png");
|
||||
*
|
||||
* image.mask(mask);
|
||||
* ```
|
||||
*/
|
||||
mask<I extends JimpClass>(image: I, options: MaskOptions) {
|
||||
MaskOptionsSchema.parse(options);
|
||||
|
||||
let src: JimpClass;
|
||||
let x: number;
|
||||
let y: number;
|
||||
|
||||
if ("bitmap" in options) {
|
||||
src = options as unknown as JimpClass;
|
||||
x = 0;
|
||||
y = 0;
|
||||
} else {
|
||||
src = options.src as unknown as JimpClass;
|
||||
x = options.x ?? 0;
|
||||
y = options.y ?? 0;
|
||||
}
|
||||
|
||||
// round input
|
||||
x = Math.round(x);
|
||||
y = Math.round(y);
|
||||
|
||||
const w = image.bitmap.width;
|
||||
const h = image.bitmap.height;
|
||||
|
||||
src.scan(function (sx, sy, idx) {
|
||||
const destX = x + sx;
|
||||
const destY = y + sy;
|
||||
|
||||
if (destX >= 0 && destY >= 0 && destX < w && destY < h) {
|
||||
const dstIdx = image.getPixelIndex(destX, destY);
|
||||
const { data } = src.bitmap;
|
||||
const avg = (data[idx + 0]! + data[idx + 1]! + data[idx + 2]!) / 3;
|
||||
|
||||
image.bitmap.data[dstIdx + 3] *= avg / 255;
|
||||
}
|
||||
});
|
||||
|
||||
return image;
|
||||
},
|
||||
};
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"extends": "@jimp/config-typescript/base.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "dist"
|
||||
}
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
import config from "@jimp/config-vitest/browser";
|
||||
|
||||
export default config;
|
||||
Reference in new issue
Block a user