Files
CyberChef/src/core/operations/ResizeImage.mjs
T

153 lines
4.2 KiB
JavaScript
Raw Normal View History

2019-02-19 16:19:34 +00:00
/**
* @author j433866 [j433866@gmail.com]
* @copyright Crown Copyright 2019
* @license Apache-2.0
*/
import Operation from "../Operation.mjs";
import OperationError from "../errors/OperationError.mjs";
import { isImage } from "../lib/FileType.mjs";
2019-02-19 16:19:34 +00:00
import { toBase64 } from "../lib/Base64.mjs";
import { isWorkerEnvironment } from "../Utils.mjs";
2026-02-18 11:34:31 +00:00
import { Jimp, JimpMime, ResizeStrategy } from "jimp";
2019-02-19 16:19:34 +00:00
/**
* Resize Image operation
*/
class ResizeImage extends Operation {
/**
* ResizeImage constructor
*/
constructor() {
super();
this.name = "Resize Image";
this.module = "Image";
2026-02-18 11:34:31 +00:00
this.description =
"Resizes an image to the specified width and height values.";
2019-03-04 13:48:13 +00:00
this.infoURL = "https://wikipedia.org/wiki/Image_scaling";
this.inputType = "ArrayBuffer";
this.outputType = "ArrayBuffer";
2019-02-19 16:19:34 +00:00
this.presentType = "html";
this.args = [
{
name: "Width",
type: "number",
2019-03-04 11:52:54 +00:00
value: 100,
2026-02-18 11:34:31 +00:00
min: 1,
2019-02-19 16:19:34 +00:00
},
{
name: "Height",
type: "number",
2019-03-04 11:52:54 +00:00
value: 100,
2026-02-18 11:34:31 +00:00
min: 1,
2019-02-19 16:19:34 +00:00
},
{
name: "Unit type",
type: "option",
2026-02-18 11:34:31 +00:00
value: ["Pixels", "Percent"],
2019-02-19 16:19:34 +00:00
},
{
name: "Maintain aspect ratio",
type: "boolean",
2026-02-18 11:34:31 +00:00
value: false,
2019-02-20 15:35:53 +00:00
},
{
name: "Resizing algorithm",
type: "option",
value: [
"Nearest Neighbour",
"Bilinear",
"Bicubic",
"Hermite",
2026-02-18 11:34:31 +00:00
"Bezier",
2019-02-20 15:35:53 +00:00
],
2026-02-18 11:34:31 +00:00
defaultIndex: 1,
},
2019-02-19 16:19:34 +00:00
];
}
/**
* @param {ArrayBuffer} input
2019-02-19 16:19:34 +00:00
* @param {Object[]} args
* @returns {byteArray}
*/
2019-02-20 11:26:39 +00:00
async run(input, args) {
2019-02-19 16:19:34 +00:00
let width = args[0],
height = args[1];
const unit = args[2],
aspect = args[3],
2019-03-09 07:23:11 +00:00
resizeAlg = args[4];
2019-02-19 16:19:34 +00:00
2019-02-20 15:35:53 +00:00
const resizeMap = {
2026-02-18 11:34:31 +00:00
"Nearest Neighbour": ResizeStrategy.NEAREST_NEIGHBOR,
Bilinear: ResizeStrategy.BILINEAR,
Bicubic: ResizeStrategy.BICUBIC,
Hermite: ResizeStrategy.HERMITE,
Bezier: ResizeStrategy.BEZIER,
2019-02-20 15:35:53 +00:00
};
2019-03-05 11:49:25 +00:00
if (!isImage(input)) {
2019-02-19 16:19:34 +00:00
throw new OperationError("Invalid file type.");
}
2019-03-05 11:49:25 +00:00
2019-03-07 11:19:04 +00:00
let image;
try {
image = await Jimp.read(input);
2019-03-07 11:19:04 +00:00
} catch (err) {
throw new OperationError(`Error loading image. (${err})`);
2019-02-20 11:26:39 +00:00
}
2019-03-07 11:19:04 +00:00
try {
if (unit === "Percent") {
2026-02-18 11:34:31 +00:00
width = image.width * (width / 100);
height = image.height * (height / 100);
2019-03-07 11:19:04 +00:00
}
2019-03-06 10:32:58 +00:00
if (isWorkerEnvironment())
2019-03-07 11:19:04 +00:00
self.sendStatusMessage("Resizing image...");
if (aspect) {
2026-02-18 11:34:31 +00:00
image.scaleToFit({
w: width,
h: height,
mode: resizeMap[resizeAlg],
});
2019-03-07 11:19:04 +00:00
} else {
2026-02-18 11:34:31 +00:00
image.resize({
w: width,
h: height,
mode: resizeMap[resizeAlg],
});
2019-03-07 11:19:04 +00:00
}
2019-02-20 11:26:39 +00:00
let imageBuffer;
2026-02-18 11:34:31 +00:00
if (image.mime === "image/gif") {
imageBuffer = await image.getBuffer(JimpMime.png);
} else {
2026-02-18 11:34:31 +00:00
imageBuffer = await image.getBuffer(image.mime);
}
return imageBuffer.buffer;
2019-03-07 11:19:04 +00:00
} catch (err) {
throw new OperationError(`Error resizing image. (${err})`);
}
2019-02-19 16:19:34 +00:00
}
/**
* Displays the resized image using HTML for web apps
* @param {ArrayBuffer} data
2019-02-19 16:19:34 +00:00
* @returns {html}
*/
present(data) {
if (!data.byteLength) return "";
const dataArray = new Uint8Array(data);
2019-02-19 16:19:34 +00:00
const type = isImage(dataArray);
2019-03-09 07:23:11 +00:00
if (!type) {
throw new OperationError("Invalid file type.");
2019-02-19 16:19:34 +00:00
}
return `<img src="data:${type};base64,${toBase64(dataArray)}">`;
2019-02-19 16:19:34 +00:00
}
}
export default ResizeImage;