-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMainController.js
More file actions
253 lines (208 loc) · 7.7 KB
/
Copy pathMainController.js
File metadata and controls
253 lines (208 loc) · 7.7 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
import { HistogramMath } from '../../domain/models/HistogramMath.js';
import { strings } from '../../shared/i18n/strings.js';
export class MainController {
constructor(view, loadUseCase, equalizeUseCase, expandUseCase, chartRenderer) {
this.view = view;
this.loadUseCase = loadUseCase;
this.equalizeUseCase = equalizeUseCase;
this.expandUseCase = expandUseCase;
this.chartRenderer = chartRenderer;
this.currentImageModel = null;
this.currentHistogram = null;
this.lastOperation = null;
this.init();
}
init() {
this.view.bindFileSelected(this.handleFileSelected.bind(this));
this.view.bindEqualize(this.handleEqualize.bind(this));
this.view.bindExpand(this.handleExpand.bind(this));
this.view.bindShowMath(this.handleShowMath.bind(this));
this.view.bindError(this.handleError.bind(this));
// Set WASM status to ready
const topNavBar = document.querySelector('top-nav-bar');
if (topNavBar) {
topNavBar.setWasmStatus('Ready');
}
}
/**
* Compute histogram statistics (Min, Max, Mean, Std Dev) from frequencies.
* @param {import('../../domain/models/HistogramModel.js').HistogramModel} histogram
* @returns {{ min: number, max: number, mean: number, std: number }}
*/
computeMetrics(histogram) {
const freq = histogram.getFrequencies();
const totalPixels = freq.reduce((sum, v) => sum + v, 0);
if (totalPixels === 0) return { min: 0, max: 0, mean: 0, std: 0 };
// Find min/max intensity levels with non-zero frequency
let min = 0;
let max = 255;
for (let i = 0; i < 256; i++) {
if (freq[i] > 0) { min = i; break; }
}
for (let i = 255; i >= 0; i--) {
if (freq[i] > 0) { max = i; break; }
}
// Weighted mean
let sum = 0;
for (let i = 0; i < 256; i++) {
sum += i * freq[i];
}
const mean = sum / totalPixels;
// Weighted standard deviation
let varianceSum = 0;
for (let i = 0; i < 256; i++) {
varianceSum += freq[i] * (i - mean) ** 2;
}
const std = Math.sqrt(varianceSum / totalPixels);
return {
min,
max,
mean: Math.round(mean * 100) / 100,
std: Math.round(std * 100) / 100,
};
}
async handleFileSelected(file) {
try {
this.view.hideError();
this.view.disableControls();
this.view.hideMathButton();
this.view.resetToOriginal();
this.view.hideResultHistogram();
this.lastOperation = null;
// Load file as base64
const base64Data = await this.loadUseCase.execute(file);
// We must wait for the hidden image to load the base64 data to process it
this.view.setHiddenImageSrc(base64Data, () => {
try {
this.view.showCanvas();
// Execute extraction and validation using the use case
this.currentImageModel = this.loadUseCase.executeMathematicalExtraction(
this.view.getHiddenImageId(),
this.view.getWorkspaceCanvasId()
);
if (!this.currentImageModel.isStrictGrayscale) {
console.info("Color image detected. It has been converted to grayscale automatically.");
}
// Valid Grayscale image
const metadata = this.currentImageModel.getMetadata();
this.view.updateImageInfo(
metadata.width,
metadata.height,
1 // Channels
);
// Update thumbnail from workspace canvas
const workspaceCanvas = this.view.workspace.getCanvas();
this.view.updateThumbnail(workspaceCanvas);
// Render Original Histogram
this.currentHistogram = this.currentImageModel.getHistogram();
this.chartRenderer.render(
this.view.getOriginalHistogramCanvas(),
this.currentHistogram
);
// Show original histogram metrics
const originalMetrics = this.computeMetrics(this.currentHistogram);
this.view.showMetrics('original-metrics', originalMetrics);
// Show histogram containers and hide empty state
this.view.showHistogramContainers();
this.view.hideEmptyStates();
// Hide result metrics (no processing yet)
this.view.hideMetrics('result-metrics');
// Compute Math Visualizations
this.histogramMath = new HistogramMath(this.currentHistogram);
this.chartRenderer.renderMath(
this.view.getMathEqCanvas(),
this.histogramMath,
"equalization"
);
this.chartRenderer.renderMath(
this.view.getMathExpCanvas(),
this.histogramMath,
"expansion"
);
this.view.switchToVisualTab();
this.view.enableControls();
} catch (error) {
console.error(error);
this.view.showError(strings.errors.processingFailed);
}
});
} catch (error) {
console.error(error);
this.view.showError(strings.errors.loadFailed);
}
}
handleEqualize() {
try {
this.lastOperation = 'equalize';
const newHistogram = this.equalizeUseCase.execute(
this.view.getHiddenImageId(),
this.view.getProcessedCanvasId()
);
this.view.showProcessedCanvas();
this.view.showMathButton();
this.view.showResultHistogram();
// Dispatch processed state change event
this.view.workspace.dispatchEvent(new CustomEvent('on-processed-state-changed', {
bubbles: true,
composed: true,
detail: { processed: true }
}));
// Update thumbnail from processed canvas
const processedCanvas = this.view.workspace.getProcessedCanvas();
this.view.updateThumbnail(processedCanvas);
this.chartRenderer.render(
this.view.getResultHistogramCanvas(),
newHistogram
);
// Show result histogram metrics
const resultMetrics = this.computeMetrics(newHistogram);
this.view.showMetrics('result-metrics', resultMetrics);
this.view.switchToVisualTab();
} catch (error) {
console.error(error);
this.view.showError(strings.errors.equalizeFailed);
}
}
handleExpand() {
try {
this.lastOperation = 'expand';
const newHistogram = this.expandUseCase.execute(
this.view.getHiddenImageId(),
this.view.getProcessedCanvasId()
);
this.view.showProcessedCanvas();
this.view.showMathButton();
this.view.showResultHistogram();
// Dispatch processed state change event
this.view.workspace.dispatchEvent(new CustomEvent('on-processed-state-changed', {
bubbles: true,
composed: true,
detail: { processed: true }
}));
// Update thumbnail from processed canvas
const processedCanvas = this.view.workspace.getProcessedCanvas();
this.view.updateThumbnail(processedCanvas);
this.chartRenderer.render(
this.view.getResultHistogramCanvas(),
newHistogram
);
// Show result histogram metrics
const resultMetrics = this.computeMetrics(newHistogram);
this.view.showMetrics('result-metrics', resultMetrics);
this.view.switchToVisualTab();
} catch (error) {
console.error(error);
this.view.showError(strings.errors.expandFailed);
}
}
handleShowMath() {
if (this.lastOperation === 'expand') {
this.view.switchToMathExpTab();
} else {
this.view.switchToMathEqTab();
}
}
handleError(message) {
this.view.showError(message);
}
}