prepare for bi-colour signals
[sparrow.git] / sparrow.h
blobe137c883f4c3e2276cab11ee53ee5319dff4cac2
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 void init_wait_for_grid(GstSparrow *sparrow);
29 INVISIBLE void init_find_grid(GstSparrow *sparrow);
30 INVISIBLE void init_find_edges(GstSparrow *sparrow);
31 INVISIBLE sparrow_state mode_find_self(GstSparrow *sparrow, guint8 *in, guint8 *out);
32 INVISIBLE sparrow_state mode_wait_for_grid(GstSparrow *sparrow, guint8 *in, guint8 *out);
33 INVISIBLE sparrow_state mode_find_grid(GstSparrow *sparrow, guint8 *in, guint8 *out);
34 INVISIBLE sparrow_state mode_find_edges(GstSparrow *sparrow, guint8 *in, guint8 *out);
36 /* sparrow.c */
37 INVISIBLE void debug_frame(GstSparrow *sparrow, guint8 *data, guint32 width, guint32 height);
38 INVISIBLE void sparrow_rotate_history(GstSparrow *sparrow, GstBuffer *inbuf);
39 INVISIBLE void sparrow_pre_init(GstSparrow *sparrow);
40 INVISIBLE gboolean sparrow_init(GstSparrow *sparrow, GstCaps *incaps, GstCaps *outcaps);
41 INVISIBLE void sparrow_transform(GstSparrow *sparrow, guint8 *in, guint8 *out);
42 INVISIBLE void sparrow_finalise(GstSparrow *sparrow);
44 /*itworks.c*/
45 INVISIBLE sparrow_state mode_process_frame(GstSparrow *sparrow, guint8 *in, guint8 *out);
48 #define SPARROW_CALIBRATE_ON 1
51 #define CALIBRATE_WAIT_SIGNAL_THRESHOLD 32
52 #define ALIGNMENT 16
55 /*memory allocation */
57 static inline __attribute__((malloc)) UNUSED void *
58 malloc_or_die(size_t size){
59 void *p = malloc(size);
60 if (!p){
61 GST_ERROR("malloc would not allocate %u bytes! seriously!\n", size);
62 exit(EXIT_FAILURE);
64 return p;
67 static inline __attribute__((malloc)) UNUSED void *
68 malloc_aligned_or_die(size_t size){
69 void *mem;
70 int err = posix_memalign(&mem, ALIGNMENT, size);
71 if (err){
72 GST_ERROR("posix_memalign returned %d trying to allocate %u bytes aligned on %u byte boundaries\n",
73 err, size, ALIGNMENT);
74 exit(EXIT_FAILURE);
76 return mem;
79 static inline __attribute__((malloc)) UNUSED void *
80 zalloc_aligned_or_die(size_t size){
81 void *mem = malloc_aligned_or_die(size);
82 memset(mem, 0, size);
83 return mem;
86 /*RNG macros */
88 static inline UNUSED guint32
89 rng_uniform_int(GstSparrow *sparrow, guint32 limit){
90 double d = dsfmt_genrand_close_open(sparrow->dsfmt);
91 double d2 = d * limit;
92 guint32 i = (guint32)d2;
93 return i;
96 static inline UNUSED double
97 rng_uniform_double(GstSparrow *sparrow, double limit){
98 return dsfmt_genrand_close_open(sparrow->dsfmt) * limit;
101 #define rng_uniform(sparrow) dsfmt_genrand_close_open((sparrow)->dsfmt)
103 #define RANDINT(sparrow, start, end)((start) + rng_uniform_int(sparrow, (end) - (start)))
106 #define DISASTEROUS_CRASH(msg) GST_ERROR("DISASTER: %s\n%-25s line %4d \n", (msg), __func__, __LINE__);
110 static inline guint32
111 popcount64(guint64 x64)
113 guint32 x = x64 & (guint32)-1;
114 guint32 y = x64 >> 32;
115 x = x - ((x >> 1) & 0x55555555);
116 y = y - ((y >> 1) & 0x55555555);
117 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
118 y = (y & 0x33333333) + ((y >> 2) & 0x33333333);
119 x = (x + (x >> 4)) & 0x0F0F0F0F;
120 y = (y + (y >> 4)) & 0x0F0F0F0F;
121 x = x + (x >> 8);
122 y = y + (y >> 8);
123 x = x + (x >> 16);
124 y = y + (y >> 16);
125 return (x + y) & 0x000000FF;
128 static inline guint32
129 hamming_distance64(guint64 a, guint64 b, guint64 mask){
130 a &= mask;
131 b &= mask;
132 /* count where the two differ */
133 return popcount64(a ^ b);
136 static inline gint
137 mask_to_shift(guint32 mask){
138 /*mask is big-endian, so these numbers are reversed */
139 switch(mask){
140 case 0x000000ff:
141 return 24;
142 case 0x0000ff00:
143 return 16;
144 case 0x00ff0000:
145 return 8;
146 case 0xff000000:
147 return 0;
149 GST_WARNING("mask not byte aligned: %x\n", mask);
150 return 0;
153 static inline IplImage *
154 init_ipl_image(sparrow_format *dim){
155 CvSize size = {dim->width, dim->height};
156 IplImage* im = cvCreateImageHeader(size, IPL_DEPTH_8U, PIXSIZE);
157 return cvInitImageHeader(im, size, IPL_DEPTH_8U, PIXSIZE, 0, 8);
161 #endif /* __SPARROW_SPARROW_H__ */