gitignore ignorable gtksparrow executable
[sparrow.git] / sparrow.h
blob03d37d339be26090ec61531ec2fc0543f699274b
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"
25 /* calibrate.c */
26 INVISIBLE void init_find_self(GstSparrow *sparrow);
27 INVISIBLE sparrow_state mode_find_self(GstSparrow *sparrow, GstBuffer *inbuf, GstBuffer *outbuf);
28 INVISIBLE void finalise_find_self(GstSparrow *sparrow);
30 /* edges.c */
31 INVISIBLE void init_find_edges(GstSparrow *sparrow);
32 INVISIBLE sparrow_state mode_find_edges(GstSparrow *sparrow, GstBuffer *inbuf, GstBuffer *outbuf);
33 INVISIBLE void finalise_find_edges(GstSparrow *sparrow);
35 /* floodfill.c */
36 INVISIBLE void init_find_screen(GstSparrow *sparrow);
37 INVISIBLE sparrow_state mode_find_screen(GstSparrow *sparrow, GstBuffer *inbuf, GstBuffer *outbuf);
38 INVISIBLE void finalise_find_screen(GstSparrow *sparrow);
40 /* play.c */
41 INVISIBLE void init_play(GstSparrow *sparrow);
42 INVISIBLE sparrow_state mode_play(GstSparrow *sparrow, GstBuffer *inbuf, GstBuffer *outbuf);
43 INVISIBLE void finalise_play(GstSparrow *sparrow);
45 /* sparrow.c */
46 INVISIBLE void debug_frame(GstSparrow *sparrow, guint8 *data, guint32 width, guint32 height, int pixsize);
47 INVISIBLE void sparrow_pre_init(GstSparrow *sparrow);
48 INVISIBLE gboolean sparrow_init(GstSparrow *sparrow, GstCaps *incaps, GstCaps *outcaps);
49 INVISIBLE void sparrow_transform(GstSparrow *sparrow, GstBuffer *inbuf, GstBuffer *outbuf);
50 INVISIBLE void sparrow_finalise(GstSparrow *sparrow);
51 INVISIBLE void ppm_dump(sparrow_format *rgb, guint8 *data, guint32 width, guint32 height, const char *name);
52 INVISIBLE void pgm_dump(guint8 *data, guint32 width, guint32 height, const char *name);
55 /* jpeg_src.c */
56 INVISIBLE void decompress_buffer(struct jpeg_decompress_struct *cinfo, guint8 *src,
57 int size, guint8 *dest, int *width, int *height);
58 INVISIBLE void begin_reading_jpeg(GstSparrow *sparrow, guint8* src, int size);
59 INVISIBLE void read_one_line(GstSparrow *sparrow, guint8* dest);
60 INVISIBLE void finish_reading_jpeg(GstSparrow *sparrow);
61 INVISIBLE void init_jpeg_src(GstSparrow *sparrow);
62 INVISIBLE void finalise_jpeg_src(GstSparrow *sparrow);
66 /*load_images.c */
67 INVISIBLE sparrow_shared_t * sparrow_get_shared(void);
68 INVISIBLE void maybe_load_images(GstSparrow *sparrow);
69 INVISIBLE void maybe_unload_images(GstSparrow *sparrow);
70 INVISIBLE void maybe_load_index(GstSparrow *sparrow);
71 INVISIBLE void maybe_unload_index(GstSparrow *sparrow);
74 #define SPARROW_CALIBRATE_ON 1
76 #define MAYBE_DEBUG_IPL(ipl)((sparrow->debug) ? \
77 debug_frame(sparrow, (guint8*)(ipl)->imageData, (ipl)->width, \
78 (ipl)->height, (ipl)->nChannels):(void)0)
81 #define CALIBRATE_ON_MIN_T 2
82 #define CALIBRATE_ON_MAX_T 7
83 #define CALIBRATE_OFF_MIN_T 2
84 #define CALIBRATE_OFF_MAX_T 9
85 #define CALIBRATE_MAX_T MAX(CALIBRATE_OFF_MAX_T, CALIBRATE_ON_MAX_T)
87 /*memory allocation */
88 #define ALIGNMENT 16
90 static inline __attribute__((malloc)) UNUSED void *
91 malloc_or_die(size_t size){
92 void *p = malloc(size);
93 if (!p){
94 GST_ERROR("malloc would not allocate %u bytes! seriously!\n", size);
95 exit(EXIT_FAILURE);
97 return p;
100 static inline __attribute__((malloc)) UNUSED void *
101 malloc_aligned_or_die(size_t size){
102 void *mem;
103 int err = posix_memalign(&mem, ALIGNMENT, size);
104 if (err){
105 GST_ERROR("posix_memalign returned %d trying to allocate %u bytes aligned on %u byte boundaries\n",
106 err, size, ALIGNMENT);
107 exit(EXIT_FAILURE);
109 return mem;
112 static inline __attribute__((malloc)) UNUSED void *
113 zalloc_aligned_or_die(size_t size){
114 void *mem = malloc_aligned_or_die(size);
115 memset(mem, 0, size);
116 return mem;
119 static inline __attribute__((malloc)) UNUSED void *
120 zalloc_or_die(size_t size){
121 void *mem = calloc(size, 1);
122 if (!mem){
123 GST_ERROR("calloc would not allocate %u bytes!\n", size);
124 exit(EXIT_FAILURE);
126 return mem;
129 /*RNG macros */
131 static inline UNUSED guint32
132 rng_uniform_int(GstSparrow *sparrow, guint32 limit){
133 double d = dsfmt_genrand_close_open(sparrow->dsfmt);
134 double d2 = d * limit;
135 guint32 i = (guint32)d2;
136 return i;
139 static inline UNUSED double
140 rng_uniform_double(GstSparrow *sparrow, double limit){
141 return dsfmt_genrand_close_open(sparrow->dsfmt) * limit;
144 #define rng_uniform(sparrow) dsfmt_genrand_close_open((sparrow)->dsfmt)
146 #define RANDINT(sparrow, start, end)((start) + rng_uniform_int(sparrow, (end) - (start)))
149 #define DISASTEROUS_CRASH(msg) GST_ERROR("DISASTER: %s\n%-25s line %4d \n", (msg), __func__, __LINE__);
151 static inline guint32
152 popcount32(guint32 x)
154 x = x - ((x >> 1) & 0x55555555);
155 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
156 x = (x + (x >> 4)) & 0x0F0F0F0F;
157 x = x + (x >> 8);
158 x = x + (x >> 16);
159 return x & 0x000000FF;
163 /*XXX optimised for 32 bit!*/
164 static inline guint32
165 popcount64(guint64 x64)
167 guint32 x = x64 & (guint32)-1;
168 guint32 y = x64 >> 32;
169 x = x - ((x >> 1) & 0x55555555);
170 y = y - ((y >> 1) & 0x55555555);
171 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
172 y = (y & 0x33333333) + ((y >> 2) & 0x33333333);
173 x = (x + (x >> 4)) & 0x0F0F0F0F;
174 y = (y + (y >> 4)) & 0x0F0F0F0F;
175 x = x + (x >> 8);
176 y = y + (y >> 8);
177 x = x + (x >> 16);
178 y = y + (y >> 16);
179 return (x + y) & 0x000000FF;
182 static inline guint32
183 hamming_distance64(guint64 a, guint64 b, guint64 mask){
184 a &= mask;
185 b &= mask;
186 /* count where the two differ */
187 return popcount64(a ^ b);
190 static inline gint
191 mask_to_shift(guint32 mask){
192 /*mask is big-endian, so these numbers are reversed */
193 switch(mask){
194 case 0x000000ff:
195 return 24;
196 case 0x0000ff00:
197 return 16;
198 case 0x00ff0000:
199 return 8;
200 case 0xff000000:
201 return 0;
203 GST_WARNING("mask not byte aligned: %x\n", mask);
204 return 0;
207 static inline IplImage *
208 init_ipl_image(sparrow_format *dim, int channels){
209 CvSize size = {dim->width, dim->height};
210 IplImage* im = cvCreateImageHeader(size, IPL_DEPTH_8U, channels);
211 return cvInitImageHeader(im, size, IPL_DEPTH_8U, channels, 0, 8);
214 #define SPARROW_IMAGE_DIR "/home/douglas/sparrow/content/jpg"
216 #endif /* __SPARROW_SPARROW_H__ */