Advertisement
Thunder-Menu

EncodeFileToBmp.html

Aug 15th, 2023
1,267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 3.85 KB | Source Code | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <title>Générateur de Motif de Carrés</title>
  7. </head>
  8. <body>
  9.     <h1>Générateur de Motif de Carrés</h1>
  10.     <input type="file" id="fileInput" style="display: none;">
  11.     <button id="loadButton">Sélectionner un fichier</button>
  12.     <button id="generateButton" disabled>Générer</button>
  13.     <button id="saveButton" disabled>Enregistrer le motif</button>
  14.     <textarea id="textBox" rows="10" cols="50"></textarea>
  15.     <div id="pictureBox"></div>
  16.  
  17.     <script>
  18.         document.addEventListener("DOMContentLoaded", function () {
  19.             const fileInput = document.getElementById("fileInput");
  20.             const loadButton = document.getElementById("loadButton");
  21.             const generateButton = document.getElementById("generateButton");
  22.             const saveButton = document.getElementById("saveButton");
  23.             const textBox = document.getElementById("textBox");
  24.             const pictureBox = document.getElementById("pictureBox");
  25.  
  26.             loadButton.addEventListener("click", function () {
  27.                 fileInput.click();
  28.             });
  29.  
  30.             fileInput.addEventListener("change", function (e) {
  31.                 const file = e.target.files[0];
  32.                 if (file) {
  33.                     const reader = new FileReader();
  34.                     reader.onload = function (event) {
  35.                         const arrayBuffer = event.target.result;
  36.                         const byteArray = new Uint8Array(arrayBuffer);
  37.                         let binaryText = "";
  38.                         byteArray.forEach(function (byte) {
  39.                             binaryText += byte.toString(2).padStart(8, "0");
  40.                         });
  41.                         textBox.value = binaryText;
  42.                         generateButton.disabled = false;
  43.                     };
  44.                     reader.readAsArrayBuffer(file);
  45.                 }
  46.             });
  47.  
  48.             generateButton.addEventListener("click", function () {
  49.                 const binaryText = textBox.value;
  50.                 const imageSize = Math.ceil(Math.sqrt(binaryText.length));
  51.                 const bitmap = new ImageData(imageSize, imageSize);
  52.  
  53.                 for (let i = 0; i < binaryText.length; i++) {
  54.                     const x = i % imageSize;
  55.                     const y = Math.floor(i / imageSize);
  56.                     const color = (binaryText[i] === "1") ? [0, 0, 0, 255] : [255, 255, 255, 255];
  57.                     const index = (x + y * imageSize) * 4;
  58.                     bitmap.data[index] = color[0];
  59.                     bitmap.data[index + 1] = color[1];
  60.                     bitmap.data[index + 2] = color[2];
  61.                     bitmap.data[index + 3] = color[3];
  62.                 }
  63.  
  64.                 const canvas = document.createElement("canvas");
  65.                 canvas.width = imageSize;
  66.                 canvas.height = imageSize;
  67.                 const context = canvas.getContext("2d");
  68.                 context.putImageData(bitmap, 0, 0);
  69.  
  70.                 pictureBox.innerHTML = "";
  71.                 pictureBox.appendChild(canvas);
  72.                 saveButton.disabled = false;
  73.             });
  74.  
  75.             saveButton.addEventListener("click", function () {
  76.                 const canvas = pictureBox.querySelector("canvas");
  77.                 const image = canvas.toDataURL("image/bmp").replace("image/bmp", "image/octet-stream");
  78.                 const a = document.createElement("a");
  79.                 a.href = image;
  80.                 a.download = prompt("Entrez le nom du fichier BMP :", "motif.bmp") || "motif.bmp";
  81.                 document.body.appendChild(a);
  82.                 a.click();
  83.                 document.body.removeChild(a);
  84.             });
  85.         });
  86.     </script>
  87. </body>
  88. </html>
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement