image-js
Version:
Image processing and manipulation in JavaScript
50 lines • 2.34 kB
JavaScript
import { Image } from '../Image.js';
import { getDefaultColor } from '../utils/getDefaultColor.js';
import { getOutputImage, maskToOutputMask } from '../utils/getOutputImage.js';
import { setBlendedVisiblePixel } from '../utils/setBlendedVisiblePixel.js';
import checkProcessable from '../utils/validators/checkProcessable.js';
import { validateColor } from "../utils/validators/validators.js";
/**
* Draw a rectangle defined by position of the top-left corner, width and height.
* @param image - Image to process.
* @param options - Draw rectangle options.
* @returns The image with the rectangle drawing.
*/
export function drawRectangle(image, options = {}) {
const { width: rectangleWidth = image.width, height: rectangleHeight = image.height, origin = { column: 0, row: 0 }, strokeColor = getDefaultColor(image), fillColor, } = options;
const width = Math.round(rectangleWidth);
const height = Math.round(rectangleHeight);
const column = Math.round(origin.column);
const row = Math.round(origin.row);
let newImage;
if (image instanceof Image) {
checkProcessable(image, {
bitDepth: [8, 16],
});
validateColor(strokeColor, image);
if (fillColor) {
validateColor(fillColor, image);
}
newImage = getOutputImage(image, options, { clone: true });
}
else {
newImage = maskToOutputMask(image, options, { clone: true });
}
for (let currentColumn = column; currentColumn < column + width; currentColumn++) {
setBlendedVisiblePixel(newImage, currentColumn, row, strokeColor);
setBlendedVisiblePixel(newImage, currentColumn, row + height - 1, strokeColor);
}
for (let currentRow = row + 1; currentRow < row + height - 1; currentRow++) {
setBlendedVisiblePixel(newImage, column, currentRow, strokeColor);
setBlendedVisiblePixel(newImage, column + width - 1, currentRow, strokeColor);
}
if (fillColor) {
for (let currentRow = row + 1; currentRow < row + height - 1; currentRow++) {
for (let currentColumn = column + 1; currentColumn < column + width - 1; currentColumn++) {
setBlendedVisiblePixel(newImage, currentColumn, currentRow, fillColor);
}
}
}
return newImage;
}
//# sourceMappingURL=drawRectangle.js.map