Files

39 lines
1.1 KiB
JavaScript

import assert from "node:assert/strict";
import { readFile } from "node:fs/promises";
const source = await readFile(new URL("../js/mce_mask_source.js", import.meta.url), "utf8");
const moduleUrl = `data:text/javascript;base64,${Buffer.from(source).toString("base64")}`;
const { computeMaskBBox, maskChannelFromNode } = await import(moduleUrl);
const pixels = new Uint8ClampedArray(6 * 5 * 4).fill(255);
for (let y = 1; y <= 3; y += 1) {
for (let x = 2; x <= 4; x += 1) {
pixels[(y * 6 + x) * 4 + 3] = 0;
}
}
assert.deepEqual(computeMaskBBox(pixels, 6, 5, "alpha"), {
x: 2,
y: 1,
width: 3,
height: 3,
empty: false,
});
const redPixels = new Uint8ClampedArray(4 * 3 * 4);
redPixels[(2 * 4 + 1) * 4] = 255;
assert.deepEqual(computeMaskBBox(redPixels, 4, 3, "red"), {
x: 1,
y: 2,
width: 1,
height: 1,
empty: false,
});
assert.equal(maskChannelFromNode({
comfyClass: "LoadImageMask",
widgets: [{ name: "channel", value: "blue" }],
}), "blue");
assert.equal(maskChannelFromNode({ comfyClass: "LoadImage" }), "alpha");
console.log("connected mask bbox extraction: OK");