ViewBitPlane.mjs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 Utils from "../Utils.mjs";
  9. import { isImage } from "../lib/FileType.mjs";
  10. import { toBase64 } from "../lib/Base64.mjs";
  11. import jimplib from "jimp/es/index.js";
  12. const jimp = jimplib.default ? jimplib.default : jimplib;
  13. /**
  14. * View Bit Plane operation
  15. */
  16. class ViewBitPlane extends Operation {
  17. /**
  18. * ViewBitPlane constructor
  19. */
  20. constructor() {
  21. super();
  22. this.name = "View Bit Plane";
  23. this.module = "Image";
  24. this.description = "Extracts and displays a bit plane of any given image. These show only a single bit from each pixel, and can be used to hide messages in Steganography.";
  25. this.infoURL = "https://wikipedia.org/wiki/Bit_plane";
  26. this.inputType = "ArrayBuffer";
  27. this.outputType = "ArrayBuffer";
  28. this.presentType = "html";
  29. this.args = [
  30. {
  31. name: "Colour",
  32. type: "option",
  33. value: COLOUR_OPTIONS
  34. },
  35. {
  36. name: "Bit",
  37. type: "number",
  38. value: 0
  39. }
  40. ];
  41. }
  42. /**
  43. * @param {ArrayBuffer} input
  44. * @param {Object[]} args
  45. * @returns {ArrayBuffer}
  46. */
  47. async run(input, args) {
  48. if (!isImage(input)) throw new OperationError("Please enter a valid image file.");
  49. const [colour, bit] = args,
  50. parsedImage = await jimp.read(input),
  51. width = parsedImage.bitmap.width,
  52. height = parsedImage.bitmap.height,
  53. colourIndex = COLOUR_OPTIONS.indexOf(colour),
  54. bitIndex = 7-bit;
  55. if (bit < 0 || bit > 7) {
  56. throw new OperationError("Error: Bit argument must be between 0 and 7");
  57. }
  58. let pixel, bin, newPixelValue;
  59. parsedImage.scan(0, 0, width, height, function(x, y, idx) {
  60. pixel = this.bitmap.data[idx + colourIndex];
  61. bin = Utils.bin(pixel);
  62. newPixelValue = 255;
  63. if (bin.charAt(bitIndex) === "1") newPixelValue = 0;
  64. for (let i=0; i < 3; i++) {
  65. this.bitmap.data[idx + i] = newPixelValue;
  66. }
  67. this.bitmap.data[idx + 3] = 255;
  68. });
  69. const imageBuffer = await parsedImage.getBufferAsync(jimp.AUTO);
  70. return new Uint8Array(imageBuffer).buffer;
  71. }
  72. /**
  73. * Displays the extracted data as an image for web apps.
  74. * @param {ArrayBuffer} data
  75. * @returns {html}
  76. */
  77. present(data) {
  78. if (!data.length) return "";
  79. const type = isImage(data);
  80. return `<img src="data:${type};base64,${toBase64(data)}">`;
  81. }
  82. }
  83. const COLOUR_OPTIONS = [
  84. "Red",
  85. "Green",
  86. "Blue",
  87. "Alpha"
  88. ];
  89. export default ViewBitPlane;