From 6028bb419e702950454757637e389ccb9b8c9ac5 Mon Sep 17 00:00:00 2001 From: Vftdan Date: Thu, 3 Oct 2024 11:26:45 +0200 Subject: [PATCH] Avoid duplicate palette entries Don't make an item an empty cluster warp candidate, if it is already warp candidate for another cluster --- img2cpi.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/img2cpi.c b/img2cpi.c index 960bb54..9b3fb11 100644 --- a/img2cpi.c +++ b/img2cpi.c @@ -711,9 +711,21 @@ bool k_means_iteration(struct k_means_state *state) { closest_distance = dist; closest_cluster = cluster; } - if (dist <= state->centroid_intermediate[cluster].closest_present_distance) { - state->centroid_intermediate[cluster].closest_present_item = item; - state->centroid_intermediate[cluster].closest_present_distance = dist; + if (dist < state->centroid_intermediate[cluster].closest_present_distance) { + bool can_update = true; + for (int other_cluster = 0; other_cluster < state->clusters->count; other_cluster++) { + if (other_cluster == cluster) { + continue; + } + if (state->centroid_intermediate[other_cluster].closest_present_item.v == item.v) { + can_update = false; + break; + } + } + if (can_update) { + state->centroid_intermediate[cluster].closest_present_item = item; + state->centroid_intermediate[cluster].closest_present_distance = dist; + } } } if (!changed) {