Skip to content

Commit 72e81d5

Browse files
Update tests
1 parent a6cd96e commit 72e81d5

File tree

4 files changed

+57
-12
lines changed

4 files changed

+57
-12
lines changed

public/app.js

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,16 +148,41 @@ function pointDansPolygone(px, py, sommets) {
148148
return dedans;
149149
}
150150

151-
function couleurImageKMeans(pixels, larg, haut, maxSamples = 96) {
151+
function couleurImageMoyenne(pixels, larg, haut, maxSamples = 96) {
152152
const total = larg * haut;
153+
if (total <= 0) return [0, 0, 0];
153154
const step = Math.max(1, Math.floor(total / maxSamples));
154-
km_init(3);
155+
let rTot = 0, gTot = 0, bTot = 0, compte = 0;
155156
for (let i = 0; i < total; i += step) {
156157
const idx = i * 4;
157-
km_ajouter(pixels[idx], pixels[idx + 1], pixels[idx + 2]);
158+
rTot += pixels[idx];
159+
gTot += pixels[idx + 1];
160+
bTot += pixels[idx + 2];
161+
compte++;
162+
}
163+
if (compte === 0) return [0, 0, 0];
164+
return [
165+
couleur_moyenne(rTot, compte),
166+
couleur_moyenne(gTot, compte),
167+
couleur_moyenne(bTot, compte),
168+
];
169+
}
170+
171+
function couleurImageKMeans(pixels, larg, haut, maxSamples = 96) {
172+
try {
173+
const total = larg * haut;
174+
const step = Math.max(1, Math.floor(total / maxSamples));
175+
km_init(3);
176+
for (let i = 0; i < total; i += step) {
177+
const idx = i * 4;
178+
km_ajouter(pixels[idx], pixels[idx + 1], pixels[idx + 2]);
179+
}
180+
km_calculer(20);
181+
return km_resultat();
182+
} catch (err) {
183+
console.warn("[pixel2polygon] K-means indisponible, repli sur la moyenne :", err);
184+
return couleurImageMoyenne(pixels, larg, haut, maxSamples);
158185
}
159-
km_calculer(20);
160-
return km_resultat();
161186
}
162187

163188
function couleurTuile(pixels, larg, haut, sommets) {

public/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ <h4>Compilation</h4>
268268

269269
</main>
270270

271-
<script type="module" src="app.js?v=2026-03-31-3"></script>
271+
<script type="module" src="app.js?v=2026-03-31-4"></script>
272272
</body>
273273

274274
</html>

src/hexagonify_wasm.ml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,11 @@ déf km_calculer(max_iter):
177177
si n == 0:
178178
retour 0
179179
k = min(_km_k, n)
180-
soit c_r = [_km_r[entier(i * n / k)] pour i dans range(k)]
181-
soit c_g = [_km_g[entier(i * n / k)] pour i dans range(k)]
182-
soit c_b = [_km_b[entier(i * n / k)] pour i dans range(k)]
180+
si k <= 0:
181+
retour 0
182+
soit c_r = [_km_r[min(n - 1, i * n // k)] pour i dans range(k)]
183+
soit c_g = [_km_g[min(n - 1, i * n // k)] pour i dans range(k)]
184+
soit c_b = [_km_b[min(n - 1, i * n // k)] pour i dans range(k)]
183185
soit labels = [0 pour _ dans range(n)]
184186
pour _ dans range(entier(max_iter)):
185187
soit change = 0
@@ -194,8 +196,6 @@ déf km_calculer(max_iter):
194196
si labels[i] != best:
195197
labels[i] = best
196198
change = 1
197-
si change == 0:
198-
retour 0
199199
pour c dans range(k):
200200
soit sr = 0.0
201201
soit sg = 0.0

tests/smoke.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ function testHtmlSmoke() {
182182
assert.match(html, /id="btn-apply"/);
183183
assert.match(html, /id="tab-github"/);
184184
assert.match(html, />GitHub</);
185-
assert.match(html, /src="app\.js"/);
185+
assert.match(html, /src="app\.js(?:\?[^"]+)?"\s*><\/script>/);
186186
}
187187

188188
function testMethodChangeTriggersRender() {
@@ -408,12 +408,32 @@ function testKMeansSamplingStaysUnderWasmLimit() {
408408
assert.ok(sampleCount <= 100, `expected at most 100 k-means samples, got ${sampleCount}`);
409409
}
410410

411+
function testKMeansFallsBackToMeanWhenWasmThrows() {
412+
const { api } = buildHarness();
413+
api.setWasm({
414+
couleur_moyenne(total, count) { return Math.round(total / count); },
415+
km_init() { throw new Error("index out of bounds"); },
416+
km_ajouter() {},
417+
km_calculer() {},
418+
km_r() { return 0; },
419+
km_g() { return 0; },
420+
km_b() { return 0; },
421+
});
422+
423+
const pixels = new Uint8ClampedArray([
424+
10, 20, 30, 255,
425+
30, 40, 50, 255,
426+
]);
427+
assert.deepStrictEqual(api.couleurImageKMeans(pixels, 2, 1, 96), [20, 30, 40]);
428+
}
429+
411430
async function run() {
412431
testHtmlSmoke();
413432
testMethodChangeTriggersRender();
414433
testTabSwitching();
415434
testAllMethodsGenerateTiles();
416435
testKMeansSamplingStaysUnderWasmLimit();
436+
testKMeansFallsBackToMeanWhenWasmThrows();
417437
await testFlowerImageProducesTilesInWasm();
418438
await testRenderSanitizesTileSizeBeforeWasm();
419439
await testRenderSkipsInvalidTilesWithoutCrashing();

0 commit comments

Comments
 (0)