Get reloading working and fix corner exrapolation.
[sparrow.git] / sparrow.h
blob0946271e7fae6c636f9a962b1bbb152d76bd0c27
1 /* Copyright (C) <2010> Douglas Bagnall <douglas@halo.gen.nz>
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Library General Public
5 * License as published by the Free Software Foundation; either
6 * version 2 of the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Library General Public License for more details.
13 * You should have received a copy of the GNU Library General Public
14 * License along with this library; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 02111-1307, USA.
18 #ifndef __SPARROW_SPARROW_H__
19 #define __SPARROW_SPARROW_H__
21 #include <stdlib.h>
22 #include "gstsparrow.h"
23 #include "sparrow_false_colour_lut.h"
24 #include "sparrow_gamma_lut.h"
26 /* calibrate.c */
27 INVISIBLE void init_find_self(GstSparrow *sparrow);
28 INVISIBLE sparrow_state mode_find_self(GstSparrow *sparrow, guint8 *in, guint8 *out);
29 INVISIBLE void finalise_find_self(GstSparrow *sparrow);
31 /* edges.c */
32 INVISIBLE void init_find_edges(GstSparrow *sparrow);
33 INVISIBLE sparrow_state mode_find_edges(GstSparrow *sparrow, guint8 *in, guint8 *out);
34 INVISIBLE void finalise_find_edges(GstSparrow *sparrow);
36 /* floodfill.c */
37 INVISIBLE void init_find_screen(GstSparrow *sparrow);
38 INVISIBLE sparrow_state mode_find_screen(GstSparrow *sparrow, guint8 *in, guint8 *out);
39 INVISIBLE void finalise_find_screen(GstSparrow *sparrow);
41 /* play.c */
42 INVISIBLE void init_play(GstSparrow *sparrow);
43 INVISIBLE sparrow_state mode_play(GstSparrow *sparrow, guint8 *in, guint8 *out);
44 INVISIBLE void finalise_play(GstSparrow *sparrow);
46 /* sparrow.c */
47 INVISIBLE void debug_frame(GstSparrow *sparrow, guint8 *data, guint32 width, guint32 height, int pixsize);
48 INVISIBLE void sparrow_rotate_history(GstSparrow *sparrow, GstBuffer *inbuf);
49 INVISIBLE void sparrow_pre_init(GstSparrow *sparrow);
50 INVISIBLE gboolean sparrow_init(GstSparrow *sparrow, GstCaps *incaps, GstCaps *outcaps);
51 INVISIBLE void sparrow_transform(GstSparrow *sparrow, guint8 *in, guint8 *out);
52 INVISIBLE void sparrow_finalise(GstSparrow *sparrow);
55 #define SPARROW_CALIBRATE_ON 1
57 #define MAYBE_DEBUG_IPL(ipl)((sparrow->debug) ? \
58 debug_frame(sparrow, (guint8*)(ipl)->imageData, (ipl)->width, \
59 (ipl)->height, (ipl)->nChannels):0)
62 #define CALIBRATE_WAIT_SIGNAL_THRESHOLD 32
66 /*memory allocation */
67 #define ALIGNMENT 16
69 static inline __attribute__((malloc)) UNUSED void *
70 malloc_or_die(size_t size){
71 void *p = malloc(size);
72 if (!p){
73 GST_ERROR("malloc would not allocate %u bytes! seriously!\n", size);
74 exit(EXIT_FAILURE);
76 return p;
79 static inline __attribute__((malloc)) UNUSED void *
80 malloc_aligned_or_die(size_t size){
81 void *mem;
82 int err = posix_memalign(&mem, ALIGNMENT, size);
83 if (err){
84 GST_ERROR("posix_memalign returned %d trying to allocate %u bytes aligned on %u byte boundaries\n",
85 err, size, ALIGNMENT);
86 exit(EXIT_FAILURE);
88 return mem;
91 static inline __attribute__((malloc)) UNUSED void *
92 zalloc_aligned_or_die(size_t size){
93 void *mem = malloc_aligned_or_die(size);
94 memset(mem, 0, size);
95 return mem;
98 static inline __attribute__((malloc)) UNUSED void *
99 zalloc_or_die(size_t size){
100 void *mem = calloc(size, 1);
101 if (!mem){
102 GST_ERROR("calloc would not allocate %u bytes!\n", size);
103 exit(EXIT_FAILURE);
105 return mem;
108 /*RNG macros */
110 static inline UNUSED guint32
111 rng_uniform_int(GstSparrow *sparrow, guint32 limit){
112 double d = dsfmt_genrand_close_open(sparrow->dsfmt);
113 double d2 = d * limit;
114 guint32 i = (guint32)d2;
115 return i;
118 static inline UNUSED double
119 rng_uniform_double(GstSparrow *sparrow, double limit){
120 return dsfmt_genrand_close_open(sparrow->dsfmt) * limit;
123 #define rng_uniform(sparrow) dsfmt_genrand_close_open((sparrow)->dsfmt)
125 #define RANDINT(sparrow, start, end)((start) + rng_uniform_int(sparrow, (end) - (start)))
128 #define DISASTEROUS_CRASH(msg) GST_ERROR("DISASTER: %s\n%-25s line %4d \n", (msg), __func__, __LINE__);
130 static inline guint32
131 popcount32(guint32 x)
133 x = x - ((x >> 1) & 0x55555555);
134 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
135 x = (x + (x >> 4)) & 0x0F0F0F0F;
136 x = x + (x >> 8);
137 x = x + (x >> 16);
138 return x & 0x000000FF;
142 /*XXX optimised for 32 bit!*/
143 static inline guint32
144 popcount64(guint64 x64)
146 guint32 x = x64 & (guint32)-1;
147 guint32 y = x64 >> 32;
148 x = x - ((x >> 1) & 0x55555555);
149 y = y - ((y >> 1) & 0x55555555);
150 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
151 y = (y & 0x33333333) + ((y >> 2) & 0x33333333);
152 x = (x + (x >> 4)) & 0x0F0F0F0F;
153 y = (y + (y >> 4)) & 0x0F0F0F0F;
154 x = x + (x >> 8);
155 y = y + (y >> 8);
156 x = x + (x >> 16);
157 y = y + (y >> 16);
158 return (x + y) & 0x000000FF;
161 static inline guint32
162 hamming_distance64(guint64 a, guint64 b, guint64 mask){
163 a &= mask;
164 b &= mask;
165 /* count where the two differ */
166 return popcount64(a ^ b);
169 static inline gint
170 mask_to_shift(guint32 mask){
171 /*mask is big-endian, so these numbers are reversed */
172 switch(mask){
173 case 0x000000ff:
174 return 24;
175 case 0x0000ff00:
176 return 16;
177 case 0x00ff0000:
178 return 8;
179 case 0xff000000:
180 return 0;
182 GST_WARNING("mask not byte aligned: %x\n", mask);
183 return 0;
186 static inline IplImage *
187 init_ipl_image(sparrow_format *dim){
188 CvSize size = {dim->width, dim->height};
189 IplImage* im = cvCreateImageHeader(size, IPL_DEPTH_8U, PIXSIZE);
190 return cvInitImageHeader(im, size, IPL_DEPTH_8U, PIXSIZE, 0, 8);
194 #endif /* __SPARROW_SPARROW_H__ */