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__
22 #include "gstsparrow.h"
23 #include "sparrow_false_colour_lut.h"
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
);
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
);
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
);
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
);
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
);
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
);
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 */
90 static inline __attribute__((malloc
)) UNUSED
void *
91 malloc_or_die(size_t size
){
92 void *p
= malloc(size
);
94 GST_ERROR("malloc would not allocate %u bytes! seriously!\n", size
);
100 static inline __attribute__((malloc
)) UNUSED
void *
101 malloc_aligned_or_die(size_t size
){
103 int err
= posix_memalign(&mem
, ALIGNMENT
, size
);
105 GST_ERROR("posix_memalign returned %d trying to allocate %u bytes aligned on %u byte boundaries\n",
106 err
, size
, ALIGNMENT
);
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
);
119 static inline __attribute__((malloc
)) UNUSED
void *
120 zalloc_or_die(size_t size
){
121 void *mem
= calloc(size
, 1);
123 GST_ERROR("calloc would not allocate %u bytes!\n", size
);
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
;
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;
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;
179 return (x
+ y
) & 0x000000FF;
182 static inline guint32
183 hamming_distance64(guint64 a
, guint64 b
, guint64 mask
){
186 /* count where the two differ */
187 return popcount64(a
^ b
);
191 mask_to_shift(guint32 mask
){
192 /*mask is big-endian, so these numbers are reversed */
203 GST_WARNING("mask not byte aligned: %x\n", mask
);
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__ */