2
2
* The Python Imaging Library
3
* $Id: //modules/pil/libImaging/GetBBox.c#4 $
3
* $Id: GetBBox.c 2298 2005-02-17 21:17:29Z fredrik $
5
* get bounding box for image
5
* helpers to bounding boxes, min/max values, number of colors, etc.
8
8
* 1996-07-22 fl Created
9
9
* 1996-12-30 fl Added projection stuff
10
10
* 1998-07-12 fl Added extrema stuff
11
* 2004-09-17 fl Added colors stuff
12
* Copyright (c) 1997-2003 by Secret Labs AB.
13
* Copyright (c) 1996-2003 by Fredrik Lundh.
13
* Copyright (c) 1997-2004 by Secret Labs AB.
14
* Copyright (c) 1996-2004 by Fredrik Lundh.
15
16
* See the README file for details on usage and redistribution.
187
187
return 1; /* ok */
191
/* static ImagingColorItem* getcolors8(Imaging im, int maxcolors, int* size);*/
192
static ImagingColorItem* getcolors32(Imaging im, int maxcolors, int* size);
195
ImagingGetColors(Imaging im, int maxcolors, int* size)
197
/* FIXME: add support for 8-bit images */
198
return getcolors32(im, maxcolors, size);
201
static ImagingColorItem*
202
getcolors32(Imaging im, int maxcolors, int* size)
205
unsigned int i, incr;
209
ImagingColorItem* table;
212
unsigned int code_size;
213
unsigned int code_poly;
214
unsigned int code_mask;
216
/* note: the hash algorithm used here is based on the dictionary
217
code in Python 2.1.3; the exact implementation is borrowed from
218
Python's Unicode property database (written by yours truly) /F */
220
static int SIZES[] = {
221
4,3, 8,3, 16,3, 32,5, 64,3, 128,3, 256,29, 512,17, 1024,9, 2048,5,
222
4096,83, 8192,27, 16384,43, 32768,3, 65536,45, 131072,9, 262144,39,
223
524288,39, 1048576,9, 2097152,5, 4194304,3, 8388608,33, 16777216,27,
224
33554432,9, 67108864,71, 134217728,39, 268435456,9, 536870912,5,
228
code_size = code_poly = code_mask = 0;
230
for (i = 0; SIZES[i]; i += 2) {
231
if (SIZES[i] > maxcolors) {
232
code_size = SIZES[i];
233
code_poly = SIZES[i+1];
234
code_mask = code_size - 1;
239
/* printf("code_size=%d\n", code_size); */
240
/* printf("code_poly=%d\n", code_poly); */
243
ImagingError_MemoryError(); /* just give up */
248
ImagingError_ModeError();
252
table = calloc(code_size + 1, sizeof(ImagingColorItem));
254
ImagingError_MemoryError();
258
pixel_mask = 0xffffffff;
260
((UINT8*) &pixel_mask)[3] = 0;
264
for (y = 0; y < im->ysize; y++) {
265
INT32* p = im->image32[y];
266
for (x = 0; x < im->xsize; x++) {
267
INT32 pixel = p[x] & pixel_mask;
268
h = (pixel); /* null hashing */
269
i = (~h) & code_mask;
273
if (colors++ == maxcolors)
279
} else if (v->pixel == pixel) {
283
incr = (h ^ (h >> 3)) & code_mask;
287
i = (i + incr) & code_mask;
291
if (colors++ == maxcolors)
297
} else if (v->pixel == pixel) {
302
if (incr > code_mask)
303
incr = incr ^ code_poly;
311
for (x = y = 0; x < (int) code_size; x++)
312
if (table[x].count) {
317
table[y].count = 0; /* mark end of table */