filters/gp_filter_resize_alloc: Check w and h
[gfxprim.git] / include / filters / GP_HilbertCurve.h
blob8559820598270ca423c390fd4e930bf12bb1ae2d
1 /*****************************************************************************
2 * This file is part of gfxprim library. *
3 * *
4 * Gfxprim is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU Lesser General Public *
6 * License as published by the Free Software Foundation; either *
7 * version 2.1 of the License, or (at your option) any later version. *
8 * *
9 * Gfxprim is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12 * Lesser General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with gfxprim; if not, write to the Free Software *
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301 USA *
18 * *
19 * Copyright (C) 2009-2012 Cyril Hrubis <metan@ucw.cz> *
20 * *
21 *****************************************************************************/
25 Hilbert curve implementation.
29 #ifndef FILTERS_GP_HILBERT_CURVE_H
30 #define FILTERS_GP_HILBERT_CURVE_H
32 typedef struct gp_curve_state {
33 /* half of the number of bits of curve size */
34 unsigned int n;
35 /* coordinates */
36 unsigned int x, y;
37 /* current curve lenght */
38 unsigned int s;
39 } gp_curve_state;
42 * Resets curve to initial state i.e. x = 0, y = 0, (length) s = 0.
44 static inline void gp_hilbert_curve_init(gp_curve_state *state, int n)
46 state->n = n;
47 state->s = 0;
48 state->x = 0;
49 state->y = 0;
53 * Variant of Lam and Shapiro
55 static inline void gp_hilbert_curve_getxy(gp_curve_state *state)
57 int sa, sb;
59 * Older gcc thinks that x and y are used uninitialized that is not
60 * true so we silence the warning by initializing them.
62 unsigned int i, temp, x = 0, y = 0;
64 for (i = 0; i < 2 * state->n; i += 2) {
65 sa = (state->s >> (i+1)) & 0x01;
66 sb = (state->s >> i) & 0x01;
68 if ((sa ^ sb) == 0) {
69 temp = x;
70 x = y ^ (-sa);
71 y = temp ^ (-sa);
74 x = (x >> 1) | (sa << 31);
75 y = (y >> 1) | ((sa ^ sb) << 31);
78 state->x = x >> (32 - state->n);
79 state->y = y >> (32 - state->n);
84 * Finds next X and Y
86 static inline void gp_hilbert_curve_next(gp_curve_state *state)
89 /* increment length */
90 state->s++;
91 /* get X and Y */
92 gp_hilbert_curve_getxy(state);
96 * Returns true if we are not at curve endpoint
98 static inline int gp_hilbert_curve_continues(gp_curve_state *state)
100 return state->s < (1U<<(2*state->n));
103 #endif /* FILTERS_GP_HILBERT_CURVE_H */