change in_x, iny to x, y, and remove unused lut types
[sparrow.git] / sparrow.c
blob9df3cadbb4b72bc22801d0a5f055ad59e519d3a7
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.
19 #include "sparrow.h"
20 #include "gstsparrow.h"
22 #include <string.h>
23 #include <math.h>
25 /* static functions (via `make cproto`) */
26 static void change_state(GstSparrow *sparrow, sparrow_state state);
29 /* set up whatever debugging methods are enabled */
30 static void
31 init_debug(GstSparrow *sparrow){
32 if (!sparrow->debug_frame){
33 sparrow->debug_frame = malloc_aligned_or_die(MAX(sparrow->in.size, sparrow->out.size));
37 /*RNG code */
38 /*seed with -1 for automatic seed choice */
39 static void rng_init(GstSparrow *sparrow, guint32 seed){
40 GST_DEBUG("in RNG init\n");
41 if (seed == (guint32)-1){
42 /* XXX should really use /dev/urandom */
43 seed = rand() ^ (size_t)&(sparrow);
44 GST_DEBUG("Real seed %u\n", seed);
46 if (seed == 0)
47 seed = 12345;
49 dsfmt_init_gen_rand(sparrow->dsfmt, seed);
50 dsfmt_gen_rand_all(sparrow->dsfmt);
51 GST_DEBUG("RNG seeded with %u\n", seed);
54 /** debugging: write frames out somewhere. **/
56 /*spit out the frame as a ppm image */
57 void INVISIBLE
58 ppm_dump(sparrow_format *rgb, guint8 *data, guint32 width, guint32 height, const char *name)
60 guint i;
61 FILE *fh = fopen(name, "w");
62 guint32 size = width * height;
63 fprintf(fh, "P6\n%u %u\n255\n", width, height);
64 /* 4 cases: xBGR xRGB BGRx RGBx
65 need to convert to 24bit R G B
66 XX maybe could optimise some cases?
68 guint32 *p = (guint32 *)data;
69 for (i = 0; i < size; i++){
70 putc((*p >> rgb->rshift) & 255, fh);
71 putc((*p >> rgb->gshift) & 255, fh);
72 putc((*p >> rgb->bshift) & 255, fh);
73 p++;
75 fflush(fh);
76 fclose(fh);
79 /*pgm for greyscale */
80 void INVISIBLE
81 pgm_dump(guint8 *data, guint32 width, guint32 height, const char *name)
83 FILE *fh = fopen(name, "w");
84 size_t size = width * height;
85 fprintf(fh, "P5\n%u %u\n255\n", width, height);
86 size_t wrote = fwrite(data, 1, size, fh);
87 if (wrote != size){
88 GST_DEBUG("wanted to write %u bytes; fwrite said %u\n", size, wrote);
90 fflush(fh);
91 fclose(fh);
95 #define PPM_FILENAME_TEMPLATE "/tmp/sparrow_%05d.%s"
96 #define PPM_FILENAME_LENGTH (sizeof(PPM_FILENAME_TEMPLATE) + 15)
98 void INVISIBLE
99 debug_frame(GstSparrow *sparrow, guint8 *data, guint32 width, guint32 height, int pixsize){
100 char name[PPM_FILENAME_LENGTH];
101 int res;
102 if (pixsize == 4){ /*rgb*/
103 res = snprintf(name, PPM_FILENAME_LENGTH, PPM_FILENAME_TEMPLATE, sparrow->frame_count, "ppm");
104 if (res > 0){
105 ppm_dump(&(sparrow->in), data, width, height, name);
108 else {/*greyscale*/
109 res = snprintf(name, PPM_FILENAME_LENGTH, PPM_FILENAME_TEMPLATE, sparrow->frame_count, "pgm");
110 if (res > 0){
111 pgm_dump(data, width, height, name);
116 /** interpret gst attributes **/
118 /* Extract a colour (R,G,B) bitmask from gobject */
119 static guint32 get_mask(GstStructure *s, char *mask_name){
120 gint32 mask;
121 int res = gst_structure_get_int(s, mask_name, &mask);
122 if (!res){
123 GST_WARNING("No mask for '%s' !\n", mask_name);
125 return (guint32)mask;
128 static void
129 extract_caps(sparrow_format *im, GstCaps *caps)
131 GstStructure *s = gst_caps_get_structure (caps, 0);
132 gst_structure_get_int(s, "width", &(im->width));
133 gst_structure_get_int(s, "height", &(im->height));
134 im->rshift = mask_to_shift(get_mask(s, "red_mask"));
135 im->gshift = mask_to_shift(get_mask(s, "green_mask"));
136 im->bshift = mask_to_shift(get_mask(s, "blue_mask"));
137 /* recalculate shifts as little-endian */
138 im->rmask = 0xff << im->rshift;
139 im->gmask = 0xff << im->gshift;
140 im->bmask = 0xff << im->bshift;
141 im->rbyte = im->rshift / 8;
142 im->gbyte = im->gshift / 8;
143 im->bbyte = im->bshift / 8;
145 im->pixcount = im->width * im->height;
146 im->size = im->pixcount * PIXSIZE;
147 im->colours[SPARROW_WHITE] = im->rmask | im->gmask | im->bmask;
148 im->colours[SPARROW_GREEN] = im->gmask;
149 im->colours[SPARROW_MAGENTA] = im->rmask | im->bmask;
151 GST_DEBUG("\ncaps:\n%" GST_PTR_FORMAT, caps);
152 GST_DEBUG("shifts: r %u g %u b %u\n", im->rshift, im->gshift, im->bshift);
153 GST_DEBUG("dimensions: w %u h %u pix %u size %u\n", im->width, im->height,
154 im->pixcount, im->size);
159 /*Most functions below here are called from gstsparrow.c and are NOT static */
161 /* called by gst_sparrow_init(). The source/sink capabilities (and commandline
162 arguments[?]) are unknown at this stage, so there isn't much useful to do
163 here.*/
164 void INVISIBLE
165 sparrow_pre_init(GstSparrow *sparrow){
168 /* called by gst_sparrow_set_caps(). This sets up everything after gstreamer
169 has worked out what the pipeline will look like.
171 gboolean INVISIBLE
172 sparrow_init(GstSparrow *sparrow, GstCaps *incaps, GstCaps *outcaps){
173 change_state(sparrow, SPARROW_INIT);
174 extract_caps(&(sparrow->in), incaps);
175 extract_caps(&(sparrow->out), outcaps);
176 sparrow_format *in = &(sparrow->in);
178 sparrow->shared = sparrow_get_shared();
179 maybe_load_images(sparrow);
180 maybe_load_index(sparrow);
182 sparrow->dsfmt = zalloc_aligned_or_die(sizeof(dsfmt_t));
183 sparrow->screenmask = malloc_aligned_or_die(in->pixcount);
185 size_t lutsize = sizeof(sparrow_map_lut_t) * sparrow->out.pixcount;
186 sparrow->map_lut = zalloc_aligned_or_die(lutsize);
188 sparrow->timer_start.tv_sec = 0;
189 sparrow->timer_stop.tv_sec = 0;
191 rng_init(sparrow, sparrow->rng_seed);
193 if (sparrow->debug){
194 init_debug(sparrow);
197 if (sparrow->colour >= SPARROW_LAST_COLOUR ||
198 sparrow->colour == SPARROW_WHITE){
199 sparrow->colour = (sparrow->rng_seed & 1) ? SPARROW_GREEN : SPARROW_MAGENTA;
202 sparrow->timer_log = (sparrow->use_timer) ? fopen(TIMER_LOG_FILE, "w") : NULL;
204 if(sparrow->reload){
205 change_state(sparrow, SPARROW_FIND_EDGES);
207 else {
208 change_state(sparrow, SPARROW_NEXT_STATE);
210 return TRUE;
213 void INVISIBLE
214 sparrow_finalise(GstSparrow *sparrow)
216 free(sparrow->dsfmt);
217 free(sparrow->screenmask);
218 #if ! USE_FULL_LUT
219 free(sparrow->map.point_mem);
220 free(sparrow->map.rows);
221 #else
222 free(sparrow->map_lut);
223 #endif
226 if (sparrow->timer_log){
227 fclose(sparrow->timer_log);
229 //free everything
230 //cvReleaseImageHeader(IplImage** image)
234 /* initialisation functions and sparrow_transform() use this to set up a new
235 state. */
236 static void
237 change_state(GstSparrow *sparrow, sparrow_state state)
239 GST_DEBUG("state is %d, sparrow->state is %d\n", state, sparrow->state);
240 switch(sparrow->state){
241 case SPARROW_FIND_SELF:
242 finalise_find_self(sparrow);
243 break;
244 case SPARROW_FIND_SCREEN:
245 finalise_find_screen(sparrow);
246 break;
247 case SPARROW_FIND_EDGES:
248 finalise_find_edges(sparrow);
249 break;
250 case SPARROW_PLAY:
251 finalise_play(sparrow);
252 break;
253 case SPARROW_INIT:
254 break;
255 default:
256 GST_DEBUG("change_state got unknown state: %d\n", state);
258 if (state == SPARROW_NEXT_STATE){
259 state = sparrow->state + 1;
261 switch(state){
262 case SPARROW_FIND_SELF:
263 init_find_self(sparrow);
264 break;
265 case SPARROW_FIND_SCREEN:
266 init_find_screen(sparrow);
267 break;
268 case SPARROW_FIND_EDGES:
269 init_find_edges(sparrow);
270 break;
271 case SPARROW_PLAY:
272 init_play(sparrow);
273 break;
274 case SPARROW_INIT:
275 //init_init(sparrow);
276 break;
277 default:
278 GST_DEBUG("change_state got unknown state: %d\n", state);
280 sparrow->state = state;
284 /*called by gst_sparrow_transform_ip every frame.
286 decide what to do based on sparrow->state. All the processing is done in a
287 "mode_*" function, which returns a state or SPARROW_STATUS_QUO. If a state
288 is returned, then change_state() is called to initialise the state, even if
289 it is the current state (so states can use this to reset).
291 void INVISIBLE
292 sparrow_transform(GstSparrow *sparrow, guint8 *in, guint8 *out)
294 sparrow_state new_state;
295 #if TIME_TRANSFORM
296 TIMER_START(sparrow);
297 #endif
298 //GST_DEBUG("in %p, out %p\n", in, out);
299 switch(sparrow->state){
300 case SPARROW_FIND_SELF:
301 new_state = mode_find_self(sparrow, in, out);
302 break;
303 case SPARROW_FIND_SCREEN:
304 new_state = mode_find_screen(sparrow, in, out);
305 break;
306 case SPARROW_FIND_EDGES:
307 new_state = mode_find_edges(sparrow, in, out);
308 break;
309 case SPARROW_PLAY:
310 new_state = mode_play(sparrow, in, out);
311 break;
312 default:
313 GST_DEBUG("unknown state:%d\n", sparrow->state);
314 new_state = SPARROW_STATUS_QUO;
316 sparrow->frame_count++;
317 if (new_state != SPARROW_STATUS_QUO){
318 change_state(sparrow, new_state);
320 #if TIME_TRANSFORM
321 TIMER_STOP(sparrow);
322 #endif