From 121189c1426b8a1f8621cd2711571b4cf58ca9fd Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Thu, 27 Aug 2026 15:55:07 +0300 Subject: [PATCH 1/8] Remove NDEBUG bits from Mode.c NDEBUG is "not debug"; the debug checks were _enabled_ when it was set --- src/libImaging/Mode.c | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) diff --git a/src/libImaging/Mode.c b/src/libImaging/Mode.c index fc29e9baeed..f3ef7a2c18e 100644 --- a/src/libImaging/Mode.c +++ b/src/libImaging/Mode.c @@ -1,11 +1,6 @@ #include "Mode.h" #include -#ifdef NDEBUG -#include -#include -#endif - const ModeData MODES[] = { [IMAGING_MODE_UNKNOWN] = {""}, @@ -28,12 +23,7 @@ findModeID(const char *const name) { return IMAGING_MODE_UNKNOWN; } for (size_t i = 0; i < sizeof(MODES) / sizeof(*MODES); i++) { -#ifdef NDEBUG - if (MODES[i].name == NULL) { - fprintf(stderr, "Mode ID %zu is not defined.\n", (size_t)i); - } else -#endif - if (strcmp(MODES[i].name, name) == 0) { + if (strcmp(MODES[i].name, name) == 0) { return (ModeID)i; } } @@ -230,12 +220,7 @@ findRawModeID(const char *const name) { return IMAGING_RAWMODE_UNKNOWN; } for (size_t i = 0; i < sizeof(RAWMODES) / sizeof(*RAWMODES); i++) { -#ifdef NDEBUG - if (RAWMODES[i].name == NULL) { - fprintf(stderr, "Rawmode ID %zu is not defined.\n", (size_t)i); - } else -#endif - if (strcmp(RAWMODES[i].name, name) == 0) { + if (strcmp(RAWMODES[i].name, name) == 0) { return (RawModeID)i; } } From a1f2e0842c1feb0d50d4a518efd2d69a1f5dbc5e Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Thu, 27 Aug 2026 15:59:34 +0300 Subject: [PATCH 2/8] Remove #ifdef VERBOSE debug prints from _imaging.c --- src/_imaging.c | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/_imaging.c b/src/_imaging.c index e946bd37bb3..9dc282b74ac 100644 --- a/src/_imaging.c +++ b/src/_imaging.c @@ -165,10 +165,6 @@ PyImagingNew(Imaging imOut) { return NULL; } -#ifdef VERBOSE - printf("imaging %p allocated\n", imagep); -#endif - imagep->image = imOut; imagep->access = ImagingAccessNew(imOut); @@ -177,10 +173,6 @@ PyImagingNew(Imaging imOut) { static void _dealloc(ImagingObject *imagep) { -#ifdef VERBOSE - printf("imaging %p deleted\n", imagep); -#endif - if (imagep->access) { ImagingAccessDelete(imagep->image, imagep->access); } From 025c0a49611df5ed4de8be95f0eca52a9215b8ba Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Thu, 27 Aug 2026 16:00:35 +0300 Subject: [PATCH 3/8] Remove debug code from Quant/QuantHeap --- src/libImaging/Quant.c | 354 ------------------------------------- src/libImaging/QuantHeap.c | 37 ---- 2 files changed, 391 deletions(-) diff --git a/src/libImaging/Quant.c b/src/libImaging/Quant.c index 99c2dbac3af..38b2b28e8bc 100644 --- a/src/libImaging/Quant.c +++ b/src/libImaging/Quant.c @@ -20,10 +20,7 @@ #include "Imaging.h" -#include #include -#include -#include #include "QuantTypes.h" #include "QuantOctree.h" @@ -36,7 +33,6 @@ #define UINT32_MAX 0xffffffff #endif -// #define DEBUG // #define TEST_NEAREST_NEIGHBOUR typedef struct { @@ -145,9 +141,6 @@ create_pixel_hash(Pixel *pixelData, uint32_t nPixels) { PixelHashData *d; HashTable *hash; uint32_t i; -#ifdef DEBUG - uint32_t timer, timer2, timer3; -#endif /* malloc check ok, small constant allocation */ d = malloc(sizeof(PixelHashData)); @@ -157,9 +150,6 @@ create_pixel_hash(Pixel *pixelData, uint32_t nPixels) { hash = hashtable_new(pixel_hash, pixel_cmp); hashtable_set_user_data(hash, d); d->scale = 0; -#ifdef DEBUG - timer = timer3 = clock(); -#endif for (i = 0; i < nPixels; i++) { if (!hashtable_insert_or_update_computed( hash, pixelData[i], new_count_func, exists_count_func @@ -168,24 +158,9 @@ create_pixel_hash(Pixel *pixelData, uint32_t nPixels) { } while (hashtable_get_count(hash) > MAX_HASH_ENTRIES) { d->scale++; -#ifdef DEBUG - printf("rehashing - new scale: %d\n", (int)d->scale); - timer2 = clock(); -#endif hashtable_rehash_compute(hash, rehash_collide); -#ifdef DEBUG - timer2 = clock() - timer2; - printf("rehash took %f sec\n", timer2 / (double)CLOCKS_PER_SEC); - timer += timer2; -#endif } } -#ifdef DEBUG - printf("inserts took %f sec\n", (clock() - timer) / (double)CLOCKS_PER_SEC); -#endif -#ifdef DEBUG - printf("total %f sec\n", (clock() - timer3) / (double)CLOCKS_PER_SEC); -#endif return hash; } @@ -305,25 +280,6 @@ mergesort_pixels(PixelList *head, int i) { return head; } -#ifdef DEBUG -static int -test_sorted(PixelList *pl[3]) { - int i, l; - PixelList *t; - - for (i = 0; i < 3; i++) { - l = 256; - for (t = pl[i]; t; t = t->next[i]) { - if (l < t->p.a.v[i]) { - return 0; - } - l = t->p.a.v[i]; - } - } - return 1; -} -#endif - static int box_heap_cmp(const Heap *h, const void *A, const void *B) { BoxNode *a = (BoxNode *)A; @@ -348,71 +304,14 @@ splitlists( PixelList *l, *r, *c, *n; int i; int nRight; -#ifdef DEBUG - int nLeft; -#endif int splitColourVal; -#ifdef DEBUG - { - PixelList *_prevTest, *_nextTest; - int _i, _nextCount[3], _prevCount[3]; - for (_i = 0; _i < 3; _i++) { - for (_nextCount[_i] = 0, _nextTest = h[_i]; - _nextTest && _nextTest->next[_i]; - _nextTest = _nextTest->next[_i], _nextCount[_i]++); - for (_prevCount[_i] = 0, _prevTest = t[_i]; - _prevTest && _prevTest->prev[_i]; - _prevTest = _prevTest->prev[_i], _prevCount[_i]++); - if (_nextTest != t[_i]) { - printf("next-list of axis %d does not end at tail\n", _i); - exit(1); - } - if (_prevTest != h[_i]) { - printf("prev-list of axis %d does not end at head\n", _i); - exit(1); - } - for (; _nextTest && _nextTest->prev[_i]; _nextTest = _nextTest->prev[_i]); - for (; _prevTest && _prevTest->next[_i]; _prevTest = _prevTest->next[_i]); - if (_nextTest != h[_i]) { - printf("next-list of axis %d does not loop back to head\n", _i); - exit(1); - } - if (_prevTest != t[_i]) { - printf("prev-list of axis %d does not loop back to tail\n", _i); - exit(1); - } - } - for (_i = 1; _i < 3; _i++) { - if (_prevCount[_i] != _prevCount[_i - 1] || - _nextCount[_i] != _nextCount[_i - 1] || - _prevCount[_i] != _nextCount[_i]) { - printf( - "{%d %d %d} {%d %d %d}\n", - _prevCount[0], - _prevCount[1], - _prevCount[2], - _nextCount[0], - _nextCount[1], - _nextCount[2] - ); - exit(1); - } - } - } -#endif nCount[0] = nCount[1] = 0; nRight = 0; -#ifdef DEBUG - nLeft = 0; -#endif for (left = 0, c = h[axis]; c;) { left = left + c->count; nCount[0] += c->count; c->flag = 0; -#ifdef DEBUG - nLeft++; -#endif c = c->next[axis]; if (left * 2 > pixelCount) { break; @@ -425,9 +324,6 @@ splitlists( break; } c->flag = 0; -#ifdef DEBUG - nLeft++; -#endif nCount[0] += c->count; } } @@ -443,21 +339,10 @@ splitlists( } c->flag = 1; nRight++; -#ifdef DEBUG - nLeft--; -#endif nCount[0] -= c->count; nCount[1] += c->count; } } -#ifdef DEBUG - if (!nLeft) { - 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("warning... trivial split\n"); - } -#endif for (i = 0; i < 3; i++) { l = r = NULL; @@ -512,9 +397,6 @@ split(BoxNode *node) { gl = node->tail[1]->p.c.g; bh = node->head[2]->p.c.b; bl = node->tail[2]->p.c.b; -#ifdef DEBUG - printf("splitting node [%d %d %d] [%d %d %d] ", rl, gl, bl, rh, gh, bh); -#endif f[0] = (rh - rl) * 77; f[1] = (gh - gl) * 150; f[2] = (bh - bl) * 29; @@ -527,84 +409,12 @@ split(BoxNode *node) { axis = i; } } -#ifdef DEBUG - printf("along axis %d\n", axis + 1); - { - PixelList *_prevTest, *_nextTest; - int _i, _nextCount[3], _prevCount[3]; - for (_i = 0; _i < 3; _i++) { - if (node->tail[_i]->next[_i]) { - printf("tail is not tail\n"); - printf( - "node->tail[%d]->next[%d]=%p\n", _i, _i, node->tail[_i]->next[_i] - ); - } - if (node->head[_i]->prev[_i]) { - printf("head is not head\n"); - printf( - "node->head[%d]->prev[%d]=%p\n", _i, _i, node->head[_i]->prev[_i] - ); - } - } - - for (_i = 0; _i < 3; _i++) { - for (_nextCount[_i] = 0, _nextTest = node->head[_i]; - _nextTest && _nextTest->next[_i]; - _nextTest = _nextTest->next[_i], _nextCount[_i]++); - for (_prevCount[_i] = 0, _prevTest = node->tail[_i]; - _prevTest && _prevTest->prev[_i]; - _prevTest = _prevTest->prev[_i], _prevCount[_i]++); - if (_nextTest != node->tail[_i]) { - printf("next-list of axis %d does not end at tail\n", _i); - } - if (_prevTest != node->head[_i]) { - printf("prev-list of axis %d does not end at head\n", _i); - } - for (; _nextTest && _nextTest->prev[_i]; _nextTest = _nextTest->prev[_i]); - for (; _prevTest && _prevTest->next[_i]; _prevTest = _prevTest->next[_i]); - if (_nextTest != node->head[_i]) { - printf("next-list of axis %d does not loop back to head\n", _i); - } - if (_prevTest != node->tail[_i]) { - printf("prev-list of axis %d does not loop back to tail\n", _i); - } - } - for (_i = 1; _i < 3; _i++) { - if (_prevCount[_i] != _prevCount[_i - 1] || - _nextCount[_i] != _nextCount[_i - 1] || - _prevCount[_i] != _nextCount[_i]) { - printf( - "{%d %d %d} {%d %d %d}\n", - _prevCount[0], - _prevCount[1], - _prevCount[2], - _nextCount[0], - _nextCount[1], - _nextCount[2] - ); - } - } - } -#endif node->axis = axis; if (!splitlists( node->head, node->tail, heads, tails, newCounts, axis, node->pixelCount )) { -#ifdef DEBUG - printf("list split failed.\n"); -#endif return 0; } -#ifdef DEBUG - if (!test_sorted(heads[0])) { - printf("bug in split"); - exit(1); - } - if (!test_sorted(heads[1])) { - printf("bug in split"); - exit(1); - } -#endif /* malloc check ok, small constant allocation */ left = malloc(sizeof(BoxNode)); right = malloc(sizeof(BoxNode)); @@ -621,26 +431,6 @@ split(BoxNode *node) { node->head[i] = NULL; node->tail[i] = NULL; } -#ifdef DEBUG - if (left->head[0]) { - rh = left->head[0]->p.c.r; - rl = left->tail[0]->p.c.r; - gh = left->head[1]->p.c.g; - gl = left->tail[1]->p.c.g; - bh = left->head[2]->p.c.b; - bl = left->tail[2]->p.c.b; - printf(" left node [%3d %3d %3d] [%3d %3d %3d]\n", rl, gl, bl, rh, gh, bh); - } - if (right->head[0]) { - rh = right->head[0]->p.c.r; - rl = right->tail[0]->p.c.r; - gh = right->head[1]->p.c.g; - gl = right->tail[1]->p.c.g; - bh = right->head[2]->p.c.b; - bl = right->tail[2]->p.c.b; - printf(" right node [%3d %3d %3d] [%3d %3d %3d]\n", rl, gl, bl, rh, gh, bh); - } -#endif left->l = left->r = NULL; right->l = right->r = NULL; left->axis = right->axis = -1; @@ -685,9 +475,6 @@ median_cut(PixelList *hl[3], uint32_t imPixelCount, int nPixels) { } } while (compute_box_volume(thisNode) == 1); if (!split(thisNode)) { -#ifdef DEBUG - printf("Oops, split failed...\n"); -#endif exit(1); } ImagingQuantHeapAdd(h, (void *)(thisNode->l)); @@ -714,25 +501,6 @@ free_box_tree(BoxNode *n) { free(n); } -#ifdef DEBUG -static int -checkContained(BoxNode *n, Pixel *pp) { - if (n->l && n->r) { - return checkContained(n->l, pp) + checkContained(n->r, pp); - } - if (n->l || n->r) { - printf("box tree is dead\n"); - return 0; - } - if (pp->c.r <= n->head[0]->p.c.r && pp->c.r >= n->tail[0]->p.c.r && - pp->c.g <= n->head[1]->p.c.g && pp->c.g >= n->tail[1]->p.c.g && - pp->c.b <= n->head[2]->p.c.b && pp->c.b >= n->tail[2]->p.c.b) { - return 1; - } - return 0; -} -#endif - static int annotate_hash_table(BoxNode *n, HashTable *h, uint32_t *box) { PixelList *p; @@ -742,17 +510,11 @@ 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); } if (n->l || n->r) { -#ifdef DEBUG - printf("box tree is dead\n"); -#endif return 0; } for (p = n->head[0]; p; p = p->next[0]) { PIXEL_UNSCALE(&(p->p), &q, d->scale); if (!hashtable_insert(h, q, *box)) { -#ifdef DEBUG - printf("hashtable insert failed\n"); -#endif return 0; } } @@ -974,9 +736,6 @@ map_image_pixels_from_median_box( continue; } if (!hashtable_lookup(medianBoxHash, pixelData[i], &pixelVal)) { -#ifdef DEBUG - printf("pixel lookup failed\n"); -#endif return 0; } initialdist = _DISTSQR(paletteData + pixelVal, pixelData + i); @@ -1040,24 +799,7 @@ compute_palette_from_median_cut( } } for (i = 0; i < nPixels; i++) { -#ifdef DEBUG - if (!(i % 100)) { - printf("%05d\r", i); - fflush(stdout); - } - if (checkContained(root, pixelData + i) > 1) { - printf("pixel in two boxes\n"); - for (i = 0; i < 3; i++) { - free(avg[i]); - } - free(count); - return 0; - } -#endif if (!hashtable_lookup(medianBoxHash, pixelData[i], &paletteEntry)) { -#ifdef DEBUG - printf("pixel lookup failed\n"); -#endif for (i = 0; i < 3; i++) { free(avg[i]); } @@ -1065,13 +807,6 @@ compute_palette_from_median_cut( return 0; } if (paletteEntry >= nPaletteEntries) { -#ifdef DEBUG - printf( - "panic - paletteEntry>=nPaletteEntries (%d>=%d)\n", - (int)paletteEntry, - (int)nPaletteEntries - ); -#endif for (i = 0; i < 3; i++) { free(avg[i]); } @@ -1137,9 +872,6 @@ compute_palette_from_quantized_pixels( } for (i = 0; i < nPixels; i++) { if (qp[i] >= nPaletteEntries) { -#ifdef DEBUG - printf("scream\n"); -#endif return 0; } avg[0][qp[i]] += pixelData[i].c.r; @@ -1205,10 +937,6 @@ k_means( goto error_2; } -#ifdef DEBUG - printf("["); - fflush(stdout); -#endif while (1) { if (!built) { compute_palette_from_quantized_pixels( @@ -1240,17 +968,10 @@ k_means( if (changes < 0) { goto error_3; } -#ifdef DEBUG - printf(".(%d)", changes); - fflush(stdout); -#endif if (changes <= threshold) { break; } } -#ifdef DEBUG - printf("]\n"); -#endif if (avgDistSortKey) { free(avgDistSortKey); } @@ -1308,92 +1029,34 @@ quantize( uint32_t **avgDistSortKey; Pixel *p; -#ifdef DEBUG - uint32_t timer, timer2; -#endif - -#ifdef DEBUG - timer2 = clock(); - printf("create hash table..."); - fflush(stdout); - timer = clock(); -#endif h = create_pixel_hash(pixelData, nPixels); -#ifdef DEBUG - printf("done (%f)\n", (clock() - timer) / (double)CLOCKS_PER_SEC); -#endif if (!h) { goto error_0; } -#ifdef DEBUG - printf("create lists from hash table..."); - fflush(stdout); - timer = clock(); -#endif hl[0] = hl[1] = hl[2] = NULL; hashtable_foreach(h, hash_to_list, hl); -#ifdef DEBUG - printf("done (%f)\n", (clock() - timer) / (double)CLOCKS_PER_SEC); -#endif if (!hl[0]) { goto error_1; } -#ifdef DEBUG - printf("mergesort lists..."); - fflush(stdout); - timer = clock(); -#endif for (i = 0; i < 3; i++) { hl[i] = mergesort_pixels(hl[i], i); } -#ifdef DEBUG - if (!test_sorted(hl)) { - printf("bug in mergesort\n"); - goto error_1; - } - printf("done (%f)\n", (clock() - timer) / (double)CLOCKS_PER_SEC); -#endif - -#ifdef DEBUG - printf("median cut..."); - fflush(stdout); - timer = clock(); -#endif root = median_cut(hl, nPixels, nQuantPixels); -#ifdef DEBUG - printf("done (%f)\n", (clock() - timer) / (double)CLOCKS_PER_SEC); -#endif if (!root) { goto error_1; } nPaletteEntries = 0; -#ifdef DEBUG - printf("median cut tree to hash table..."); - fflush(stdout); - timer = clock(); -#endif if (!annotate_hash_table(root, h, &nPaletteEntries)) { goto error_3; } -#ifdef DEBUG - printf("done (%f)\n", (clock() - timer) / (double)CLOCKS_PER_SEC); -#endif -#ifdef DEBUG - printf("compute palette...\n"); - fflush(stdout); - timer = clock(); -#endif if (!compute_palette_from_median_cut( pixelData, nPixels, h, &p, nPaletteEntries, root )) { goto error_3; } -#ifdef DEBUG - printf("done (%f)\n", (clock() - timer) / (double)CLOCKS_PER_SEC); -#endif free_box_tree(root); root = NULL; @@ -1477,28 +1140,15 @@ quantize( } hashtable_free(h2); } -#endif -#ifdef DEBUG - printf("k means...\n"); - fflush(stdout); - timer = clock(); #endif if (kmeans > 0) { k_means(pixelData, nPixels, p, nPaletteEntries, qp, kmeans - 1); } -#ifdef DEBUG - printf("done (%f)\n", (clock() - timer) / (double)CLOCKS_PER_SEC); -#endif *quantizedPixels = qp; *palette = p; *paletteLength = nPaletteEntries; -#ifdef DEBUG - printf("cleanup..."); - fflush(stdout); - timer = clock(); -#endif if (avgDist) { free(avgDist); } @@ -1506,10 +1156,6 @@ quantize( free(avgDistSortKey); } destroy_pixel_hash(h); -#ifdef DEBUG - printf("done (%f)\n", (clock() - timer) / (double)CLOCKS_PER_SEC); - printf("-----\ntotal time %f\n", (clock() - timer2) / (double)CLOCKS_PER_SEC); -#endif return 1; error_7: diff --git a/src/libImaging/QuantHeap.c b/src/libImaging/QuantHeap.c index 6fb52d8902e..20f17a6ebef 100644 --- a/src/libImaging/QuantHeap.c +++ b/src/libImaging/QuantHeap.c @@ -33,13 +33,6 @@ struct _Heap { #define INITIAL_SIZE 256 -// #define DEBUG - -#ifdef DEBUG -static int -_heap_test(Heap *); -#endif - void ImagingQuantHeapFree(Heap *h) { free(h->heap); @@ -71,24 +64,6 @@ _heap_grow(Heap *h, unsigned int newsize) { return 1; } -#ifdef DEBUG -static int -_heap_test(Heap *h) { - unsigned int k; - for (k = 1; k * 2 <= h->heapcount; k++) { - if (h->cf(h, h->heap[k], h->heap[k * 2]) < 0) { - printf("heap is bad\n"); - return 0; - } - if (k * 2 + 1 <= h->heapcount && h->cf(h, h->heap[k], h->heap[k * 2 + 1]) < 0) { - printf("heap is bad\n"); - return 0; - } - } - return 1; -} -#endif - int ImagingQuantHeapRemove(Heap *h, void **r) { unsigned int k, l; @@ -112,12 +87,6 @@ ImagingQuantHeapRemove(Heap *h, void **r) { h->heap[k] = h->heap[l]; } h->heap[k] = v; -#ifdef DEBUG - if (!_heap_test(h)) { - printf("oops - heap_remove messed up the heap\n"); - exit(1); - } -#endif return 1; } @@ -136,12 +105,6 @@ ImagingQuantHeapAdd(Heap *h, void *val) { k >>= 1; } h->heap[k] = val; -#ifdef DEBUG - if (!_heap_test(h)) { - printf("oops - heap_add messed up the heap\n"); - exit(1); - } -#endif return 1; } From ec3e3bac2c12d92c50fd751b49c7008e87130847 Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Thu, 27 Aug 2026 16:01:15 +0300 Subject: [PATCH 4/8] Remove TEST_NEAREST_NEIGHBOUR code from Quant.c --- src/libImaging/Quant.c | 51 ------------------------------------------ 1 file changed, 51 deletions(-) diff --git a/src/libImaging/Quant.c b/src/libImaging/Quant.c index 38b2b28e8bc..1ffe6dce385 100644 --- a/src/libImaging/Quant.c +++ b/src/libImaging/Quant.c @@ -33,8 +33,6 @@ #define UINT32_MAX 0xffffffff #endif -// #define TEST_NEAREST_NEIGHBOUR - typedef struct { uint32_t scale; } PixelHashData; @@ -1092,55 +1090,6 @@ quantize( goto error_7; } -#ifdef TEST_NEAREST_NEIGHBOUR -#include - { - uint32_t bestmatch, bestdist, dist; - HashTable *h2; - printf("nearest neighbour search (full search)..."); - fflush(stdout); - timer = clock(); - h2 = hashtable_new(unshifted_pixel_hash, unshifted_pixel_cmp); - for (i = 0; i < nPixels; i++) { - if (hashtable_lookup(h2, pixelData[i], &paletteEntry)) { - bestmatch = paletteEntry; - } else { - bestmatch = 0; - bestdist = _SQR(pixelData[i].c.r - p[0].c.r) + - _SQR(pixelData[i].c.g - p[0].c.g) + - _SQR(pixelData[i].c.b - p[0].c.b); - for (j = 1; j < nPaletteEntries; j++) { - dist = _SQR(pixelData[i].c.r - p[j].c.r) + - _SQR(pixelData[i].c.g - p[j].c.g) + - _SQR(pixelData[i].c.b - p[j].c.b); - if (dist == bestdist && j == qp[i]) { - bestmatch = j; - } - if (dist < bestdist) { - bestdist = dist; - bestmatch = j; - } - } - hashtable_insert(h2, pixelData[i], bestmatch); - } - if (qp[i] != bestmatch) { - printf( - "discrepancy in matching algorithms pixel %d [%d %d] %f %f\n", - i, - qp[i], - bestmatch, - sqrt((double)(_SQR(pixelData[i].c.r - p[qp[i]].c.r) + - _SQR(pixelData[i].c.g - p[qp[i]].c.g) + - _SQR(pixelData[i].c.b - p[qp[i]].c.b))), - sqrt((double)(_SQR(pixelData[i].c.r - p[bestmatch].c.r) + - _SQR(pixelData[i].c.g - p[bestmatch].c.g) + - _SQR(pixelData[i].c.b - p[bestmatch].c.b))) - ); - } - } - hashtable_free(h2); - } -#endif if (kmeans > 0) { k_means(pixelData, nPixels, p, nPaletteEntries, qp, kmeans - 1); } From 84c95267e5f6274ca372c7224b09f36a0b88b624 Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Thu, 27 Aug 2026 16:02:40 +0300 Subject: [PATCH 5/8] Quant.c: do not exit program if node split fails --- src/libImaging/Quant.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libImaging/Quant.c b/src/libImaging/Quant.c index 1ffe6dce385..ff6106a2c7a 100644 --- a/src/libImaging/Quant.c +++ b/src/libImaging/Quant.c @@ -473,7 +473,8 @@ median_cut(PixelList *hl[3], uint32_t imPixelCount, int nPixels) { } } while (compute_box_volume(thisNode) == 1); if (!split(thisNode)) { - exit(1); + ImagingQuantHeapFree(h); + return NULL; } ImagingQuantHeapAdd(h, (void *)(thisNode->l)); ImagingQuantHeapAdd(h, (void *)(thisNode->r)); From d3cf685a5a94f33849b9f4e45ecf91b6a4694c82 Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Thu, 27 Aug 2026 16:04:08 +0300 Subject: [PATCH 6/8] Quant.c: Remove MSVC9.0 compat code, use standard --- src/libImaging/Quant.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/libImaging/Quant.c b/src/libImaging/Quant.c index ff6106a2c7a..683750da00f 100644 --- a/src/libImaging/Quant.c +++ b/src/libImaging/Quant.c @@ -21,6 +21,7 @@ #include "Imaging.h" #include +#include #include "QuantTypes.h" #include "QuantOctree.h" @@ -28,11 +29,6 @@ #include "QuantHash.h" #include "QuantHeap.h" -/* MSVC9.0 */ -#ifndef UINT32_MAX -#define UINT32_MAX 0xffffffff -#endif - typedef struct { uint32_t scale; } PixelHashData; From 3bdfe10628e02ac48994b6416a46ed37e7e0a3a6 Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Tue, 1 Sep 2026 12:31:12 +0300 Subject: [PATCH 7/8] Remove unreachable dbg code from Webp --- src/_webp.c | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/src/_webp.c b/src/_webp.c index 9f9fbf3978a..1731ed9ed61 100644 --- a/src/_webp.c +++ b/src/_webp.c @@ -675,7 +675,6 @@ WebPEncode_wrapper(PyObject *self, PyObject *args) { WebPData exif = {exif_bytes, i_exif_size}; WebPData xmp = {xmp_bytes, i_xmp_size}; WebPMuxError err; - int dbg = 0; int copy_data = 0; // value 1 indicates given data WILL be copied to the mux // and value 0 indicates data will NOT be copied. @@ -687,41 +686,21 @@ WebPEncode_wrapper(PyObject *self, PyObject *args) { } WebPMuxSetImage(mux, &image, copy_data); - if (dbg) { - /* was getting %ld icc_size == 0, icc_size>0 was true */ - fprintf(stderr, "icc size %d, %d \n", i_icc_size, i_icc_size > 0); - } - if (i_icc_size > 0) { - if (dbg) { - fprintf(stderr, "Adding ICC Profile\n"); - } err = WebPMuxSetChunk(mux, "ICCP", &icc_profile, copy_data); if (err != WEBP_MUX_OK) { return HandleMuxError(err, "ICCP"); } } - if (dbg) { - fprintf(stderr, "exif size %d \n", i_exif_size); - } if (i_exif_size > 0) { - if (dbg) { - fprintf(stderr, "Adding Exif Data\n"); - } err = WebPMuxSetChunk(mux, "EXIF", &exif, copy_data); if (err != WEBP_MUX_OK) { return HandleMuxError(err, "EXIF"); } } - if (dbg) { - fprintf(stderr, "xmp size %d \n", i_xmp_size); - } if (i_xmp_size > 0) { - if (dbg) { - fprintf(stderr, "Adding XMP Data\n"); - } err = WebPMuxSetChunk(mux, "XMP ", &xmp, copy_data); if (err != WEBP_MUX_OK) { return HandleMuxError(err, "XMP "); From 16931ce20b1a22f1e8ff7973f0314f791d63b62e Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Tue, 1 Sep 2026 12:35:11 +0300 Subject: [PATCH 8/8] Remove TIFF TRACE/dump_state debug code ("legacy, don't enable for Python 3.x") --- src/decode.c | 2 - src/encode.c | 11 -- src/libImaging/TiffDecode.c | 265 +----------------------------------- src/libImaging/TiffDecode.h | 12 -- 4 files changed, 1 insertion(+), 289 deletions(-) diff --git a/src/decode.c b/src/decode.c index bdb8b0a2dbc..a8a34b59efa 100644 --- a/src/decode.c +++ b/src/decode.c @@ -522,8 +522,6 @@ PyImaging_LibTiffDecoderNew(PyObject *self, PyObject *args) { const ModeID mode = findModeID(mode_name); const RawModeID rawmode = findRawModeID(rawmode_name); - TRACE(("new tiff decoder %s\n", compname)); - decoder = PyImaging_DecoderNew(sizeof(TIFFSTATE)); if (decoder == NULL) { return NULL; diff --git a/src/encode.c b/src/encode.c index bffc32938e8..1744000a74f 100644 --- a/src/encode.c +++ b/src/encode.c @@ -718,7 +718,6 @@ PyImaging_LibTiffEncoderNew(PyObject *self, PyObject *args) { return NULL; } else { tags_size = PyList_Size(tags); - TRACE(("tags size: %d\n", (int)tags_size)); for (pos = 0; pos < tags_size; pos++) { item = PyList_GetItemRef(tags, pos); if (item == NULL) { @@ -739,8 +738,6 @@ PyImaging_LibTiffEncoderNew(PyObject *self, PyObject *args) { return NULL; } - TRACE(("new tiff encoder %s fp: %d, filename: %s \n", compname, fp, filename)); - encoder = PyImaging_EncoderNew(sizeof(TIFFSTATE)); if (encoder == NULL) { return NULL; @@ -865,7 +862,6 @@ PyImaging_LibTiffEncoderNew(PyObject *self, PyObject *args) { ); } else if (is_var_length) { Py_ssize_t len, i; - TRACE(("Setting from Tuple: %d \n", key_int)); len = PyTuple_Size(value); if (key_int == TIFFTAG_COLORMAP) { @@ -1050,17 +1046,10 @@ PyImaging_LibTiffEncoderNew(PyObject *self, PyObject *args) { status = ImagingLibTiffSetField( &encoder->state, (ttag_t)key_int, (uint64_t)PyLong_AsLongLong(value) ); - } else { - TRACE( - ("Unhandled type for key %d : %s \n", - key_int, - PyBytes_AsString(PyObject_Str(value))) - ); } } Py_DECREF(item); if (!status) { - TRACE(("Error setting Field\n")); Py_DECREF(encoder); PyErr_SetString(PyExc_RuntimeError, "Error setting from dictionary"); return NULL; diff --git a/src/libImaging/TiffDecode.c b/src/libImaging/TiffDecode.c index 8a57c523dad..6e43a3e598c 100644 --- a/src/libImaging/TiffDecode.c +++ b/src/libImaging/TiffDecode.c @@ -36,18 +36,6 @@ #define fd_to_tiff_fd(fd) ((int)_get_osfhandle(fd)) #endif -void -dump_state(const TIFFSTATE *state) { - TRACE( - ("State: Location %u size %d eof %d data: %p ifd: %d\n", - (uint)state->loc, - (int)state->size, - (uint)state->eof, - state->data, - state->ifd) - ); -} - /* procs for TIFFOpenClient */ @@ -57,9 +45,6 @@ _tiffReadProc(thandle_t hdata, tdata_t buf, tsize_t size) { TIFFSTATE *state = (TIFFSTATE *)hdata; tsize_t to_read; - TRACE(("_tiffReadProc: %d \n", (int)size)); - dump_state(state); - if (state->loc > state->eof) { TIFFError( "_tiffReadProc", @@ -70,12 +55,10 @@ _tiffReadProc(thandle_t hdata, tdata_t buf, tsize_t size) { return 0; } to_read = MIN(size, MIN(state->size, (tsize_t)state->eof) - (tsize_t)state->loc); - TRACE(("to_read: %d\n", (int)to_read)); _TIFFmemcpy(buf, (UINT8 *)state->data + state->loc, to_read); state->loc += (toff_t)to_read; - TRACE(("location: %u\n", (uint)state->loc)); return to_read; } @@ -84,9 +67,6 @@ _tiffWriteProc(thandle_t hdata, tdata_t buf, tsize_t size) { TIFFSTATE *state = (TIFFSTATE *)hdata; tsize_t to_write; - TRACE(("_tiffWriteProc: %d \n", (int)size)); - dump_state(state); - to_write = MIN(size, state->size - (tsize_t)state->loc); if (state->flrealloc && size > to_write) { tdata_t new_data; @@ -98,7 +78,6 @@ _tiffWriteProc(thandle_t hdata, tdata_t buf, tsize_t size) { newsize += 64 * 1024; // newsize*=2; // UNDONE, by 64k chunks? } - TRACE(("Reallocing in write to %d bytes\n", (int)newsize)); /* malloc check ok, overflow checked above */ new_data = realloc(state->data, newsize); if (!new_data) { @@ -110,13 +89,10 @@ _tiffWriteProc(thandle_t hdata, tdata_t buf, tsize_t size) { to_write = size; } - TRACE(("to_write: %d\n", (int)to_write)); - _TIFFmemcpy((UINT8 *)state->data + state->loc, buf, to_write); state->loc += (toff_t)to_write; state->eof = MAX(state->loc, state->eof); - dump_state(state); return to_write; } @@ -124,8 +100,6 @@ toff_t _tiffSeekProc(thandle_t hdata, toff_t off, int whence) { TIFFSTATE *state = (TIFFSTATE *)hdata; - TRACE(("_tiffSeekProc: off: %u whence: %d \n", (uint)off, whence)); - dump_state(state); switch (whence) { case 0: state->loc = off; @@ -137,40 +111,25 @@ _tiffSeekProc(thandle_t hdata, toff_t off, int whence) { state->loc = state->eof + off; break; } - dump_state(state); return state->loc; } int _tiffCloseProc(thandle_t hdata) { - TIFFSTATE *state = (TIFFSTATE *)hdata; - - TRACE(("_tiffCloseProc \n")); - dump_state(state); - return 0; } toff_t _tiffSizeProc(thandle_t hdata) { TIFFSTATE *state = (TIFFSTATE *)hdata; - - TRACE(("_tiffSizeProc \n")); - dump_state(state); - return (toff_t)state->size; } int _tiffMapProc(thandle_t hdata, tdata_t *pbase, toff_t *psize) { TIFFSTATE *state = (TIFFSTATE *)hdata; - - TRACE(("_tiffMapProc input size: %u, data: %p\n", (uint)*psize, *pbase)); - dump_state(state); - *pbase = state->data; *psize = state->size; - TRACE(("_tiffMapProc returning size: %u, data: %p\n", (uint)*psize, *pbase)); return (1); } @@ -184,7 +143,6 @@ _tiffNullMapProc(thandle_t hdata, tdata_t *pbase, toff_t *psize) { void _tiffUnmapProc(thandle_t hdata, tdata_t base, toff_t size) { - TRACE(("_tiffUnMapProc\n")); (void)hdata; (void)base; (void)size; @@ -194,26 +152,6 @@ int ImagingLibTiffInit(ImagingCodecState state, int fp, uint32_t offset) { TIFFSTATE *clientstate = (TIFFSTATE *)state->context; - TRACE(("initing libtiff\n")); - TRACE(("filepointer: %d \n", fp)); - TRACE( - ("State: count %d, state %d, x %d, y %d, ystep %d\n", - state->count, - state->state, - state->x, - state->y, - state->ystep) - ); - TRACE( - ("State: xsize %d, ysize %d, xoff %d, yoff %d \n", - state->xsize, - state->ysize, - state->xoff, - state->yoff) - ); - TRACE(("State: bits %d, bytes %d \n", state->bits, state->bytes)); - TRACE(("State: context %p \n", state->context)); - clientstate->loc = 0; clientstate->size = 0; clientstate->data = 0; @@ -238,7 +176,6 @@ _pickUnpackers( TIFFGetFieldDefaulted(tiff, TIFFTAG_BITSPERSAMPLE, &bits_per_sample); if (bits_per_sample != 8 && bits_per_sample != 16) { - TRACE(("Invalid value for bits per sample: %d\n", bits_per_sample)); state->errcode = IMAGING_CODEC_BROKEN; return -1; } @@ -302,10 +239,7 @@ _decodeAsRGBA(Imaging im, ImagingCodecState state, TIFF *tiff) { rows_per_block = state->ysize; } - TRACE(("RowsPerBlock: %u \n", rows_per_block)); - if (!(TIFFRGBAImageOK(tiff, emsg) && TIFFRGBAImageBegin(&img, tiff, 0, emsg))) { - TRACE(("Decode error, msg: %s", emsg)); state->errcode = IMAGING_CODEC_BROKEN; // nothing to clean up, just return return -1; @@ -332,8 +266,6 @@ _decodeAsRGBA(Imaging im, ImagingCodecState state, TIFF *tiff) { state->bytes = rows_per_block * row_byte_size; - TRACE(("BlockSize: %d \n", state->bytes)); - /* realloc to fit whole strip */ /* malloc check above */ new_data = realloc(state->buffer, state->bytes); @@ -349,7 +281,6 @@ _decodeAsRGBA(Imaging im, ImagingCodecState state, TIFF *tiff) { rows_to_read = MIN(rows_per_block, img.height - state->y); if (!TIFFRGBAImageGet(&img, (UINT32 *)state->buffer, img.width, rows_to_read)) { - TRACE(("Decode Error, y: %d\n", state->y)); state->errcode = IMAGING_CODEC_BROKEN; goto decodergba_err; } @@ -358,18 +289,10 @@ _decodeAsRGBA(Imaging im, ImagingCodecState state, TIFF *tiff) { TIFFSwabArrayOfLong((UINT32 *)state->buffer, img.width * rows_to_read); #endif - TRACE(("Decoded strip for row %d \n", state->y)); - // iterate over each row in the strip and stuff data into image for (current_row = 0; current_row < MIN((INT32)rows_per_block, state->ysize - state->y); current_row++) { - TRACE(("Writing data into line %d ; \n", state->y + current_row)); - - // UINT8 * bbb = state->buffer + current_row * (state->bytes / - // rows_per_block); TRACE(("chars: %x %x %x %x\n", ((UINT8 *)bbb)[0], - // ((UINT8 *)bbb)[1], ((UINT8 *)bbb)[2], ((UINT8 *)bbb)[3])); - state->shuffle( (UINT8 *)im->image[state->y + state->yoff + current_row] + state->xoff * im->pixelsize, @@ -403,7 +326,6 @@ _decodeTile( tile_bytes_size = TIFFTileSize(tiff); if (tile_bytes_size == 0) { - TRACE(("Decode Error, Can not calculate TileSize\n")); state->errcode = IMAGING_CODEC_BROKEN; return -1; } @@ -411,7 +333,6 @@ _decodeTile( row_byte_size = TIFFTileRowSize(tiff); if (row_byte_size == 0 || row_byte_size > tile_bytes_size) { - TRACE(("Decode Error, Can not calculate TileRowSize\n")); state->errcode = IMAGING_CODEC_BROKEN; return -1; } @@ -441,8 +362,6 @@ _decodeTile( state->bytes = tile_bytes_size; - TRACE(("TIFFTileSize: %d\n", state->bytes)); - /* realloc to fit whole tile */ /* malloc check above */ new_data = realloc(state->buffer, state->bytes); @@ -458,28 +377,14 @@ _decodeTile( ImagingShuffler shuffler = unpackers[plane]; for (x = state->xoff; x < state->xsize; x += tile_width) { if (TIFFReadTile(tiff, (tdata_t)state->buffer, x, y, 0, plane) == -1) { - TRACE(("Decode Error, Tile at %dx%d\n", x, y)); state->errcode = IMAGING_CODEC_BROKEN; return -1; } - TRACE(("Read tile at %dx%d; \n\n", x, y)); - current_tile_width = MIN((INT32)tile_width, state->xsize - x); current_tile_length = MIN((INT32)tile_length, state->ysize - y); // iterate over each line in the tile and stuff data into image for (tile_y = 0; tile_y < current_tile_length; tile_y++) { - TRACE( - ("Writing tile data at %dx%d using tile_width: %d; \n", - tile_y + y, - x, - current_tile_width) - ); - - // UINT8 * bbb = state->buffer + tile_y * row_byte_size; - // TRACE(("chars: %x%x%x%x\n", ((UINT8 *)bbb)[0], ((UINT8 *)bbb)[1], - // ((UINT8 *)bbb)[2], ((UINT8 *)bbb)[3])); - shuffler( (UINT8 *)im->image[tile_y + y] + x * im->pixelsize, state->buffer + tile_y * row_byte_size, @@ -517,8 +422,6 @@ _decodeStrip( return -1; } - TRACE(("RowsPerStrip: %u\n", rows_per_strip)); - strip_size = TIFFStripSize(tiff); if (strip_size > INT_MAX - 1) { state->errcode = IMAGING_CODEC_MEMORY; @@ -536,8 +439,6 @@ _decodeStrip( state->bytes = strip_size; - TRACE(("StripSize: %d \n", state->bytes)); - row_byte_size = TIFFScanlineSize(tiff); // if the unpacker calculated row size is > row byte size, (at least) the last @@ -547,8 +448,6 @@ _decodeStrip( return -1; } - TRACE(("RowsByteSize: %u \n", row_byte_size)); - /* realloc to fit whole strip */ /* malloc check above */ new_data = realloc(state->buffer, state->bytes); @@ -569,25 +468,14 @@ _decodeStrip( (tdata_t)state->buffer, strip_size ) == -1) { - TRACE( - ("Decode Error, strip %d\n", TIFFComputeStrip(tiff, state->y, 0)) - ); state->errcode = IMAGING_CODEC_BROKEN; return -1; } - TRACE(("Decoded strip for row %d \n", state->y)); - // iterate over each row in the strip and stuff data into image for (strip_row = 0; strip_row < MIN((INT32)rows_per_strip, state->ysize - state->y); strip_row++) { - TRACE(("Writing data into line %d ; \n", state->y + strip_row)); - - // UINT8 * bbb = state->buffer + strip_row * (state->bytes / - // rows_per_strip); TRACE(("chars: %x %x %x %x\n", ((UINT8 *)bbb)[0], - // ((UINT8 *)bbb)[1], ((UINT8 *)bbb)[2], ((UINT8 *)bbb)[3])); - shuffler( (UINT8 *)im->image[state->y + state->yoff + strip_row] + state->xoff * im->pixelsize, @@ -622,72 +510,19 @@ ImagingLibTiffDecode( /* buffer is the encoded file, bytes is the length of the encoded file */ /* it all ends up in state->buffer, which is a uint8* from Imaging.h */ - TRACE(("in decoder: bytes %d\n", bytes)); - TRACE( - ("State: count %d, state %d, x %d, y %d, ystep %d\n", - state->count, - state->state, - state->x, - state->y, - state->ystep) - ); - TRACE( - ("State: xsize %d, ysize %d, xoff %d, yoff %d \n", - state->xsize, - state->ysize, - state->xoff, - state->yoff) - ); - TRACE(("State: bits %d, bytes %d \n", state->bits, state->bytes)); - TRACE( - ("Buffer: %p: %c%c%c%c\n", - buffer, - (char)buffer[0], - (char)buffer[1], - (char)buffer[2], - (char)buffer[3]) - ); - TRACE( - ("State->Buffer: %c%c%c%c\n", - (char)state->buffer[0], - (char)state->buffer[1], - (char)state->buffer[2], - (char)state->buffer[3]) - ); - TRACE( - ("Image: mode %s, type %d, bands: %d, xsize %d, ysize %d \n", - getModeData(im->mode)->name, - im->type, - im->bands, - im->xsize, - im->ysize) - ); - TRACE( - ("Image: image8 %p, image32 %p, image %p, block %p \n", - im->image8, - im->image32, - im->image, - im->block) - ); - TRACE(("Image: pixelsize: %d, linesize %d \n", im->pixelsize, im->linesize)); - - dump_state(clientstate); clientstate->size = bytes; clientstate->eof = clientstate->size; clientstate->loc = 0; clientstate->data = (tdata_t)buffer; clientstate->flrealloc = 0; - dump_state(clientstate); TIFFSetWarningHandler(NULL); TIFFSetWarningHandlerExt(NULL); if (clientstate->fp) { - TRACE(("Opening using fd: %d\n", clientstate->fp)); lseek(clientstate->fp, 0, SEEK_SET); // Sometimes, I get it set to the end. tiff = TIFFFdOpen(fd_to_tiff_fd(clientstate->fp), filename, mode); } else { - TRACE(("Opening from string\n")); tiff = TIFFClientOpen( filename, mode, @@ -703,7 +538,6 @@ ImagingLibTiffDecode( } if (!tiff) { - TRACE(("Error, didn't get the tiff\n")); state->errcode = IMAGING_CODEC_BROKEN; return -1; } @@ -711,10 +545,8 @@ ImagingLibTiffDecode( if (clientstate->ifd) { int rv; uint32_t ifdoffset = clientstate->ifd; - TRACE(("reading tiff ifd %u\n", ifdoffset)); rv = TIFFSetSubDirectory(tiff, ifdoffset); if (!rv) { - TRACE(("error in TIFFSetSubDirectory")); goto decode_err; } } @@ -723,13 +555,6 @@ ImagingLibTiffDecode( TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &img_height); if (state->xsize != img_width || state->ysize != img_height) { - TRACE( - ("Inconsistent Image Error: %d =? %d, %d =? %d", - state->xsize, - img_width, - state->ysize, - img_height) - ); state->errcode = IMAGING_CODEC_BROKEN; goto decode_err; } @@ -804,7 +629,6 @@ ImagingLibTiffDecode( // that is fine, as it does not close the file TIFFClose(tiff); } - TRACE(("Done Decoding, Returning \n")); // Returning -1 here to force ImageFile.load to break, rather than // even think about looping back around. return -1; @@ -823,26 +647,6 @@ ImagingLibTiffEncodeInit(ImagingCodecState state, char *filename, int fp) { int bufsize = 64 * 1024; char *mode = "w"; - TRACE(("initing libtiff\n")); - TRACE(("Filename %s, filepointer: %d \n", filename, fp)); - TRACE( - ("State: count %d, state %d, x %d, y %d, ystep %d\n", - state->count, - state->state, - state->x, - state->y, - state->ystep) - ); - TRACE( - ("State: xsize %d, ysize %d, xoff %d, yoff %d \n", - state->xsize, - state->ysize, - state->xoff, - state->yoff) - ); - TRACE(("State: bits %d, bytes %d \n", state->bits, state->bytes)); - TRACE(("State: context %p \n", state->context)); - clientstate->loc = 0; clientstate->size = 0; clientstate->eof = 0; @@ -853,19 +657,16 @@ ImagingLibTiffEncodeInit(ImagingCodecState state, char *filename, int fp) { state->state = 0; if (fp) { - TRACE(("Opening using fd: %d for writing \n", clientstate->fp)); clientstate->tiff = TIFFFdOpen(fd_to_tiff_fd(clientstate->fp), filename, mode); } else { // calloc a buffer to write the tif, we're going to need to realloc or something // if we need bigger. - TRACE(("Opening a buffer for writing \n")); /* calloc check ok, small constant allocation */ clientstate->data = calloc(bufsize, 1); clientstate->size = bufsize; clientstate->flrealloc = 1; if (!clientstate->data) { - TRACE(("Error, couldn't allocate a buffer of size %d\n", bufsize)); return 0; } @@ -884,7 +685,6 @@ ImagingLibTiffEncodeInit(ImagingCodecState state, char *filename, int fp) { } if (!clientstate->tiff) { - TRACE(("Error, couldn't open tiff file\n")); return 0; } @@ -971,59 +771,7 @@ ImagingLibTiffEncode(Imaging im, ImagingCodecState state, UINT8 *buffer, int byt TIFFSTATE *clientstate = (TIFFSTATE *)state->context; TIFF *tiff = clientstate->tiff; - TRACE(("in encoder: bytes %d\n", bytes)); - TRACE( - ("State: count %d, state %d, x %d, y %d, ystep %d\n", - state->count, - state->state, - state->x, - state->y, - state->ystep) - ); - TRACE( - ("State: xsize %d, ysize %d, xoff %d, yoff %d \n", - state->xsize, - state->ysize, - state->xoff, - state->yoff) - ); - TRACE(("State: bits %d, bytes %d \n", state->bits, state->bytes)); - TRACE( - ("Buffer: %p: %c%c%c%c\n", - buffer, - (char)buffer[0], - (char)buffer[1], - (char)buffer[2], - (char)buffer[3]) - ); - TRACE( - ("State->Buffer: %c%c%c%c\n", - (char)state->buffer[0], - (char)state->buffer[1], - (char)state->buffer[2], - (char)state->buffer[3]) - ); - TRACE( - ("Image: mode %s, type %d, bands: %d, xsize %d, ysize %d \n", - getModeData(im->mode)->name, - im->type, - im->bands, - im->xsize, - im->ysize) - ); - TRACE( - ("Image: image8 %p, image32 %p, image %p, block %p \n", - im->image8, - im->image32, - im->image, - im->block) - ); - TRACE(("Image: pixelsize: %d, linesize %d \n", im->pixelsize, im->linesize)); - - dump_state(clientstate); - if (state->state == 0) { - TRACE(("Encoding line by line")); while (state->y < state->ysize) { state->shuffle( state->buffer, @@ -1035,7 +783,6 @@ ImagingLibTiffEncode(Imaging im, ImagingCodecState state, UINT8 *buffer, int byt if (TIFFWriteScanline( tiff, (tdata_t)(state->buffer), (uint32_t)state->y, 0 ) == -1) { - TRACE(("Encode Error, row %d\n", state->y)); state->errcode = IMAGING_CODEC_BROKEN; if (clientstate->fp) { @@ -1052,9 +799,7 @@ ImagingLibTiffEncode(Imaging im, ImagingCodecState state, UINT8 *buffer, int byt if (state->y == state->ysize) { state->state = 1; - TRACE(("Flushing \n")); if (!TIFFFlush(tiff)) { - TRACE(("Error flushing the tiff")); // likely reason is memory. state->errcode = IMAGING_CODEC_MEMORY; if (!clientstate->fp) { @@ -1071,16 +816,8 @@ ImagingLibTiffEncode(Imaging im, ImagingCodecState state, UINT8 *buffer, int byt if (state->state == 1 && !clientstate->fp) { int read = (int)_tiffReadProc((thandle_t)clientstate, (tdata_t)buffer, (tsize_t)bytes); - TRACE( - ("Buffer: %p: %c%c%c%c\n", - buffer, - (char)buffer[0], - (char)buffer[1], - (char)buffer[2], - (char)buffer[3]) - ); + if (clientstate->loc == clientstate->eof) { - TRACE(("Hit EOF, calling an end, freeing data")); state->errcode = IMAGING_CODEC_END; free(clientstate->data); } diff --git a/src/libImaging/TiffDecode.h b/src/libImaging/TiffDecode.h index c31d0b9d8ee..b26eb2c1d56 100644 --- a/src/libImaging/TiffDecode.h +++ b/src/libImaging/TiffDecode.h @@ -43,16 +43,4 @@ ImagingLibTiffMergeFieldInfo( extern int ImagingLibTiffSetField(ImagingCodecState state, ttag_t tag, ...); -/* - Trace debugging - legacy, don't enable for Python 3.x, unicode issues. -*/ - -/* -#define VA_ARGS(...) __VA_ARGS__ -#define TRACE(args) fprintf(stderr, VA_ARGS args) -*/ - -#define TRACE(args) - #endif