ignore images directory
[sparrow.git] / sparrow.h
blob809411a88e137530b7c84cdb7b9cd6c2882342a1
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"
27 void INVISIBLE sparrow_rotate_history(GstSparrow *sparrow, GstBuffer *inbuf);
28 void INVISIBLE sparrow_pre_init(GstSparrow *sparrow);
29 gboolean INVISIBLE sparrow_init(GstSparrow *sparrow, GstCaps *incaps, GstCaps *outcaps);
30 void INVISIBLE sparrow_transform(GstSparrow *sparrow, guint8 *in, guint8 *out);
31 void INVISIBLE sparrow_finalise(GstSparrow *sparrow);
34 #define SPARROW_CALIBRATE_ON 1
37 #define CALIBRATE_WAIT_SIGNAL_THRESHOLD 32
38 #define ALIGNMENT 16
41 /*memory allocation */
43 static inline __attribute__((malloc)) UNUSED void *
44 malloc_or_die(size_t size){
45 void *p = malloc(size);
46 if (!p){
47 GST_ERROR("malloc would not allocate %u bytes! seriously!\n", size);
48 exit(EXIT_FAILURE);
50 return p;
53 static inline __attribute__((malloc)) UNUSED void *
54 malloc_aligned_or_die(size_t size){
55 void *mem;
56 int err = posix_memalign(&mem, ALIGNMENT, size);
57 if (err){
58 GST_ERROR("posix_memalign returned %d trying to allocate %u bytes aligned on %u byte boundaries\n",
59 err, size, ALIGNMENT);
60 exit(EXIT_FAILURE);
62 return mem;
65 static inline __attribute__((malloc)) UNUSED void *
66 zalloc_aligned_or_die(size_t size){
67 void *mem = malloc_aligned_or_die(size);
68 memset(mem, 0, size);
69 return mem;
72 /*RNG macros */
74 static inline UNUSED guint32
75 rng_uniform_int(GstSparrow *sparrow, guint32 limit){
76 double d = dsfmt_genrand_close_open(sparrow->dsfmt);
77 double d2 = d * limit;
78 guint32 i = (guint32)d2;
79 return i;
82 static inline UNUSED double
83 rng_uniform_double(GstSparrow *sparrow, double limit){
84 return dsfmt_genrand_close_open(sparrow->dsfmt) * limit;
87 #define rng_uniform(sparrow) dsfmt_genrand_close_open((sparrow)->dsfmt)
89 #define RANDINT(sparrow, start, end)((start) + rng_uniform_int(sparrow, (end) - (start)))
92 #define DISASTEROUS_CRASH(msg) GST_ERROR("DISASTER: %s\n%-25s line %4d \n", (msg), __func__, __LINE__);
96 static inline guint32
97 popcount64(guint64 x64)
99 guint32 x = x64 & (guint32)-1;
100 guint32 y = x64 >> 32;
101 x = x - ((x >> 1) & 0x55555555);
102 y = y - ((y >> 1) & 0x55555555);
103 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
104 y = (y & 0x33333333) + ((y >> 2) & 0x33333333);
105 x = (x + (x >> 4)) & 0x0F0F0F0F;
106 y = (y + (y >> 4)) & 0x0F0F0F0F;
107 x = x + (x >> 8);
108 y = y + (y >> 8);
109 x = x + (x >> 16);
110 y = y + (y >> 16);
111 return (x + y) & 0x000000FF;
114 static inline guint32
115 hamming_distance64(guint64 a, guint64 b, guint64 mask){
116 a &= mask;
117 b &= mask;
118 /* count where the two differ */
119 return popcount64(a ^ b);
122 #endif /* __SPARROW_SPARROW_H__ */