-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage-diff.html
More file actions
725 lines (665 loc) · 30.3 KB
/
Copy pathimage-diff.html
File metadata and controls
725 lines (665 loc) · 30.3 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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Image Diff Tool</title>
<style>
* { box-sizing: border-box; }
body {
font-family: Helvetica, -apple-system, BlinkMacSystemFont, "Segoe UI", Arial, sans-serif;
line-height: 1.6;
max-width: 980px;
margin: 0 auto;
padding: 20px;
color: #24292e;
background: #ffffff;
}
h1 {
border-bottom: 1px solid #eaecef;
padding-bottom: 0.3em;
margin-top: 0;
font-size: 32px;
font-weight: 600;
}
.back-link {
display: inline-block;
margin-bottom: 16px;
color: #0366d6;
text-decoration: none;
font-size: 14px;
}
.back-link:hover { text-decoration: underline; }
.instruction {
font-size: 13px;
color: #586069;
margin-bottom: 20px;
padding: 12px;
background: #f6f8fa;
border-radius: 6px;
border: 1px solid #e1e4e8;
}
.upload-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 16px;
margin-bottom: 16px;
}
.upload-panel {
border: 2px dashed #e1e4e8;
border-radius: 6px;
background: #f6f8fa;
padding: 20px;
text-align: center;
cursor: pointer;
transition: border-color 0.2s, background 0.2s;
min-height: 160px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 10px;
}
.upload-panel:hover, .upload-panel.dragging { border-color: #0366d6; background: #f0f7ff; }
.upload-panel.loaded { border-style: solid; border-color: #0366d6; cursor: default; }
.upload-panel img {
max-width: 100%;
max-height: 200px;
border-radius: 4px;
border: 1px solid #e1e4e8;
display: block;
}
.upload-label { font-size: 14px; color: #586069; }
.upload-title { font-size: 13px; font-weight: 600; color: #24292e; margin-bottom: 4px; }
.img-meta { font-size: 12px; color: #586069; font-family: monospace; }
input[type="file"] { display: none; }
.options-row {
display: flex;
gap: 16px;
align-items: center;
margin-bottom: 16px;
flex-wrap: wrap;
}
.options-row label { font-size: 14px; font-weight: 600; color: #24292e; white-space: nowrap; }
.options-row select {
padding: 6px 10px;
font-size: 14px;
font-family: Helvetica, sans-serif;
border: 1px solid #e1e4e8;
border-radius: 6px;
background: #fff;
color: #24292e;
}
.range-group { display: flex; align-items: center; gap: 8px; }
.range-val { font-family: monospace; min-width: 32px; font-size: 14px; }
.opt-label { font-size: 14px; color: #24292e; white-space: nowrap; }
.mode-desc { font-size: 12px; color: #586069; margin: -10px 0 10px 0; padding: 0 2px; }
.num-input {
width: 72px;
padding: 5px 8px;
font-size: 14px;
font-family: monospace;
border: 1px solid #e1e4e8;
border-radius: 6px;
background: #fff;
color: #24292e;
}
.btn {
background: #fafbfc;
color: #24292e;
padding: 8px 14px;
border-radius: 6px;
border: 1px solid rgba(27,31,35,0.15);
cursor: pointer;
font-size: 14px;
font-family: Helvetica, -apple-system, BlinkMacSystemFont, "Segoe UI", Arial, sans-serif;
white-space: nowrap;
}
.btn:hover { background: #f3f4f6; }
.btn.primary { background: #0366d6; color: #fff; }
.btn.primary:hover { background: #0256c5; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.stats-bar {
display: none;
gap: 20px;
flex-wrap: wrap;
background: #f6f8fa;
border: 1px solid #e1e4e8;
border-radius: 6px;
padding: 12px 16px;
margin-bottom: 16px;
font-size: 14px;
}
.stats-bar.visible { display: flex; }
.stat { display: flex; flex-direction: column; }
.stat-label { font-size: 11px; text-transform: uppercase; letter-spacing: 0.05em; color: #586069; }
.stat-value { font-family: monospace; font-size: 16px; font-weight: 600; color: #24292e; }
.stat-value.bad { color: #cb2431; }
.stat-value.ok { color: #176f2c; }
.results-section { display: none; }
.results-section.visible { display: block; }
.results-label { font-size: 14px; font-weight: 600; color: #24292e; margin-bottom: 8px; }
.results-tabs {
display: flex;
gap: 0;
border-bottom: 2px solid #e1e4e8;
margin-bottom: 12px;
}
.rtab {
padding: 6px 16px;
cursor: pointer;
border: none;
background: none;
font-size: 14px;
font-family: Helvetica, -apple-system, BlinkMacSystemFont, "Segoe UI", Arial, sans-serif;
color: #586069;
border-bottom: 2px solid transparent;
margin-bottom: -2px;
}
.rtab:hover { color: #0366d6; }
.rtab.active { color: #0366d6; border-bottom-color: #0366d6; font-weight: 600; }
.rtab-panel { display: none; }
.rtab-panel.active { display: block; }
.canvas-wrap {
border: 1px solid #e1e4e8;
border-radius: 6px;
overflow: auto;
background: repeating-conic-gradient(#eee 0% 25%, #fff 0% 50%) 0 0 / 16px 16px;
max-height: 600px;
}
canvas.result { display: block; margin: 0 auto; }
.dl-row { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; }
@media (max-width: 600px) {
.upload-grid { grid-template-columns: 1fr; }
}
</style>
</head>
<body>
<a href="index.html" class="back-link">← Back to Browser Tools</a>
<h1>Image Diff Tool</h1>
<p class="instruction">
Load two images to compare them pixel-by-pixel. First use the <strong>Align B</strong> controls to scale
and position Image B relative to Image A — or click <strong>Auto-align</strong> to detect the best offset
automatically (set scale manually first if the images are at different resolutions). The diff updates
automatically as you adjust any setting.
</p>
<div class="upload-grid">
<div class="upload-panel" id="drop-a" title="Click or drop image A">
<div class="upload-title">Image A</div>
<div class="upload-label" id="label-a">Drop or click to load</div>
<input type="file" id="file-a" accept="image/*" />
</div>
<div class="upload-panel" id="drop-b" title="Click or drop image B">
<div class="upload-title">Image B</div>
<div class="upload-label" id="label-b">Drop or click to load</div>
<input type="file" id="file-b" accept="image/*" />
</div>
</div>
<div class="options-row">
<label>Align B</label>
<span class="opt-label">Scale</span>
<div class="range-group">
<input type="range" id="scale-b" min="0.25" max="4" step="0.05" value="1" />
<span class="range-val" id="scale-b-val">1.00×</span>
</div>
<span class="opt-label">Offset X</span>
<input type="number" id="offset-x" value="0" step="1" class="num-input" />
<span class="opt-label">Y</span>
<input type="number" id="offset-y" value="0" step="1" class="num-input" />
<button class="btn" id="auto-align-btn" disabled>Auto-align</button>
<button class="btn" id="reset-transform-btn">Reset</button>
<button class="btn" id="clear-btn">Clear</button>
</div>
<div class="options-row">
<label>Mode</label>
<select id="diff-mode">
<option value="highlight">Highlight differences</option>
<option value="heatmap">Heatmap</option>
<option value="greyscale">Greyscale diff</option>
<option value="a-minus-b">A minus B (signed)</option>
<option value="slider">Slider</option>
</select>
<label>Threshold</label>
<div class="range-group">
<input type="range" id="threshold" min="0" max="255" value="80" />
<span class="range-val" id="threshold-val">80</span>
</div>
</div>
<p class="mode-desc" id="mode-desc"></p>
<div class="stats-bar" id="stats-bar">
<div class="stat"><span class="stat-label">Dimensions</span><span class="stat-value" id="stat-dim">—</span></div>
<div class="stat"><span class="stat-label">Different pixels</span><span class="stat-value" id="stat-diff">—</span></div>
<div class="stat"><span class="stat-label">% different</span><span class="stat-value" id="stat-pct">—</span></div>
<div class="stat"><span class="stat-label">Max channel delta</span><span class="stat-value" id="stat-max">—</span></div>
<div class="stat"><span class="stat-label">Mean delta (different px)</span><span class="stat-value" id="stat-mean">—</span></div>
</div>
<div class="results-section" id="results">
<div class="rtab-panel active" id="rtab-diff">
<div class="canvas-wrap"><canvas id="canvas-diff" class="result"></canvas></div>
<div class="dl-row">
<button class="btn" id="dl-diff-btn">Download diff image</button>
<button class="btn" id="dl-diff-crop-btn">Download cropped diff</button>
</div>
</div>
<div class="rtab-panel" id="rtab-slider">
<div style="border:1px solid #e1e4e8;border-radius:6px;overflow:hidden;">
<div id="slider-wrap" style="position:relative;display:block;overflow:hidden;margin:0 auto;">
<canvas id="canvas-a-s" class="result"></canvas>
<canvas id="canvas-b-s" class="result" style="position:absolute;top:0;left:0;"></canvas>
<div id="slide-divider" style="position:absolute;top:0;bottom:0;left:50%;width:2px;background:#fff;box-shadow:0 0 4px rgba(0,0,0,0.6);transform:translateX(-50%);pointer-events:none;z-index:5;"></div>
<span style="position:absolute;top:8px;left:10px;background:rgba(0,0,0,0.5);color:#fff;font-size:11px;font-weight:600;padding:2px 6px;border-radius:3px;pointer-events:none;z-index:6;">A</span>
<span style="position:absolute;top:8px;right:10px;background:rgba(0,0,0,0.5);color:#fff;font-size:11px;font-weight:600;padding:2px 6px;border-radius:3px;pointer-events:none;z-index:6;">B</span>
<input type="range" id="slide-pos" min="0" max="100" value="50"
style="position:absolute;bottom:10px;left:0;width:100%;z-index:10;cursor:ew-resize;margin:0;padding:0;" />
</div>
</div>
<div class="dl-row">
<button class="btn" id="dl-slider-crop-btn">Download A & B cropped</button>
<button class="btn" id="dl-slider-side-btn">Download side by side</button>
</div>
</div>
</div>
<canvas id="canvas-a" style="display:none"></canvas>
<canvas id="canvas-b" style="display:none"></canvas>
<script type="module">
const fileA = document.getElementById("file-a");
const fileB = document.getElementById("file-b");
const dropA = document.getElementById("drop-a");
const dropB = document.getElementById("drop-b");
const labelA = document.getElementById("label-a");
const labelB = document.getElementById("label-b");
const clearBtn = document.getElementById("clear-btn");
const diffMode = document.getElementById("diff-mode");
const threshEl = document.getElementById("threshold");
const threshVal = document.getElementById("threshold-val");
const statsBar = document.getElementById("stats-bar");
const resultsEl = document.getElementById("results");
const scaleBEl = document.getElementById("scale-b");
const scaleBVal = document.getElementById("scale-b-val");
const offsetXEl = document.getElementById("offset-x");
const offsetYEl = document.getElementById("offset-y");
const autoAlignBtn = document.getElementById("auto-align-btn");
const resetBtn = document.getElementById("reset-transform-btn");
const canvasA = document.getElementById("canvas-a");
const canvasB = document.getElementById("canvas-b");
const canvasDiff = document.getElementById("canvas-diff");
const canvasAS = document.getElementById("canvas-a-s");
const canvasBS = document.getElementById("canvas-b-s");
const ctxA = canvasA.getContext("2d", { willReadFrequently: true });
const ctxB = canvasB.getContext("2d", { willReadFrequently: true });
const ctxD = canvasDiff.getContext("2d");
const ctxAS = canvasAS.getContext("2d");
const ctxBS = canvasBS.getContext("2d");
threshEl.addEventListener("input", () => {
threshVal.textContent = threshEl.value;
if (imgA && imgB) runDiff();
});
const modeDescEl = document.getElementById("mode-desc");
const modeDescriptions = {
"highlight": "Different pixels are shown in bright red; identical pixels are rendered as semi-transparent greyscale, keeping the original image visible beneath.",
"heatmap": "Difference intensity is mapped to colour: blue = identical, green = small difference, yellow = moderate, red = large difference.",
"greyscale": "Each pixel is shown as a shade of grey proportional to its difference — black means identical, white means maximum difference.",
"a-minus-b": "Shows the signed difference between A and B. Mid-grey (128) means no difference; lighter means A was brighter, darker means B was brighter.",
"slider": "Drag the divider to reveal Image A or B — a direct visual comparison with no pixel processing.",
};
function updateModeDesc() {
modeDescEl.textContent = modeDescriptions[diffMode.value] || "";
}
diffMode.addEventListener("change", updateModeDesc);
updateModeDesc();
scaleBEl.addEventListener("input", () => {
scaleBVal.textContent = parseFloat(scaleBEl.value).toFixed(2) + "×";
if (resultsEl.classList.contains("visible")) runDiff();
});
offsetXEl.addEventListener("input", () => { if (resultsEl.classList.contains("visible")) runDiff(); });
offsetYEl.addEventListener("input", () => { if (resultsEl.classList.contains("visible")) runDiff(); });
let imgA = null, imgB = null;
function setupDrop(dropEl, fileEl, label, setter) {
dropEl.addEventListener("click", () => { if (!dropEl.classList.contains("loaded")) fileEl.click(); });
fileEl.addEventListener("change", () => {
if (fileEl.files[0]) loadImage(fileEl.files[0], dropEl, label, setter);
});
dropEl.addEventListener("dragover", e => { e.preventDefault(); dropEl.classList.add("dragging"); });
dropEl.addEventListener("dragleave", () => dropEl.classList.remove("dragging"));
dropEl.addEventListener("drop", e => {
e.preventDefault();
dropEl.classList.remove("dragging");
if (e.dataTransfer.files[0]) loadImage(e.dataTransfer.files[0], dropEl, label, setter);
});
}
function loadImage(file, dropEl, labelEl, setter) {
if (!file.type.startsWith("image/")) return;
const reader = new FileReader();
reader.onload = e => {
const img = new Image();
img.onload = () => {
setter(img);
dropEl.classList.add("loaded");
dropEl.innerHTML = "";
const title = document.createElement("div");
title.className = "upload-title";
title.textContent = dropEl.id === "drop-a" ? "Image A" : "Image B";
const preview = document.createElement("img");
preview.src = e.target.result;
preview.alt = "preview";
const meta = document.createElement("div");
meta.className = "img-meta";
meta.textContent = `${img.naturalWidth} × ${img.naturalHeight} px`;
const changeLink = document.createElement("button");
changeLink.className = "btn";
changeLink.textContent = "Change image";
changeLink.style.marginTop = "6px";
changeLink.addEventListener("click", e => {
e.stopPropagation();
dropEl.id === "drop-a" ? fileA.click() : fileB.click();
});
dropEl.append(title, preview, meta, changeLink);
checkReady();
};
img.src = e.target.result;
};
reader.readAsDataURL(file);
}
setupDrop(dropA, fileA, labelA, img => imgA = img);
setupDrop(dropB, fileB, labelB, img => imgB = img);
function checkReady() {
const ready = !!(imgA && imgB);
autoAlignBtn.disabled = !ready;
if (ready) runDiff();
}
diffMode.addEventListener("change", () => { if (imgA && imgB) runDiff(); });
function getTransform() {
return {
sc: Math.max(0.01, parseFloat(scaleBEl.value) || 1.0),
ox: parseInt(offsetXEl.value) || 0,
oy: parseInt(offsetYEl.value) || 0,
};
}
const uploadGrid = document.querySelector(".upload-grid");
function getAvailWidth() { return uploadGrid.offsetWidth || 900; }
function showSlider() {
const { sc, ox, oy } = getTransform();
const W = Math.max(imgA.naturalWidth, imgB.naturalWidth);
const H = Math.max(imgA.naturalHeight, imgB.naturalHeight);
canvasAS.width = W; canvasAS.height = H;
canvasBS.width = W; canvasBS.height = H;
ctxAS.drawImage(imgA, 0, 0);
ctxBS.save();
ctxBS.translate(ox, oy);
ctxBS.scale(sc, sc);
ctxBS.drawImage(imgB, 0, 0);
ctxBS.restore();
const availW = getAvailWidth();
const fit = Math.min(1, availW / W, 600 / H);
const dW = Math.round(W * fit);
const dH = Math.round(H * fit);
canvasAS.style.width = dW + "px"; canvasAS.style.height = dH + "px";
canvasBS.style.width = dW + "px"; canvasBS.style.height = dH + "px";
document.getElementById("slider-wrap").style.width = dW + "px";
document.getElementById("rtab-diff").classList.remove("active");
document.getElementById("rtab-slider").classList.add("active");
resultsEl.classList.add("visible");
resultsEl.scrollIntoView({ behavior: "smooth", block: "nearest" });
setSlider(50);
}
function runDiff() {
if (diffMode.value === "slider") { showSlider(); return; }
const { sc, ox, oy } = getTransform();
const W = Math.max(imgA.naturalWidth, imgB.naturalWidth);
const H = Math.max(imgA.naturalHeight, imgB.naturalHeight);
const thresh = parseInt(threshEl.value);
canvasA.width = W; canvasA.height = H;
ctxA.clearRect(0, 0, W, H);
ctxA.drawImage(imgA, 0, 0);
canvasB.width = W; canvasB.height = H;
ctxB.clearRect(0, 0, W, H);
ctxB.save();
ctxB.translate(ox, oy);
ctxB.scale(sc, sc);
ctxB.drawImage(imgB, 0, 0);
ctxB.restore();
const dataA = ctxA.getImageData(0, 0, W, H).data;
const dataB = ctxB.getImageData(0, 0, W, H).data;
canvasDiff.width = W; canvasDiff.height = H;
const outId = ctxD.createImageData(W, H);
const out = outId.data;
const mode = diffMode.value;
let diffCount = 0, maxDelta = 0, sumDelta = 0;
for (let i = 0; i < W * H * 4; i += 4) {
const ra = dataA[i], ga = dataA[i+1], ba = dataA[i+2];
const rb = dataB[i], gb = dataB[i+1], bb = dataB[i+2];
const dr = Math.abs(ra - rb), dg = Math.abs(ga - gb), db = Math.abs(ba - bb);
const delta = Math.max(dr, dg, db);
const different = delta > thresh;
if (different) {
diffCount++;
if (delta > maxDelta) maxDelta = delta;
sumDelta += (dr + dg + db) / 3;
}
out[i + 3] = 255;
if (mode === "highlight") {
if (different) {
out[i] = 255; out[i+1] = 50; out[i+2] = 50;
} else {
const grey = Math.round(0.299 * ra + 0.587 * ga + 0.114 * ba);
out[i] = grey; out[i+1] = grey; out[i+2] = grey;
out[i+3] = 180;
}
} else if (mode === "heatmap") {
const t = delta / 255;
out[i] = Math.round(t < 0.5 ? 0 : (t - 0.5) * 2 * 255);
out[i+1] = Math.round(t < 0.5 ? t * 2 * 255 : (1 - (t - 0.5) * 2) * 255);
out[i+2] = Math.round(t < 0.5 ? (1 - t * 2) * 255 : 0);
} else if (mode === "greyscale") {
const g = delta;
out[i] = g; out[i+1] = g; out[i+2] = g;
} else if (mode === "a-minus-b") {
const rd = (ra - rb + 255) >> 1;
const gd = (ga - gb + 255) >> 1;
const bd = (ba - bb + 255) >> 1;
out[i] = rd; out[i+1] = gd; out[i+2] = bd;
}
}
ctxD.putImageData(outId, 0, 0);
canvasAS.width = W; canvasAS.height = H;
canvasBS.width = W; canvasBS.height = H;
ctxAS.drawImage(imgA, 0, 0);
ctxBS.save();
ctxBS.translate(ox, oy);
ctxBS.scale(sc, sc);
ctxBS.drawImage(imgB, 0, 0);
ctxBS.restore();
// Scale all result canvases to fit the viewing area
const availW = getAvailWidth();
const fit = Math.min(1, availW / W, 600 / H);
const dW = Math.round(W * fit);
const dH = Math.round(H * fit);
canvasDiff.style.width = dW + "px";
canvasDiff.style.height = dH + "px";
canvasAS.style.width = dW + "px";
canvasAS.style.height = dH + "px";
canvasBS.style.width = dW + "px";
canvasBS.style.height = dH + "px";
document.getElementById("slider-wrap").style.width = dW + "px";
const totalPx = W * H;
const pct = ((diffCount / totalPx) * 100).toFixed(2);
const meanDelta = diffCount > 0 ? (sumDelta / diffCount).toFixed(1) : "0";
document.getElementById("stat-dim").textContent = `${W} × ${H}`;
const sdEl = document.getElementById("stat-diff");
sdEl.textContent = diffCount.toLocaleString();
sdEl.className = "stat-value " + (diffCount === 0 ? "ok" : "bad");
const spEl = document.getElementById("stat-pct");
spEl.textContent = pct + "%";
spEl.className = "stat-value " + (parseFloat(pct) === 0 ? "ok" : "bad");
document.getElementById("stat-max").textContent = maxDelta;
document.getElementById("stat-mean").textContent = meanDelta;
document.getElementById("rtab-diff").classList.add("active");
document.getElementById("rtab-slider").classList.remove("active");
statsBar.classList.add("visible");
resultsEl.classList.add("visible");
resultsEl.scrollIntoView({ behavior: "smooth", block: "nearest" });
}
// --- Auto-align: SSD-based translation search at thumbnail scale ---
function toGrey(data) {
const n = data.length >>> 2;
const g = new Float32Array(n);
for (let i = 0, j = 0; i < data.length; i += 4, j++) {
g[j] = 0.299 * data[i] + 0.587 * data[i+1] + 0.114 * data[i+2];
}
return g;
}
function computeSSD(gA, wA, hA, gB, wB, hB, dx, dy) {
const xsA = dx > 0 ? dx : 0, xsB = dx < 0 ? -dx : 0;
const ysA = dy > 0 ? dy : 0, ysB = dy < 0 ? -dy : 0;
const ow = Math.min(wA - xsA, wB - xsB);
const oh = Math.min(hA - ysA, hB - ysB);
if (ow <= 0 || oh <= 0) return Infinity;
// Require the overlap to cover ≥25% of A's area.
// Using A (not min/max) as the reference means a tiny scaled-down B
// can never win just by fitting entirely inside a small uniform patch of A.
if (ow * oh < 0.25 * wA * hA) return Infinity;
let sum = 0;
for (let r = 0; r < oh; r++) {
const baseA = (ysA + r) * wA + xsA;
const baseB = (ysB + r) * wB + xsB;
for (let c = 0; c < ow; c++) {
const d = gA[baseA + c] - gB[baseB + c];
sum += d * d;
}
}
return sum / (ow * oh);
}
autoAlignBtn.addEventListener("click", () => {
autoAlignBtn.disabled = true;
autoAlignBtn.textContent = "Aligning…";
// Defer to let the browser repaint before the synchronous search
setTimeout(autoAlign, 10);
});
function autoAlign() {
const THUMB = 64;
// A thumbnail is the fixed reference — tScale is based on A's dimensions only
const tScale = THUMB / Math.max(imgA.naturalWidth, imgA.naturalHeight);
const taW = Math.max(1, Math.round(imgA.naturalWidth * tScale));
const taH = Math.max(1, Math.round(imgA.naturalHeight * tScale));
const ta = document.createElement("canvas");
ta.width = taW; ta.height = taH;
const ctA2 = ta.getContext("2d", { willReadFrequently: true });
ctA2.drawImage(imgA, 0, 0, taW, taH);
const gA = toGrey(ctA2.getImageData(0, 0, taW, taH).data);
// Candidate scales: standard range + natural A/B dimension ratios
const snap = s => Math.round(s * 20) / 20; // round to nearest 0.05
const rH = snap(imgA.naturalHeight / imgB.naturalHeight);
const rW = snap(imgA.naturalWidth / imgB.naturalWidth);
const SCALES = [...new Set(
[0.5, 0.6, 0.67, 0.75, 0.8, 0.85, 0.9, 0.95, 1.0, 1.05, 1.1, 1.25, 1.33, 1.5, 2.0, rH, rW]
.map(snap).filter(s => s >= 0.5 && s <= 2.0)
)].sort((a, b) => a - b);
let bestSSD = Infinity, bestDx = 0, bestDy = 0, bestSc = 1.0;
for (const sc of SCALES) {
const tbW = Math.max(1, Math.round(imgB.naturalWidth * sc * tScale));
const tbH = Math.max(1, Math.round(imgB.naturalHeight * sc * tScale));
const tb = document.createElement("canvas");
tb.width = tbW; tb.height = tbH;
const ctB2 = tb.getContext("2d", { willReadFrequently: true });
ctB2.drawImage(imgB, 0, 0, tbW, tbH);
const gB = toGrey(ctB2.getImageData(0, 0, tbW, tbH).data);
// Search offsets up to ±A-thumbnail dimensions (covers full image in full-res)
for (let dy = -taH; dy <= taH; dy++) {
for (let dx = -taW; dx <= taW; dx++) {
const s = computeSSD(gA, taW, taH, gB, tbW, tbH, dx, dy);
if (s < bestSSD) { bestSSD = s; bestDx = dx; bestDy = dy; bestSc = sc; }
}
}
}
scaleBEl.value = bestSc;
scaleBVal.textContent = bestSc.toFixed(2) + "×";
offsetXEl.value = Math.round(bestDx / tScale);
offsetYEl.value = Math.round(bestDy / tScale);
autoAlignBtn.disabled = false;
autoAlignBtn.textContent = "Auto-align";
runDiff();
}
resetBtn.addEventListener("click", () => {
scaleBEl.value = 1;
scaleBVal.textContent = "1.00×";
offsetXEl.value = 0;
offsetYEl.value = 0;
if (resultsEl.classList.contains("visible")) runDiff();
});
// Slider
const slidePos = document.getElementById("slide-pos");
const slideDivider = document.getElementById("slide-divider");
function setSlider(pct) {
slidePos.value = pct;
canvasBS.style.clipPath = `inset(0 0 0 ${pct}%)`;
slideDivider.style.left = pct + "%";
}
slidePos.addEventListener("input", () => setSlider(+slidePos.value));
// Download helpers
function dlCanvas(canvas, filename) {
const a = document.createElement("a");
a.href = canvas.toDataURL("image/png");
a.download = filename;
a.click();
}
function cropCanvas(src, x, y, w, h) {
const c = document.createElement("canvas");
c.width = w; c.height = h;
c.getContext("2d").drawImage(src, x, y, w, h, 0, 0, w, h);
return c;
}
function getIntersection() {
const { sc, ox, oy } = getTransform();
const x0 = Math.round(Math.max(0, ox));
const y0 = Math.round(Math.max(0, oy));
const x1 = Math.round(Math.min(imgA.naturalWidth, ox + imgB.naturalWidth * sc));
const y1 = Math.round(Math.min(imgA.naturalHeight, oy + imgB.naturalHeight * sc));
const w = x1 - x0, h = y1 - y0;
if (w <= 0 || h <= 0) return null;
return { x: x0, y: y0, w, h };
}
document.getElementById("dl-diff-btn").addEventListener("click", () => {
dlCanvas(canvasDiff, "image-diff.png");
});
document.getElementById("dl-diff-crop-btn").addEventListener("click", () => {
const r = getIntersection();
if (!r) { alert("The images do not overlap with the current alignment."); return; }
dlCanvas(cropCanvas(canvasDiff, r.x, r.y, r.w, r.h), "image-diff-cropped.png");
});
document.getElementById("dl-slider-crop-btn").addEventListener("click", () => {
const r = getIntersection();
if (!r) { alert("The images do not overlap with the current alignment."); return; }
dlCanvas(cropCanvas(canvasAS, r.x, r.y, r.w, r.h), "image-a-cropped.png");
setTimeout(() => dlCanvas(cropCanvas(canvasBS, r.x, r.y, r.w, r.h), "image-b-cropped.png"), 150);
});
document.getElementById("dl-slider-side-btn").addEventListener("click", () => {
const r = getIntersection();
if (!r) { alert("The images do not overlap with the current alignment."); return; }
const c = document.createElement("canvas");
c.width = r.w * 2; c.height = r.h;
const ctx = c.getContext("2d");
ctx.drawImage(canvasAS, r.x, r.y, r.w, r.h, 0, 0, r.w, r.h);
ctx.drawImage(canvasBS, r.x, r.y, r.w, r.h, r.w, 0, r.w, r.h);
dlCanvas(c, "image-a-b-side-by-side.png");
});
// Clear
clearBtn.addEventListener("click", () => {
imgA = imgB = null;
[dropA, dropB].forEach((el, i) => {
el.classList.remove("loaded");
el.innerHTML = `<div class="upload-title">Image ${i === 0 ? "A" : "B"}</div>
<div class="upload-label">Drop or click to load</div>`;
});
dropA.appendChild(fileA);
dropB.appendChild(fileB);
statsBar.classList.remove("visible");
resultsEl.classList.remove("visible");
autoAlignBtn.disabled = true;
scaleBEl.value = 1;
scaleBVal.textContent = "1.00×";
offsetXEl.value = 0;
offsetYEl.value = 0;
});
</script>
</body>
</html>