"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { default: () => src_default, getTrimEdges: () => getTrimEdges, trimImageData: () => trimImageData }); module.exports = __toCommonJS(src_exports); var import_crop_image_data = __toESM(require("crop-image-data"), 1); function getEmptyImageData() { return new ImageData(1, 1); } function getRgba(data, i) { return [data[i], data[i + 1], data[i + 2], data[i + 3]]; } function getTrimColorFunc(option) { if (typeof option === "function") { return option; } if (Array.isArray(option)) { return ([r, g, b, a]) => { return r === option[0] && g === option[1] && b === option[2] && a === option[3]; }; } return (rgba) => rgba[3] === 0; } function scanSide(imageData, side, trimColor) { const { data, width, height } = imageData; const horizontal = side === "left" || side === "right"; const reverse = side === "right" || side === "bottom"; const primaryAxis = horizontal ? width : height; const secondaryAxis = horizontal ? height : width; const step = reverse ? -1 : 1; const start = reverse ? primaryAxis - 1 : 0; for (let p = start; reverse ? p > -1 : p < primaryAxis; p += step) { for (let s = 0; s < secondaryAxis; s++) { const index = (horizontal ? width * s + p : width * p + s) * 4; const rgba = getRgba(data, index); if (!trimColor(rgba)) { return reverse ? start - p : p; } } } return null; } function getTrimEdges(imageData, trimOptions) { const trimColor = getTrimColorFunc(trimOptions == null ? void 0 : trimOptions.trimColor); const trimEdges = {}; const sides = ["top", "bottom", "left", "right"]; for (let i = 0; i < sides.length; i++) { const side = sides[i]; const crop = scanSide(imageData, side, trimColor); if (crop === null) { return null; } trimEdges[side] = crop; } return trimEdges; } function trimImageData(imageData, trimOptions) { const trimEdges = getTrimEdges(imageData, trimOptions); if (trimEdges === null) { return getEmptyImageData(); } return (0, import_crop_image_data.default)(imageData, trimEdges); } var src_default = trimImageData; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { getTrimEdges, trimImageData });