Merge pull request #8211 from uploadcare/remove-c-flags

Remove all WITH_* flags from _imaging.c and other flags
This commit is contained in:
Alexander Karpinsky 2024-08-02 16:10:10 +04:00 committed by GitHub
commit d8447de24d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 68 additions and 160 deletions

View File

@ -85,3 +85,10 @@ others prepare for 3.13, and to ensure Pillow could be used immediately at the r
of 3.13.0 final (2024-10-01, :pep:`719`). of 3.13.0 final (2024-10-01, :pep:`719`).
Pillow 11.0.0 now officially supports Python 3.13. Pillow 11.0.0 now officially supports Python 3.13.
C-level Flags
^^^^^^^^^^^^^
Some compiling flags like ``WITH_THREADING``, ``WITH_IMAGECHOPS``, and other
``WITH_*`` were removed. These flags were not available through the build system,
but they could be edited in the C source.

View File

@ -94,19 +94,6 @@
#include <math.h> #include <math.h>
#include <stddef.h> #include <stddef.h>
/* Configuration stuff. Feel free to undef things you don't need. */
#define WITH_IMAGECHOPS /* ImageChops support */
#define WITH_IMAGEDRAW /* ImageDraw support */
#define WITH_MAPPING /* use memory mapping to read some file formats */
#define WITH_IMAGEPATH /* ImagePath stuff */
#define WITH_ARROW /* arrow graphics stuff (experimental) */
#define WITH_EFFECTS /* special effects */
#define WITH_QUANTIZE /* quantization support */
#define WITH_RANKFILTER /* rank filter */
#define WITH_MODEFILTER /* mode filter */
#define WITH_THREADING /* "friendly" threading support */
#define WITH_UNSHARPMASK /* Kevin Cazabon's unsharpmask module */
#undef VERBOSE #undef VERBOSE
#define B16(p, i) ((((int)p[(i)]) << 8) + p[(i) + 1]) #define B16(p, i) ((((int)p[(i)]) << 8) + p[(i) + 1])
@ -124,8 +111,6 @@ typedef struct {
static PyTypeObject Imaging_Type; static PyTypeObject Imaging_Type;
#ifdef WITH_IMAGEDRAW
typedef struct { typedef struct {
/* to write a character, cut out sxy from glyph data, place /* to write a character, cut out sxy from glyph data, place
at current position plus dxy, and advance by (dx, dy) */ at current position plus dxy, and advance by (dx, dy) */
@ -152,8 +137,6 @@ typedef struct {
static PyTypeObject ImagingDraw_Type; static PyTypeObject ImagingDraw_Type;
#endif
typedef struct { typedef struct {
PyObject_HEAD ImagingObject *image; PyObject_HEAD ImagingObject *image;
int readonly; int readonly;
@ -216,16 +199,12 @@ PyImaging_AsImaging(PyObject *op) {
void void
ImagingSectionEnter(ImagingSectionCookie *cookie) { ImagingSectionEnter(ImagingSectionCookie *cookie) {
#ifdef WITH_THREADING
*cookie = (PyThreadState *)PyEval_SaveThread(); *cookie = (PyThreadState *)PyEval_SaveThread();
#endif
} }
void void
ImagingSectionLeave(ImagingSectionCookie *cookie) { ImagingSectionLeave(ImagingSectionCookie *cookie) {
#ifdef WITH_THREADING
PyEval_RestoreThread((PyThreadState *)*cookie); PyEval_RestoreThread((PyThreadState *)*cookie);
#endif
} }
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
@ -1092,7 +1071,6 @@ _filter(ImagingObject *self, PyObject *args) {
return imOut; return imOut;
} }
#ifdef WITH_UNSHARPMASK
static PyObject * static PyObject *
_gaussian_blur(ImagingObject *self, PyObject *args) { _gaussian_blur(ImagingObject *self, PyObject *args) {
Imaging imIn; Imaging imIn;
@ -1117,7 +1095,6 @@ _gaussian_blur(ImagingObject *self, PyObject *args) {
return PyImagingNew(imOut); return PyImagingNew(imOut);
} }
#endif
static PyObject * static PyObject *
_getpalette(ImagingObject *self, PyObject *args) { _getpalette(ImagingObject *self, PyObject *args) {
@ -1375,7 +1352,6 @@ _entropy(ImagingObject *self, PyObject *args) {
return PyFloat_FromDouble(-entropy); return PyFloat_FromDouble(-entropy);
} }
#ifdef WITH_MODEFILTER
static PyObject * static PyObject *
_modefilter(ImagingObject *self, PyObject *args) { _modefilter(ImagingObject *self, PyObject *args) {
int size; int size;
@ -1385,7 +1361,6 @@ _modefilter(ImagingObject *self, PyObject *args) {
return PyImagingNew(ImagingModeFilter(self->image, size)); return PyImagingNew(ImagingModeFilter(self->image, size));
} }
#endif
static PyObject * static PyObject *
_offset(ImagingObject *self, PyObject *args) { _offset(ImagingObject *self, PyObject *args) {
@ -1717,8 +1692,6 @@ _putdata(ImagingObject *self, PyObject *args) {
return Py_None; return Py_None;
} }
#ifdef WITH_QUANTIZE
static PyObject * static PyObject *
_quantize(ImagingObject *self, PyObject *args) { _quantize(ImagingObject *self, PyObject *args) {
int colours = 256; int colours = 256;
@ -1735,7 +1708,6 @@ _quantize(ImagingObject *self, PyObject *args) {
return PyImagingNew(ImagingQuantize(self->image, colours, method, kmeans)); return PyImagingNew(ImagingQuantize(self->image, colours, method, kmeans));
} }
#endif
static PyObject * static PyObject *
_putpalette(ImagingObject *self, PyObject *args) { _putpalette(ImagingObject *self, PyObject *args) {
@ -1871,7 +1843,6 @@ _putpixel(ImagingObject *self, PyObject *args) {
return Py_None; return Py_None;
} }
#ifdef WITH_RANKFILTER
static PyObject * static PyObject *
_rankfilter(ImagingObject *self, PyObject *args) { _rankfilter(ImagingObject *self, PyObject *args) {
int size, rank; int size, rank;
@ -1881,7 +1852,6 @@ _rankfilter(ImagingObject *self, PyObject *args) {
return PyImagingNew(ImagingRankFilter(self->image, size, rank)); return PyImagingNew(ImagingRankFilter(self->image, size, rank));
} }
#endif
static PyObject * static PyObject *
_resize(ImagingObject *self, PyObject *args) { _resize(ImagingObject *self, PyObject *args) {
@ -2163,7 +2133,6 @@ _transpose(ImagingObject *self, PyObject *args) {
return PyImagingNew(imOut); return PyImagingNew(imOut);
} }
#ifdef WITH_UNSHARPMASK
static PyObject * static PyObject *
_unsharp_mask(ImagingObject *self, PyObject *args) { _unsharp_mask(ImagingObject *self, PyObject *args) {
Imaging imIn; Imaging imIn;
@ -2187,7 +2156,6 @@ _unsharp_mask(ImagingObject *self, PyObject *args) {
return PyImagingNew(imOut); return PyImagingNew(imOut);
} }
#endif
static PyObject * static PyObject *
_box_blur(ImagingObject *self, PyObject *args) { _box_blur(ImagingObject *self, PyObject *args) {
@ -2464,9 +2432,7 @@ _split(ImagingObject *self) {
return list; return list;
} }
/* -------------------------------------------------------------------- */ /* Channel operations (ImageChops) ------------------------------------ */
#ifdef WITH_IMAGECHOPS
static PyObject * static PyObject *
_chop_invert(ImagingObject *self) { _chop_invert(ImagingObject *self) {
@ -2647,11 +2613,8 @@ _chop_overlay(ImagingObject *self, PyObject *args) {
return PyImagingNew(ImagingOverlay(self->image, imagep->image)); return PyImagingNew(ImagingOverlay(self->image, imagep->image));
} }
#endif
/* -------------------------------------------------------------------- */ /* Fonts (ImageDraw and ImageFont) ------------------------------------ */
#ifdef WITH_IMAGEDRAW
static PyObject * static PyObject *
_font_new(PyObject *self_, PyObject *args) { _font_new(PyObject *self_, PyObject *args) {
@ -2880,7 +2843,7 @@ static struct PyMethodDef _font_methods[] = {
{NULL, NULL} /* sentinel */ {NULL, NULL} /* sentinel */
}; };
/* -------------------------------------------------------------------- */ /* Graphics (ImageDraw) ----------------------------------------------- */
static PyObject * static PyObject *
_draw_new(PyObject *self_, PyObject *args) { _draw_new(PyObject *self_, PyObject *args) {
@ -3234,8 +3197,6 @@ _draw_points(ImagingDrawObject *self, PyObject *args) {
return Py_None; return Py_None;
} }
#ifdef WITH_ARROW
/* from outline.c */ /* from outline.c */
extern ImagingOutline extern ImagingOutline
PyOutline_AsOutline(PyObject *outline); PyOutline_AsOutline(PyObject *outline);
@ -3265,8 +3226,6 @@ _draw_outline(ImagingDrawObject *self, PyObject *args) {
return Py_None; return Py_None;
} }
#endif
static PyObject * static PyObject *
_draw_pieslice(ImagingDrawObject *self, PyObject *args) { _draw_pieslice(ImagingDrawObject *self, PyObject *args) {
double *xy; double *xy;
@ -3432,12 +3391,9 @@ _draw_rectangle(ImagingDrawObject *self, PyObject *args) {
} }
static struct PyMethodDef _draw_methods[] = { static struct PyMethodDef _draw_methods[] = {
#ifdef WITH_IMAGEDRAW
/* Graphics (ImageDraw) */ /* Graphics (ImageDraw) */
{"draw_lines", (PyCFunction)_draw_lines, METH_VARARGS}, {"draw_lines", (PyCFunction)_draw_lines, METH_VARARGS},
#ifdef WITH_ARROW
{"draw_outline", (PyCFunction)_draw_outline, METH_VARARGS}, {"draw_outline", (PyCFunction)_draw_outline, METH_VARARGS},
#endif
{"draw_polygon", (PyCFunction)_draw_polygon, METH_VARARGS}, {"draw_polygon", (PyCFunction)_draw_polygon, METH_VARARGS},
{"draw_rectangle", (PyCFunction)_draw_rectangle, METH_VARARGS}, {"draw_rectangle", (PyCFunction)_draw_rectangle, METH_VARARGS},
{"draw_points", (PyCFunction)_draw_points, METH_VARARGS}, {"draw_points", (PyCFunction)_draw_points, METH_VARARGS},
@ -3447,12 +3403,9 @@ static struct PyMethodDef _draw_methods[] = {
{"draw_ellipse", (PyCFunction)_draw_ellipse, METH_VARARGS}, {"draw_ellipse", (PyCFunction)_draw_ellipse, METH_VARARGS},
{"draw_pieslice", (PyCFunction)_draw_pieslice, METH_VARARGS}, {"draw_pieslice", (PyCFunction)_draw_pieslice, METH_VARARGS},
{"draw_ink", (PyCFunction)_draw_ink, METH_VARARGS}, {"draw_ink", (PyCFunction)_draw_ink, METH_VARARGS},
#endif
{NULL, NULL} /* sentinel */ {NULL, NULL} /* sentinel */
}; };
#endif
static PyObject * static PyObject *
pixel_access_new(ImagingObject *imagep, PyObject *args) { pixel_access_new(ImagingObject *imagep, PyObject *args) {
PixelAccessObject *self; PixelAccessObject *self;
@ -3533,11 +3486,9 @@ pixel_access_setitem(PixelAccessObject *self, PyObject *xy, PyObject *color) {
} }
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
/* EFFECTS (experimental) */ /* EFFECTS (experimental) */
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
#ifdef WITH_EFFECTS
static PyObject * static PyObject *
_effect_mandelbrot(ImagingObject *self, PyObject *args) { _effect_mandelbrot(ImagingObject *self, PyObject *args) {
int xsize = 512; int xsize = 512;
@ -3589,8 +3540,6 @@ _effect_spread(ImagingObject *self, PyObject *args) {
return PyImagingNew(ImagingEffectSpread(self->image, dist)); return PyImagingNew(ImagingEffectSpread(self->image, dist));
} }
#endif
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
/* UTILITIES */ /* UTILITIES */
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
@ -3671,20 +3620,14 @@ static struct PyMethodDef methods[] = {
{"filter", (PyCFunction)_filter, METH_VARARGS}, {"filter", (PyCFunction)_filter, METH_VARARGS},
{"histogram", (PyCFunction)_histogram, METH_VARARGS}, {"histogram", (PyCFunction)_histogram, METH_VARARGS},
{"entropy", (PyCFunction)_entropy, METH_VARARGS}, {"entropy", (PyCFunction)_entropy, METH_VARARGS},
#ifdef WITH_MODEFILTER
{"modefilter", (PyCFunction)_modefilter, METH_VARARGS}, {"modefilter", (PyCFunction)_modefilter, METH_VARARGS},
#endif
{"offset", (PyCFunction)_offset, METH_VARARGS}, {"offset", (PyCFunction)_offset, METH_VARARGS},
{"paste", (PyCFunction)_paste, METH_VARARGS}, {"paste", (PyCFunction)_paste, METH_VARARGS},
{"point", (PyCFunction)_point, METH_VARARGS}, {"point", (PyCFunction)_point, METH_VARARGS},
{"point_transform", (PyCFunction)_point_transform, METH_VARARGS}, {"point_transform", (PyCFunction)_point_transform, METH_VARARGS},
{"putdata", (PyCFunction)_putdata, METH_VARARGS}, {"putdata", (PyCFunction)_putdata, METH_VARARGS},
#ifdef WITH_QUANTIZE
{"quantize", (PyCFunction)_quantize, METH_VARARGS}, {"quantize", (PyCFunction)_quantize, METH_VARARGS},
#endif
#ifdef WITH_RANKFILTER
{"rankfilter", (PyCFunction)_rankfilter, METH_VARARGS}, {"rankfilter", (PyCFunction)_rankfilter, METH_VARARGS},
#endif
{"resize", (PyCFunction)_resize, METH_VARARGS}, {"resize", (PyCFunction)_resize, METH_VARARGS},
{"reduce", (PyCFunction)_reduce, METH_VARARGS}, {"reduce", (PyCFunction)_reduce, METH_VARARGS},
{"transpose", (PyCFunction)_transpose, METH_VARARGS}, {"transpose", (PyCFunction)_transpose, METH_VARARGS},
@ -3710,7 +3653,6 @@ static struct PyMethodDef methods[] = {
{"putpalettealpha", (PyCFunction)_putpalettealpha, METH_VARARGS}, {"putpalettealpha", (PyCFunction)_putpalettealpha, METH_VARARGS},
{"putpalettealphas", (PyCFunction)_putpalettealphas, METH_VARARGS}, {"putpalettealphas", (PyCFunction)_putpalettealphas, METH_VARARGS},
#ifdef WITH_IMAGECHOPS
/* Channel operations (ImageChops) */ /* Channel operations (ImageChops) */
{"chop_invert", (PyCFunction)_chop_invert, METH_NOARGS}, {"chop_invert", (PyCFunction)_chop_invert, METH_NOARGS},
{"chop_lighter", (PyCFunction)_chop_lighter, METH_VARARGS}, {"chop_lighter", (PyCFunction)_chop_lighter, METH_VARARGS},
@ -3729,20 +3671,14 @@ static struct PyMethodDef methods[] = {
{"chop_hard_light", (PyCFunction)_chop_hard_light, METH_VARARGS}, {"chop_hard_light", (PyCFunction)_chop_hard_light, METH_VARARGS},
{"chop_overlay", (PyCFunction)_chop_overlay, METH_VARARGS}, {"chop_overlay", (PyCFunction)_chop_overlay, METH_VARARGS},
#endif /* Unsharpmask extension */
#ifdef WITH_UNSHARPMASK
/* Kevin Cazabon's unsharpmask extension */
{"gaussian_blur", (PyCFunction)_gaussian_blur, METH_VARARGS}, {"gaussian_blur", (PyCFunction)_gaussian_blur, METH_VARARGS},
{"unsharp_mask", (PyCFunction)_unsharp_mask, METH_VARARGS}, {"unsharp_mask", (PyCFunction)_unsharp_mask, METH_VARARGS},
#endif
{"box_blur", (PyCFunction)_box_blur, METH_VARARGS}, {"box_blur", (PyCFunction)_box_blur, METH_VARARGS},
#ifdef WITH_EFFECTS
/* Special effects */ /* Special effects */
{"effect_spread", (PyCFunction)_effect_spread, METH_VARARGS}, {"effect_spread", (PyCFunction)_effect_spread, METH_VARARGS},
#endif
/* Misc. */ /* Misc. */
{"new_block", (PyCFunction)_new_block, METH_VARARGS}, {"new_block", (PyCFunction)_new_block, METH_VARARGS},
@ -3871,8 +3807,6 @@ static PyTypeObject Imaging_Type = {
getsetters, /*tp_getset*/ getsetters, /*tp_getset*/
}; };
#ifdef WITH_IMAGEDRAW
static PyTypeObject ImagingFont_Type = { static PyTypeObject ImagingFont_Type = {
PyVarObject_HEAD_INIT(NULL, 0) "ImagingFont", /*tp_name*/ PyVarObject_HEAD_INIT(NULL, 0) "ImagingFont", /*tp_name*/
sizeof(ImagingFontObject), /*tp_basicsize*/ sizeof(ImagingFontObject), /*tp_basicsize*/
@ -3939,8 +3873,6 @@ static PyTypeObject ImagingDraw_Type = {
0, /*tp_getset*/ 0, /*tp_getset*/
}; };
#endif
static PyMappingMethods pixel_access_as_mapping = { static PyMappingMethods pixel_access_as_mapping = {
(lenfunc)NULL, /*mp_length*/ (lenfunc)NULL, /*mp_length*/
(binaryfunc)pixel_access_getitem, /*mp_subscript*/ (binaryfunc)pixel_access_getitem, /*mp_subscript*/
@ -4309,13 +4241,11 @@ static PyMethodDef functions[] = {
{"zip_encoder", (PyCFunction)PyImaging_ZipEncoderNew, METH_VARARGS}, {"zip_encoder", (PyCFunction)PyImaging_ZipEncoderNew, METH_VARARGS},
#endif #endif
/* Memory mapping */ /* Memory mapping */
#ifdef WITH_MAPPING
{"map_buffer", (PyCFunction)PyImaging_MapBuffer, METH_VARARGS}, {"map_buffer", (PyCFunction)PyImaging_MapBuffer, METH_VARARGS},
#endif
/* Display support */
#ifdef _WIN32 #ifdef _WIN32
/* Display support */
{"display", (PyCFunction)PyImaging_DisplayWin32, METH_VARARGS}, {"display", (PyCFunction)PyImaging_DisplayWin32, METH_VARARGS},
{"display_mode", (PyCFunction)PyImaging_DisplayModeWin32, METH_VARARGS}, {"display_mode", (PyCFunction)PyImaging_DisplayModeWin32, METH_VARARGS},
{"grabscreen_win32", (PyCFunction)PyImaging_GrabScreenWin32, METH_VARARGS}, {"grabscreen_win32", (PyCFunction)PyImaging_GrabScreenWin32, METH_VARARGS},
@ -4331,30 +4261,22 @@ static PyMethodDef functions[] = {
/* Utilities */ /* Utilities */
{"getcodecstatus", (PyCFunction)_getcodecstatus, METH_VARARGS}, {"getcodecstatus", (PyCFunction)_getcodecstatus, METH_VARARGS},
/* Special effects (experimental) */ /* Special effects (experimental) */
#ifdef WITH_EFFECTS
{"effect_mandelbrot", (PyCFunction)_effect_mandelbrot, METH_VARARGS}, {"effect_mandelbrot", (PyCFunction)_effect_mandelbrot, METH_VARARGS},
{"effect_noise", (PyCFunction)_effect_noise, METH_VARARGS}, {"effect_noise", (PyCFunction)_effect_noise, METH_VARARGS},
{"linear_gradient", (PyCFunction)_linear_gradient, METH_VARARGS}, {"linear_gradient", (PyCFunction)_linear_gradient, METH_VARARGS},
{"radial_gradient", (PyCFunction)_radial_gradient, METH_VARARGS}, {"radial_gradient", (PyCFunction)_radial_gradient, METH_VARARGS},
{"wedge", (PyCFunction)_linear_gradient, METH_VARARGS}, /* Compatibility */ {"wedge", (PyCFunction)_linear_gradient, METH_VARARGS}, /* Compatibility */
#endif
/* Drawing support stuff */ /* Drawing support stuff */
#ifdef WITH_IMAGEDRAW
{"font", (PyCFunction)_font_new, METH_VARARGS}, {"font", (PyCFunction)_font_new, METH_VARARGS},
{"draw", (PyCFunction)_draw_new, METH_VARARGS}, {"draw", (PyCFunction)_draw_new, METH_VARARGS},
#endif
/* Experimental path stuff */ /* Experimental path stuff */
#ifdef WITH_IMAGEPATH
{"path", (PyCFunction)PyPath_Create, METH_VARARGS}, {"path", (PyCFunction)PyPath_Create, METH_VARARGS},
#endif
/* Experimental arrow graphics stuff */ /* Experimental arrow graphics stuff */
#ifdef WITH_ARROW
{"outline", (PyCFunction)PyOutline_Create, METH_VARARGS}, {"outline", (PyCFunction)PyOutline_Create, METH_VARARGS},
#endif
/* Resource management */ /* Resource management */
{"get_stats", (PyCFunction)_get_stats, METH_VARARGS}, {"get_stats", (PyCFunction)_get_stats, METH_VARARGS},
@ -4379,16 +4301,12 @@ setup_module(PyObject *m) {
if (PyType_Ready(&Imaging_Type) < 0) { if (PyType_Ready(&Imaging_Type) < 0) {
return -1; return -1;
} }
#ifdef WITH_IMAGEDRAW
if (PyType_Ready(&ImagingFont_Type) < 0) { if (PyType_Ready(&ImagingFont_Type) < 0) {
return -1; return -1;
} }
if (PyType_Ready(&ImagingDraw_Type) < 0) { if (PyType_Ready(&ImagingDraw_Type) < 0) {
return -1; return -1;
} }
#endif
if (PyType_Ready(&PixelAccess_Type) < 0) { if (PyType_Ready(&PixelAccess_Type) < 0) {
return -1; return -1;
} }

View File

@ -258,16 +258,6 @@ void
ImagingPackRGB(UINT8 *out, const UINT8 *in, int pixels) { ImagingPackRGB(UINT8 *out, const UINT8 *in, int pixels) {
int i = 0; int i = 0;
/* RGB triplets */ /* RGB triplets */
#ifdef __sparc
/* SPARC CPUs cannot read integers from nonaligned addresses. */
for (; i < pixels; i++) {
out[0] = in[R];
out[1] = in[G];
out[2] = in[B];
out += 3;
in += 4;
}
#else
for (; i < pixels - 1; i++) { for (; i < pixels - 1; i++) {
memcpy(out, in + i * 4, 4); memcpy(out, in + i * 4, 4);
out += 3; out += 3;
@ -278,7 +268,6 @@ ImagingPackRGB(UINT8 *out, const UINT8 *in, int pixels) {
out[2] = in[i * 4 + B]; out[2] = in[i * 4 + B];
out += 3; out += 3;
} }
#endif
} }
void void

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