ExtractRGBA.mjs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /**
  2. * @author Ge0rg3 [georgeomnet+cyberchef@gmail.com]
  3. * @copyright Crown Copyright 2019
  4. * @license Apache-2.0
  5. */
  6. import Operation from "../Operation.mjs";
  7. import OperationError from "../errors/OperationError.mjs";
  8. import { isImage } from "../lib/FileType.mjs";
  9. import jimp from "jimp";
  10. import {RGBA_DELIM_OPTIONS} from "../lib/Delim.mjs";
  11. /**
  12. * Extract RGBA operation
  13. */
  14. class ExtractRGBA extends Operation {
  15. /**
  16. * ExtractRGBA constructor
  17. */
  18. constructor() {
  19. super();
  20. this.name = "Extract RGBA";
  21. this.module = "Image";
  22. this.description = "Extracts each pixel's RGBA value in an image. These are sometimes used in Steganography to hide text or data.";
  23. this.infoURL = "https://wikipedia.org/wiki/RGBA_color_space";
  24. this.inputType = "ArrayBuffer";
  25. this.outputType = "string";
  26. this.args = [
  27. {
  28. name: "Delimiter",
  29. type: "editableOption",
  30. value: RGBA_DELIM_OPTIONS
  31. },
  32. {
  33. name: "Include Alpha",
  34. type: "boolean",
  35. value: true
  36. }
  37. ];
  38. }
  39. /**
  40. * @param {ArrayBuffer} input
  41. * @param {Object[]} args
  42. * @returns {string}
  43. */
  44. async run(input, args) {
  45. if (!isImage(input)) throw new OperationError("Please enter a valid image file.");
  46. const delimiter = args[0],
  47. includeAlpha = args[1],
  48. parsedImage = await jimp.read(input);
  49. let bitmap = parsedImage.bitmap.data;
  50. bitmap = includeAlpha ? bitmap : bitmap.filter((val, idx) => idx % 4 !== 3);
  51. return bitmap.join(delimiter);
  52. }
  53. }
  54. export default ExtractRGBA;