Innovenergy_trunk/frontend/node_modules/testcafe/lib/screenshots/crop.js

106 lines
14 KiB
JavaScript
Raw Normal View History

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.cropScreenshot = exports.calculateClipInfo = exports.getClipInfoByCropDimensions = exports.getClipInfoByMarkPosition = exports.calculateMarkPosition = exports.markSeedToId = void 0;
const utils_1 = require("./utils");
const limit_number_1 = __importDefault(require("../utils/limit-number"));
const promisified_functions_1 = require("../utils/promisified-functions");
const test_run_1 = require("../errors/test-run/");
const constants_1 = require("./constants");
const MARK_SEED_ERROR_THRESHOLD = 10;
const WHITE_COLOR_PART = 255;
const BLACK_COLOR_PART = 0;
function markSeedToId(markSeed) {
let id = 0;
for (let i = 0; i < constants_1.MARK_LENGTH; i++)
id = id * 2 + (markSeed[i * constants_1.MARK_BYTES_PER_PIXEL] ? 1 : 0);
return id;
}
exports.markSeedToId = markSeedToId;
function getCorrectedColorPart(colorPart) {
const isWhite = colorPart > WHITE_COLOR_PART - MARK_SEED_ERROR_THRESHOLD;
const isBlack = colorPart < MARK_SEED_ERROR_THRESHOLD;
if (isBlack)
return BLACK_COLOR_PART;
if (isWhite)
return WHITE_COLOR_PART;
return colorPart;
}
async function validateClipInfo(clipInfo, path) {
const clipWidth = clipInfo.clipRight - clipInfo.clipLeft;
const clipHeight = clipInfo.clipBottom - clipInfo.clipTop;
if (clipWidth <= 0 || clipHeight <= 0) {
await (0, promisified_functions_1.deleteFile)(path);
throw new test_run_1.InvalidElementScreenshotDimensionsError(clipWidth, clipHeight);
}
}
function calculateMarkPosition(pngImage, markSeed) {
const mark = Buffer.from(markSeed);
const filtImg = Buffer.from(pngImage.data);
for (let i = 0; i < filtImg.length; i++)
filtImg[i] = getCorrectedColorPart(filtImg[i]);
const markIndex = filtImg.indexOf(mark);
if (markIndex < 0)
return null;
const endPosition = markIndex / constants_1.MARK_BYTES_PER_PIXEL + constants_1.MARK_LENGTH + constants_1.MARK_RIGHT_MARGIN;
const x = endPosition % pngImage.width || pngImage.width;
const y = (endPosition - x) / pngImage.width + 1;
return { x, y };
}
exports.calculateMarkPosition = calculateMarkPosition;
function getClipInfoByMarkPosition(markPosition, { width, height }) {
const { x, y } = markPosition;
const clipRight = x;
const clipBottom = y;
const clipLeft = clipRight - width;
const clipTop = clipBottom - height;
return {
clipLeft,
clipTop,
clipRight,
clipBottom,
};
}
exports.getClipInfoByMarkPosition = getClipInfoByMarkPosition;
function getClipInfoByCropDimensions({ clipRight, clipLeft, clipBottom, clipTop }, cropDimensions) {
if (cropDimensions) {
const { right, top, bottom, left } = cropDimensions;
clipRight = (0, limit_number_1.default)(clipLeft + right, clipLeft, clipRight);
clipBottom = (0, limit_number_1.default)(clipTop + bottom, clipTop, clipBottom);
clipLeft = (0, limit_number_1.default)(clipLeft + left, clipLeft, clipRight);
clipTop = (0, limit_number_1.default)(clipTop + top, clipTop, clipBottom);
}
return {
clipLeft,
clipTop,
clipRight,
clipBottom,
};
}
exports.getClipInfoByCropDimensions = getClipInfoByCropDimensions;
function calculateClipInfo(pngImage, markSeedPosition, clientAreaDimensions, cropDimensions) {
let clipInfo = {
clipRight: pngImage.width,
clipBottom: pngImage.height,
clipLeft: 0,
clipTop: 0,
};
if (markSeedPosition && clientAreaDimensions)
clipInfo = getClipInfoByMarkPosition(markSeedPosition, clientAreaDimensions);
clipInfo = getClipInfoByCropDimensions(clipInfo, cropDimensions);
if (markSeedPosition && markSeedPosition.y === clipInfo.clipBottom)
clipInfo.clipBottom--;
return clipInfo;
}
exports.calculateClipInfo = calculateClipInfo;
async function cropScreenshot(image, { path, markSeedPosition, clientAreaDimensions, cropDimensions }) {
if (!markSeedPosition && !cropDimensions)
return null;
const clip = calculateClipInfo(image, markSeedPosition, clientAreaDimensions, cropDimensions);
await validateClipInfo(clip, path);
return (0, utils_1.copyImagePart)(image, clip);
}
exports.cropScreenshot = cropScreenshot;
//# sourceMappingURL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