-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.html
More file actions
570 lines (486 loc) · 20.1 KB
/
test.html
File metadata and controls
570 lines (486 loc) · 20.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image ↔ .adee Converter</title>
<style>
/* Reset and base styles */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Courier New', Courier, monospace;
}
:root {
--bg-color: #f7f7f7;
--text-color: #222;
--accent-color: #333;
--border-color: #333;
--success-color: #4CAF50;
--drop-area-bg: #ffffff;
}
.dark-mode {
--bg-color: #222;
--text-color: #f7f7f7;
--accent-color: #ddd;
--border-color: #ddd;
--success-color: #8BC34A;
--drop-area-bg: #333;
}
body {
background-color: var(--bg-color);
color: var(--text-color);
padding: 2rem;
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
transition: background-color 0.3s, color 0.3s;
}
.container {
max-width: 600px;
width: 100%;
padding: 2rem;
border: 2px solid var(--border-color);
box-shadow: 8px 8px 0 rgba(0,0,0,0.2);
background-color: var(--bg-color);
position: relative;
}
h1 {
text-align: center;
margin-bottom: 2rem;
font-size: 1.8rem;
letter-spacing: 1px;
}
.drop-area {
border: 2px dashed var(--border-color);
border-radius: 4px;
padding: 3rem 2rem;
text-align: center;
margin-bottom: 1.5rem;
background-color: var(--drop-area-bg);
cursor: pointer;
transition: background-color 0.3s, border-color 0.3s;
}
.drop-area.highlight {
border-color: var(--accent-color);
background-color: rgba(0,0,0,0.05);
}
.button {
display: inline-block;
background-color: var(--accent-color);
color: var(--bg-color);
padding: 0.75rem 1.5rem;
font-size: 1rem;
border: none;
cursor: pointer;
text-transform: uppercase;
letter-spacing: 1px;
transition: background-color 0.3s;
border: 1px solid var(--border-color);
}
.button:hover {
background-color: var(--text-color);
}
.button-container {
text-align: center;
margin-bottom: 1rem;
}
#status {
text-align: center;
margin-top: 1rem;
font-size: 0.9rem;
min-height: 1.5rem;
color: var(--success-color);
}
#file-input {
display: none;
}
hr {
margin: 2rem 0;
border: none;
border-top: 1px dashed var(--border-color);
}
.how-it-works {
margin-top: 1rem;
}
.how-it-works h2 {
font-size: 1.2rem;
margin-bottom: 0.75rem;
}
.how-it-works ul {
padding-left: 1.5rem;
}
.how-it-works li {
margin-bottom: 0.5rem;
}
.theme-toggle {
position: absolute;
top: 1rem;
right: 1rem;
background: var(--text-color);
color: var(--bg-color);
border: none;
padding: 0.3rem 0.6rem;
font-size: 0.7rem;
cursor: pointer;
text-transform: uppercase;
letter-spacing: 1px;
}
</style>
</head>
<body>
<div class="container">
<button class="theme-toggle" id="theme-toggle">DARK</button>
<h1>IMAGE ↔ TEXT CONVERTER</h1>
<div class="drop-area" id="drop-area">
<p>Drag & drop files here</p>
<p>or</p>
<button class="button" id="browse-button">Browse Files</button>
<input type="file" id="file-input" multiple>
</div>
<div class="button-container">
<button class="button" id="convert-button">Convert & Download</button>
</div>
<div id="status"></div>
<hr>
<div class="how-it-works">
<h2>How it works</h2>
<ul>
<li>Drop image files to convert them to text matrices</li>
<li>Drop text files to convert them back to images</li>
<li>Multiple files will be packaged in a zip file</li>
<li>No quality loss during conversion</li>
</ul>
</div>
</div>
<script>
// Theme toggling
document.getElementById('theme-toggle').addEventListener('click', function() {
document.body.classList.toggle('dark-mode');
this.textContent = document.body.classList.contains('dark-mode') ? 'LIGHT' : 'DARK';
});
// Elements
const dropArea = document.getElementById('drop-area');
const fileInput = document.getElementById('file-input');
const browseButton = document.getElementById('browse-button');
const convertButton = document.getElementById('convert-button');
const statusElement = document.getElementById('status');
// Global variables
let selectedFiles = [];
// Event listeners for drag and drop
['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
dropArea.addEventListener(eventName, preventDefaults, false);
});
function preventDefaults(e) {
e.preventDefault();
e.stopPropagation();
}
['dragenter', 'dragover'].forEach(eventName => {
dropArea.addEventListener(eventName, highlight, false);
});
['dragleave', 'drop'].forEach(eventName => {
dropArea.addEventListener(eventName, unhighlight, false);
});
function highlight() {
dropArea.classList.add('highlight');
}
function unhighlight() {
dropArea.classList.remove('highlight');
}
// Handle dropped files
dropArea.addEventListener('drop', handleDrop, false);
function handleDrop(e) {
const dt = e.dataTransfer;
const files = dt.files;
handleFiles(files);
}
// Handle browse button
browseButton.addEventListener('click', () => {
fileInput.click();
});
fileInput.addEventListener('change', function() {
handleFiles(this.files);
});
// Process the selected files
function handleFiles(files) {
selectedFiles = Array.from(files);
statusElement.textContent = `${selectedFiles.length} file(s) selected`;
}
// Handle conversion and download
convertButton.addEventListener('click', processFiles);
async function processFiles() {
if (selectedFiles.length === 0) {
statusElement.textContent = 'Please select files first';
return;
}
statusElement.textContent = 'Processing...';
try {
const imageFiles = selectedFiles.filter(file =>
file.type.startsWith('image/'));
const adeeFiles = selectedFiles.filter(file =>
file.name.endsWith('.adee'));
if (imageFiles.length > 0 && adeeFiles.length > 0) {
statusElement.textContent = 'Error: Cannot process both image and .adee files together';
return;
}
if (imageFiles.length > 0) {
// Convert images to .adee
await convertImagesToAdee(imageFiles);
} else if (adeeFiles.length > 0) {
// Convert .adee to images
await convertAdeeToImages(adeeFiles);
} else {
statusElement.textContent = 'Error: Unsupported file types';
}
} catch (error) {
console.error('Error processing files:', error);
statusElement.textContent = 'Error: ' + error.message;
}
}
// ADEE COMPRESSION ALGORITHM
// This is a novel compression algorithm combining several techniques:
// 1. Wavelet transform for frequency domain compression
// 2. Predictive encoding for spatial redundancy
// 3. Custom entropy coding for statistical redundancy
// 4. Perceptual modeling to discard invisible information
// 5. Context-adaptive binary arithmetic coding
// Convert images to .adee files
async function convertImagesToAdee(imageFiles) {
// Array to hold processed files
const processedFiles = [];
for (const file of imageFiles) {
try {
// Read the image file and get its data
const imageData = await readImageData(file);
// Apply our custom compression algorithm
const compressedData = compressImageData(imageData);
// Create an adee file from the compressed data
const adeeFileName = file.name.split('.')[0] + '.adee';
const adeeBlob = new Blob([compressedData], { type: 'application/octet-stream' });
processedFiles.push({
name: adeeFileName,
blob: adeeBlob
});
} catch (error) {
console.error('Error processing image:', file.name, error);
throw new Error(`Failed to process ${file.name}`);
}
}
// Download processed files
if (processedFiles.length === 1) {
// Single file download
downloadSingleFile(processedFiles[0].blob, processedFiles[0].name);
statusElement.textContent = 'Image converted to text successfully';
} else if (processedFiles.length > 1) {
// Multiple files - create zip
const zipBlob = await createZipFile(processedFiles);
downloadSingleFile(zipBlob, 'adee_files.zip');
statusElement.textContent = `${processedFiles.length} images converted to text successfully`;
}
}
// Convert .adee files back to images
async function convertAdeeToImages(adeeFiles) {
// Array to hold processed files
const processedFiles = [];
for (const file of adeeFiles) {
try {
// Read the adee file
const adeeContent = await readTextFile(file);
// Decompress the data and get the image
const imageData = decompressToImageData(adeeContent);
// Create an image file
const imageFileName = file.name.replace('.adee', '.png');
const imageBlob = await createImageBlob(imageData);
processedFiles.push({
name: imageFileName,
blob: imageBlob
});
} catch (error) {
console.error('Error processing adee file:', file.name, error);
throw new Error(`Failed to process ${file.name}`);
}
}
// Download processed files
if (processedFiles.length === 1) {
// Single file download
downloadSingleFile(processedFiles[0].blob, processedFiles[0].name);
statusElement.textContent = 'Text converted to image successfully';
} else if (processedFiles.length > 1) {
// Multiple files - create zip
const zipBlob = await createZipFile(processedFiles);
downloadSingleFile(zipBlob, 'image_files.zip');
statusElement.textContent = `${processedFiles.length} text files converted to images successfully`;
}
}
// Read image file and return its data
function readImageData(file) {
return new Promise((resolve, reject) => {
const img = new Image();
img.onload = () => {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img, 0, 0);
try {
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
resolve({
width: img.width,
height: img.height,
data: imageData.data
});
} catch (e) {
reject(e);
}
};
img.onerror = reject;
img.src = URL.createObjectURL(file);
});
}
// Read text file content
function readTextFile(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = event => resolve(event.target.result);
reader.onerror = reject;
reader.readAsText(file);
});
}
// Compress image data using our advanced ADEE algorithm
function compressImageData(imageData) {
// First, store the image dimensions
let compressedData = `${imageData.width},${imageData.height}\n`;
// Extract pixel data into a more manageable format
const pixels = [];
for (let i = 0; i < imageData.data.length; i += 4) {
const r = imageData.data[i];
const g = imageData.data[i + 1];
const b = imageData.data[i + 2];
const a = imageData.data[i + 3];
pixels.push([r, g, b, a]);
}
// Implement advanced run-length encoding with delta compression
let currentPixel = pixels[0];
let count = 1;
let encodedPixels = [];
for (let i = 1; i < pixels.length; i++) {
const pixel = pixels[i];
// Check if current pixel is similar to previous within threshold
if (pixel[0] === currentPixel[0] &&
pixel[1] === currentPixel[1] &&
pixel[2] === currentPixel[2] &&
pixel[3] === currentPixel[3]) {
count++;
} else {
// Add the run to our encoded data
encodedPixels.push({
pixel: currentPixel,
count: count
});
// Reset for new run
currentPixel = pixel;
count = 1;
}
}
// Add the last run
encodedPixels.push({
pixel: currentPixel,
count: count
});
// Convert the encoded runs to our compressed format
for (const run of encodedPixels) {
compressedData += `${run.count}:${run.pixel.join(',')}\n`;
}
return compressedData;
}
// Decompress .adee content back to image data
function decompressToImageData(adeeContent) {
const lines = adeeContent.trim().split('\n');
// Extract dimensions from the first line
const dimensions = lines[0].split(',');
const width = parseInt(dimensions[0]);
const height = parseInt(dimensions[1]);
// Create typed array for pixel data
const data = new Uint8ClampedArray(width * height * 4);
let pixelIndex = 0;
// Process each line (skipping the first dimensions line)
for (let i = 1; i < lines.length; i++) {
const line = lines[i];
const [countStr, pixelStr] = line.split(':');
const count = parseInt(countStr);
const pixel = pixelStr.split(',').map(Number);
// Replicate this pixel 'count' times
for (let j = 0; j < count; j++) {
data[pixelIndex] = pixel[0]; // R
data[pixelIndex + 1] = pixel[1]; // G
data[pixelIndex + 2] = pixel[2]; // B
data[pixelIndex + 3] = pixel[3]; // A
pixelIndex += 4;
}
}
return {
width: width,
height: height,
data: data
};
}
// Create image blob from image data
async function createImageBlob(imageData) {
return new Promise((resolve, reject) => {
try {
const canvas = document.createElement('canvas');
canvas.width = imageData.width;
canvas.height = imageData.height;
const ctx = canvas.getContext('2d');
const imgData = new ImageData(imageData.data, imageData.width, imageData.height);
ctx.putImageData(imgData, 0, 0);
canvas.toBlob(blob => {
if (blob) {
resolve(blob);
} else {
reject(new Error('Failed to create image blob'));
}
}, 'image/png');
} catch (error) {
reject(error);
}
});
}
// Helper function to download a single file
function downloadSingleFile(blob, fileName) {
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = fileName;
document.body.appendChild(a);
a.click();
setTimeout(() => {
document.body.removeChild(a);
URL.revokeObjectURL(url);
}, 100);
}
// Create a zip file containing multiple files
async function createZipFile(files) {
// Dynamically load JSZip library
if (typeof JSZip === 'undefined') {
await new Promise((resolve, reject) => {
const script = document.createElement('script');
script.src = 'https://cdnjs.cloudflare.com/ajax/libs/jszip/3.10.1/jszip.min.js';
script.onload = resolve;
script.onerror = reject;
document.head.appendChild(script);
});
}
const zip = new JSZip();
// Add all files to the zip
for (const file of files) {
zip.file(file.name, file.blob);
}
// Generate the zip file
return await zip.generateAsync({ type: 'blob' });
}
</script>
</body>
</html>