Rename Not NO_OUTPUT to DEBUG, remove TEST_MERGESORT and TEST_SPLIT* flags

This commit is contained in:
Aleksandr Karpinskii 2024-07-08 11:08:32 +04:00
parent dc53356c1a
commit d00fb87fa3

View File

@ -36,7 +36,8 @@
#define UINT32_MAX 0xffffffff #define UINT32_MAX 0xffffffff
#endif #endif
#define NO_OUTPUT // #define DEBUG
// #define TEST_NEAREST_NEIGHBOUR
typedef struct { typedef struct {
uint32_t scale; uint32_t scale;
@ -144,7 +145,7 @@ create_pixel_hash(Pixel *pixelData, uint32_t nPixels) {
PixelHashData *d; PixelHashData *d;
HashTable *hash; HashTable *hash;
uint32_t i; uint32_t i;
#ifndef NO_OUTPUT #ifdef DEBUG
uint32_t timer, timer2, timer3; uint32_t timer, timer2, timer3;
#endif #endif
@ -156,7 +157,7 @@ create_pixel_hash(Pixel *pixelData, uint32_t nPixels) {
hash = hashtable_new(pixel_hash, pixel_cmp); hash = hashtable_new(pixel_hash, pixel_cmp);
hashtable_set_user_data(hash, d); hashtable_set_user_data(hash, d);
d->scale = 0; d->scale = 0;
#ifndef NO_OUTPUT #ifdef DEBUG
timer = timer3 = clock(); timer = timer3 = clock();
#endif #endif
for (i = 0; i < nPixels; i++) { for (i = 0; i < nPixels; i++) {
@ -167,22 +168,22 @@ create_pixel_hash(Pixel *pixelData, uint32_t nPixels) {
} }
while (hashtable_get_count(hash) > MAX_HASH_ENTRIES) { while (hashtable_get_count(hash) > MAX_HASH_ENTRIES) {
d->scale++; d->scale++;
#ifndef NO_OUTPUT #ifdef DEBUG
printf("rehashing - new scale: %d\n", (int)d->scale); printf("rehashing - new scale: %d\n", (int)d->scale);
timer2 = clock(); timer2 = clock();
#endif #endif
hashtable_rehash_compute(hash, rehash_collide); hashtable_rehash_compute(hash, rehash_collide);
#ifndef NO_OUTPUT #ifdef DEBUG
timer2 = clock() - timer2; timer2 = clock() - timer2;
printf("rehash took %f sec\n", timer2 / (double)CLOCKS_PER_SEC); printf("rehash took %f sec\n", timer2 / (double)CLOCKS_PER_SEC);
timer += timer2; timer += timer2;
#endif #endif
} }
} }
#ifndef NO_OUTPUT #ifdef DEBUG
printf("inserts took %f sec\n", (clock() - timer) / (double)CLOCKS_PER_SEC); printf("inserts took %f sec\n", (clock() - timer) / (double)CLOCKS_PER_SEC);
#endif #endif
#ifndef NO_OUTPUT #ifdef DEBUG
printf("total %f sec\n", (clock() - timer3) / (double)CLOCKS_PER_SEC); printf("total %f sec\n", (clock() - timer3) / (double)CLOCKS_PER_SEC);
#endif #endif
return hash; return hash;
@ -304,7 +305,7 @@ mergesort_pixels(PixelList *head, int i) {
return head; return head;
} }
#if defined(TEST_MERGESORT) || defined(TEST_SORTED) #ifdef DEBUG
static int static int
test_sorted(PixelList *pl[3]) { test_sorted(PixelList *pl[3]) {
int i, n, l; int i, n, l;
@ -347,12 +348,12 @@ splitlists(
PixelList *l, *r, *c, *n; PixelList *l, *r, *c, *n;
int i; int i;
int nRight; int nRight;
#ifndef NO_OUTPUT #ifdef DEBUG
int nLeft; int nLeft;
#endif #endif
int splitColourVal; int splitColourVal;
#ifdef TEST_SPLIT #ifdef DEBUG
{ {
PixelList *_prevTest, *_nextTest; PixelList *_prevTest, *_nextTest;
int _i, _nextCount[3], _prevCount[3]; int _i, _nextCount[3], _prevCount[3];
@ -402,14 +403,14 @@ splitlists(
#endif #endif
nCount[0] = nCount[1] = 0; nCount[0] = nCount[1] = 0;
nRight = 0; nRight = 0;
#ifndef NO_OUTPUT #ifdef DEBUG
nLeft = 0; nLeft = 0;
#endif #endif
for (left = 0, c = h[axis]; c;) { for (left = 0, c = h[axis]; c;) {
left = left + c->count; left = left + c->count;
nCount[0] += c->count; nCount[0] += c->count;
c->flag = 0; c->flag = 0;
#ifndef NO_OUTPUT #ifdef DEBUG
nLeft++; nLeft++;
#endif #endif
c = c->next[axis]; c = c->next[axis];
@ -424,7 +425,7 @@ splitlists(
break; break;
} }
c->flag = 0; c->flag = 0;
#ifndef NO_OUTPUT #ifdef DEBUG
nLeft++; nLeft++;
#endif #endif
nCount[0] += c->count; nCount[0] += c->count;
@ -442,14 +443,14 @@ splitlists(
} }
c->flag = 1; c->flag = 1;
nRight++; nRight++;
#ifndef NO_OUTPUT #ifdef DEBUG
nLeft--; nLeft--;
#endif #endif
nCount[0] -= c->count; nCount[0] -= c->count;
nCount[1] += c->count; nCount[1] += c->count;
} }
} }
#ifndef NO_OUTPUT #ifdef DEBUG
if (!nLeft) { if (!nLeft) {
for (c = h[axis]; c; c = c->next[axis]) { for (c = h[axis]; c; c = c->next[axis]) {
printf("[%d %d %d]\n", c->p.c.r, c->p.c.g, c->p.c.b); printf("[%d %d %d]\n", c->p.c.r, c->p.c.g, c->p.c.b);
@ -511,7 +512,7 @@ split(BoxNode *node) {
gl = node->tail[1]->p.c.g; gl = node->tail[1]->p.c.g;
bh = node->head[2]->p.c.b; bh = node->head[2]->p.c.b;
bl = node->tail[2]->p.c.b; bl = node->tail[2]->p.c.b;
#ifdef TEST_SPLIT #ifdef DEBUG
printf("splitting node [%d %d %d] [%d %d %d] ", rl, gl, bl, rh, gh, bh); printf("splitting node [%d %d %d] [%d %d %d] ", rl, gl, bl, rh, gh, bh);
#endif #endif
f[0] = (rh - rl) * 77; f[0] = (rh - rl) * 77;
@ -526,11 +527,8 @@ split(BoxNode *node) {
axis = i; axis = i;
} }
} }
#ifdef TEST_SPLIT #ifdef DEBUG
printf("along axis %d\n", axis + 1); printf("along axis %d\n", axis + 1);
#endif
#ifdef TEST_SPLIT
{ {
PixelList *_prevTest, *_nextTest; PixelList *_prevTest, *_nextTest;
int _i, _nextCount[3], _prevCount[3]; int _i, _nextCount[3], _prevCount[3];
@ -592,12 +590,12 @@ split(BoxNode *node) {
if (!splitlists( if (!splitlists(
node->head, node->tail, heads, tails, newCounts, axis, node->pixelCount node->head, node->tail, heads, tails, newCounts, axis, node->pixelCount
)) { )) {
#ifndef NO_OUTPUT #ifdef DEBUG
printf("list split failed.\n"); printf("list split failed.\n");
#endif #endif
return 0; return 0;
} }
#ifdef TEST_SPLIT #ifdef DEBUG
if (!test_sorted(heads[0])) { if (!test_sorted(heads[0])) {
printf("bug in split"); printf("bug in split");
exit(1); exit(1);
@ -623,7 +621,7 @@ split(BoxNode *node) {
node->head[i] = NULL; node->head[i] = NULL;
node->tail[i] = NULL; node->tail[i] = NULL;
} }
#ifdef TEST_SPLIT #ifdef DEBUG
if (left->head[0]) { if (left->head[0]) {
rh = left->head[0]->p.c.r; rh = left->head[0]->p.c.r;
rl = left->tail[0]->p.c.r; rl = left->tail[0]->p.c.r;
@ -687,7 +685,7 @@ median_cut(PixelList *hl[3], uint32_t imPixelCount, int nPixels) {
} }
} while (compute_box_volume(thisNode) == 1); } while (compute_box_volume(thisNode) == 1);
if (!split(thisNode)) { if (!split(thisNode)) {
#ifndef NO_OUTPUT #ifdef DEBUG
printf("Oops, split failed...\n"); printf("Oops, split failed...\n");
#endif #endif
exit(1); exit(1);
@ -716,16 +714,14 @@ free_box_tree(BoxNode *n) {
free(n); free(n);
} }
#ifdef TEST_SPLIT_INTEGRITY #ifdef DEBUG
static int static int
checkContained(BoxNode *n, Pixel *pp) { checkContained(BoxNode *n, Pixel *pp) {
if (n->l && n->r) { if (n->l && n->r) {
return checkContained(n->l, pp) + checkContained(n->r, pp); return checkContained(n->l, pp) + checkContained(n->r, pp);
} }
if (n->l || n->r) { if (n->l || n->r) {
#ifndef NO_OUTPUT
printf("box tree is dead\n"); printf("box tree is dead\n");
#endif
return 0; return 0;
} }
if (pp->c.r <= n->head[0]->p.c.r && pp->c.r >= n->tail[0]->p.c.r && if (pp->c.r <= n->head[0]->p.c.r && pp->c.r >= n->tail[0]->p.c.r &&
@ -746,7 +742,7 @@ annotate_hash_table(BoxNode *n, HashTable *h, uint32_t *box) {
return annotate_hash_table(n->l, h, box) && annotate_hash_table(n->r, h, box); return annotate_hash_table(n->l, h, box) && annotate_hash_table(n->r, h, box);
} }
if (n->l || n->r) { if (n->l || n->r) {
#ifndef NO_OUTPUT #ifdef DEBUG
printf("box tree is dead\n"); printf("box tree is dead\n");
#endif #endif
return 0; return 0;
@ -754,7 +750,7 @@ annotate_hash_table(BoxNode *n, HashTable *h, uint32_t *box) {
for (p = n->head[0]; p; p = p->next[0]) { for (p = n->head[0]; p; p = p->next[0]) {
PIXEL_UNSCALE(&(p->p), &q, d->scale); PIXEL_UNSCALE(&(p->p), &q, d->scale);
if (!hashtable_insert(h, q, *box)) { if (!hashtable_insert(h, q, *box)) {
#ifndef NO_OUTPUT #ifdef DEBUG
printf("hashtable insert failed\n"); printf("hashtable insert failed\n");
#endif #endif
return 0; return 0;
@ -978,7 +974,7 @@ map_image_pixels_from_median_box(
continue; continue;
} }
if (!hashtable_lookup(medianBoxHash, pixelData[i], &pixelVal)) { if (!hashtable_lookup(medianBoxHash, pixelData[i], &pixelVal)) {
#ifndef NO_OUTPUT #ifdef DEBUG
printf("pixel lookup failed\n"); printf("pixel lookup failed\n");
#endif #endif
return 0; return 0;
@ -1043,7 +1039,7 @@ compute_palette_from_median_cut(
} }
} }
for (i = 0; i < nPixels; i++) { for (i = 0; i < nPixels; i++) {
#ifdef TEST_SPLIT_INTEGRITY #ifdef DEBUG
if (!(i % 100)) { if (!(i % 100)) {
printf("%05d\r", i); printf("%05d\r", i);
fflush(stdout); fflush(stdout);
@ -1058,7 +1054,7 @@ compute_palette_from_median_cut(
} }
#endif #endif
if (!hashtable_lookup(medianBoxHash, pixelData[i], &paletteEntry)) { if (!hashtable_lookup(medianBoxHash, pixelData[i], &paletteEntry)) {
#ifndef NO_OUTPUT #ifdef DEBUG
printf("pixel lookup failed\n"); printf("pixel lookup failed\n");
#endif #endif
for (i = 0; i < 3; i++) { for (i = 0; i < 3; i++) {
@ -1068,7 +1064,7 @@ compute_palette_from_median_cut(
return 0; return 0;
} }
if (paletteEntry >= nPaletteEntries) { if (paletteEntry >= nPaletteEntries) {
#ifndef NO_OUTPUT #ifdef DEBUG
printf( printf(
"panic - paletteEntry>=nPaletteEntries (%d>=%d)\n", "panic - paletteEntry>=nPaletteEntries (%d>=%d)\n",
(int)paletteEntry, (int)paletteEntry,
@ -1140,7 +1136,7 @@ compute_palette_from_quantized_pixels(
} }
for (i = 0; i < nPixels; i++) { for (i = 0; i < nPixels; i++) {
if (qp[i] >= nPaletteEntries) { if (qp[i] >= nPaletteEntries) {
#ifndef NO_OUTPUT #ifdef DEBUG
printf("scream\n"); printf("scream\n");
#endif #endif
return 0; return 0;
@ -1208,7 +1204,7 @@ k_means(
goto error_2; goto error_2;
} }
#ifndef NO_OUTPUT #ifdef DEBUG
printf("["); printf("[");
fflush(stdout); fflush(stdout);
#endif #endif
@ -1243,7 +1239,7 @@ k_means(
if (changes < 0) { if (changes < 0) {
goto error_3; goto error_3;
} }
#ifndef NO_OUTPUT #ifdef DEBUG
printf(".(%d)", changes); printf(".(%d)", changes);
fflush(stdout); fflush(stdout);
#endif #endif
@ -1251,7 +1247,7 @@ k_means(
break; break;
} }
} }
#ifndef NO_OUTPUT #ifdef DEBUG
printf("]\n"); printf("]\n");
#endif #endif
if (avgDistSortKey) { if (avgDistSortKey) {
@ -1311,32 +1307,32 @@ quantize(
uint32_t **avgDistSortKey; uint32_t **avgDistSortKey;
Pixel *p; Pixel *p;
#ifndef NO_OUTPUT #ifdef DEBUG
uint32_t timer, timer2; uint32_t timer, timer2;
#endif #endif
#ifndef NO_OUTPUT #ifdef DEBUG
timer2 = clock(); timer2 = clock();
printf("create hash table..."); printf("create hash table...");
fflush(stdout); fflush(stdout);
timer = clock(); timer = clock();
#endif #endif
h = create_pixel_hash(pixelData, nPixels); h = create_pixel_hash(pixelData, nPixels);
#ifndef NO_OUTPUT #ifdef DEBUG
printf("done (%f)\n", (clock() - timer) / (double)CLOCKS_PER_SEC); printf("done (%f)\n", (clock() - timer) / (double)CLOCKS_PER_SEC);
#endif #endif
if (!h) { if (!h) {
goto error_0; goto error_0;
} }
#ifndef NO_OUTPUT #ifdef DEBUG
printf("create lists from hash table..."); printf("create lists from hash table...");
fflush(stdout); fflush(stdout);
timer = clock(); timer = clock();
#endif #endif
hl[0] = hl[1] = hl[2] = NULL; hl[0] = hl[1] = hl[2] = NULL;
hashtable_foreach(h, hash_to_list, hl); hashtable_foreach(h, hash_to_list, hl);
#ifndef NO_OUTPUT #ifdef DEBUG
printf("done (%f)\n", (clock() - timer) / (double)CLOCKS_PER_SEC); printf("done (%f)\n", (clock() - timer) / (double)CLOCKS_PER_SEC);
#endif #endif
@ -1344,7 +1340,7 @@ quantize(
goto error_1; goto error_1;
} }
#ifndef NO_OUTPUT #ifdef DEBUG
printf("mergesort lists..."); printf("mergesort lists...");
fflush(stdout); fflush(stdout);
timer = clock(); timer = clock();
@ -1352,39 +1348,37 @@ quantize(
for (i = 0; i < 3; i++) { for (i = 0; i < 3; i++) {
hl[i] = mergesort_pixels(hl[i], i); hl[i] = mergesort_pixels(hl[i], i);
} }
#ifdef TEST_MERGESORT #ifdef DEBUG
if (!test_sorted(hl)) { if (!test_sorted(hl)) {
printf("bug in mergesort\n"); printf("bug in mergesort\n");
goto error_1; goto error_1;
} }
#endif
#ifndef NO_OUTPUT
printf("done (%f)\n", (clock() - timer) / (double)CLOCKS_PER_SEC); printf("done (%f)\n", (clock() - timer) / (double)CLOCKS_PER_SEC);
#endif #endif
#ifndef NO_OUTPUT #ifdef DEBUG
printf("median cut..."); printf("median cut...");
fflush(stdout); fflush(stdout);
timer = clock(); timer = clock();
#endif #endif
root = median_cut(hl, nPixels, nQuantPixels); root = median_cut(hl, nPixels, nQuantPixels);
#ifndef NO_OUTPUT #ifdef DEBUG
printf("done (%f)\n", (clock() - timer) / (double)CLOCKS_PER_SEC); printf("done (%f)\n", (clock() - timer) / (double)CLOCKS_PER_SEC);
#endif #endif
if (!root) { if (!root) {
goto error_1; goto error_1;
} }
nPaletteEntries = 0; nPaletteEntries = 0;
#ifndef NO_OUTPUT #ifdef DEBUG
printf("median cut tree to hash table..."); printf("median cut tree to hash table...");
fflush(stdout); fflush(stdout);
timer = clock(); timer = clock();
#endif #endif
annotate_hash_table(root, h, &nPaletteEntries); annotate_hash_table(root, h, &nPaletteEntries);
#ifndef NO_OUTPUT #ifdef DEBUG
printf("done (%f)\n", (clock() - timer) / (double)CLOCKS_PER_SEC); printf("done (%f)\n", (clock() - timer) / (double)CLOCKS_PER_SEC);
#endif #endif
#ifndef NO_OUTPUT #ifdef DEBUG
printf("compute palette...\n"); printf("compute palette...\n");
fflush(stdout); fflush(stdout);
timer = clock(); timer = clock();
@ -1392,7 +1386,7 @@ quantize(
if (!compute_palette_from_median_cut(pixelData, nPixels, h, &p, nPaletteEntries)) { if (!compute_palette_from_median_cut(pixelData, nPixels, h, &p, nPaletteEntries)) {
goto error_3; goto error_3;
} }
#ifndef NO_OUTPUT #ifdef DEBUG
printf("done (%f)\n", (clock() - timer) / (double)CLOCKS_PER_SEC); printf("done (%f)\n", (clock() - timer) / (double)CLOCKS_PER_SEC);
#endif #endif
@ -1479,7 +1473,7 @@ quantize(
hashtable_free(h2); hashtable_free(h2);
} }
#endif #endif
#ifndef NO_OUTPUT #ifdef DEBUG
printf("k means...\n"); printf("k means...\n");
fflush(stdout); fflush(stdout);
timer = clock(); timer = clock();
@ -1487,7 +1481,7 @@ quantize(
if (kmeans > 0) { if (kmeans > 0) {
k_means(pixelData, nPixels, p, nPaletteEntries, qp, kmeans - 1); k_means(pixelData, nPixels, p, nPaletteEntries, qp, kmeans - 1);
} }
#ifndef NO_OUTPUT #ifdef DEBUG
printf("done (%f)\n", (clock() - timer) / (double)CLOCKS_PER_SEC); printf("done (%f)\n", (clock() - timer) / (double)CLOCKS_PER_SEC);
#endif #endif
@ -1495,7 +1489,7 @@ quantize(
*palette = p; *palette = p;
*paletteLength = nPaletteEntries; *paletteLength = nPaletteEntries;
#ifndef NO_OUTPUT #ifdef DEBUG
printf("cleanup..."); printf("cleanup...");
fflush(stdout); fflush(stdout);
timer = clock(); timer = clock();
@ -1507,7 +1501,7 @@ quantize(
free(avgDistSortKey); free(avgDistSortKey);
} }
destroy_pixel_hash(h); destroy_pixel_hash(h);
#ifndef NO_OUTPUT #ifdef DEBUG
printf("done (%f)\n", (clock() - timer) / (double)CLOCKS_PER_SEC); printf("done (%f)\n", (clock() - timer) / (double)CLOCKS_PER_SEC);
printf("-----\ntotal time %f\n", (clock() - timer2) / (double)CLOCKS_PER_SEC); printf("-----\ntotal time %f\n", (clock() - timer2) / (double)CLOCKS_PER_SEC);
#endif #endif