-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJPEGVisualRepairTool.html
3243 lines (3163 loc) · 112 KB
/
JPEGVisualRepairTool.html
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>
<head>
<style>
table {border-collapse:collapse; border:1px solid; width:100%; table-layout:fixed}
th,td {padding:5px; border:1px solid;overflow-wrap: break-word;}
.loader {
border: 6px solid #f3f3f3; /* Light grey */
border-top: 6px solid #3498db; /* Blue */
border-radius: 50%;
width: 30px;
height: 30px;
animation: spin 1s linear infinite;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
</style>
</head>
<body style="margin:0;">
<canvas id="pictureCanvas" width="1" height="1" style="border:1px solid #d3d3d3;"></canvas>
<div id="tools" style="display:inline-block; width:250px; border:1px solid #d3d3d3; vertical-align: top;">
<b>JPEG visual repair tool v1.3a</b> <a href="#" onclick=infoDialog.showModal()>info</a><br>
<input type = "button" value = "Open..." onclick=document.getElementById('imgOpen').click()>
<input id = "imgOpen" type="file" style='visibility:hidden;width:0%' onchange="readFile(this)" multiple>
<button onclick=saveFile()>Save...</button>
<button onclick=newtab()>Open new tab</button><br>
View channel
<select onchange="setLayer()" name="" id="layer">
<option value="RGB" selected>RGB</option>
<option value="Y">Y</option>
<option value="C1">Cb</option>
<option value="C2">Cr</option>
<option value="RGB">RGB</option>
</select><br>
<div id="imgInfo"></div>
<div id="MCUn"></div>
<button onclick=imageInfoDialog.showModal()>Image info</button>
<button onclick=MCUinfoDialog.showModal()>MCU info</button>
<button onclick=fixColorDiff()>Fix colors</button><br>
<button onclick=backSearchDialog.showModal()>MCU back search</button>
<input type="Checkbox" id="shift" onchange=shiftChange()>Shift MCU<br>
<button onclick=addMCU() style="width:7.5em;">Insert MCU</button>
<button onclick=removeMCU() style="width:7.5em;">Remove MCU</button><br>
<input type="radio" id="dc1" name="delta" value="1"><label for="dc1">1</label>
<input checked type="radio" id="dc10" name="delta" value="10"><label for="dc10">10</label>
<input type="radio" id="dc20" name="delta" value="20"><label for="dc20">20</label>
<input type="radio" id="dc50" name="delta" value="50"><label for="dc50">50</label>
<input type="radio" id="dc100" name="delta" value="100"><label for="dc100">100</label><br>
<input type="radio" id="dc200" name="delta" value="200"><label for="dc200">200</label>
<input type="radio" id="dcX" name="delta" value="x"><label for="dcX">custom</label>
<input type="text" maxlength="4" size="3" id="dcVal" name="dcX"><br>
<button onclick=DCmodMCU(1) id="dc+" style="width:4em;" disabled>DC+</button>
<button onclick=DCmodMCU(-1) id="dc-" style="width:4em;" disabled>DC-</button>
<button onclick=DCmodMCU(0) id="dcset" style="width:4.5em;" disabled>set DC</button><br>
<canvas id="zoomCanvas" width="1" height="1" style="border:1px solid #d3d3d3;display:none"></canvas>
<div id="rstdiv" style="display:none">show RST <input type="Checkbox" id="rst" onchange=rstEnable()>
<select id="rstNum" name="rstNum">
<option value=-1>No RST</option>
<option value=0>RST0</option>
<option value=1>RST1</option>
<option value=2>RST2</option>
<option value=3>RST3</option>
<option value=4>RST4</option>
<option value=5>RST5</option>
<option value=6>RST6</option>
<option value=7>RST7</option>
</select>
<button onclick=rstSet() id="rstNumSet" style="width:3em;">set</button></div>
<div id="MCUsel"></div>
<div id="shiftInfo"></div>
<div id="coord"></div>
<div id="zoom"></div>
<div id="busy" class="loader" style="display:none"></div></div>
<dialog id="backSearchDialog" style="border-radius:4px; border:1px solid; position: right; max-height: 60%; width: 300px">
MCU <input type="number" id="mcu" maxlength="3" size="3" value=10>
Variants <input type="number" id="variants" maxlength="3" size="4" value="100"><br>
<button onclick=backSearch()>Search</button>
<button onclick=backSrcInsert()>Insert</button>
<button value="cancel" onclick=backSearchDialog.close()>Cancel</button>
<div id="srcRes">
</div>
</dialog>
<dialog id="MCUinfoDialog" style="border-radius:4px; border:1px solid; position: right; max-width: 1000em; width: 80%">
<button onclick=infoDlgChangeSel(0)><<</button>
<button onclick=infoDlgChangeSel(1)>>></button>
bitstream<input type="Checkbox" id="details" onchange=infoDlgUpdate()>
<span id="legend">Legend: data, <font color='orange'>DC prefix</font>, <font color='red'>DC zero</font>,
<font color='green'>AC prefix</font>, <font color='violet'>ZRL (16 zeros)</font>, <font color='blue'>end of block</font></span>
<button value="ok" onclick=MCUinfoDialog.close()>OK</button>
<div id="info">
</div>
</dialog>
<dialog id="imageInfoDialog" style="border-radius:4px; border:1px solid; position: right; max-width: 80%">
<b>Image info</b>
<div id="imgInfoExt">
</div>
<button value="ok" onclick=imageInfoDialog.close()>OK</button><br><br>
Points <input type="number" id="corruptPoints" maxlength="3" size="3" value=1>
<button onclick=corruptFile(imageInfoDialog.querySelector("#corruptPoints").value)>Corrupt image</button>
</dialog>
<dialog id="infoDialog" style="border-radius:4px; border:1px solid;">
<H2 style="display:inline">JPEG visual repair tool v1.3 (Oct '23)</H2> by Alberto Maccioni<br><br>
This tool can load JPEG images while preserving MCU (Minimum Coded Unit) coding data
and allows editing at MCU level.<br>
The purpose is to correct artifacts resulting from file corruption.<br>
You can delete, insert, copy, paste MCUs.<br>
You can change DC level of each MCU.<br>
You can view the image as RGB, Y, Cb, Cr.<br>
You can automatically fix color differences.<br>
You can view MCU pixel levels, coefficients, and binary datastream.<br>
A crossed red rectangle appears on MCUs that generated decode errors.<br>
Color fixing is performed by minimizing color difference between the top row of an MCU
and the bottom row of the corresponding MCU in the previous line.<br>
More info at <a href="https://github.com/albmac/JPEGVisualRepairTool">github project page</a><br>
Controls:<br>
<b>mouse wheel</b> ⇒ zoom image<br>
<b>left click</b> ⇒ select MCU; a black or white rectangle appears on selected MCU<br>
<b>shift+left click</b> ⇒ extend selection<br>
<b>left button drag</b> ⇒ drag image<br>
<b>right click</b> ⇒ select MCU for color fixing; a red rectangle appears on selected MCU<br>
<b>right button drag</b> ⇒ extend selection for color fixing<br>
<b>esc</b> ⇒ unselect MCUs<br>
<b>ctrl-c</b> ⇒ copy selected MCUs<br>
<b>ctrl-v</b> ⇒ paste before selected MCU<br>
<b>ctrl-z</b> ⇒ undo last operation<br>
<b>del</b> ⇒ delete MCU<br>
<b>arrows</b> ⇒ change selected MCU<br>
<b>f</b> ⇒ zoom fit<br>
<b>i</b> ⇒ extra MCU info<br>
<b>s</b> ⇒ shift MCU rendering using left/right keys<br>
<b>1</b> ⇒ switch to RGB view<br>
<b>2</b> ⇒ switch to Y view<br>
<b>3</b> ⇒ switch to Cb view<br>
<b>4</b> ⇒ switch to Cr view<br>
<b>] [</b> ⇒ zoom in/out<br>
<b>h</b> ⇒ show this help dialog<br>
<br>
<button value="ok" onclick=infoDialog.close()>OK</button>
</dialog>
<script>
/* JPEG visual repair tool
Copyright (C) 2023 Alberto Maccioni
This file is part of <https://github.com/jpegVisualRepairTool>.
JPEG visual repair tool is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
JPEG visual repair tool is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with JPEG visual repair tool. If not, see <http://www.gnu.org/licenses/>.
*/
/*
*/
filename="";
numMCU=0;
mcuPixX=0,mcuPixY=0;
Mx=0,My=0;
MCUdef="";
MCUarray=[];
mcucount=0;
var selectedMCU={};
selectedMCU.x=-1;
selectedMCU.y=-1;
selectedMCU.i=-1;
selectedMCU.list=[];
iblock=0;
Rnumbit=0; //reserved to getbit
RDbyte=0; //reserved to getbit
Rptr=0; //reserved to getbit
WRbyte=0; //reserved to putbit
Wnumbit=0; //reserved to putbit
Wptr=0; //reserved to putbit
bitstuffing=0;
bufsize=128;
scanoffset=0;
var filebuf;
Rbitcount=0;
Wbitcount=0;
MCUdef=['Y','Y','C','C']; //default MCU composition
restartInt=-1;
YDC=[];
YAC=[];
CDC=[];
CAC=[];
qmatY=[];
qmatC=[];
dqt=[];
dht=[];
zoomFactor=1;
zDx=0,zDy=0;
isDragging=0;
dragStart={x:0,y:0};
noClick=false;
displayRows=0;
colorFixList=new Array();
ErrCnt=0;
originalMCUcount=0;
drawRestart=false;
var pictInt = new OffscreenCanvas(1,1);
var ctx=pictInt.getContext("2d");
var cZoom = document.getElementById("zoomCanvas");
var ctxZoom = cZoom.getContext("2d");
var cPicture = document.getElementById("pictureCanvas");
var ctxPicture = cPicture.getContext("2d");
MCUsrcArray=[];
busy=false;
shiftRendering=0;
undoList=[];
//show coordinates of original picture
cPicture.addEventListener("mousemove", function(e){
var x = e.pageX - cPicture.offsetLeft;
var y = e.pageY - cPicture.offsetTop;
var rate=ctx.canvas.width/ctxPicture.canvas.width/zoomFactor;
var x1=(x-zDx)*rate,y1=(y-zDy)*rate;
var str="xy("+x1.toFixed(0)+","+y1.toFixed(0)+")";
if(mcuPixX>0) str+=", MCU("+Math.floor(x1/mcuPixX)+";"+Math.floor(y1/mcuPixY)+")";
document.getElementById("coord").innerHTML=str;
});
//select MCU under the cursor
cPicture.addEventListener("click", function(e) {
if(noClick){
noClick=false;
return;
}
if(shiftRendering!=0){
resetShift(); //reset shift
selectedUpdate();
}
var x = e.pageX - cPicture.offsetLeft;
var y = e.pageY - cPicture.offsetTop;
var rate=ctx.canvas.width/ctxPicture.canvas.width/zoomFactor;
var x1=(x-zDx)*rate,y1=(y-zDy)*rate;
if(e.shiftKey==false){
if(selectedMCU.x>-1) render(selectedMCU.i,document.getElementById("layer").value); //redraw last block
selectedMCU.x=Math.floor(x1/mcuPixX);
selectedMCU.y=Math.floor(y1/mcuPixY);
var j=selectedMCU.y*Mx+selectedMCU.x;
selectedMCU.i=j;
console.log("selected MCU"+j+" ("+selectedMCU.x+";"+selectedMCU.y+")");
for(j=0;j<selectedMCU.list.length;j++){
render(selectedMCU.list[j],document.getElementById("layer").value); //clear selection rectangles
}
selectedMCU.list=[];
}
else if(selectedMCU.x>-1){ //extend selection
var sel2x=Math.floor(x1/mcuPixX);
var sel2y=Math.floor(y1/mcuPixY);
var j=sel2y*Mx+sel2x;
for(;j<selectedMCU.i;j++) selectedMCU.list.push(j);
for(;j>selectedMCU.i;j--) selectedMCU.list.push(j);
selectedMCU.list=removeDuplicates(selectedMCU.list);
console.log(selectedMCU.list);
}
for(var i=0;i<colorFixList.length;i++){
render(colorFixList[i].i,document.getElementById("layer").value); //redraw block without selection rectangle
}
colorFixList.length=0; //clear fix list if present
selectedUpdate();
});
//right click: select or deselect MCU under the cursor for color fix
cPicture.addEventListener("contextmenu", function(e) {
e.preventDefault();
if(noClick){
noClick=false;
return;
}
var x = e.pageX - cPicture.offsetLeft;
var y = e.pageY - cPicture.offsetTop;
var rate=ctx.canvas.width/ctxPicture.canvas.width/zoomFactor;
var x1=(x-zDx)*rate,y1=(y-zDy)*rate;
var targetMCU={};
targetMCU.x=Math.floor(x1/mcuPixX);
targetMCU.y=Math.floor(y1/mcuPixY);
targetMCU.i=targetMCU.y*Mx+targetMCU.x;
var i;
for(i=0;i<colorFixList.length;i++){
if(colorFixList[i].i==targetMCU.i){
colorFixList.splice(i,1); //remove if present
i=colorFixList.length+10;
render(targetMCU.i,document.getElementById("layer").value); //redraw block without selection rectangle
ctxPicture.drawImage(pictInt,zDx,zDy,ctxPicture.canvas.width*zoomFactor,ctxPicture.canvas.height*zoomFactor);
}
}
if(i<colorFixList.length+10){
colorFixList.push(targetMCU);
ctx.beginPath();
ctx.lineWidth = "1";
ctx.strokeStyle = "red";
ctx.rect(targetMCU.x*mcuPixX,targetMCU.y*mcuPixY,mcuPixX-1,mcuPixY-1);
ctx.stroke();
ctxPicture.drawImage(pictInt,zDx,zDy,ctxPicture.canvas.width*zoomFactor,ctxPicture.canvas.height*zoomFactor);
}
});
//zoom via mouse wheel
cPicture.addEventListener( 'wheel', function(e){
e.preventDefault();
var xorig=(e.pageX-cPicture.offsetLeft-zDx)/zoomFactor; //pointer in original picture coordinates
var yorig=(e.pageY-cPicture.offsetTop-zDy)/zoomFactor;
zoomFactor*=1-e.deltaY*0.001;
if(zoomFactor<1){
zoomFactor=1;
zDx=zDy=0;
}
else{
zDx=(e.pageX-cPicture.offsetLeft)-xorig*zoomFactor; //move original pointer under actual one
zDy=(e.pageY-cPicture.offsetTop)-yorig*zoomFactor;
if(zDx>0) zDx=0;
if(zDy>0) zDy=0;
if(zDx<ctxPicture.canvas.width*(1-zoomFactor)) zDx=ctxPicture.canvas.width*(1-zoomFactor);
if(zDy<ctxPicture.canvas.height*(1-zoomFactor)) zDy=ctxPicture.canvas.height*(1-zoomFactor);
}
// zDx=e.pageX*(1-a)+cPicture.offsetLeft*(a-1)+zDx*a;
ctxPicture.drawImage(pictInt,zDx,zDy,ctxPicture.canvas.width*zoomFactor,ctxPicture.canvas.height*zoomFactor);
document.getElementById("zoom").innerHTML="Zoom:"+zoomFactor.toFixed(2);
});
cPicture.addEventListener( 'mousedown', function(e){
if(e.button==0) isDragging=1; //left click -> drag image
if(e.button==2) isDragging=2; //right click -> select for color fix
dragStart.x=e.pageX;
dragStart.y=e.pageY;
});
cPicture.addEventListener( 'mouseup', function(e){
if(e.button==0){ //left button
isDragging=0;
zDx+=e.pageX-dragStart.x;
zDy+=e.pageY-dragStart.y;
if(zDx<ctxPicture.canvas.width*(1-zoomFactor)) zDx=ctxPicture.canvas.width*(1-zoomFactor);
if(zDy<ctxPicture.canvas.height*(1-zoomFactor)) zDy=ctxPicture.canvas.height*(1-zoomFactor);
if(zDx>0) zDx=0;
if(zDy>0) zDy=0;
ctxPicture.drawImage(pictInt,zDx,zDy,ctxPicture.canvas.width*zoomFactor,ctxPicture.canvas.height*zoomFactor);
}
isDragging=0;
});
//stop dragging if mouse exits picture area
cPicture.addEventListener( 'mouseout', function(e){
if(isDragging){
isDragging=0;
zDx+=e.pageX-dragStart.x;
zDy+=e.pageY-dragStart.y;
if(zDx<ctxPicture.canvas.width*(1-zoomFactor)) zDx=ctxPicture.canvas.width*(1-zoomFactor);
if(zDy<ctxPicture.canvas.height*(1-zoomFactor)) zDy=ctxPicture.canvas.height*(1-zoomFactor);
if(zDx>0) zDx=0;
if(zDy>0) zDy=0;
ctxPicture.drawImage(pictInt,zDx,zDy,ctxPicture.canvas.width*zoomFactor,ctxPicture.canvas.height*zoomFactor);
}
});
//drag image or select blocks
cPicture.addEventListener( 'mousemove', function(e){
if(isDragging==2){ //extend selection for color fix
noClick=true;
var x = e.pageX - cPicture.offsetLeft;
var y = e.pageY - cPicture.offsetTop;
var rate=ctx.canvas.width/ctxPicture.canvas.width/zoomFactor;
var x1=(x-zDx)*rate,y1=(y-zDy)*rate;
var targetMCU={};
targetMCU.x=Math.floor(x1/mcuPixX);
targetMCU.y=Math.floor(y1/mcuPixY);
targetMCU.i=targetMCU.y*Mx+targetMCU.x;
var i;
for(i=0;i<colorFixList.length;i++){
if(colorFixList[i].i==targetMCU.i){
i=colorFixList.length+10;
}
}
if(i<colorFixList.length+10){
colorFixList.push(targetMCU);
ctx.beginPath();
ctx.lineWidth = "1";
ctx.strokeStyle = "red";
ctx.rect(targetMCU.x*mcuPixX,targetMCU.y*mcuPixY,mcuPixX-1,mcuPixY-1);
ctx.stroke();
ctxPicture.drawImage(pictInt,zDx,zDy,ctxPicture.canvas.width*zoomFactor,ctxPicture.canvas.height*zoomFactor);
}
}
if(isDragging==1){ //drag image
noClick=true;
var dx=e.pageX-dragStart.x;
var dy=e.pageY-dragStart.y;
if(dx+zDx<ctxPicture.canvas.width*(1-zoomFactor)) dx=ctxPicture.canvas.width*(1-zoomFactor)-zDx;
if(dy+zDy<ctxPicture.canvas.height*(1-zoomFactor)) dy=ctxPicture.canvas.height*(1-zoomFactor)-zDy;
if(dx+zDx>0) dx=-zDx;
if(dy+zDy>0) dy=-zDy;
ctxPicture.drawImage(pictInt,zDx+dx,zDy+dy,ctxPicture.canvas.width*zoomFactor,ctxPicture.canvas.height*zoomFactor);
}
});
//show coordinates of original picture
cZoom.addEventListener("mousemove", function(e){
var x=Math.floor((e.pageX-cZoom.offsetLeft)/10);
var y=Math.floor((e.pageY-cZoom.offsetTop)/10);
if(x>=mcuPixX) x=mcuPixX-1;
if(y>=mcuPixY) y=mcuPixY-1;
var str="xy("+x+","+y+")";
if(selectedMCU.i>=0){
var Y=MCUarray[selectedMCU.i].viewY[x+y*mcuPixY];
var C1=MCUarray[selectedMCU.i].viewC1[x+y*mcuPixY];
var C2=MCUarray[selectedMCU.i].viewC2[x+y*mcuPixY];
str+="YCbCr["+Y+";"+C1+";"+C2+"]";
}
document.getElementById("coord").innerHTML=str;
});
//paste from clipboard before currently selected MCU
document.body.addEventListener("paste",function(ev){
if(selectedMCU.i<0) return;
ev.preventDefault();
var data=(ev.clipboardData||window.clipboardData).getData("text");
var MCUp=JSON.parse(data);
var n=0;
for(var i=MCUp.length-1;i>=0;i--){ //reverse order
if(MCUp[i].length!=MCUarray[0].length){
console.log("not a standard MCU!");
mcucount=MCUarray.length;
break;
}
MCUp[i].bufferY=new ArrayBuffer(mcuPixX*mcuPixY);
MCUp[i].bufferC1=new ArrayBuffer(mcuPixX*mcuPixY);
MCUp[i].bufferC2=new ArrayBuffer(mcuPixX*mcuPixY);
MCUp[i].viewY=new Uint8Array(MCUp[i].bufferY);
MCUp[i].viewC1=new Uint8Array(MCUp[i].bufferC1);
MCUp[i].viewC2=new Uint8Array(MCUp[i].bufferC2);
for(var j=0;j<MCUp[i].viewY.length;j++) MCUp[i].viewY[j]=MCUp[i].viewC1[j]=MCUp[i].viewC2[j]=255;
MCUarray.splice(selectedMCU.i,0,MCUp[i]); //insert new block
mcucount=MCUarray.length;
n++;
}
undoList.push({action: "paste", position: selectedMCU.i, count: n});
updateAllMCU(selectedMCU.i);
document.getElementById("MCUn").innerHTML="MCU count: "+mcucount;
console.log("Paste MCU@"+selectedMCU.i+","+n);
});
//copy selected MCU to clipboard
document.body.addEventListener("copy",function(ev){
if(selectedMCU.i<0) return;
if(!MCUinfoDialog.open) ev.preventDefault();
var list=selectedMCU.list;
list.push(selectedMCU.i);
list=list.sort((a,b)=>a-b);
console.log("copied MCU "+list);
ev.clipboardData.setData("text/plain",JSON.stringify(list.map((x)=>MCUarray[x])));
});
//keyboard shortcuts
document.body.addEventListener("keydown",function(ev){
ev=ev||window.event;
var key=ev.which||ev.keycode;
if(key==72) infoDialog.showModal(); //h
if(key==73) MCUinfoDialog.showModal(); //i
if(key==49){ //1
document.getElementById("layer").value="RGB";
setLayer();
}
if(key==50){ //2
document.getElementById("layer").value="Y";
setLayer();
}
if(key==51){ //3
document.getElementById("layer").value="C1";
setLayer();
}
if(key==52){ //4
document.getElementById("layer").value="C2";
setLayer();
}
if(key==37){ // left
if(MCUinfoDialog.open) infoDlgChangeSel(0);
else{
if(ev.shiftKey){ //extend selection
if(shiftRendering!=0) resetShift(); //reset shift
if(selectedMCU.i>-1){
if(selectedMCU.list.length==0){
selectedMCU.list.push(selectedMCU.i-1);
}
else{
if(selectedMCU.list[0]>selectedMCU.i){
render(selectedMCU.list.pop(),document.getElementById("layer").value); //clear selection rectangles
}
else selectedMCU.list.push(Math.min(...selectedMCU.list)-1);
}
// console.log(selectedMCU.list);
selectedUpdate();
}
}
else if(document.getElementById("shift").checked){ //shift MCU rendering
if(selectedMCU.i>-1){
shiftRendering--;
render(selectedMCU.i,document.getElementById("layer").value,mcucount,shiftRendering);
var shifted=selectedMCU.i+shiftRendering;
ctx.strokeStyle = "yellow";
ctx.beginPath();
ctx.lineWidth = "1";
ctx.rect(shifted%Mx*mcuPixX,Math.floor(shifted/Mx)*mcuPixY,mcuPixX-1,mcuPixY-1);
ctx.stroke();
ctxPicture.drawImage(pictInt,zDx,zDy,ctxPicture.canvas.width*zoomFactor,ctxPicture.canvas.height*zoomFactor);
document.getElementById("shiftInfo").innerHTML="Shift rendering by "+shiftRendering+" MCU";
}
}
else{ //change selection
if(shiftRendering!=0) resetShift(); //reset shift
if(selectedMCU.x>-1) render(selectedMCU.y*Mx+selectedMCU.x,document.getElementById("layer").value); //redraw last block
selectedMCU.i--;
if(selectedMCU.i<0) selectedMCU.i=0;
selectedMCU.x=selectedMCU.i%Mx;
var y0=selectedMCU.y;
selectedMCU.y=Math.floor(selectedMCU.i/Mx);
for(var j=0;j<selectedMCU.list.length;j++){
render(selectedMCU.list[j],document.getElementById("layer").value); //clear selection rectangles
}
selectedMCU.list=[];
var rate=ctx.canvas.width/ctxPicture.canvas.width/zoomFactor;
var x=(-zDx)*rate; //min x coordinate on screen
if(selectedMCU.y!=y0){ //changed row, scroll to end of image
zDx=ctxPicture.canvas.width*(1-zoomFactor);
ctxPicture.drawImage(pictInt,zDx,zDy,ctxPicture.canvas.width*zoomFactor,ctxPicture.canvas.height*zoomFactor);
}
else if((selectedMCU.x)*mcuPixX<=x){ //shift left by one MCU width
zDx+=mcuPixX/rate;
if(zDx>0) zDx=0;
ctxPicture.drawImage(pictInt,zDx,zDy,ctxPicture.canvas.width*zoomFactor,ctxPicture.canvas.height*zoomFactor);
}
selectedUpdate();
}
}
}
if(key==39){ // right
if(MCUinfoDialog.open) infoDlgChangeSel(1);
else{
if(ev.shiftKey){ //extend selection
if(shiftRendering!=0) resetShift(); //reset shift
if(selectedMCU.i>-1){
if(selectedMCU.list.length==0){
selectedMCU.list.push(selectedMCU.i+1);
}
else{
if(selectedMCU.list[0]<selectedMCU.i){
render(selectedMCU.list.pop(),document.getElementById("layer").value); //clear selection rectangles
}
else selectedMCU.list.push(Math.max(...selectedMCU.list)+1);
}
selectedUpdate();
}
}
else if(document.getElementById("shift").checked){ //shift MCU rendering
if(selectedMCU.i>-1){
render(selectedMCU.i+shiftRendering,document.getElementById("layer").value); //uncover original block
shiftRendering++;
render(selectedMCU.i,document.getElementById("layer").value,mcucount,shiftRendering);
var shifted=selectedMCU.i+shiftRendering;
ctx.strokeStyle = "yellow";
ctx.beginPath();
ctx.lineWidth = "1";
ctx.rect(shifted%Mx*mcuPixX,Math.floor(shifted/Mx)*mcuPixY,mcuPixX,mcuPixY);
ctx.stroke();
ctxPicture.drawImage(pictInt,zDx,zDy,ctxPicture.canvas.width*zoomFactor,ctxPicture.canvas.height*zoomFactor);
document.getElementById("shiftInfo").innerHTML="Shift rendering by "+shiftRendering+" MCU";
}
}
else{ //change selection
if(shiftRendering!=0) resetShift(); //reset shift
if(selectedMCU.x>-1) render(selectedMCU.y*Mx+selectedMCU.x,document.getElementById("layer").value); //redraw last block
selectedMCU.i++;
if(selectedMCU.i>=mcucount) selectedMCU.i=mcucount-1;
selectedMCU.x=selectedMCU.i%Mx;
var y0=selectedMCU.y;
selectedMCU.y=Math.floor(selectedMCU.i/Mx);
for(var j=0;j<selectedMCU.list.length;j++){
render(selectedMCU.list[j],document.getElementById("layer").value); //clear selection rectangles
}
selectedMCU.list=[];
var rate=ctx.canvas.width/ctxPicture.canvas.width/zoomFactor;
var x=(ctxPicture.canvas.width-zDx)*rate; //max x coordinate on screen
if(selectedMCU.y!=y0){ //changed row, scroll to end of image
zDx=0;
ctxPicture.drawImage(pictInt,zDx,zDy,ctxPicture.canvas.width*zoomFactor,ctxPicture.canvas.height*zoomFactor);
}
else if((selectedMCU.x+1)*mcuPixX>=x){ //shift left by one MCU width
zDx-=mcuPixX/rate;
if(zDx<ctxPicture.canvas.width*(1-zoomFactor)) zDx=ctxPicture.canvas.width*(1-zoomFactor);
ctxPicture.drawImage(pictInt,zDx,zDy,ctxPicture.canvas.width*zoomFactor,ctxPicture.canvas.height*zoomFactor);
}
selectedUpdate();
}
}
}
if(key==38){ //up
if(!MCUinfoDialog.open){
if(shiftRendering!=0) resetShift(); //reset shift
if(selectedMCU.x>-1) render(selectedMCU.y*Mx+selectedMCU.x,document.getElementById("layer").value); //redraw last block
if(selectedMCU.y>0) selectedMCU.y--;
selectedMCU.i=selectedMCU.y*Mx+selectedMCU.x;
for(var j=0;j<selectedMCU.list.length;j++){
render(selectedMCU.list[j],document.getElementById("layer").value); //clear selection rectangles
}
selectedMCU.list=[];
var rate=ctx.canvas.width/ctxPicture.canvas.width/zoomFactor;
var y=(-zDy)*rate; //min y coordinate on screen
if((selectedMCU.y)*mcuPixY<=y){ //shift up by one MCU height
zDy+=mcuPixY/rate;
if(zDy>0) zDy=0;
ctxPicture.drawImage(pictInt,zDx,zDy,ctxPicture.canvas.width*zoomFactor,ctxPicture.canvas.height*zoomFactor);
}
selectedUpdate();
ev.preventDefault();
}
}
if(key==40){ //down
if(!MCUinfoDialog.open){
if(shiftRendering!=0) resetShift(); //reset shift
if(selectedMCU.x>-1) render(selectedMCU.y*Mx+selectedMCU.x,document.getElementById("layer").value); //redraw last block
if(selectedMCU.y<Math.floor(mcucount/Mx)-1) selectedMCU.y++;
selectedMCU.i=selectedMCU.y*Mx+selectedMCU.x;
for(var j=0;j<selectedMCU.list.length;j++){
render(selectedMCU.list[j],document.getElementById("layer").value); //clear selection rectangles
}
selectedMCU.list=[];
var rate=ctx.canvas.width/ctxPicture.canvas.width/zoomFactor;
var y=(ctxPicture.canvas.height-zDy)*rate; //max y coordinate on screen
if((selectedMCU.y+1)*mcuPixY>=y){ //shift down by one MCU height
zDy-=mcuPixY/rate;
if(zDy<ctxPicture.canvas.height*(1-zoomFactor)) zDy=ctxPicture.canvas.height*(1-zoomFactor);
ctxPicture.drawImage(pictInt,zDx,zDy,ctxPicture.canvas.width*zoomFactor,ctxPicture.canvas.height*zoomFactor);
}
selectedUpdate();
ev.preventDefault();
}
}
if(key==46){ //del
if(shiftRendering!=0) resetShift(); //reset shift
removeMCU();
}
if(key==27){ //esc
if(shiftRendering!=0) resetShift(); //reset shift
for(var j=0;j<selectedMCU.list.length;j++){
render(selectedMCU.list[j],document.getElementById("layer").value); //clear selection rectangles
}
selectedMCU.list=[]; //clear extended selection
for(var i=0;i<colorFixList.length;i++){
render(colorFixList[i].i,document.getElementById("layer").value); //redraw block without selection rectangle
}
colorFixList.length=0; //clear fix list if present
selectedUpdate();
}
if(key==83){ //s
var sh=document.getElementById("shift");
sh.checked=!sh.checked;
if(sh.checked==false&&shiftRendering!=0){
resetShift(); //reset shift
selectedUpdate();
}
}
if(key==90){ //z
if(ev.ctrlKey&&undoList.length>0){ //undo
if(shiftRendering!=0) resetShift(); //reset shift
item=undoList.pop();
console.log("undo: "+item.action);
switch(item.action){
case "add":
MCUarray.splice(item.position,1); //remove elements
break;
case "paste":
MCUarray.splice(item.position,item.count); //remove elements
break;
case "delete":
for(var i=item.cells.length-1;i>=0;i--)
MCUarray.splice(item.position,0,item.cells[i]); //insert elements
break;
case "DC":
MCUarray.splice(item.position,1,item.cell); //insert elements
break;
case "fixColor":
MCUarray.splice(item.position,item.cells.length); //remove elements
for(var i=item.cells.length-1;i>=0;i--)
MCUarray.splice(item.position,0,item.cells[i]); //insert elements
break;
}
mcucount=MCUarray.length;
updateAllMCU(item.position);
}
}
if(key==70){ //f fit
zoomFactor=1;
zDx=zDy=0;
ctxPicture.drawImage(pictInt,zDx,zDy,ctxPicture.canvas.width*zoomFactor,ctxPicture.canvas.height*zoomFactor);
document.getElementById("zoom").innerHTML="Zoom:"+zoomFactor.toFixed(2);
}
if(key==219){ //[ zoom-out
var xorig=(cPicture.offsetWidth/2-zDx)/zoomFactor; //center in original picture coordinates
var yorig=(cPicture.offsetHeight/2-zDy)/zoomFactor;
zoomFactor*=0.8;
if(zoomFactor<1){
zoomFactor=1;
zDx=zDy=0;
}
else{
zDx=(cPicture.offsetWidth/2-cPicture.offsetLeft)-xorig*zoomFactor; //shift to center
zDy=(cPicture.offsetHeight/2-cPicture.offsetTop)-yorig*zoomFactor;
if(zDx>0) zDx=0;
if(zDy>0) zDy=0;
if(zDx<ctxPicture.canvas.width*(1-zoomFactor)) zDx=ctxPicture.canvas.width*(1-zoomFactor);
if(zDy<ctxPicture.canvas.height*(1-zoomFactor)) zDy=ctxPicture.canvas.height*(1-zoomFactor);
}
ctxPicture.drawImage(pictInt,zDx,zDy,ctxPicture.canvas.width*zoomFactor,ctxPicture.canvas.height*zoomFactor);
document.getElementById("zoom").innerHTML="Zoom:"+zoomFactor.toFixed(2);
}
if(key==221){ //] zoom-in
var xorig=(cPicture.offsetWidth/2-zDx)/zoomFactor; //center in original picture coordinates
var yorig=(cPicture.offsetHeight/2-zDy)/zoomFactor;
zoomFactor*=1.25;
if(zoomFactor>100) zoomFactor=100;
zDx=(cPicture.offsetWidth/2-cPicture.offsetLeft)-xorig*zoomFactor; //shift to center
zDy=(cPicture.offsetHeight/2-cPicture.offsetTop)-yorig*zoomFactor;
if(zDx>0) zDx=0;
if(zDy>0) zDy=0;
if(zDx<ctxPicture.canvas.width*(1-zoomFactor)) zDx=ctxPicture.canvas.width*(1-zoomFactor);
if(zDy<ctxPicture.canvas.height*(1-zoomFactor)) zDy=ctxPicture.canvas.height*(1-zoomFactor);
ctxPicture.drawImage(pictInt,zDx,zDy,ctxPicture.canvas.width*zoomFactor,ctxPicture.canvas.height*zoomFactor);
document.getElementById("zoom").innerHTML="Zoom:"+zoomFactor.toFixed(2);
}
});
function shiftChange(){
var sh=document.getElementById("shift");
if(sh.checked==false&&shiftRendering!=0){
resetShift(); //reset shift
selectedUpdate();
}
}
function removeDuplicates(arr){
return arr.filter((item,index)=>arr.indexOf(item)===index);
}
//open another tab with the same document
function newtab(){
window.open(window.location.href, "_blank");
}
//update zoom view and info about selected block
function selectedUpdate(){
if(selectedMCU.i<0) return;
var j=selectedMCU.i;
var i;
var list=selectedMCU.list.slice();
list.push(selectedMCU.i);
for(j=0;j<list.length;j++){
avg1=getAvgComponents(list[j],0); //average components
avg2=getAvgComponents(list[j],mcuPixX*(mcuPixY/2)); //average components, middle row
avg3=getAvgComponents(list[j],mcuPixX*(mcuPixY-1)); //average components, lower row
avgY=(avg1.Y+avg2.Y+avg3.Y)/3;
if(avgY>100) ctx.strokeStyle = "black";
else ctx.strokeStyle = "white";
ctx.beginPath();
ctx.lineWidth = "1";
ctx.rect(list[j]%Mx*mcuPixX,Math.floor(list[j]/Mx)*mcuPixY,mcuPixX-1,mcuPixY-1);
ctx.stroke();
}
renderZoom(selectedMCU.i,document.getElementById("layer").value);
var str="MCU"+selectedMCU.i+" ("+selectedMCU.x+";"+selectedMCU.y+")";
var str2=list.length>1?(" ("+list.length+" total)"):"";
document.getElementById("MCUsel").innerHTML="Selected "+str+str2;
ctxPicture.drawImage(pictInt,zDx,zDy,ctxPicture.canvas.width*zoomFactor,ctxPicture.canvas.height*zoomFactor);
var dlg=document.getElementById("MCUinfoDialog");
var div=document.getElementById("info");
while(div.firstChild) div.removeChild(div.firstChild);
details=dlg.querySelector("#details").checked;
document.getElementById("legend").style.display=details?'':'none';
j=selectedMCU.i;
if(MCUarray[j].restart>-1) str+=" Restart marker: "+MCUarray[j].restart;
div.insertAdjacentHTML("beforeend",str+"<br>");
var table="<table><tr><th style='width:5.5em'>Address</th>"+
"<th style='width:2.1em'>Type</th><th style='width:2.5em'>DC</th>"+
"<th>Coefficients</th><th style='width:5em'>Errors</th></tr>";
var str={str:""}; //trick to pass modifiable string
MCUarray[mcucount]=structuredClone(MCUarray[0]); //MCU for decodeBlock
for(i=0;i<MCUarray[j].blocks.length;i++){
table+="<tr><td>0x"+(MCUarray[j].blocks[i].offset>>3).toString(16).toUpperCase()+"."+(MCUarray[j].blocks[i].offset&7)+"</td>"+
"<td>"+MCUarray[j].blocks[i].blockType+"</td>"+
"<td>"+MCUarray[j].blocks[i].DC+"</td><td>";
if(details){
setBitAddr(MCUarray[j].blocks[i].offset);
str.str="";
decodeBlock(MCUdef[i]=='Y'?Y_BLOCK:C_BLOCK,0,str);
table+=str.str+"<br>";
}
table+=MCUarray[j].blocks[i].coeff.toString().replace(/(,0)+$/,"")+"</td>"+
"<td>"+MCUarray[j].blocks[i].errors+"</td></tr>";
}
table+="</table>";
div.insertAdjacentHTML("beforeend",table);
MCUarray.length=mcucount; //remove temporary MCU
}
//update info dialog from within
function infoDlgUpdate(){
selectedUpdate();
MCUinfoDialog.close();
MCUinfoDialog.showModal();
}
//change selected block from info dialog
function infoDlgChangeSel(direction=1){
if(selectedMCU.x>-1) render(selectedMCU.y*Mx+selectedMCU.x,document.getElementById("layer").value); //redraw last block
if(direction) selectedMCU.i++;
else selectedMCU.i--;
if(selectedMCU.i<0) selectedMCU.i=0;
if(selectedMCU.i>=mcucount) selectedMCU.i=mcucount-1;
selectedMCU.x=selectedMCU.i%Mx;
selectedMCU.y=Math.floor(selectedMCU.i/Mx);
selectedUpdate();
MCUinfoDialog.close();
MCUinfoDialog.showModal();
}
//add MCU before selected one and update view
//new MCU is neutral (same DC as previous one)
function addMCU(){
if(selectedMCU.x>-1&&selectedMCU.y>-1){
bufferY=new ArrayBuffer(mcuPixX*mcuPixY);
bufferC1=new ArrayBuffer(mcuPixX*mcuPixY);
bufferC2=new ArrayBuffer(mcuPixX*mcuPixY);
viewY=new Uint8Array(bufferY);
viewC1=new Uint8Array(bufferC1);
viewC2=new Uint8Array(bufferC2);
for(j=0;j<viewY.length;j++) viewY[j]=viewC1[j]=viewC2[j]=255;
var MCU={
bufferY: bufferY, //pre-decoded Y data
bufferC1: bufferC1, //pre-decoded C1 data
bufferC2: bufferC2, //pre-decoded C2 data
viewY: viewY,
viewC1: viewC1,
viewC2: viewC2,
blocks: new Array,
row: -1,
col: -1,
offset: -1,
restart: -1
}
for(var i=0;i<MCUdef.length;i++){
MCU.blocks[i]={
blockType:MCUdef[i],
DC: 0,
coeff: [0],
errors: ""
}
}
MCUarray.splice(selectedMCU.i,0,MCU); //insert new block
mcucount=MCUarray.length;
extractPixel(selectedMCU.i);
render(-1,document.getElementById("layer").value);
selectedUpdate();
document.getElementById("MCUn").innerHTML="MCU count: "+mcucount;
undoList.push({action: "add", position: selectedMCU.i});
console.log("Inserted MCU@"+selectedMCU.i);
}
}
//remove selected MCU and update view
function removeMCU(){
if(busy) return;
if(selectedMCU.i>-1){
var min=selectedMCU.i,max=min;
for(var j=0;j<selectedMCU.list.length;j++){
if(selectedMCU.list[j]<min) min=selectedMCU.list[j];
if(selectedMCU.list[j]>max) max=selectedMCU.list[j];
}
removed=MCUarray.splice(min,1+max-min); //remove elements
undoList.push({action: "delete", position: min, cells: removed});
selectedMCU.list=[];
mcucount=MCUarray.length;
updateAllMCU(min);
document.getElementById("MCUn").innerHTML="MCU count: "+mcucount;
console.log("Deleted MCU: "+min+","+removed.length);
}
}
//Modify DC coefficient in selected MCU (in first block of relevant type)
//value: new value or delta
//0 -> new value
//+-1 -> delta w.r.t actual value
function DCmodMCU(value=1){
if(busy) return;
if(selectedMCU.i>-1&&document.getElementById("layer").value!="RGB"){ //only single channel
undoList.push({action: "DC", position: selectedMCU.i, cell: structuredClone(MCUarray[selectedMCU.i])});
if(value==0){
var newDC=parseInt(document.getElementById("dcVal").value);
if (Number.isNaN(parseInt(newDC))) {
; // no value or invalid value
}
else if(document.getElementById("layer").value=="Y"){
MCUarray[selectedMCU.i].blocks[0].coeff[0]=newDC;
}
else if(document.getElementById("layer").value=="C1"){
for(b=1;b<MCUarray[selectedMCU.i].blocks.length&&MCUarray[selectedMCU.i].blocks[b].blockType!="C";b++);
MCUarray[selectedMCU.i].blocks[b].coeff[0]=newDC;
}
else if(document.getElementById("layer").value=="C2"){
for(b=1;b<MCUarray[selectedMCU.i].blocks.length&&MCUarray[selectedMCU.i].blocks[b].blockType!="C2";b++);
MCUarray[selectedMCU.i].blocks[b].coeff[0]=newDC;
}
}
else{
var selected = document.querySelector('input[type=radio][name=delta]:checked');
delta=value*selected.value;
if(selected.value=="x") delta=value*parseInt(document.getElementById("dcVal").value);
var b;
if(document.getElementById("layer").value=="Y"){
MCUarray[selectedMCU.i].blocks[0].coeff[0]+=delta;
}
else if(document.getElementById("layer").value=="C1"){
for(b=1;b<MCUarray[selectedMCU.i].blocks.length&&MCUarray[selectedMCU.i].blocks[b].blockType!="C";b++);
MCUarray[selectedMCU.i].blocks[b].coeff[0]+=delta;
}
else if(document.getElementById("layer").value=="C2"){
for(b=1;b<MCUarray[selectedMCU.i].blocks.length&&MCUarray[selectedMCU.i].blocks[b].blockType!="C2";b++);
MCUarray[selectedMCU.i].blocks[b].coeff[0]+=delta;
}
}
updateAllMCU(selectedMCU.i); //update DC values and pixel data
console.log("DC mod @"+selectedMCU.i);
}
}
function readFile(input) {
file = input.files[0];
filename = file.name;
ctxPicture.canvas.width=700;
ctxPicture.canvas.height=200;
ctxPicture.font = "30px Arial";
ctxPicture.fillStyle = "black";
ctxPicture.fillText("Loading "+filename+"...",10,100);
let reader = new FileReader();
reader.readAsArrayBuffer(file);
document.getElementById("busy").style.display='';
reader.onload = function() {
filebuf = new Uint8Array(reader.result);
decodeJpeg();
originalMCUcount=mcucount;
document.getElementById("MCUn").innerHTML="MCU count: "+mcucount;
}
reader.onerror = function() {
console.log(reader.error);
}
for(let i=1;i<input.files.length;i++){ //in case of multiple files open additional tabs
let w=window.open(window.location.href,"_blank");
w.onload=(ev)=>{w.postMessage(input.files[i],"*")};
console.log("postMessage: "+input.files[i].name);
}
}
//this receives a message containing a file to open
window.addEventListener("message",(event)=>{
console.log("message: "+event.data.name);
filename = event.data.name;
let reader = new FileReader();
reader.readAsArrayBuffer(event.data);
reader.onload = function() {
filebuf = new Uint8Array(reader.result);
decodeJpeg();
document.getElementById("MCUn").innerHTML="MCU count: "+mcucount;
}
},false);
//save file
function saveFile(){
var newsize=filebuf.length+1000;
if(mcucount>originalMCUcount) newsize+=(mcucount-originalMCUcount)*1000; //just a guess
var buf=new Uint8Array(newsize);
buf.set(filebuf);
putbit(0,0); //reset count
Wptr=scanoffset;
Wbitcount=Wptr*8;
var i,j,k,Htable;
for(i=0;i<YAC.length&&YAC[i][2]!=0x00;i++);
var EOBY={n:YAC[i][0],code:YAC[i][1]};
for(i=0;i<CAC.length&&CAC[i][2]!=0x00;i++);
var EOBC={n:CAC[i][0],code:CAC[i][1]};
for(i=0;i<YAC.length&&YAC[i][2]!=0xF0;i++);
var ZRLY={n:YAC[i][0],code:YAC[i][1]};