"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; 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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { dividePixelScale: () => dividePixelScale, getPixelScale: () => getPixelScale, multiplyPixelScale: () => multiplyPixelScale, scalePixels: () => scalePixels }); module.exports = __toCommonJS(src_exports); // src/helpers/commonDivisors.ts function findGCD(a, b) { let x = Math.min(a, b); let y = Math.max(a, b); let r; while (x % y > 0) { r = x % y; x = y; y = r; } return y; } function findCommonDivisors(a, b) { const hasOdd = a % 2 !== 0 || b % 2 !== 0; const increment = hasOdd ? 2 : 1; const divisors = []; const gcd = findGCD(a, b); for (let num = 1; num * 2 <= gcd; num += increment) { if (gcd % num === 0) { divisors.push(num); } } divisors.push(gcd); return divisors; } // src/getPixelScale.ts function isMatchingColor(rgbaA, rgbaB, maxColorDiff) { return rgbaA.every((rgba, i) => { const colorDiff = Math.abs(rgba - rgbaB[i]); return colorDiff <= maxColorDiff; }); } function isMatchingRange(data, length, indexA, indexB, maxColorDiff) { for (let i = 0; i < length; i++) { const colorDiff = Math.abs(data[indexA + i] - data[indexB + i]); if (colorDiff > maxColorDiff) { return false; } } return true; } function getRGBATuple(data, index) { const red = data[index]; const green = data[index + 1]; const blue = data[index + 2]; const alpha = data[index + 3]; return [red, green, blue, alpha]; } function isValidRow(data, scale, rowStart, rowEnd, maxColorDiff) { const scaledPixel = scale * 4; for (let scaledStart = rowStart; scaledStart < rowEnd; scaledStart += scaledPixel) { const scaledEnd = scaledStart + scaledPixel; const expectedRGBA = getRGBATuple(data, scaledStart); for (let pixelStart = scaledStart + 4; pixelStart < scaledEnd; pixelStart += 4) { const rgba = getRGBATuple(data, pixelStart); if (!isMatchingColor(expectedRGBA, rgba, maxColorDiff)) { return false; } } } return true; } function isValidScale(imageData, scale, maxColorDiff) { const { data, width } = imageData; const dataLength = data.length; const rowLength = width * 4; const scaledRowLength = rowLength * scale; for (let scaledRowStart = 0; scaledRowStart < dataLength; scaledRowStart += scaledRowLength) { const firstRowStart = scaledRowStart; const firstRowEnd = firstRowStart + rowLength; const validRow = isValidRow(data, scale, firstRowStart, firstRowEnd, maxColorDiff); if (!validRow) { return false; } for (let rowCount = 1; rowCount < scale; rowCount++) { const rowStart = firstRowStart + rowCount * rowLength; const matchinRow = isMatchingRange(data, rowLength, firstRowStart, rowStart, maxColorDiff); if (!matchinRow) { return false; } } } return true; } function getPixelScale(imageData, options) { const { maxColorDiff = 0 } = options || {}; const { width, height } = imageData; const possibleScales = findCommonDivisors(width, height); if (findCommonDivisors.length === 1) { return 1; } possibleScales.shift(); for (let scaleIndex = possibleScales.length - 1; scaleIndex >= 0; scaleIndex--) { const scale = possibleScales[scaleIndex]; const validScale = isValidScale(imageData, scale, maxColorDiff); if (validScale) { return scale; } } return 1; } // src/scalePixels.ts function scalePixels(imageData, to, options) { const { from, maxColorDiff } = options || {}; const { data, width, height } = imageData; const currentScale = from || getPixelScale(imageData, { maxColorDiff: maxColorDiff || 0 }); const dataLength = data.length; const rowLength = width * 4; const scaledRowLength = currentScale * rowLength; const newScale = to; const newWidth = width / currentScale * newScale; const newHeight = height / currentScale * newScale; const newData = []; for (let rowStart = 0; rowStart < dataLength; rowStart += scaledRowLength) { const rowEnd = rowStart + rowLength; for (let rowCount = 0; rowCount < newScale; rowCount++) { for (let pStart = rowStart; pStart < rowEnd; pStart += currentScale * 4) { for (let colCount = 0; colCount < newScale; colCount++) { newData.push(data[pStart], data[pStart + 1], data[pStart + 2], data[pStart + 3]); } } } } return new ImageData(Uint8ClampedArray.from(newData), newWidth, newHeight); } function multiplyPixelScale(imageData, multiplier, options) { const scale = getPixelScale(imageData, options); return scalePixels(imageData, scale * multiplier, options); } function dividePixelScale(imageData, divider, options) { const scale = getPixelScale(imageData, options); return scalePixels(imageData, scale / divider, options); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { dividePixelScale, getPixelScale, multiplyPixelScale, scalePixels });