浏览代码

Randomize Colour Palette Operation

Ge0rg3 5 年之前
父节点
当前提交
d23a584b9e
共有 2 个文件被更改,包括 85 次插入0 次删除
  1. 1 0
      src/core/config/Categories.json
  2. 84 0
      src/core/operations/RandomizeColourPalette.mjs

+ 1 - 0
src/core/config/Categories.json

@@ -380,6 +380,7 @@
             "Split Colour Channels",
             "Extract RGBA",
             "View Bit Plane",
+            "Randomize Colour Palette",
             "Extract LSB",
             "Rotate Image",
             "Resize Image",

+ 84 - 0
src/core/operations/RandomizeColourPalette.mjs

@@ -0,0 +1,84 @@
+/**
+ * @author Ge0rg3 [georgeomnet+cyberchef@gmail.com]
+ * @copyright Crown Copyright 2019
+ * @license Apache-2.0
+ */
+
+import Operation from "../Operation.mjs";
+import OperationError from "../errors/OperationError.mjs";
+import Utils from "../Utils";
+import PseudoRandomNumberGenerator from "./PseudoRandomNumberGenerator.mjs";
+import { isImage } from "../lib/FileType";
+import { runHash } from "../lib/Hash.mjs";
+import { toBase64 } from "../lib/Base64";
+import jimp from "jimp";
+
+/**
+ * Randomize Colour Palette operation
+ */
+class RandomizeColourPalette extends Operation {
+
+    /**
+     * RandomizeColourPalette constructor
+     */
+    constructor() {
+        super();
+
+        this.name = "Randomize Colour Palette";
+        this.module = "Image";
+        this.description = "Randomize's each colour in an image's colour palette. This can often reveal text or symbols that were previously a very similar colour to their surroundings.";
+        this.infoURL = "https://en.wikipedia.org/wiki/Indexed_color";
+        this.inputType = "byteArray";
+        this.outputType = "byteArray";
+        this.presentType = "html";
+        this.args = [
+            {
+                name: "Seed",
+                type: "string",
+                value: ""
+            }
+        ];
+    }
+
+    /**
+     * @param {byteArray} input
+     * @param {Object[]} args
+     * @returns {byteArray}
+     */
+    async run(input, args) {
+        if (!isImage(input)) throw new OperationError("Please enter a valid image file.");
+
+        const seed = args[0] || (new PseudoRandomNumberGenerator()).run("", [5, "Hex"]),
+            parsedImage = await jimp.read(Buffer.from(input)),
+            width = parsedImage.bitmap.width,
+            height = parsedImage.bitmap.height;
+
+        let rgbString, rgbHash, rgbHex;
+
+        parsedImage.scan(0, 0, width, height, function(x, y, idx) {
+            rgbString = this.bitmap.data.slice(idx, idx+3).join(".");
+            rgbHash = runHash("md5", Utils.strToArrayBuffer(seed + rgbString));
+            rgbHex = rgbHash.substr(0, 6) + "ff";
+            parsedImage.setPixelColor(parseInt(rgbHex, 16), x, y);
+        });
+
+        const imageBuffer = await parsedImage.getBufferAsync(jimp.AUTO);
+
+        return Array.from(imageBuffer);
+    }
+
+    /**
+     * Displays the extracted data as an image for web apps.
+     * @param {byteArray} data
+     * @returns {html}
+     */
+    present(data) {
+        if (!data.length) return "";
+        const type = isImage(data);
+
+        return `<img src="data:${type};base64,${toBase64(data)}">`;
+    }
+
+}
+
+export default RandomizeColourPalette;