-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitmap.html
More file actions
1172 lines (1000 loc) · 47.6 KB
/
bitmap.html
File metadata and controls
1172 lines (1000 loc) · 47.6 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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Video & Image Dithering Effect</title>
<style>
body {
font-family: 'Inter', sans-serif;
display: flex;
flex-direction: column;
align-items: center;
margin: 0;
padding: 20px;
background-color: #1f2937; /* Dark background */
color: #f3f4f6;
min-height: 100vh;
}
h1 {
color: #a78bfa; /* Violet highlight */
font-size: 2rem;
margin-bottom: 0.5rem;
text-align: center;
}
p {
max-width: 600px;
text-align: center;
color: #d1d5db;
margin-bottom: 20px;
}
#controls {
margin: 20px 0;
padding: 15px 25px;
border-radius: 12px;
background-color: #374151; /* Slightly lighter dark card */
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4);
display: flex;
flex-direction: column;
align-items: flex-start; /* Align items to the left within the control box */
gap: 15px; /* Spacing between elements */
width: 90%;
max-width: 600px;
}
#input-group {
display: flex;
flex-direction: column;
width: 100%;
}
.slider-group {
display: flex;
align-items: center;
width: 100%;
transition: opacity 0.3s, filter 0.3s; /* Smooth transition for disabling */
}
/* New style for disabled controls */
.disabled-group, .disabled-header, .disabled-button {
opacity: 0.5;
pointer-events: none; /* Prevents clicking the controls */
filter: grayscale(50%);
}
/* General layout for labels and buttons */
.slider-header {
display: flex;
align-items: center;
width: 100%;
justify-content: space-between;
transition: opacity 0.3s, filter 0.3s;
}
.slider-header label {
margin: 0;
}
/* Icon button styling */
.icon-button {
background: none;
border: none;
cursor: pointer;
padding: 5px;
line-height: 1;
color: #d1d5db; /* Light gray color */
transition: color 0.2s;
border-radius: 4px;
}
.icon-button:hover {
color: #a78bfa; /* Violet hover */
background-color: #4b5563;
}
.icon-button:disabled {
opacity: 0.3;
cursor: not-allowed;
}
label {
font-weight: 500;
margin-bottom: 5px;
white-space: nowrap;
}
input[type="file"] {
display: block;
padding: 10px;
border: 1px solid #4b5563;
border-radius: 8px;
cursor: pointer;
background-color: #4b5563;
color: #f3f4f6;
transition: background-color 0.2s;
width: 100%; /* Full width within its group */
}
input[type="file"]:hover {
background-color: #6b7280;
}
input[type="range"] {
flex-grow: 1;
margin: 0 10px;
-webkit-appearance: none;
background: transparent;
}
input[type="range"]::-webkit-slider-thumb {
-webkit-appearance: none;
height: 15px;
width: 15px;
border-radius: 50%;
background: #a78bfa;
cursor: pointer;
box-shadow: 0 0 2px rgba(0, 0, 0, 0.5);
margin-top: -5px; /* Adjust vertical position */
}
input[type="range"]::-webkit-slider-runnable-track {
width: 100%;
height: 5px;
background: #4b5563;
border-radius: 3px;
}
/* Style for Select element */
select {
padding: 8px;
border-radius: 6px;
background-color: #4b5563;
color: #f3f4f6;
border: 1px solid #6b7280;
flex-grow: 1;
}
.toggle-button {
width: 100%;
padding: 12px;
border: none;
border-radius: 8px;
font-weight: bold;
cursor: pointer;
transition: background-color 0.2s, transform 0.1s, opacity 0.3s;
}
.toggle-button:disabled {
opacity: 0.5;
cursor: not-allowed;
}
.toggle-on {
background-color: #a78bfa; /* Violet */
color: #1f2937;
}
.toggle-off {
background-color: #ef4444; /* Red */
color: #f3f4f6;
}
/* New style for the export button */
.export-button {
background-color: #34d399; /* Green color */
color: #1f2937;
}
.toggle-button:active {
transform: scale(0.98);
}
/* --- Threshold Graph Styles --- */
.threshold-graph-container {
width: 100%;
height: 150px;
position: relative;
margin-bottom: 5px;
border-radius: 4px;
overflow: hidden;
border: 1px solid #4b5563;
transition: opacity 0.3s;
}
.threshold-graph-container.hidden {
display: none;
}
.threshold-graph-container.disabled-graph {
opacity: 0.3;
}
#histogramCanvas {
width: 100%;
height: 100%;
display: block;
position: absolute;
top: 0;
left: 0;
}
.threshold-indicator {
position: absolute;
top: 0;
width: 2px;
height: 100%;
background-color: #ef4444; /* Bright red line */
box-shadow: 0 0 5px rgba(239, 68, 68, 0.8);
transform: translateX(-50%); /* Center the line on the position */
z-index: 10; /* Ensure the indicator is on top of the canvas */
}
/* Custom style for the Dither Type Toggle */
#ditherTypeButton {
width: 100%;
padding: 10px;
border-radius: 8px;
font-weight: 500;
background-color: #4b5563;
color: #d1d5db;
border: 1px solid #6b7280;
}
/* Container for side-by-side display */
#media-container {
display: flex;
justify-content: center;
gap: 20px;
flex-wrap: wrap;
margin-top: 20px;
width: 100%;
max-width: 1200px;
}
.media-box {
text-align: center;
flex-grow: 1;
min-width: 300px;
max-width: 45%;
}
.media-box p {
font-weight: bold;
color: #d1d5db;
margin-bottom: 10px;
font-size: 1.1rem;
}
/* Container for original video/canvas to manage visibility */
.source-wrapper {
position: relative;
width: 100%;
border: 4px solid #4b5563;
border-radius: 8px;
overflow: hidden;
background-color: #4b5563; /* Placeholder BG */
}
#originalCanvas, #originalVideo {
display: block;
width: 100%;
height: auto;
border-radius: 4px; /* Inner radius */
}
#videoControls {
position: absolute;
bottom: 10px;
left: 50%;
transform: translateX(-50%);
background-color: rgba(20, 20, 20, 0.7);
border-radius: 8px;
padding: 8px;
display: none; /* Hidden by default */
}
#playPauseButton {
background: none;
border: none;
cursor: pointer;
padding: 0;
color: #f3f4f6;
}
#playPauseButton:disabled {
opacity: 0.5;
cursor: not-allowed;
}
#processedCanvas {
border: 4px solid #4b5563;
border-radius: 8px;
display: block;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.5);
width: 100%;
height: auto;
image-rendering: pixelated;
}
</style>
</head>
<body>
<h1>Video & Image Dithering Effect</h1>
<p>Upload an image or MP4 video. All processing happens live in your browser.</p>
<div id="controls">
<div id="input-group">
<label for="mediaUpload">Choose an Image or Video File:</label>
<input type="file" id="mediaUpload" accept="image/*,video/mp4">
</div>
<!-- Toggle Bitmap Mode Button -->
<button id="toggleBitmapMode" class="toggle-button toggle-on">Bitmap Mode: ON</button>
<button id="exportImageButton" class="toggle-button export-button">Export Current Frame (PNG)</button>
<!-- New Video Export Button -->
<button id="exportVideoButton" class="toggle-button export-button" style="background-color: #f59e0b; display: none;">Export Video (WebM)</button>
<hr style="width:100%; border-color: #4b5563;">
<!-- Dithering Type Toggle -->
<button id="ditherTypeButton" class="toggle-button">Dithering Type: Random Noise</button>
<!-- DITHERING MATRIX SIZE -->
<div class="slider-group disabled-group" id="matrixSizeGroup">
<label for="matrixSizeSelect">Dither Matrix Size:</label>
<select id="matrixSizeSelect">
<option value="2">2x2</option>
<option value="4" selected>4x4</option>
<option value="8">8x8</option>
<option value="16">16x16</option>
<option value="32">32x32</option>
<option value="64">64x64</option>
<option value="128">128x128</option>
</select>
</div>
<hr style="width:100%; border-color: #4b5563;">
<!-- Saturation Slider (Group gets disabled when Bitmap Mode is ON) -->
<div class="slider-group" id="saturationGroup">
<label for="saturationSlider">Saturation (%):</label>
<input type="range" id="saturationSlider" min="0" max="200" value="100">
<span id="saturationValue" style="width: 30px; text-align: right;">100</span>
</div>
<!-- Posterization Slider -->
<div class="slider-group">
<label for="posterizationSlider">Poster Levels (2-32):</label>
<input type="range" id="posterizationSlider" min="2" max="32" value="8">
<span id="posterizationValue" style="width: 30px; text-align: right;">8</span>
</div>
<!-- Resolution Slider -->
<div class="slider-group">
<label for="resolutionSlider">Resolution (%):</label>
<input type="range" id="resolutionSlider" min="10" max="100" value="100">
<span id="resolutionValue" style="width: 40px; text-align: right;">100%</span>
</div>
<!-- Noise Magnitude Slider -->
<div class="slider-group" id="noiseMagnitudeGroup">
<label for="noiseMagnitudeSlider">Noise Magnitude:</label>
<input type="range" id="noiseMagnitudeSlider" min="0" max="255" value="64">
<span id="noiseMagnitudeValue" style="width: 30px; text-align: right;">64</span>
</div>
<!-- Threshold Controls Wrapper -->
<div style="width: 100%;">
<!-- Threshold Header with Icon Button -->
<div class="slider-header" id="thresholdHeader">
<label for="thresholdSlider">Threshold Value:</label>
<!-- Icon: Bar Chart (representing a histogram) -->
<button id="toggleGraphButton" class="icon-button" title="Toggle Histogram Visualization">
<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="18" x2="18" y1="20" y2="10"/><line x1="12" x2="12" y1="20" y2="4"/><line x1="6" x2="6" y1="20" y2="14"/></svg>
</button>
</div>
<!-- Threshold Graph Visualization (Histogram) -->
<div class="threshold-graph-container hidden" id="thresholdGraphContainer">
<canvas id="histogramCanvas"></canvas>
<div id="thresholdIndicator" class="threshold-indicator"></div>
</div>
<!-- Threshold Slider - This group gets disabled when Bitmap Mode is OFF -->
<div class="slider-group" id="thresholdGroup">
<input type="range" id="thresholdSlider" min="0" max="255" value="128">
<span id="thresholdValue" style="width: 30px; text-align: right;">128</span>
</div>
</div>
</div>
<div id="media-container">
<div class="media-box">
<p id="originalSourceTitle">Original Image</p>
<div class="source-wrapper">
<canvas id="originalCanvas"></canvas>
<video id="originalVideo" loop muted playsinline style="display: none;"></video>
<div id="videoControls">
<button id="playPauseButton">
<!-- Play Icon -->
<svg id="playIcon" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polygon points="5 3 19 12 5 21 5 3"></polygon></svg>
<!-- Pause Icon -->
<svg id="pauseIcon" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="display: none;"><rect x="6" y="4" width="4" height="16"></rect><rect x="14" y="4" width="4" height="16"></rect></svg>
</button>
</div>
</div>
</div>
<div class="media-box">
<p>Processed Output</p>
<canvas id="processedCanvas"></canvas>
</div>
</div>
<script>
// --- Global DOM Elements ---
const mediaUpload = document.getElementById('mediaUpload');
const originalCanvas = document.getElementById('originalCanvas');
const originalVideo = document.getElementById('originalVideo');
const videoControls = document.getElementById('videoControls');
const playPauseButton = document.getElementById('playPauseButton');
const playIcon = document.getElementById('playIcon');
const pauseIcon = document.getElementById('pauseIcon');
const originalSourceTitle = document.getElementById('originalSourceTitle');
const processedCanvas = document.getElementById('processedCanvas');
const noiseMagnitudeSlider = document.getElementById('noiseMagnitudeSlider');
const noiseMagnitudeValueSpan = document.getElementById('noiseMagnitudeValue');
const noiseMagnitudeGroup = document.getElementById('noiseMagnitudeGroup');
const thresholdSlider = document.getElementById('thresholdSlider');
const thresholdValueSpan = document.getElementById('thresholdValue');
const thresholdGroup = document.getElementById('thresholdGroup');
const thresholdHeader = document.getElementById('thresholdHeader');
const saturationSlider = document.getElementById('saturationSlider');
const saturationValueSpan = document.getElementById('saturationValue');
const saturationGroup = document.getElementById('saturationGroup');
const posterizationSlider = document.getElementById('posterizationSlider');
const posterizationValueSpan = document.getElementById('posterizationValue');
const resolutionSlider = document.getElementById('resolutionSlider');
const resolutionValueSpan = document.getElementById('resolutionValue');
const toggleBitmapModeButton = document.getElementById('toggleBitmapMode');
const toggleGraphButton = document.getElementById('toggleGraphButton');
const exportImageButton = document.getElementById('exportImageButton');
const ditherTypeButton = document.getElementById('ditherTypeButton');
const matrixSizeSelect = document.getElementById('matrixSizeSelect');
const matrixSizeGroup = document.getElementById('matrixSizeGroup');
const thresholdGraphContainer = document.getElementById('thresholdGraphContainer');
const thresholdIndicator = document.getElementById('thresholdIndicator');
// New Video Export Button
const exportVideoButton = document.getElementById('exportVideoButton');
// Canvas Contexts
const originalCtx = originalCanvas.getContext('2d');
const processedCtx = processedCanvas.getContext('2d');
const histogramCanvas = document.getElementById('histogramCanvas');
const histogramCtx = histogramCanvas.getContext('2d');
// Offscreen canvas for processing
const processOffscreenCanvas = document.createElement('canvas');
const processOffscreenCtx = processOffscreenCanvas.getContext('2d', { willReadFrequently: true });
const sourceOffscreenCanvas = document.createElement('canvas');
const sourceOffscreenCtx = sourceOffscreenCanvas.getContext('2d', { willReadFrequently: true });
// --- Global State ---
let currentMedia = {
element: null, // Will be originalVideo or originalCanvas
type: 'none', // 'image' or 'video'
width: 400,
height: 300
};
let currentNoiseMagnitude = 64;
let currentThreshold = 128;
let currentSaturation = 100;
let currentPosterizationLevels = 8;
let currentResolution = 100;
let isBitmapMode = true;
let isGraphVisible = false;
let isVideoPlaying = false;
let ditherType = 'random';
let currentMatrixSize = 4;
let currentMatrix = null; // Will be set after generation
// MediaRecorder state
let recorder = null;
let recordedChunks = [];
// --- Bayer Matrix Generation ---
function generateBayerMatrix(size) {
// FIX: Ensure size is a number for the base case
size = Number(size);
if (size === 2) {
return [
[0, 2],
[3, 1]
];
}
const m_prev = generateBayerMatrix(size / 2);
const newMatrix = Array(size).fill(0).map(() => Array(size));
const factor = (size * size) / 4;
for (let y = 0; y < size; y++) {
for (let x = 0; x < size; x++) {
const half_x = x % (size / 2);
const half_y = y % (size / 2);
if (y < size / 2) {
if (x < size / 2) { // Top-left
newMatrix[y][x] = 4 * m_prev[half_y][half_x];
} else { // Top-right
newMatrix[y][x] = 4 * m_prev[half_y][half_x] + 2;
}
} else {
if (x < size / 2) { // Bottom-left
newMatrix[y][x] = 4 * m_prev[half_y][half_x] + 3;
} else { // Bottom-right
newMatrix[y][x] = 4 * m_prev[half_y][half_x] + 1;
}
}
}
}
return newMatrix;
}
// Generate all matrices on load
const BAYER_MATRICES = { 2:0, 4:0, 8:0, 16:0, 32:0, 64:0, 128:0 };
for(const size in BAYER_MATRICES) { BAYER_MATRICES[size] = generateBayerMatrix(parseInt(size)); }
currentMatrix = BAYER_MATRICES[currentMatrixSize];
// --- Core Processing Loop ---
function processFrame() {
if (!currentMedia.element) return;
const scaledWidth = Math.floor(currentMedia.width * (currentResolution / 100));
const scaledHeight = Math.floor(currentMedia.height * (currentResolution / 100));
// Ensure offscreen canvases are the correct size
if (processOffscreenCanvas.width !== scaledWidth || processOffscreenCanvas.height !== scaledHeight) {
processOffscreenCanvas.width = scaledWidth;
processOffscreenCanvas.height = scaledHeight;
}
if (sourceOffscreenCanvas.width !== currentMedia.width || sourceOffscreenCanvas.height !== currentMedia.height) {
sourceOffscreenCanvas.width = currentMedia.width;
sourceOffscreenCanvas.height = currentMedia.height;
}
// 1. Get source data
let sourceElement = currentMedia.element;
let sourceImageData;
if (currentMedia.type === 'video') {
// Draw video frame to source offscreen canvas, then get data
sourceOffscreenCtx.drawImage(sourceElement, 0, 0, currentMedia.width, currentMedia.height);
sourceImageData = sourceOffscreenCtx.getImageData(0, 0, currentMedia.width, currentMedia.height);
} else {
// Image data is already on the original canvas
sourceImageData = originalCtx.getImageData(0, 0, currentMedia.width, currentMedia.height);
}
// 2. Draw source to the scaled-down processing canvas (for resolution drop)
processOffscreenCtx.drawImage(sourceElement, 0, 0, scaledWidth, scaledHeight);
// 3. Get scaled-down image data
const processedImageData = processOffscreenCtx.getImageData(0, 0, scaledWidth, scaledHeight);
const data = processedImageData.data;
const SAT_FACTOR = currentSaturation / 100.0;
const validPosterLevels = Math.max(2, currentPosterizationLevels);
const POSTER_STEP = 256 / validPosterLevels;
const size = currentMatrixSize;
const scale = size * size;
// 4. Main processing loop
for (let i = 0; i < data.length; i += 4) {
const pixelIndex = i / 4;
const x = pixelIndex % scaledWidth;
const y = Math.floor(pixelIndex / scaledWidth);
let r = data[i];
let g = data[i + 1];
let b = data[i + 2];
// --- 1. Apply Saturation ---
const L = 0.299 * r + 0.587 * g + 0.114 * b;
r = L + (r - L) * SAT_FACTOR;
g = L + (g - L) * SAT_FACTOR;
b = L + (b - L) * SAT_FACTOR;
r = Math.max(0, Math.min(255, r));
g = Math.max(0, Math.min(255, g));
b = Math.max(0, Math.min(255, b));
// --- 2. Apply Posterization ---
r = Math.floor(r / POSTER_STEP) * POSTER_STEP;
g = Math.floor(g / POSTER_STEP) * POSTER_STEP;
b = Math.floor(b / POSTER_STEP) * POSTER_STEP;
let finalR, finalG, finalB;
if (isBitmapMode) {
// --- 3a. Bitmap Mode ---
const finalLuminance = 0.299 * r + 0.587 * g + 0.114 * b;
let newColor;
if (ditherType === 'bayer') {
const matrixValue = currentMatrix[y % size][x % size];
const adjustment = currentThreshold - 128;
const adjustedLuminance = Math.max(0, Math.min(255, finalLuminance + adjustment));
newColor = (adjustedLuminance / 255.0) > ((matrixValue + 0.5) / scale) ? 255 : 0;
} else { // 'random'
newColor = (finalLuminance + (Math.random() - 0.5) * currentNoiseMagnitude > currentThreshold) ? 255 : 0;
}
finalR = finalG = finalB = newColor;
} else {
// --- 3b. Color Mode ---
const noise = (Math.random() - 0.5) * currentNoiseMagnitude;
finalR = Math.max(0, Math.min(255, r + noise));
finalG = Math.max(0, Math.min(255, g + noise));
finalB = Math.max(0, Math.min(255, b + noise));
}
data[i] = finalR;
data[i + 1] = finalG;
data[i + 2] = finalB;
// Alpha (keep original)
}
// 5. Put processed data back onto the offscreen canvas
processOffscreenCtx.putImageData(processedImageData, 0, 0);
// 6. Draw final result to visible canvas (scaling up)
processedCtx.imageSmoothingEnabled = false;
processedCtx.clearRect(0, 0, processedCanvas.width, processedCanvas.height);
processedCtx.drawImage(processOffscreenCanvas, 0, 0, processedCanvas.width, processedCanvas.height);
// 7. Update histogram (if visible)
if (isGraphVisible && isBitmapMode) {
calculateAndDrawHistogram(sourceImageData.data); // Use original data for histogram
}
// 8. Loop if video is playing
// --- FIX 2 ---
// This logic is now simplified. The 'ended' event listener
// in startVideoExport handles stopping the loop.
if (currentMedia.type === 'video' && isVideoPlaying) {
requestAnimationFrame(processFrame);
}
// --- END FIX 2 ---
}
// --- Histogram Logic ---
function calculateAndDrawHistogram(originalData) {
if (!originalData) return;
histogramCanvas.width = thresholdGraphContainer.clientWidth;
histogramCanvas.height = thresholdGraphContainer.clientHeight;
histogramCtx.clearRect(0, 0, histogramCanvas.width, histogramCanvas.height);
const histogram = new Array(256).fill(0);
const SAT_FACTOR = currentSaturation / 100.0;
const validPosterLevels = Math.max(2, currentPosterizationLevels);
const POSTER_STEP = 256 / validPosterLevels;
for (let i = 0; i < originalData.length; i += 4) {
let r = originalData[i], g = originalData[i + 1], b = originalData[i + 2];
const L_sat = 0.299 * r + 0.587 * g + 0.114 * b;
r = L_sat + (r - L_sat) * SAT_FACTOR;
g = L_sat + (g - L_sat) * SAT_FACTOR;
b = L_sat + (b - L_sat) * SAT_FACTOR;
r = Math.max(0, Math.min(255, r));
g = Math.max(0, Math.min(255, g));
b = Math.max(0, Math.min(255, b));
r = Math.floor(r / POSTER_STEP) * POSTER_STEP;
g = Math.floor(g / POSTER_STEP) * POSTER_STEP;
b = Math.floor(b / POSTER_STEP) * POSTER_STEP;
const finalLuminance = Math.round(0.299 * r + 0.587 * g + 0.114 * b);
histogram[Math.min(255, Math.max(0, finalLuminance))]++;
}
const smoothedHistogram = new Array(256).fill(0);
let smoothedMaxCount = 0;
for (let i = 0; i < 256; i++) {
let sum = 0, count = 0;
for (let j = -2; j <= 2; j++) {
const index = i + j;
if (index >= 0 && index < 256) {
sum += histogram[index];
count++;
}
}
const smoothedValue = sum / count;
smoothedHistogram[i] = smoothedValue;
if (smoothedValue > smoothedMaxCount) smoothedMaxCount = smoothedValue;
}
if (smoothedMaxCount === 0) return;
const canvasWidth = histogramCanvas.width, canvasHeight = histogramCanvas.height;
const binWidth = canvasWidth / 256;
const gradient = histogramCtx.createLinearGradient(0, 0, canvasWidth, 0);
gradient.addColorStop(0, 'rgba(0,0,0,0.15)');
gradient.addColorStop(1, 'rgba(255,255,255,0.15)');
histogramCtx.fillStyle = gradient;
histogramCtx.fillRect(0, 0, canvasWidth, canvasHeight);
histogramCtx.beginPath();
histogramCtx.strokeStyle = '#a78bfa';
histogramCtx.fillStyle = 'rgba(167, 139, 250, 0.4)';
histogramCtx.lineWidth = 1.5;
histogramCtx.moveTo(0, canvasHeight);
for (let i = 0; i < 256; i++) {
const normalizedHeight = (smoothedHistogram[i] / smoothedMaxCount) * canvasHeight;
histogramCtx.lineTo(i * binWidth, canvasHeight - normalizedHeight);
}
histogramCtx.lineTo(canvasWidth, canvasHeight);
histogramCtx.closePath();
histogramCtx.fill();
histogramCtx.stroke();
}
// --- UI State Management ---
function updateGraphVisibility() {
if (isGraphVisible && isBitmapMode) {
thresholdGraphContainer.classList.remove('hidden');
processFrame(); // This will trigger the histogram draw
} else {
thresholdGraphContainer.classList.add('hidden');
}
}
function updateThresholdControlsState() {
const positionPercent = (currentThreshold / 255) * 100;
thresholdIndicator.style.left = `${positionPercent}%`;
if (isBitmapMode) {
thresholdGroup.classList.remove('disabled-group');
thresholdHeader.classList.remove('disabled-header');
thresholdGraphContainer.classList.remove('disabled-graph');
thresholdIndicator.style.display = 'block';
saturationGroup.classList.add('disabled-group');
updateGraphVisibility();
} else {
thresholdGroup.classList.add('disabled-group');
thresholdHeader.classList.add('disabled-header');
thresholdGraphContainer.classList.add('disabled-graph');
thresholdIndicator.style.display = 'none';
saturationGroup.classList.remove('disabled-group');
if (isGraphVisible) updateGraphVisibility(); // Hide graph if it was visible
}
ditherTypeButton.textContent = `Dither Type: ${ditherType === 'random' ? 'Random Noise' : 'Bayer Dithering'}`;
if (ditherType === 'bayer') {
noiseMagnitudeGroup.classList.add('disabled-group');
matrixSizeGroup.classList.remove('disabled-group');
} else {
noiseMagnitudeGroup.classList.remove('disabled-group');
matrixSizeGroup.classList.add('disabled-group');
}
}
function updatePlayPauseButton() {
if (isVideoPlaying) {
playIcon.style.display = 'none';
pauseIcon.style.display = 'block';
} else {
playIcon.style.display = 'block';
pauseIcon.style.display = 'none';
}
}
// --- Initial Setup & Event Listeners ---
// Set initial placeholder canvas
originalCanvas.width = 400;
originalCanvas.height = 300;
processedCanvas.width = 400;
processedCanvas.height = 300;
originalCtx.fillStyle = '#4b5563';
originalCtx.fillRect(0, 0, 400, 300);
processedCtx.fillStyle = '#4b5563';
processedCtx.fillRect(0, 0, 400, 300);
// --- Media Loading ---
mediaUpload.addEventListener('change', (event) => {
const file = event.target.files[0];
if (!file) return;
const fileURL = URL.createObjectURL(file);
// Stop any previous video
if (currentMedia.type === 'video' && isVideoPlaying) {
originalVideo.pause();
isVideoPlaying = false;
updatePlayPauseButton();
}
if (file.type.startsWith('video/')) {
currentMedia.type = 'video';
currentMedia.element = originalVideo;
originalVideo.src = fileURL;
originalVideo.style.display = 'block';
videoControls.style.display = 'block';
originalCanvas.style.display = 'none';
originalSourceTitle.textContent = "Original Video";
// Show video-specific buttons
exportImageButton.textContent = "Export Current Frame (PNG)";
exportVideoButton.style.display = 'block';
originalVideo.onloadedmetadata = () => {
currentMedia.width = originalVideo.videoWidth;
currentMedia.height = originalVideo.videoHeight;
// Set initial processed canvas size based on resolution
const scaledWidth = Math.floor(currentMedia.width * (currentResolution / 100));
const scaledHeight = Math.floor(currentMedia.height * (currentResolution / 100));
processedCanvas.width = scaledWidth;
processedCanvas.height = scaledHeight;
// Start playing and processing
originalVideo.play();
isVideoPlaying = true;
updatePlayPauseButton();
processFrame();
};
} else if (file.type.startsWith('image/')) {
currentMedia.type = 'image';
currentMedia.element = originalCanvas;
originalVideo.style.display = 'none';
videoControls.style.display = 'none';
originalCanvas.style.display = 'block';
originalSourceTitle.textContent = "Original Image";
// Show image-specific buttons
exportImageButton.textContent = "Export Image (PNG)";
exportVideoButton.style.display = 'none';
const img = new Image();
img.onload = () => {
currentMedia.width = img.width;
currentMedia.height = img.height;
originalCanvas.width = currentMedia.width;
originalCanvas.height = currentMedia.height;
// Set initial processed canvas size based on resolution
const scaledWidth = Math.floor(currentMedia.width * (currentResolution / 100));
const scaledHeight = Math.floor(currentMedia.height * (currentResolution / 100));
processedCanvas.width = scaledWidth;
processedCanvas.height = scaledHeight;
originalCtx.drawImage(img, 0, 0, currentMedia.width, currentMedia.height);
processFrame();
};
img.src = fileURL;
}
});
// Video controls
playPauseButton.addEventListener('click', () => {
if (currentMedia.type !== 'video') return;
if (isVideoPlaying) {
originalVideo.pause();
} else {
originalVideo.play();
processFrame(); // Re-kick animation loop
}
isVideoPlaying = !isVideoPlaying;
updatePlayPauseButton();
});
// Disables all interactive controls during video export
function setInteractiveControlsDisabled(disabled) {
document.querySelectorAll('#controls input, #controls select, #controls button').forEach(el => {
if (el.id !== 'exportVideoButton') { // Don't disable the export button itself
el.disabled = disabled;
}
});
// Re-enable/disable based on state
if (!disabled) {
updateThresholdControlsState();
}
}
// Generic handler for controls that trigger a re-process
function addProcessTrigger(element, valueUpdater) {
element.addEventListener('input', (e) => {
valueUpdater(e);
if (currentMedia.type === 'image') {
processFrame();
} else if (currentMedia.type === 'video' && !isVideoPlaying) {
// Update static frame if video is paused
processFrame();
}
});
}
addProcessTrigger(noiseMagnitudeSlider, (e) => {
currentNoiseMagnitude = parseInt(e.target.value);
noiseMagnitudeValueSpan.textContent = currentNoiseMagnitude;
});
addProcessTrigger(thresholdSlider, (e) => {
currentThreshold = parseInt(e.target.value);
thresholdValueSpan.textContent = currentThreshold;
});
addProcessTrigger(saturationSlider, (e) => {
currentSaturation = parseInt(e.target.value);
saturationValueSpan.textContent = currentSaturation;
});
addProcessTrigger(posterizationSlider, (e) => {
currentPosterizationLevels = parseInt(e.target.value);
posterizationValueSpan.textContent = currentPosterizationLevels;
});
addProcessTrigger(resolutionSlider, (e) => {
currentResolution = parseInt(e.target.value);
resolutionValueSpan.textContent = `${currentResolution}%`;
// Need to resize processed canvas
const scaledWidth = Math.floor(currentMedia.width * (currentResolution / 100));
const scaledHeight = Math.floor(currentMedia.height * (currentResolution / 100));
// Keep aspect ratio of the *output canvas* consistent with the *processed* aspect ratio
processedCanvas.width = scaledWidth;
processedCanvas.height = scaledHeight;
// The CSS will scale this up to fit the box
});
addProcessTrigger(matrixSizeSelect, (e) => {
currentMatrixSize = parseInt(e.target.value);
currentMatrix = BAYER_MATRICES[currentMatrixSize];
});
// Bitmap Mode Toggle
toggleBitmapModeButton.addEventListener('click', () => {
isBitmapMode = !isBitmapMode;
toggleBitmapModeButton.textContent = isBitmapMode ? 'Bitmap Mode: ON' : 'Bitmap Mode: OFF';
toggleBitmapModeButton.classList.toggle('toggle-on', isBitmapMode);
toggleBitmapModeButton.classList.toggle('toggle-off', !isBitmapMode);
updateThresholdControlsState();
if (currentMedia.type === 'image' || !isVideoPlaying) processFrame();
});
// Dither Type Toggle
ditherTypeButton.addEventListener('click', () => {
ditherType = ditherType === 'random' ? 'bayer' : 'random';
updateThresholdControlsState();
if (currentMedia.type === 'image' || !isVideoPlaying) processFrame();
});
// Frame Export
exportImageButton.addEventListener('click', () => {
if (!currentMedia.element) return;
// Ensure the processed canvas is at the correct resolution for export
const exportWidth = Math.floor(currentMedia.width * (currentResolution / 100));
const exportHeight = Math.floor(currentMedia.height * (currentResolution / 100));
// Check if canvas size needs update
if (processedCanvas.width !== exportWidth || processedCanvas.height !== exportHeight) {
processedCanvas.width = exportWidth;
processedCanvas.height = exportHeight;
processFrame(); // Run one more process pass at export res
}
const dataURL = processedCanvas.toDataURL('image/png');