fix colour and shape of jpeg
[sparrow.git] / sparrow.h
blob9d000a17ed760ea98398a0a9abf022331d2f4bf9
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, guint8 *in, guint8 *out);
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, guint8 *in, guint8 *out);
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, guint8 *in, guint8 *out);
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, guint8 *in, guint8 *out);
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, guint8 *in, guint8 *out);
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_WAIT_SIGNAL_THRESHOLD 32
85 /*memory allocation */
86 #define ALIGNMENT 16
88 static inline __attribute__((malloc)) UNUSED void *
89 malloc_or_die(size_t size){
90 void *p = malloc(size);
91 if (!p){
92 GST_ERROR("malloc would not allocate %u bytes! seriously!\n", size);
93 exit(EXIT_FAILURE);
95 return p;
98 static inline __attribute__((malloc)) UNUSED void *
99 malloc_aligned_or_die(size_t size){
100 void *mem;
101 int err = posix_memalign(&mem, ALIGNMENT, size);
102 if (err){
103 GST_ERROR("posix_memalign returned %d trying to allocate %u bytes aligned on %u byte boundaries\n",
104 err, size, ALIGNMENT);
105 exit(EXIT_FAILURE);
107 return mem;
110 static inline __attribute__((malloc)) UNUSED void *
111 zalloc_aligned_or_die(size_t size){
112 void *mem = malloc_aligned_or_die(size);
113 memset(mem, 0, size);
114 return mem;
117 static inline __attribute__((malloc)) UNUSED void *
118 zalloc_or_die(size_t size){
119 void *mem = calloc(size, 1);
120 if (!mem){
121 GST_ERROR("calloc would not allocate %u bytes!\n", size);
122 exit(EXIT_FAILURE);
124 return mem;
127 /*RNG macros */
129 static inline UNUSED guint32
130 rng_uniform_int(GstSparrow *sparrow, guint32 limit){
131 double d = dsfmt_genrand_close_open(sparrow->dsfmt);
132 double d2 = d * limit;
133 guint32 i = (guint32)d2;
134 return i;
137 static inline UNUSED double
138 rng_uniform_double(GstSparrow *sparrow, double limit){
139 return dsfmt_genrand_close_open(sparrow->dsfmt) * limit;
142 #define rng_uniform(sparrow) dsfmt_genrand_close_open((sparrow)->dsfmt)
144 #define RANDINT(sparrow, start, end)((start) + rng_uniform_int(sparrow, (end) - (start)))
147 #define DISASTEROUS_CRASH(msg) GST_ERROR("DISASTER: %s\n%-25s line %4d \n", (msg), __func__, __LINE__);
149 static inline guint32
150 popcount32(guint32 x)
152 x = x - ((x >> 1) & 0x55555555);
153 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
154 x = (x + (x >> 4)) & 0x0F0F0F0F;
155 x = x + (x >> 8);
156 x = x + (x >> 16);
157 return x & 0x000000FF;
161 /*XXX optimised for 32 bit!*/
162 static inline guint32
163 popcount64(guint64 x64)
165 guint32 x = x64 & (guint32)-1;
166 guint32 y = x64 >> 32;
167 x = x - ((x >> 1) & 0x55555555);
168 y = y - ((y >> 1) & 0x55555555);
169 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
170 y = (y & 0x33333333) + ((y >> 2) & 0x33333333);
171 x = (x + (x >> 4)) & 0x0F0F0F0F;
172 y = (y + (y >> 4)) & 0x0F0F0F0F;
173 x = x + (x >> 8);
174 y = y + (y >> 8);
175 x = x + (x >> 16);
176 y = y + (y >> 16);
177 return (x + y) & 0x000000FF;
180 static inline guint32
181 hamming_distance64(guint64 a, guint64 b, guint64 mask){
182 a &= mask;
183 b &= mask;
184 /* count where the two differ */
185 return popcount64(a ^ b);
188 static inline gint
189 mask_to_shift(guint32 mask){
190 /*mask is big-endian, so these numbers are reversed */
191 switch(mask){
192 case 0x000000ff:
193 return 24;
194 case 0x0000ff00:
195 return 16;
196 case 0x00ff0000:
197 return 8;
198 case 0xff000000:
199 return 0;
201 GST_WARNING("mask not byte aligned: %x\n", mask);
202 return 0;
205 static inline IplImage *
206 init_ipl_image(sparrow_format *dim, int channels){
207 CvSize size = {dim->width, dim->height};
208 IplImage* im = cvCreateImageHeader(size, IPL_DEPTH_8U, channels);
209 return cvInitImageHeader(im, size, IPL_DEPTH_8U, channels, 0, 8);
212 #define SPARROW_IMAGE_DIR "/home/douglas/sparrow/content/jpg"
214 #endif /* __SPARROW_SPARROW_H__ */