Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Compression, Encodage et Décodage</title>
- </head>
- <body>
- <h1>Compression, Encodage et Décodage</h1>
- <select id="operationSelect">
- <option value="compress">Compresser</option>
- <option value="encode">Encodage</option>
- <option value="decode">Décodage</option>
- </select>
- <div id="compressSection" style="display: none;">
- <h2>Compression de Fichiers</h2>
- <input type="file" id="fileInput" multiple>
- <button id="compressButton" disabled>Compresser et Enregistrer</button>
- </div>
- <div id="encodeSection" style="display: none;">
- <h2>Générateur de Motif de Carrés - Encodage</h2>
- <input type="file" id="encodeFileInput" style="display: none;">
- <button id="loadButton">Sélectionner un fichier</button>
- <button id="generateButton" disabled>Générer</button>
- <button id="saveButton" disabled>Enregistrer le motif</button>
- <textarea id="textBox" rows="10" cols="50"></textarea>
- <div id="encodePictureBox"></div>
- </div>
- <div id="decodeSection" style="display: none;">
- <h2>Générateur de Motif de Carrés - Décodage</h2>
- <input type="file" id="decodeFileInput" class="button">
- <button id="decodeGenerateButton" class="button" disabled>Générer</button>
- <button id="decodeSaveButton" class="button" style="display: none;">Enregistrer</button>
- <textarea id="decodeTextBox" rows="10" cols="50" style="margin-top: 20px;" readonly></textarea>
- <canvas id="decodePictureBox"></canvas>
- </div>
- <script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.5/jszip.min.js"></script>
- <script>
- document.addEventListener("DOMContentLoaded", function () {
- const operationSelect = document.getElementById("operationSelect");
- const compressSection = document.getElementById("compressSection");
- const encodeSection = document.getElementById("encodeSection");
- const decodeSection = document.getElementById("decodeSection");
- operationSelect.addEventListener("change", function () {
- const selectedOperation = operationSelect.value;
- if (selectedOperation === "compress") {
- compressSection.style.display = "block";
- encodeSection.style.display = "none";
- decodeSection.style.display = "none";
- } else if (selectedOperation === "encode") {
- compressSection.style.display = "none";
- encodeSection.style.display = "block";
- decodeSection.style.display = "none";
- } else if (selectedOperation === "decode") {
- compressSection.style.display = "none";
- encodeSection.style.display = "none";
- decodeSection.style.display = "block";
- }
- });
- // Compression Section
- const fileInput = document.getElementById("fileInput");
- const compressButton = document.getElementById("compressButton");
- fileInput.addEventListener("change", function (e) {
- if (e.target.files.length > 0) {
- compressButton.disabled = false;
- }
- });
- compressButton.addEventListener("click", function () {
- const filesToCompress = Array.from(fileInput.files);
- const zip = new JSZip();
- const promises = filesToCompress.map(file => {
- return new Promise(resolve => {
- const reader = new FileReader();
- reader.onload = function () {
- zip.file(file.name, reader.result);
- resolve();
- };
- reader.readAsArrayBuffer(file);
- });
- });
- Promise.all(promises).then(() => {
- const zipName = prompt("Entrez le nom du fichier ZIP :") || "compressed.zip";
- const saveFileDialog = document.createElement("a");
- saveFileDialog.style.display = "none";
- saveFileDialog.addEventListener("click", function (e) {
- e.target.style.display = "none";
- });
- document.body.appendChild(saveFileDialog);
- zip.generateAsync({ type: "blob" }).then(function (content) {
- const url = URL.createObjectURL(content);
- saveFileDialog.href = url;
- saveFileDialog.download = zipName;
- saveFileDialog.click();
- URL.revokeObjectURL(url);
- });
- });
- });
- // Encoding Section
- const encodeFileInput = document.getElementById("encodeFileInput");
- const loadButton = document.getElementById("loadButton");
- const generateButton = document.getElementById("generateButton");
- const saveButton = document.getElementById("saveButton");
- const textBox = document.getElementById("textBox");
- const encodePictureBox = document.getElementById("encodePictureBox");
- loadButton.addEventListener("click", function () {
- encodeFileInput.click();
- });
- encodeFileInput.addEventListener("change", function (e) {
- const file = e.target.files[0];
- if (file) {
- const reader = new FileReader();
- reader.onload = function (event) {
- const arrayBuffer = event.target.result;
- const byteArray = new Uint8Array(arrayBuffer);
- let binaryText = "";
- byteArray.forEach(function (byte) {
- binaryText += byte.toString(2).padStart(8, "0");
- });
- textBox.value = binaryText;
- generateButton.disabled = false;
- };
- reader.readAsArrayBuffer(file);
- }
- });
- generateButton.addEventListener("click", function () {
- const binaryText = textBox.value;
- const imageSize = Math.ceil(Math.sqrt(binaryText.length));
- const bitmap = new ImageData(imageSize, imageSize);
- for (let i = 0; i < binaryText.length; i++) {
- const x = i % imageSize;
- const y = Math.floor(i / imageSize);
- const color = (binaryText[i] === "1") ? [0, 0, 0, 255] : [255, 255, 255, 255];
- const index = (x + y * imageSize) * 4;
- bitmap.data[index] = color[0];
- bitmap.data[index + 1] = color[1];
- bitmap.data[index + 2] = color[2];
- bitmap.data[index + 3] = color[3];
- }
- const canvas = document.createElement("canvas");
- canvas.width = imageSize;
- canvas.height = imageSize;
- const context = canvas.getContext("2d");
- context.putImageData(bitmap, 0, 0);
- encodePictureBox.innerHTML = "";
- encodePictureBox.appendChild(canvas);
- saveButton.disabled = false;
- });
- saveButton.addEventListener("click", function () {
- const canvas = encodePictureBox.querySelector("canvas");
- const image = canvas.toDataURL("image/bmp").replace("image/bmp", "image/octet-stream");
- const a = document.createElement("a");
- a.href = image;
- a.download = prompt("Entrez le nom du fichier BMP :", "motif.bmp") || "motif.bmp";
- document.body.appendChild(a);
- a.click();
- document.body.removeChild(a);
- });
- // Decoding Section
- const decodeFileInput = document.getElementById("decodeFileInput");
- const decodeGenerateButton = document.getElementById("decodeGenerateButton");
- const decodeSaveButton = document.getElementById("decodeSaveButton");
- const decodePictureBox = document.getElementById("decodePictureBox");
- const decodeTextBox = document.getElementById("decodeTextBox");
- const decodeCanvas = decodePictureBox.getContext("2d");
- let loadedBitmap = null;
- let binaryText = "";
- decodeFileInput.addEventListener("change", (event) => {
- const file = event.target.files[0];
- if (file) {
- const reader = new FileReader();
- reader.onload = (e) => {
- const image = new Image();
- image.src = e.target.result;
- image.onload = () => {
- decodePictureBox.width = image.width;
- decodePictureBox.height = image.height;
- decodeCanvas.drawImage(image, 0, 0);
- loadedBitmap = image;
- decodeGenerateButton.disabled = false;
- };
- };
- reader.readAsDataURL(file);
- }
- });
- decodeGenerateButton.addEventListener("click", () => {
- if (!loadedBitmap) {
- alert("Veuillez charger une image d'abord.");
- return;
- }
- const imageData = decodeCanvas.getImageData(0, 0, decodePictureBox.width, decodePictureBox.height);
- binaryText = generateBinaryPattern(imageData);
- decodeTextBox.value = binaryText;
- decodeSaveButton.style.display = "block";
- });
- decodeSaveButton.addEventListener("click", () => {
- if (!binaryText.trim()) {
- alert("Le contenu du TextBox est vide. Veuillez générer le motif d'abord.");
- return;
- }
- const binaryBytes = convertBinaryTextToBytes(binaryText);
- const blob = new Blob([binaryBytes], { type: "application/zip" });
- const a = document.createElement("a");
- const fileName = prompt("Entrez le nom du fichier ZIP :") || "binary_pattern.zip";
- a.href = URL.createObjectURL(blob);
- a.download = fileName;
- document.body.appendChild(a);
- a.click();
- document.body.removeChild(a);
- });
- function generateBinaryPattern(imageData) {
- const threshold = 128;
- let binaryText = "";
- for (let y = 0; y < imageData.height; y++) {
- for (let x = 0; x < imageData.width; x++) {
- const index = (y * imageData.width + x) * 4;
- const grayscale = (imageData.data[index] + imageData.data[index + 1] + imageData.data[index + 2]) / 3;
- binaryText += grayscale < threshold ? "1" : "0";
- }
- binaryText += "\n";
- }
- return binaryText;
- }
- function convertBinaryTextToBytes(binaryText) {
- binaryText = binaryText.replace(/\s+/g, "");
- const numOfBytes = binaryText.length / 8;
- const bytes = new Uint8Array(numOfBytes);
- for (let i = 0; i < numOfBytes; i++) {
- const byteStr = binaryText.substr(i * 8, 8);
- const byteValue = parseInt(byteStr, 2);
- bytes[i] = byteValue;
- }
- return bytes;
- }
- });
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement