CyberChef/src/core/operations/SplitColourChannels.mjs

104 lines
3.3 KiB
JavaScript
Raw Normal View History

2018-12-21 16:57:09 +01:00
/**
* @author Matt C [matt@artemisbot.uk]
* @copyright Crown Copyright 2018
* @license Apache-2.0
*/
import Operation from "../Operation.mjs";
import OperationError from "../errors/OperationError.mjs";
import Utils from "../Utils.mjs";
import {isImage} from "../lib/FileType.mjs";
2020-12-14 18:51:12 +01:00
import jimplib from "jimp/es/index.js";
const jimp = jimplib.default ? jimplib.default : jimplib;
2018-12-21 16:57:09 +01:00
/**
* Split Colour Channels operation
*/
class SplitColourChannels extends Operation {
/**
* SplitColourChannels constructor
*/
constructor() {
super();
this.name = "Split Colour Channels";
this.module = "Image";
this.description = "Splits the given image into its red, green and blue colour channels.";
this.infoURL = "https://wikipedia.org/wiki/Channel_(digital_image)";
this.inputType = "ArrayBuffer";
2018-12-21 16:57:09 +01:00
this.outputType = "List<File>";
this.presentType = "html";
this.args = [];
2018-12-21 16:57:09 +01:00
}
/**
* @param {ArrayBuffer} input
2018-12-21 16:57:09 +01:00
* @param {Object[]} args
* @returns {List<File>}
*/
async run(input, args) {
input = new Uint8Array(input);
2018-12-21 16:57:09 +01:00
// Make sure that the input is an image
2018-12-26 19:40:27 +01:00
if (!isImage(input)) throw new OperationError("Invalid file type.");
2018-12-26 19:40:27 +01:00
const parsedImage = await jimp.read(Buffer.from(input));
2018-12-26 19:40:27 +01:00
const red = new Promise(async (resolve, reject) => {
try {
const split = parsedImage
.clone()
.color([
{apply: "blue", params: [-255]},
{apply: "green", params: [-255]}
])
.getBufferAsync(jimp.MIME_PNG);
resolve(new File([new Uint8Array((await split).values())], "red.png", {type: "image/png"}));
} catch (err) {
reject(new OperationError(`Could not split red channel: ${err}`));
}
});
2018-12-26 19:40:27 +01:00
const green = new Promise(async (resolve, reject) => {
try {
const split = parsedImage.clone()
.color([
{apply: "red", params: [-255]},
{apply: "blue", params: [-255]},
]).getBufferAsync(jimp.MIME_PNG);
resolve(new File([new Uint8Array((await split).values())], "green.png", {type: "image/png"}));
} catch (err) {
reject(new OperationError(`Could not split green channel: ${err}`));
}
});
2018-12-26 19:40:27 +01:00
const blue = new Promise(async (resolve, reject) => {
try {
const split = parsedImage
.color([
{apply: "red", params: [-255]},
{apply: "green", params: [-255]},
]).getBufferAsync(jimp.MIME_PNG);
resolve(new File([new Uint8Array((await split).values())], "blue.png", {type: "image/png"}));
} catch (err) {
reject(new OperationError(`Could not split blue channel: ${err}`));
}
});
return await Promise.all([red, green, blue]);
2018-12-21 16:57:09 +01:00
}
/**
* Displays the files in HTML for web apps.
*
* @param {File[]} files
* @returns {html}
*/
async present(files) {
return await Utils.displayFilesAsHTML(files);
}
}
export default SplitColourChannels;