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";
|
|
|
|
import OperationError from "../errors/OperationError";
|
|
|
|
import Utils from "../Utils";
|
|
|
|
import Magic from "../lib/Magic";
|
|
|
|
|
|
|
|
import jimp from "jimp";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Split Colour Channels operation
|
|
|
|
*/
|
|
|
|
class SplitColourChannels extends Operation {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SplitColourChannels constructor
|
|
|
|
*/
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.name = "Split Colour Channels";
|
|
|
|
this.module = "Image";
|
2018-12-26 17:33:10 +01:00
|
|
|
this.description = "Splits the given image into its red, green and blue colour channels.";
|
|
|
|
this.infoURL = "https://wikipedia.org/wiki/Channel_(digital_image)";
|
2018-12-21 16:57:09 +01:00
|
|
|
this.inputType = "byteArray";
|
|
|
|
this.outputType = "List<File>";
|
|
|
|
this.presentType = "html";
|
2018-12-26 17:33:10 +01:00
|
|
|
this.args = [];
|
2018-12-21 16:57:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {byteArray} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {List<File>}
|
|
|
|
*/
|
|
|
|
async run(input, args) {
|
|
|
|
const type = Magic.magicFileType(input);
|
|
|
|
// Make sure that the input is an image
|
|
|
|
if (type && type.mime.indexOf("image") === 0) {
|
|
|
|
const parsedImage = await jimp.read(Buffer.from(input));
|
2018-12-26 17:33:10 +01:00
|
|
|
|
2018-12-21 16:57:09 +01:00
|
|
|
const red = new Promise(async (resolve, reject) => {
|
|
|
|
try {
|
2018-12-26 17:33:10 +01:00
|
|
|
const split = parsedImage
|
|
|
|
.clone()
|
2018-12-21 16:57:09 +01:00
|
|
|
.color([
|
|
|
|
{apply: "blue", params: [-255]},
|
|
|
|
{apply: "green", params: [-255]}
|
2018-12-26 17:33:10 +01:00
|
|
|
])
|
|
|
|
.getBufferAsync(jimp.MIME_PNG);
|
2018-12-21 16:57:09 +01:00
|
|
|
resolve(new File([new Uint8Array((await split).values())], "red.png", {type: "image/png"}));
|
|
|
|
} catch (err) {
|
2018-12-21 18:17:11 +01:00
|
|
|
reject(new OperationError(`Could not split red channel: ${err}`));
|
2018-12-21 16:57:09 +01:00
|
|
|
}
|
|
|
|
});
|
2018-12-26 17:33:10 +01:00
|
|
|
|
2018-12-21 16:57:09 +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) {
|
2018-12-21 18:17:11 +01:00
|
|
|
reject(new OperationError(`Could not split green channel: ${err}`));
|
2018-12-21 16:57:09 +01:00
|
|
|
}
|
|
|
|
});
|
2018-12-26 17:33:10 +01:00
|
|
|
|
2018-12-21 16:57:09 +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) {
|
2018-12-21 18:17:11 +01:00
|
|
|
reject(new OperationError(`Could not split blue channel: ${err}`));
|
2018-12-21 16:57:09 +01:00
|
|
|
}
|
|
|
|
});
|
2018-12-26 17:33:10 +01:00
|
|
|
|
2018-12-21 16:57:09 +01:00
|
|
|
return await Promise.all([red, green, blue]);
|
|
|
|
} else {
|
|
|
|
throw new OperationError("Invalid file type.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Displays the files in HTML for web apps.
|
|
|
|
*
|
|
|
|
* @param {File[]} files
|
|
|
|
* @returns {html}
|
|
|
|
*/
|
|
|
|
async present(files) {
|
|
|
|
return await Utils.displayFilesAsHTML(files);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export default SplitColourChannels;
|