Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
4785 lines (4609 loc) · 149 KB
/
Copy pathscript.js
File metadata and controls
4785 lines (4609 loc) · 149 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
/**
* pic2mkcapix – Picture to MakeCode Arcade / Pixel-Art Converter
* ==============================================================
* This file is the modular JavaScript engine.
*
* Structure (preserve these section markers for future maintenance):
* //script/init/htm.js – page skeleton HTML injection
* //script/init/error.js – global error popup helpers (cleaned)
* //script/init/section.js – constants, dither matrices, palette presets
* //script/engine/matrix/cpu.js – CPU quantize + dither + ASCII
* //script/engine/matrix/gpu.js – WebGL accelerated path
* //script/engine/media/decoder.js – GIF / APNG / WebP / WebM decoder
* //script/engine/media/encoder.js – GIF / PNG encoder
* //script/main.js – UI, palette manager, conversion pipeline
*
* Humanization notes (this revision):
* - Replaced minified literals (!0 → true, !1 → false, void 0 → undefined)
* - Expanded the error-handling module with descriptive names
* - Kept original section markers exactly as required
* - Tab indentation and overall control flow preserved
* - Fixed double-quote newline bugs that caused SyntaxError
*
* Further contributions welcome: rename remaining short locals in
* decoder / encoder / main / cpu when touching those areas.
*/
//script/init/htm.js
document.querySelector("body div#preload").remove();
document.querySelector("body").insertAdjacentHTML(
"beforeend",
`
<div id="page-loader">
<h1>Convert Picture to Makecode-arcade or Pixel-art</h1>
<br />
<div class="loader-content">
<progress class="loader-progress" id="page-load-progress" max="100" value="0">0%</progress>
<div class="loader-text">Loading Page...</div>
<div class="loader-sub">Preparing Picture Converter Engine.</div>
</div>
</div>
<div id="info">
<p>
Upload your image to convert it into pixel art — with optional ASCII
output and a fully customizable dynamic color palette.
</p>
<hr />
<dl>
<dt>note for makecode arcade user:</dt>
<dd>15 palette colors for <i>makecode arcade</i> compatibility.</dd>
<dt>note for anti-ai user:</dt>
<dd>this project use <i>ai</i> to make project but you can fork this to make clean-code-reverse-engineer as human code(if possible) sorry. :(</dd>
</dl>
<div id="notification-popup-overlay">
<div class="popup-error-card">
<div class="popup-header">
<span>⚠️ PROCESS EXCEPTION DETECTED</span
><button class="popup-close-btn" id="popup-close-btn">
DISMISS
</button>
</div>
<div class="popup-body">
<p style="margin: 0 0 6px 0">
<strong>Error Type:</strong
><span id="popup-err-type">N/A</span>
</p>
<p style="margin: 0 0 10px 0">
<strong>Message:</strong
><span id="popup-err-message">N/A</span>
</p>
<div class="popup-controls">
<button class="popup-toggle-btn" id="btn-toggle-log">
Show Full Log ▼
</button>
</div>
<textarea id="popup-err-stack" readonly>
No trace details available.</textarea>
</div>
</div>
</div>
</div>
<div id="status">System Status: Awaiting Image Upload Asset...</div>
<form action="#" method="POST" id="parameters">
<span><label width="100%">Select Target Source Image:</label><input type="file" id="file" accept="image/*,video/webm,video/mp4,video/x-matroska,video/quicktime,.webm,.mp4,.mkv,.mov" /></span>
<div id="navbar">
<button class="size-settings">1.Size setting</button>
<button class="processing-option">2.Process Edit</button>
<button class="palette-manager">3.Palette Edit</button>
</div>
<div class="settings-group">
<div class="half">
<div id="size-settings" class="size-settings-grid">
<label class="full-row"
><input
type="radio"
name="resize"
id="original-size"
checked
disabled
/>
Original Size</label
><label class="full-row"
><input
type="radio"
name="resize"
id="full-width"
disabled
/>
Fix Sprite Width (160px)</label
><label class="full-row"
><input
type="radio"
name="resize"
id="full-height"
disabled
/>
Fix Sprite Height (120px)</label
><label class="full-row"
><input type="radio" name="resize" id="scale" disabled />
Custom Size</label
><label style="padding-left: 20px">Scale Factor:</label
><input
type="number"
id="factor"
value=""
step="0.1e-9"
min="0.1e-9"
max="8"
disabled
/><label
class="full-row"
style="
border-top: 1px solid #222;
margin-top: 8px;
padding-top: 12px;
"
><input type="checkbox" id="ratio" checked disabled /> Keep
Original Aspect Ratio </label
><label>Output Width (px):</label
><input
type="number"
id="width"
value=""
disabled
class="custom"
/><label>Output Height (px):</label
><input
type="number"
id="height"
value=""
disabled
class="custom"
/>
</div>
<div id="processing-option" class="dropdown-selection-group hidden">
<label for="dithering-option" class="dropdown-label"
>Dithering Options:</label
><select id="dithering-option" class="custom-dropdown">
<option value="solid" selected>Solid (No Dithering)</option>
<optgroup label="Ordered Dithering (Bayer)">
<option value="bayer4">Bayer Matrix 4×4</option>
<option value="bayer8">Bayer Matrix 8×8</option>
<option value="bayer16">Bayer Matrix 16×16</option>
</optgroup>
<optgroup label="Blue Noise Dithering">
<option value="blue8">Blue Noise 8×8</option>
<option value="blue16">Blue Noise 16×16</option>
<option value="blue32">Blue Noise 32×32</option>
</optgroup>
<optgroup label="Error Diffusion" id="optgroup-error">
<option value="error">
Floyd-Steinberg Error Diffusion
</option>
</optgroup>
</select>
<hr />
<label for="render-option" class="dropdown-label">Render Buffer:</label>
<select id="render-option" class="custom-dropdown">
<option value="bitmap" selected>Bitmap (full image)</option>
<option value="auto-tiled">Auto (2^n)×(2^n) Tiled</option>
<option value="custom-tiled">Custom (2^n)×(2^n) Tiled</option>
<option value="8x8tiled">8×8 Tiled Buffer</option>
<option value="16x16tiled">16×16 Tiled Buffer</option>
<option value="32x32tiled">32×32 Tiled Buffer</option>
<option value="64x64tiled">64×64 Tiled Buffer</option>
<option value="128x128tiled">128×128 Tiled Buffer</option>
<option value="256x256tiled">256×256 Tiled Buffer</option>
</select>
<div id="render-custom-options" style="display: none; margin-top: 8px">
<label for="render-tile-power" class="dropdown-label">Custom tile exponent (2^n):</label>
<input type="number" id="render-tile-power" class="custom" min="3" max="12" value="7" step="1" />
<small id="render-tile-size-label">Tile size: 128×128</small>
</div>
<hr />
<div id="makecode-options-group">
<label class="full-row">
<input type="checkbox" id="makecode-enable" checked /> Enable MakeCode String Output
</label>
<small id="makecode-auto-status">MakeCode output is enabled for palettes up to 15 colors.</small>
</div>
<hr />
<div id="ascii-options-group">
<label class="dropdown-label"
>ASCII Art Output Options:</label
><label class="full-row"
><input type="checkbox" id="ascii-enable" /> Enable
ASCII Output (in Output Tab)
</label>
<div
id="ascii-sub-options"
style="display: none; margin-top: 8px"
>
<label
for="ascii-charset-select"
class="dropdown-label"
style="margin-top: 8px"
>ASCII Character Set:</label
><select
id="ascii-charset-select"
class="custom-dropdown"
>
<option value="standard">
Standard (█▓▒░ + Symbols)
</option>
<option value="block">
Block Only (█ ▓ ▒ ░ ·)
</option>
<option value="alphanumeric">
Alphanumeric (A-Z, 0-9)
</option>
<option value="minimal">
Minimal ( .:-=+*#%@)
</option>
<option value="dense">
Dense (Full Printable ASCII)
</option></select
><label for="ascii-width-input" style="margin-top: 8px"
>ASCII Columns:</label
><input
type="number"
id="ascii-width-input"
value="80"
min="10"
max="400"
class="custom"
/>
</div>
</div>
<hr />
<label for="engine-select" class="dropdown-label">Processing Engine:</label
><select id="engine-select" class="custom-dropdown">
<option value="script" selected>script</option>
<option value="webgl">webgl</option>
<option value="wasm">wasm</option>
</select>
<small id="wasm-status">WASM is optional; JavaScript fallback is active.</small>
</div>
</div>
<div id="palette-manager" class="colorboard hidden">
<div class="palette-loader-container">
<label for="predefined-palette-select"
>Select Predefined Palette:</label
><select
id="predefined-palette-select"
class="custom-dropdown"
style="margin-bottom: 10px"
>
<option value="custom" class="hidden">Custom</option>
<optgroup label="Makecode Arcade">
<option value="arcade" selected>Arcade</option>
<option value="matte">Matte</option>
<option value="pastel">Pastel</option>
<option value="sweet">Sweet</option>
<option value="poke">Poke</option>
<option value="adventure">Adventure</option>
<option value="diy">DIY</option>
<option value="adafruit">Adafruit</option>
<option value="still_life">Still Life</option>
<option value="steam_punk">Steam Punk</option>
<option value="grayscale">Grayscale</option>
</optgroup></select
><label>Import Custom Palette File (.txt, .hex):</label
><input
type="file"
id="palette-file-reader"
accept=".txt,.hex"
/>
</div>
<div class="palette-header-row">
<span
style="font-weight: bold; color: rgb(255, 240, 157)"
id="palette-count-label"
>Active Color Registers (1–15):</span
>
<div class="palette-action-btns">
<button
type="button"
id="palette-add-btn"
class="palette-slot-btn"
title="Add Color Slot"
>
+ Add Slot</button
><button
type="button"
id="palette-remove-btn"
class="palette-slot-btn"
title="Remove Last Slot"
>
- Remove
</button>
</div>
</div>
<div class="palette-range-controls">
<label for="palette-limit">Palette capacity (2–65535):</label>
<input type="number" id="palette-limit" class="custom" min="2" max="65535" value="15" />
<button type="button" id="palette-prev-btn" class="palette-slot-btn">Previous</button>
<button type="button" id="palette-next-btn" class="palette-slot-btn">Next</button>
<span id="palette-range-label">Colors 1–10</span>
</div>
<div class="colorpad" id="colorpad">
<div class="color-pair">
<label>Color 1</label
><input type="color" value="#ffffff" /><input
type="text"
class="colortext"
value="#ffffff"
/>
</div>
<div class="color-pair">
<label>Color 2</label
><input type="color" value="#ff2121" /><input
type="text"
class="colortext"
value="#ff2121"
/>
</div>
<div class="color-pair">
<label>Color 3</label
><input type="color" value="#ff93c4" /><input
type="text"
class="colortext"
value="#ff93c4"
/>
</div>
<div class="color-pair">
<label>Color 4</label
><input type="color" value="#ff8135" /><input
type="text"
class="colortext"
value="#ff8135"
/>
</div>
<div class="color-pair">
<label>Color 5</label
><input type="color" value="#fff609" /><input
type="text"
class="colortext"
value="#fff609"
/>
</div>
<div class="color-pair">
<label>Color 6</label
><input type="color" value="#249ca3" /><input
type="text"
class="colortext"
value="#249ca3"
/>
</div>
<div class="color-pair">
<label>Color 7</label
><input type="color" value="#78dc52" /><input
type="text"
class="colortext"
value="#78dc52"
/>
</div>
<div class="color-pair">
<label>Color 8</label
><input type="color" value="#003fad" /><input
type="text"
class="colortext"
value="#003fad"
/>
</div>
<div class="color-pair">
<label>Color 9</label
><input type="color" value="#87f2ff" /><input
type="text"
class="colortext"
value="#87f2ff"
/>
</div>
<div class="color-pair">
<label>Color 10</label
><input type="color" value="#8e2ec4" /><input
type="text"
class="colortext"
value="#8e2ec4"
/>
</div>
<div class="color-pair">
<label>Color 11</label
><input type="color" value="#a4839f" /><input
type="text"
class="colortext"
value="#a4839f"
/>
</div>
<div class="color-pair">
<label>Color 12</label
><input type="color" value="#5c406c" /><input
type="text"
class="colortext"
value="#5c406c"
/>
</div>
<div class="color-pair">
<label>Color 13</label
><input type="color" value="#e5cdc4" /><input
type="text"
class="colortext"
value="#e5cdc4"
/>
</div>
<div class="color-pair">
<label>Color 14</label
><input type="color" value="#91463d" /><input
type="text"
class="colortext"
value="#91463d"
/>
</div>
<div class="color-pair">
<label>Color 15</label
><input type="color" value="#000000" /><input
type="text"
class="colortext"
value="#000000"
/>
</div>
</div>
</div>
</div>
<div class="action-buttons">
<button id="run" type="submit" disabled>Convert Image</button
><button id="reset" type="reset">Reset Form</button
><button id="copy" type="button" disabled>Download Text</button
><button id="download" type="button" disabled>Download Image</button>
</div>
</form>
<div class="image-preview-container">
<div class="preview-box">
<h3>Original Input</h3>
<div id="original-res" class="resolution-info">Size: 0 x 0</div>
<div id="original-preview-zone"></div>
</div>
<div class="preview-box">
<h3>Canvas Output</h3>
<div id="canvas-res" class="resolution-info">Size: 0 x 0</div>
<div class="output">
<img
id="output-image"
alt="Processed image output"
aria-hidden="true"
style="
width: 100%;
height: auto;
object-fit: contain;
image-rendering: pixelated;
background-image:
linear-gradient(45deg, #222 25%, transparent 25%),
linear-gradient(-45deg, #222 25%, transparent 25%),
linear-gradient(45deg, transparent 75%, #222 75%),
linear-gradient(-45deg, transparent 75%, #222 75%);
background-size: 10px 10px;
background-position:
0 0,
0 5px,
5px -5px,
-5px 0px;
background-color: #151515;
padding: 5px;
margin: 0 auto;
display: block;
visibility: hidden;
"
/>
</div>
</div>
</div>
<canvas
id="process-canvas"
aria-hidden="true"
style="
display: block;
position: absolute;
margin-left: -100000px;
margin-top: -100000px;
width: 1px;
height: 1px;
max-width: 1px;
max-height: 1px;
box-sizing: border-box;
padding: 0;
opacity: 0;
visibility: hidden;
pointer-events: none;
"
></canvas>
<div class="output-tabs" id="output-tabs">
<button class="tab-btn active" data-tab="pixelart">
PixelArt / MakeCode String</button
><button class="tab-btn" data-tab="ascii" id="ascii-tab-btn" disabled>
ASCII Output
</button>
</div>
<div id="tab-pixelart" class="tab-panel active">
<textarea
id="output"
placeholder="The pixel art hex matrix string will be generated here..."
readonly
></textarea>
</div>
<div id="tab-ascii" class="tab-panel">
<textarea
id="ascii-output"
placeholder="ASCII art output will appear here after conversion with ASCII mode enabled..."
readonly
></textarea>
</div>
`,
);
//end
//script/init/error.js
(function () {
const getElement = (id) => document.getElementById(id);
const overlay = getElement("notification-popup-overlay");
function displayErrorPopup(errorType, message, stackTrace) {
getElement("popup-err-type").textContent = errorType || "Runtime Error";
getElement("popup-err-message").textContent = message || "Unknown error.";
getElement("popup-err-stack").value = stackTrace || "No call stack trace records.";
getElement("popup-err-stack").style.display = "none";
getElement("btn-toggle-log").textContent = "Show Full Log ▼";
if (overlay) {
overlay.style.display = "block";
}
}
function toggleErrorLog() {
const stackTextarea = getElement("popup-err-stack");
const isCurrentlyHidden =
stackTextarea.style.display === "none" || !stackTextarea.style.display;
stackTextarea.style.display = isCurrentlyHidden ? "block" : "none";
getElement("btn-toggle-log").textContent = isCurrentlyHidden
? "Hide Full Log ▲"
: "Show Full Log ▼";
}
function closeErrorPopup() {
if (overlay) {
overlay.style.display = "none";
}
}
// Expose to global scope for use by the rest of the application
window.displayErrorPopup = displayErrorPopup;
window.toggleErrorLog = toggleErrorLog;
window.closeErrorPopup = closeErrorPopup;
getElement("popup-close-btn")?.addEventListener("click", closeErrorPopup);
getElement("btn-toggle-log")?.addEventListener("click", toggleErrorLog);
window.addEventListener("error", (event) => {
displayErrorPopup(
"Uncaught Runtime Exception",
event.message,
event.error?.stack
);
});
window.addEventListener("unhandledrejection", (event) => {
const reason =
event.reason instanceof Error
? event.reason
: new Error(String(event.reason));
displayErrorPopup(
"Unhandled Promise Rejection",
reason.message,
reason.stack
);
});
})();
//end
//script/init/section.js
const CHAR_TABLE = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/#",
BAYER4_SRC = [0, 8, 2, 10, 12, 4, 14, 6, 3, 11, 1, 9, 15, 7, 13, 5],
BAYER8_SRC = [
0, 32, 8, 40, 2, 34, 10, 42, 48, 16, 56, 24, 50, 18, 58, 26, 12, 44, 4, 36, 14, 46, 6, 38,
60, 28, 52, 20, 62, 30, 54, 22, 3, 35, 11, 43, 1, 33, 9, 41, 51, 19, 59, 27, 49, 17, 57, 25,
15, 47, 7, 39, 13, 45, 5, 37, 63, 31, 55, 23, 61, 29, 53, 21,
],
BAYER16_SRC = [
0, 128, 32, 160, 8, 136, 40, 168, 2, 130, 34, 162, 10, 138, 42, 170, 192, 64, 224, 96, 200,
72, 232, 104, 194, 66, 226, 98, 202, 74, 234, 106, 48, 176, 16, 144, 56, 184, 24, 152, 50,
178, 18, 146, 58, 186, 26, 154, 240, 112, 208, 80, 248, 120, 216, 88, 242, 114, 210, 82,
250, 122, 218, 90, 12, 140, 44, 172, 4, 132, 36, 164, 14, 142, 46, 174, 6, 134, 38, 166,
204, 76, 236, 108, 196, 68, 228, 100, 206, 78, 238, 110, 198, 70, 230, 102, 60, 188, 28,
156, 52, 180, 20, 148, 62, 190, 30, 158, 54, 182, 22, 150, 252, 124, 220, 92, 244, 116, 212,
84, 254, 126, 222, 94, 246, 118, 214, 86, 3, 131, 35, 163, 11, 139, 43, 171, 1, 129, 33,
161, 9, 137, 41, 169, 195, 67, 227, 99, 203, 75, 235, 107, 193, 65, 225, 97, 201, 73, 233,
105, 51, 179, 19, 147, 59, 187, 27, 155, 49, 177, 17, 145, 57, 185, 25, 153, 243, 115, 211,
83, 251, 123, 219, 91, 241, 113, 209, 81, 249, 121, 217, 89, 15, 143, 47, 175, 7, 135, 39,
167, 13, 141, 45, 173, 5, 133, 37, 165, 207, 79, 239, 111, 199, 71, 231, 103, 205, 77, 237,
109, 197, 69, 229, 101, 63, 191, 31, 159, 55, 183, 23, 151, 61, 189, 29, 157, 53, 181, 21,
149, 255, 127, 223, 95, 247, 119, 215, 87, 253, 125, 221, 93, 245, 117, 213, 85,
],
BLUE8_SRC = [
141, 186, 149, 52, 101, 182, 137, 105, 170, 113, 206, 20, 242, 153, 202, 246, 64, 222, 80,
32, 117, 12, 89, 40, 16, 48, 161, 210, 178, 60, 24, 0, 230, 93, 250, 145, 72, 234, 190, 109,
56, 4, 121, 198, 157, 125, 36, 214, 68, 8, 44, 174, 133, 255, 85, 28, 226, 97, 238, 76, 218,
129, 194, 165,
],
BLUE16_SRC = [
227, 141, 129, 236, 160, 134, 71, 30, 162, 235, 99, 209, 108, 192, 94, 155, 78, 190, 153,
53, 86, 224, 178, 148, 93, 42, 183, 144, 26, 73, 10, 41, 139, 3, 248, 115, 200, 13, 63, 249,
170, 126, 213, 56, 166, 232, 118, 204, 59, 220, 172, 112, 33, 154, 245, 88, 195, 14, 48,
104, 185, 6, 150, 80, 131, 241, 96, 21, 180, 70, 137, 22, 237, 84, 38, 202, 1, 67, 222, 122,
252, 46, 164, 128, 255, 107, 52, 191, 143, 218, 11, 158, 91, 176, 40, 207, 17, 110, 74, 233,
45, 198, 25, 120, 60, 247, 101, 29, 134, 50, 240, 168, 82, 197, 5, 187, 159, 77, 212, 35,
173, 8, 114, 69, 226, 147, 19, 94, 234, 136, 58, 181, 14, 92, 250, 44, 211, 132, 54, 193,
12, 86, 223, 105, 49, 171, 109, 28, 243, 125, 68, 156, 4, 182, 97, 36, 254, 151, 64, 216,
189, 31, 238, 83, 167, 47, 206, 116, 244, 17, 146, 79, 199, 42, 117, 230, 2, 161, 100, 19,
140, 221, 57, 185, 102, 72, 215, 10, 127, 253, 89, 178, 75, 208, 43, 251, 111, 6, 163, 34,
188, 129, 61, 239, 24, 165, 55, 196, 145, 27, 124, 95, 230, 152, 81, 246, 15, 113, 48, 175,
204, 37, 142, 9, 219, 66, 194, 18, 76, 135, 23, 106, 231, 51, 183, 7, 121, 247, 103, 214,
62, 169, 0, 149, 210, 87, 201, 39, 168, 98, 33, 158, 20, 228, 141, 184,
],
BLUE32_SRC = [
154, 181, 42, 20, 79, 34, 109, 18, 95, 135, 190, 84, 202, 128, 190, 68, 34, 1, 23, 84, 237,
70, 225, 144, 237, 165, 49, 102, 237, 109, 186, 70, 9, 116, 220, 248, 163, 253, 147, 212,
55, 245, 14, 167, 38, 57, 11, 233, 152, 197, 139, 174, 54, 122, 14, 92, 63, 13, 197, 180,
26, 142, 59, 217, 131, 204, 88, 52, 137, 101, 28, 194, 230, 119, 176, 244, 96, 214, 156, 46,
111, 81, 219, 32, 241, 160, 206, 132, 227, 120, 251, 89, 194, 5, 103, 168, 36, 173, 15, 229,
64, 185, 239, 73, 4, 50, 21, 126, 35, 78, 191, 138, 28, 149, 64, 113, 17, 72, 38, 177, 48,
155, 33, 67, 218, 161, 78, 21, 243, 105, 199, 58, 222, 12, 158, 91, 169, 201, 143, 59, 249,
17, 106, 223, 182, 95, 236, 6, 151, 100, 211, 10, 83, 201, 123, 242, 12, 133, 230, 47, 80,
41, 250, 124, 39, 171, 48, 134, 255, 110, 82, 188, 40, 129, 71, 195, 53, 164, 20, 127, 189,
44, 136, 254, 29, 172, 52, 107, 187, 94, 25, 209, 216, 145, 27, 183, 96, 235, 77, 19, 61,
226, 151, 8, 166, 93, 247, 31, 115, 75, 198, 58, 232, 81, 19, 117, 223, 41, 158, 76, 255,
57, 150, 114, 62, 232, 112, 7, 213, 53, 141, 203, 125, 37, 104, 197, 49, 179, 13, 85, 240,
46, 130, 243, 15, 169, 62, 146, 96, 181, 8, 199, 35, 126, 205, 88, 175, 2, 193, 66, 156, 30,
102, 247, 86, 173, 21, 68, 157, 221, 108, 194, 60, 153, 11, 87, 184, 128, 236, 51, 134, 220,
69, 148, 242, 16, 44, 179, 118, 229, 47, 139, 11, 224, 67, 181, 42, 250, 145, 33, 84, 6,
238, 74, 165, 101, 217, 43, 99, 24, 163, 79, 11, 252, 106, 31, 171, 83, 197, 121, 54, 161,
100, 255, 74, 189, 116, 5, 159, 97, 54, 210, 127, 45, 161, 29, 212, 56, 140, 191, 70, 208,
45, 190, 147, 60, 183, 224, 55, 140, 10, 236, 207, 23, 132, 81, 214, 46, 152, 231, 19, 136,
77, 186, 253, 118, 203, 52, 34, 123, 226, 5, 155, 37, 114, 234, 28, 95, 215, 40, 129, 249,
72, 165, 38, 187, 14, 172, 60, 129, 245, 108, 63, 204, 28, 92, 144, 67, 12, 176, 247, 91,
48, 170, 102, 250, 84, 6, 173, 131, 51, 199, 22, 104, 188, 53, 143, 68, 228, 93, 201, 38,
17, 78, 168, 113, 242, 56, 185, 230, 89, 41, 158, 22, 135, 59, 218, 141, 193, 122, 57, 246,
110, 80, 154, 37, 231, 98, 251, 122, 49, 195, 8, 166, 220, 44, 137, 2, 75, 149, 23, 105,
254, 147, 78, 204, 33, 116, 63, 180, 29, 159, 100, 21, 164, 235, 48, 213, 117, 7, 16, 177,
106, 34, 157, 85, 54, 192, 117, 234, 43, 162, 198, 61, 128, 19, 187, 49, 225, 94, 213, 46,
126, 205, 73, 145, 30, 112, 191, 62, 136, 219, 182, 65, 240, 13, 111, 203, 26, 91, 14, 179,
101, 38, 81, 172, 56, 221, 14, 168, 77, 151, 35, 108, 244, 17, 156, 88, 207, 42, 125, 254,
20, 96, 44, 209, 124, 50, 173, 7, 148, 63, 246, 120, 53, 209, 28, 143, 95, 34, 192, 115, 58,
230, 12, 185, 71, 138, 50, 241, 169, 28, 181, 74, 159, 46, 233, 28, 158, 86, 197, 36, 225,
131, 72, 19, 160, 247, 110, 64, 187, 252, 106, 44, 199, 82, 167, 54, 219, 97, 10, 196, 121,
64, 233, 8, 143, 111, 102, 55, 236, 19, 141, 250, 94, 43, 188, 83, 230, 15, 76, 155, 42,
128, 73, 161, 27, 113, 237, 129, 32, 176, 85, 218, 47, 153, 102, 194, 57, 248, 167, 78, 217,
60, 32, 115, 177, 57, 204, 134, 48, 171, 222, 9, 198, 61, 214, 136, 93, 181, 20, 63, 150,
242, 38, 127, 255, 70, 137, 29, 226, 84, 13, 194, 110, 147, 229, 68, 21, 165, 100, 38, 116,
195, 52, 140, 24, 83, 47, 172, 38, 248, 105, 200, 89, 25, 174, 58, 211, 95, 19, 166, 51,
123, 145, 42, 255, 92, 5, 183, 109, 237, 79, 155, 253, 69, 163, 237, 117, 45, 156, 29, 119,
68, 214, 48, 133, 61, 222, 145, 35, 189, 245, 108, 200, 75, 35, 172, 79, 21, 161, 54, 128,
46, 214, 12, 88, 146, 31, 102, 189, 254, 97, 184, 52, 227, 11, 163, 78, 236, 103, 30, 152,
81, 17, 132, 62, 238, 204, 118, 53, 198, 113, 244, 33, 191, 152, 67, 180, 41, 207, 74, 136,
21, 60, 141, 249, 16, 94, 128, 44, 191, 56, 220, 128, 194, 90, 247, 43, 156, 11, 230, 141,
37, 86, 18, 156, 96, 25, 123, 201, 58, 149, 232, 57, 166, 205, 79, 34, 117, 173, 55, 210,
123, 167, 12, 71, 185, 54, 116, 203, 31, 99, 64, 189, 246, 169, 76, 210, 139, 222, 49, 85,
173, 40, 115, 253, 88, 46, 151, 223, 62, 139, 2, 80, 158, 229, 97, 146, 38, 219, 177, 9, 68,
152, 25, 107, 14, 58, 193, 45, 235, 162, 106, 37, 154, 245, 18, 131, 72, 186, 101, 54, 193,
28, 115, 237, 49, 126, 213, 65, 251, 133, 40, 95, 182, 219, 82, 175, 128, 231, 3, 120, 71,
14, 199, 126, 61, 96, 179, 43, 210, 129, 17, 164, 88, 206, 70, 152, 34, 181, 57, 112, 29,
158, 224, 77, 144, 48, 201, 40, 91, 165, 134, 51, 187, 250, 92, 144, 28, 68, 151, 234, 57,
242, 125, 73, 195, 42, 129, 223, 91, 20, 138, 246, 84, 191, 53, 121, 0, 116, 56, 213, 30,
149, 80, 205, 110, 53, 168, 35, 113, 202, 84, 19, 160, 103, 48, 212, 36, 154, 247, 66, 175,
108, 32, 170, 63, 235, 147, 26, 198, 67, 180, 12, 241, 104, 226, 63, 19, 143, 218, 76, 183,
47, 132, 251, 95, 38, 171, 59, 140, 218, 101, 45, 133, 241, 155, 87, 199, 44, 110, 179, 250,
],
BAYER4 = new Uint8Array(BAYER4_SRC),
BAYER8 = new Uint8Array(BAYER8_SRC),
BAYER16 = new Uint8Array(BAYER16_SRC),
BLUE8 = new Uint8Array(BLUE8_SRC),
BLUE16 = new Uint8Array(BLUE16_SRC),
BLUE32 = new Uint8Array(BLUE32_SRC),
QUAD_VERTICES = new Float32Array([-1, -1, 0, 0, 1, -1, 1, 0, -1, 1, 0, 1, 1, 1, 1, 1]),
BAYER4_F32 = new Float32Array(BAYER4_SRC.map((e) => e / 16)),
BAYER8_F32 = new Float32Array(BAYER8_SRC.map((e) => e / 64)),
BAYER16_F32 = new Float32Array(BAYER16_SRC.map((e) => e / 256));
function _floatMatrixToU8(e) {
const t = new Uint8Array(e.length);
for (let a = 0; a < e.length; a++) t[a] = Math.round(255 * e[a]);
return t;
}
const BAYER4_U8 = _floatMatrixToU8(BAYER4_F32),
BAYER8_U8 = _floatMatrixToU8(BAYER8_F32),
BAYER16_U8 = _floatMatrixToU8(BAYER16_F32),
BLUE8_U8 = new Uint8Array(BLUE8_SRC),
BLUE16_U8 = new Uint8Array(BLUE16_SRC),
BLUE32_U8 = new Uint8Array(BLUE32_SRC),
htmlLog = [];
let lastIndexMap = null,
lastW = 0,
lastH = 0,
animSource = null,
processedAnimation = null;
const mediaFileInput = document.getElementById("file"),
palettemediaFileInput = document.getElementById("palette-file-reader"),
predefinedPaletteSelect = document.getElementById("predefined-palette-select"),
ditheringSelect = document.getElementById("dithering-option"),
renderSelect = document.getElementById("render-option"),
renderCustomOptions = document.getElementById("render-custom-options"),
renderTilePowerInput = document.getElementById("render-tile-power"),
renderTileSizeLabel = document.getElementById("render-tile-size-label"),
engineSelect = document.getElementById("engine-select"),
wasmStatus = document.getElementById("wasm-status"),
makecodeEnableCheck = document.getElementById("makecode-enable"),
makecodeAutoStatus = document.getElementById("makecode-auto-status"),
asciiEnableCheck = document.getElementById("ascii-enable"),
asciiSubOptions = document.getElementById("ascii-sub-options"),
asciiCharsetSelect = document.getElementById("ascii-charset-select"),
asciiWidthInput = document.getElementById("ascii-width-input"),
asciiTabBtn = document.getElementById("ascii-tab-btn"),
asciiOutputTA = document.getElementById("ascii-output"),
runButton = document.getElementById("run"),
resetButton = document.getElementById("reset"),
downloadTextButton = document.getElementById("copy"),
downloadMediaButton = document.getElementById("download"),
statusDiv = document.getElementById("status"),
textarea = document.getElementById("output"),
previewContainer = document.querySelector(".image-preview-container"),
outputImage = document.getElementById("output-image"),
outputVideo = null,
canvas = document.getElementById("process-canvas"),
ctx = canvas.getContext("2d", {
willReadFrequently: !0,
}),
inputWidth = document.getElementById("width"),
inputHeight = document.getElementById("height"),
inputFactor = document.getElementById("factor"),
inputRatio = document.getElementById("ratio"),
parametersForm = document.getElementById("parameters"),
colorpad = document.getElementById("colorpad"),
paletteAddBtn = document.getElementById("palette-add-btn"),
paletteRemoveBtn = document.getElementById("palette-remove-btn"),
paletteCountLbl = document.getElementById("palette-count-label"),
paletteLimitInput = document.getElementById("palette-limit"),
palettePrevBtn = document.getElementById("palette-prev-btn"),
paletteNextBtn = document.getElementById("palette-next-btn"),
paletteRangeLabel = document.getElementById("palette-range-label");
let originalImageSize = {
width: 0,
height: 0,
},
originalMimeType = "image/png",
sourceExtension = "png",
canvasName = "pic2pa.png",
outputBlob = null,
outputObjectUrl = null,
originalPreviewObjectUrl = null,
rgbPalette = [],
uploadedFileBuffer = null,
isTextProcessing = !1,
stopTextProcessingFlag = !1,
curResizeMode = "=",
nextResizeMode = "-";
const predefinedPalettes = {
arcade: [
"#ffffff",
"#ff2121",
"#ff93c4",
"#ff8135",
"#fff609",
"#249ca3",
"#78dc52",
"#003fad",
"#87f2ff",
"#8e2ec4",
"#a4839f",
"#5c406c",
"#e5cdc4",
"#91463d",
"#000000",
],
matte: [
"#ffffff",
"#ff455a",
"#ffaebc",
"#ffab3c",
"#fffa40",
"#278c3f",
"#37e650",
"#5e70d4",
"#99d5e5",
"#a845ff",
"#cfa4ff",
"#7a4a8b",
"#ffcca4",
"#bd7f47",
"#41344e",
],
pastel: [
"#ffffff",
"#ffb0a1",
"#ffd6ec",
"#ffdca1",
"#fffda1",
"#a1ffe1",
"#baffc1",
"#a1d6ff",
"#e1ffff",
"#d6a1ff",
"#eaccff",
"#bdb0d6",
"#fff0e1",
"#d6a1a1",
"#696a6a",
],
sweet: [
"#ffffff",
"#803d41",
"#9ad46a",
"#eb8b4a",
"#f6d86e",
"#18544a",
"#31a477",
"#365f91",
"#6bd0ff",
"#653780",
"#9f7bb1",
"#d6b8c0",
"#e7d7c1",
"#ac896e",
"#4f455a",
],
poke: [
"#ffffff",
"#e4595d",
"#f7a171",
"#fced8c",
"#69d8af",
"#71aa6a",
"#2c6eb7",
"#5196d8",
"#8aa7cc",
"#b070cc",
"#dea3ea",
"#ace6a2",
"#e7ccae",
"#9a6d5f",
"#454545",
],
adventure: [
"#ffffff",
"#e9d4a9",
"#c57e7d",
"#a74e5a",
"#f8ae49",
"#9d9d5a",
"#557d4a",
"#0f4a6d",
"#3b83a1",
"#4d5061",
"#6e81a1",
"#a1acbd",
"#e7e7e7",
"#714a47",
"#1c1f21",
],
diy: [
"#ffffff",
"#ff0000",
"#ff99aa",
"#ffcc00",
"#ffff00",
"#00ff00",
"#00cc00",
"#000000",
"#00ffff",
"#aa00ff",
"#cc99ff",
"#aaaaaa",
"#eebbaa",
"#884400",
"#000000",
],
adafruit: [
"#ffffff",
"#ff0000",
"#ff5500",
"#ffaa00",
"#ffff00",
"#00ff00",
"#00aa55",
"#000000",
"#00aaff",
"#aa00ff",
"#ff00ff",
"#aaaaaa",
"#555555",
"#ff55aa",
"#000000",
],
still_life: [
"#ffffff",
"#9be2de",
"#ff6f5a",
"#e0946a",
"#e8c466",
"#adcdd5",
"#69b477",
"#54818e",
"#61a4c4",
"#9d94d1",
"#6b5a83",
"#8d796e",
"#c7ae9e",
"#706059",
"#3d3a4f",
],
steam_punk: [
"#ffffff",
"#b4dad6",
"#3b3740",
"#664d49",
"#9f6751",
"#737156",
"#9f0866",
"#647d87",
"#8aa1ab",
"#7d7187",
"#a392a5",
"#bdbdc5",
"#e4e7ea",
"#a59487",
"#59555a",
],
grayscale: [
"#ffffff",
"#f7f7f7",
"#e1e1e1",
"#cccccc",
"#b8b8b8",
"#a3a3a3",
"#8e8e8e",
"#7a7a7a",
"#666666",
"#515151",
"#3d3d3d",
"#292929",
"#141414",
"#000000",
"#000000",
],
};
//end
//script/engine/matrix/cpu.js
const clamp = (t) => (t < 0 ? 0 : t > 255 ? 255 : t),
COLOR_CACHE_LIMIT = 4096;
function createIndexMap(length) {
return rgbPalette.length <= 256 ? new Uint8Array(length) : new Uint16Array(length);
}
function findNearestColor(t, n, e, i) {
let o = 1 / 0,
a = 1;
for (let r = 1; r < i.length; r += 1) {
const l = i[r],
c = t - l.r,
s = n - l.g,
u = e - l.b,
d = c * c + s * s + u * u;
if (d < o && ((o = d), (a = r), 0 === d)) break;
}
return a;
}
function cachedFindNearest(t, n, e, i, o) {
const a = ((255 & t) << 16) | ((255 & n) << 8) | (255 & e);
let r = o.get(a);
return (
undefined !== r ||