add reload and save options (untested)
[sparrow.git] / edges.c
blobcdbda85774b2d063e2c71635ab54394e1e829c28
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.
20 #include "sparrow.h"
21 #include "gstsparrow.h"
22 #include "edges.h"
24 #include <string.h>
25 #include <math.h>
27 #include "cv.h"
32 #define SIG_WEIGHT 2
34 /*3 pixels manhatten distance makes you an outlier */
35 #define OUTLIER_THRESHOLD 3 << (SPARROW_FIXED_POINT)
36 #define OUTLIER_PENALTY 8
41 #define SPARROW_MAP_LUT_SHIFT 1
42 #define SPARROW_FP_2_LUT (SPARROW_FIXED_POINT - SPARROW_MAP_LUT_SHIFT)
44 #define OFFSET(x, y, w)((((y) * (w)) >> SPARROW_FIXED_POINT) + ((x) >> SPARROW_FIXED_POINT))
47 #define FL_DUMPFILE "/tmp/edges.dump"
49 static void dump_edges_info(GstSparrow *sparrow, sparrow_find_lines_t *fl, const char *filename){
50 FILE *f = fopen(filename, "w");
51 /* simply write fl, map, clusters and mesh in sequence */
52 fwrite(fl, sizeof(sparrow_find_lines_t), 1, f);
54 fwrite(fl->map, sizeof(sparrow_intersect_t), sparrow->in.pixcount, f);
55 fwrite(fl->clusters, sizeof(sparrow_cluster_t), fl->n_hlines * fl->n_vlines, f);
56 fwrite(fl->mesh, sizeof(sparrow_corner_t), fl->n_hlines * fl->n_vlines, f);
57 fclose(f);
60 static void read_edges_info(GstSparrow *sparrow, sparrow_find_lines_t *fl, const char *filename){
61 FILE *f = fopen(filename, "r");
62 sparrow_find_lines_t fl2;
63 size_t read = fread(&fl2, sizeof(sparrow_find_lines_t), 1, f);
64 assert(fl2.n_hlines == fl->n_hlines);
65 assert(fl2.n_vlines == fl->n_vlines);
67 guint n_corners = fl->n_hlines * fl->n_vlines;
68 read += fread(fl->map, sizeof(sparrow_intersect_t), sparrow->in.pixcount, f);
69 read += fread(fl->clusters, sizeof(sparrow_cluster_t), n_corners, f);
70 read += fread(fl->mesh, sizeof(sparrow_corner_t), n_corners, f);
71 fclose(f);
75 int sort_median(int *a, guint n)
77 guint i, j;
78 /*stupid sort, but n is very small*/
79 for (i = 0; i < n; i++){
80 for (j = i + 1; j < n; j++){
81 if (a[i] > a[j]){
82 int tmp = a[j];
83 a[j] = a[i];
84 a[i] = tmp;
88 guint middle = n / 2;
89 int answer = a[middle];
91 if ((n & 1) == 0){
92 answer += a[middle - 1];
93 answer /= 2;
95 return answer;
102 static void corners_to_lut(GstSparrow *sparrow, sparrow_find_lines_t *fl){
103 //DEBUG_FIND_LINES(fl);
104 sparrow_map_t *map = &sparrow->map; /*rows in sparrow->out */
105 guint8 *mask = sparrow->screenmask; /*mask in sparrow->in */
106 sparrow_corner_t *mesh = fl->mesh; /*maps regular points in ->out to points in ->in */
108 int mesh_w = fl->n_vlines;
109 int mesh_h = fl->n_hlines;
110 int in_w = sparrow->in.width;
111 int mcy, mmy, mcx; /*Mesh Corner|Modulus X|Y*/
113 int x;
114 sparrow_map_row_t *row = map->rows;
115 sparrow_map_point_t *p = map->point_mem;
116 sparrow_corner_t *mesh_row = mesh;
117 for(mcy = 0; mcy < mesh_h; mcy++){
118 for (mmy = 0; mmy < LINE_PERIOD; mmy++){
119 sparrow_corner_t *mesh_square = mesh_row;
120 row->points = p;
121 row->start = 0;
122 row->end = 0;
123 for(mcx = 0; mcx < mesh_w; mcx++){
124 if (mesh_square->used){
125 int iy = mesh_square->in_y + mmy * mesh_square->dyd;
126 int ix = mesh_square->in_x + mmy * mesh_square->dxd;
127 int ii = OFFSET(ix, iy, in_w);
128 int ii_end = OFFSET(ix + (LINE_PERIOD - 1) * mesh_square->dxr,
129 iy + (LINE_PERIOD - 1) * mesh_square->dyr, in_w);
130 int start_on = mask[ii];
131 int end_on = mask[ii_end];
132 if(start_on && end_on){
133 /*add the point, maybe switch on */
134 if (row->start == row->end){/* if both are 0 */
135 row->start = mcx * LINE_PERIOD;
137 p->x = ix;
138 p->y = iy;
139 p->dx = mesh_square->dxr;
140 p->dy = mesh_square->dyr;
141 p++;
143 else if (start_on){
144 /*add the point, switch off somewhere in the middle*/
145 for (x = 1; x < LINE_PERIOD; x++){
146 iy += mesh_square->dyr;
147 ix += mesh_square->dxr;
148 ii = OFFSET(ix, iy, in_w);
149 if (mask[ii]){
150 /*point is not in the same column with the others,
151 but sparrow knows this because the row->start says so */
152 row->start = mcx + x;
153 p->x = ix;
154 p->y = iy;
155 p->dx = mesh_square->dxr;
156 p->dy = mesh_square->dyr;
157 p++;
158 break;
162 else if (end_on){
163 /* add some, switch off */
164 for (x = 1; x < LINE_PERIOD; x++){
165 iy += mesh_square->dyr;
166 ix += mesh_square->dxr;
167 ii = OFFSET(ix, iy, in_w);
168 if (! mask[ii]){
169 row->end = mcx + x;
170 break;
174 else {
175 /*3 cases:
176 start > end: this is first off pixel.
177 start == end: row hasn't started (both 0)
178 start < end: both are set -- row is done
180 if (row->start > row->end){
181 row->end = mcx * LINE_PERIOD;
183 else if (row->start < row->end){
184 break;
188 mesh_square++;
190 row++;
192 mesh_row += mesh_w;
196 UNUSED static void
197 corners_to_full_lut(GstSparrow *sparrow, sparrow_find_lines_t *fl){
198 //DEBUG_FIND_LINES(fl);
199 sparrow_corner_t *mesh = fl->mesh; /*maps regular points in ->out to points in ->in */
200 sparrow_map_lut_t *map_lut = sparrow->map_lut;
201 int mesh_w = fl->n_vlines;
202 int mesh_h = fl->n_hlines;
203 int mcy, mmy, mcx, mmx; /*Mesh Corner|Modulus X|Y*/
204 int i = 0;
205 sparrow_corner_t *mesh_row = mesh;
206 for(mcy = 0; mcy < mesh_h; mcy++){
207 for (mmy = 0; mmy < LINE_PERIOD; mmy++){
208 sparrow_corner_t *mesh_square = mesh_row;
209 for(mcx = 0; mcx < mesh_w; mcx++){
210 int iy = mesh_square->in_y + mmy * mesh_square->dyd;
211 int ix = mesh_square->in_x + mmy * mesh_square->dxd;
212 for (mmx = 0; mmx < LINE_PERIOD; mmx++, i++){
213 map_lut[i].x = ix >> SPARROW_FP_2_LUT;
214 map_lut[i].y = iy >> SPARROW_FP_2_LUT;
215 ix += mesh_square->dxr;
216 iy += mesh_square->dyr;
218 mesh_square++;
221 mesh_row += mesh_w;
223 sparrow->map_lut = map_lut;
226 #define DIV ((double)(1 << SPARROW_FIXED_POINT))
227 #define INTXY(x)((x) / (1 << SPARROW_FIXED_POINT))
228 #define FLOATXY(x)(((double)(x)) / (1 << SPARROW_FIXED_POINT))
229 static void
230 debug_corners_image(GstSparrow *sparrow, sparrow_find_lines_t *fl){
231 sparrow_corner_t *mesh = fl->mesh;
232 guint32 *data = (guint32*)fl->debug->imageData;
233 guint w = fl->debug->width;
234 memset(data, 0, sparrow->in.size);
235 guint32 colours[4] = {0xff0000ff, 0x0000ff00, 0x00ff0000, 0xcccccccc};
236 for (int i = 0; i < fl->n_vlines * fl->n_hlines; i++){
237 sparrow_corner_t *c = &mesh[i];
238 int x = c->in_x;
239 int y = c->in_y;
240 GST_DEBUG("i %d used %d x: %f, y: %f dxr %f dyr %f dxd %f dyd %f\n"
241 "int x, y %d,%d (raw %d,%d) data %p\n",
242 i, c->used, FLOATXY(x), FLOATXY(y),
243 FLOATXY(c->dxr), FLOATXY(c->dyr), FLOATXY(c->dxd), FLOATXY(c->dyd),
244 INTXY(x), INTXY(y), x, y, data);
245 int txr = x;
246 int txd = x;
247 int tyr = y;
248 int tyd = y;
249 for (int j = 1; j < LINE_PERIOD; j++){
250 txr += c->dxr;
251 txd += c->dxd;
252 tyr += c->dyr;
253 tyd += c->dyd;
254 data[INTXY(tyr) * w + INTXY(txr)] = 0x000088;
255 data[INTXY(tyd) * w + INTXY(txd)] = 0x663300;
257 #if 0
258 #define LP8 (LINE_PERIOD / 8)
259 #define LP4 (LINE_PERIOD / 4)
260 #define LP2 (LINE_PERIOD / 2)
261 data[INTXY(y + c->dyr * LP8) * w + INTXY(x + c->dxr * LP8)] = 0xbbbbbbbb;
262 data[INTXY(y + c->dyr * LP4) * w + INTXY(x + c->dxr * LP4)] = 0xaaaaaaaa;
263 data[INTXY(y + c->dyr * LP2) * w + INTXY(x + c->dxr * LP2)] = 0x99999999;
264 data[INTXY(y + c->dyd * LP8) * w + INTXY(x + c->dxd * LP8)] = 0xbb6666bb;
265 data[INTXY(y + c->dyd * LP4) * w + INTXY(x + c->dxd * LP4)] = 0xaa5555aa;
266 data[INTXY(y + c->dyd * LP2) * w + INTXY(x + c->dxd * LP2)] = 0x99444499;
267 #endif
268 data[INTXY(y) * w + INTXY(x)] = colours[MIN(c->used, 2)];
270 MAYBE_DEBUG_IPL(fl->debug);
274 static void
275 debug_clusters(GstSparrow *sparrow, sparrow_find_lines_t *fl){
276 //sparrow_cluster_t *clusters = fl->clusters;
279 /*signal product is close to 18 bits. reduce to 4 */
280 #define SIGNAL_QUANT (1 << 14)
282 /*maximum number of pixels in a cluster */
283 #define CLUSTER_SIZE 8
286 /*create the mesh */
287 static void
288 find_corners_make_clusters(GstSparrow *sparrow, guint8 *in, sparrow_find_lines_t *fl){
289 sparrow_cluster_t *clusters = fl->clusters;
290 int x, y;
291 /*each point in fl->map is in a vertical line, a horizontal line, both, or
292 neither. Only the "both" case matters. */
293 for (y = 0; y < sparrow->in.height; y++){
294 for (x = 0; x < sparrow->in.width; x++){
295 sparrow_intersect_t *p = &fl->map[y * sparrow->in.width + x];
296 guint vsig = p->signal[SPARROW_VERTICAL];
297 guint hsig = p->signal[SPARROW_HORIZONTAL];
298 /*remembering that 0 is valid as a line number, but not as a signal */
299 if (! (vsig && hsig)){
300 continue;
302 /*This one is lobbying for the position of a corner.*/
303 int vline = p->lines[SPARROW_VERTICAL];
304 int hline = p->lines[SPARROW_HORIZONTAL];
306 sparrow_cluster_t *cluster = &clusters[hline * fl->n_vlines + vline];
307 sparrow_voter_t *voters = cluster->voters;
308 int n = cluster->n;
309 guint signal = (vsig * hsig) / SIGNAL_QUANT;
310 int xfp = x << SPARROW_FIXED_POINT;
311 int yfp = y << SPARROW_FIXED_POINT;
313 GST_DEBUG("signal at %p (%d, %d): %dv %dh, product %u, lines: %dv %dh\n"
314 "cluster is %p, n is %d\n", p, x, y,
315 vsig, hsig, signal, vline, hline, cluster, n);
317 if (n < CLUSTER_SIZE){
318 voters[n].x = xfp;
319 voters[n].y = yfp;
320 voters[n].signal = signal;
321 cluster->n++;
323 else {
324 for (int j = 0; j < CLUSTER_SIZE; j++){
325 if (voters[j].signal < signal){
326 guint tmp_s = voters[j].signal;
327 int tmp_x = voters[j].x;
328 int tmp_y = voters[j].y;
329 voters[j].signal = signal;
330 voters[j].x = xfp;
331 voters[j].y = yfp;
332 signal = tmp_s;
333 xfp = tmp_x;
334 yfp = tmp_y;
335 GST_DEBUG("more than %d pixels at cluster for corner %d, %d.\n",
336 CLUSTER_SIZE, vline, hline);
344 /* look for connected group. if there is more than one connected group,
345 despair.*/
347 static inline void
348 discard_cluster_outliers(sparrow_cluster_t *cluster)
352 static inline void
353 x_discard_cluster_outliers(sparrow_cluster_t *cluster)
355 sparrow_voter_t *v = cluster->voters;
356 int i, j;
358 guint32 touch[CLUSTER_SIZE];
360 while (cluster->n){
361 guint32 all = (1 << cluster->n) - 1;
362 for (i = 0; i < cluster->n; i++){
363 touch[i] = 1 << i;
366 for (i = 0; i < cluster->n - 1; i++){
367 for (j = i + 1; j < cluster->n; j++){
368 if (((abs(v[i].x - v[j].x) <= 2) && (abs(v[i].y - v[j].y) <= 2)) ||
369 (touch[i] & touch[j])){
370 touch[i] |= touch[j];
371 touch[j] = touch[i];
375 if (touch[cluster->n - 1] == all){
376 break;
378 /* something is wrong! somebody is disconnected! expel them!?
379 backpropagate connectedness, find the maximum popcount, discard the
380 others. */
381 int bcount = 0;
382 guint bmask = 0;
384 for (i = cluster->n - 1; i >= 0; i++){
385 if (bmask != touch[i] &&
386 bcount < (int)popcount32(touch[i])){
387 bmask = touch[i];
388 bcount = popcount32(touch[i]);
390 for (j = 0; j < i; j++){
391 touch[j] = (touch[j] & touch[i]) ? touch[i] : touch[j];
394 if (bcount > cluster->n / 2){
395 j = 0;
396 for (i = 0; i < cluster->n; i++){
397 if (touch[i] == bmask){
398 v[j] = v[i];
399 j++;
402 cluster->n = j;
409 /*create the mesh */
410 static inline void
411 find_corners_make_corners(GstSparrow *sparrow, guint8 *in, sparrow_find_lines_t *fl){
412 //DEBUG_FIND_LINES(fl);
413 int width = fl->n_vlines;
414 int height = fl->n_hlines;
415 sparrow_cluster_t *clusters = fl->clusters;
416 sparrow_corner_t *mesh = fl->mesh;
417 int x, y, i;
419 i = 0;
420 for (y = 0; y < height; y++){
421 for (x = 0; x < width; x++, i++){
422 /* how to do this?
423 1. centre of gravity (x,y, weighted average)
424 2. discard outliers? look for connectedness? but if 2 are outliers?
426 sparrow_cluster_t *cluster = clusters + i;
427 if (cluster->n == 0){
428 continue;
431 discard_cluster_outliers(cluster);
433 int xsum, ysum;
434 int xmean, ymean;
435 int votes;
436 int j;
437 xsum = 0;
438 ysum = 0;
439 votes = 0;
440 for (j = 0; j < cluster->n; j++){
441 votes += cluster->voters[j].signal;
442 ysum += cluster->voters[j].y * cluster->voters[j].signal;
443 xsum += cluster->voters[j].x * cluster->voters[j].signal;
445 xmean = xsum / votes;
446 ymean = ysum / votes;
447 GST_DEBUG("corner %d: %d voters, %d votes, sum %d,%d, mean %d,%d\n",
448 i, cluster->n, votes, xsum, ysum, xmean, ymean);
450 mesh[i].in_x = xmean;
451 mesh[i].in_y = ymean;
452 mesh[i].used = TRUE;
453 GST_DEBUG("found corner %d at (%3f, %3f)\n",
454 i, FLOATXY(xmean), FLOATXY(ymean));
459 static inline void
460 find_corners_make_map(GstSparrow *sparrow, guint8 *in, sparrow_find_lines_t *fl){
461 int i;
462 int width = fl->n_vlines;
463 int height = fl->n_hlines;
464 sparrow_corner_t *mesh = fl->mesh;
465 gint x, y;
467 //DEBUG_FIND_LINES(fl);
468 /* calculate deltas toward adjacent corners */
469 /* try to extrapolate left and up, if possible, so need to go backwards. */
470 i = width * height - 1;
471 for (y = height - 1; y >= 0; y--){
472 for (x = width - 1; x >= 0; x--, i--){
473 sparrow_corner_t *corner = &mesh[i];
474 /* edge deltas will always come out as zero */
475 sparrow_corner_t *right = (x >= width - 1) ? corner : corner + 1;
476 sparrow_corner_t *down = (y >= height - 1) ? corner : corner + width;
477 GST_DEBUG("i %d xy %d,%d width %d. in_xy %d,%d; down in_xy %d,%d; right in_xy %d,%d\n",
478 i, x, y, width, corner->in_x, corner->in_y, down->in_x,
479 down->in_y, right->in_x, right->in_y);
480 if (corner->used){
481 corner->dxr = (right->in_x - corner->in_x) / LINE_PERIOD;
482 corner->dyr = (right->in_y - corner->in_y) / LINE_PERIOD;
483 corner->dxd = (down->in_x - corner->in_x) / LINE_PERIOD;
484 corner->dyd = (down->in_y - corner->in_y) / LINE_PERIOD;
486 else {
487 /*prefer copy from left unless it is itself reconstructed (for no
488 great reason), or it has no dx/dy because it is an edge piece.
489 A mixed copy would be possible and better */
490 sparrow_corner_t *rsrc = (right->used &&
491 (right->used <= down->used) &&
492 (right != corner)) ? right : down;
493 sparrow_corner_t *dsrc = (down->used &&
494 (right->used >= down->used) &&
495 (down != corner)) ? down : right;
496 corner->dxr = rsrc->dxr;
497 corner->dyr = rsrc->dyr;
498 corner->dxd = dsrc->dxd;
499 corner->dyd = dsrc->dyd;
500 /*now extrapolate position, preferably from both left and right */
501 int cx = 0, cy = 0, cc = 0;
502 if (right != corner){
503 cc = 1;
504 cx = right->in_x - corner->dxr * LINE_PERIOD;
505 cy = right->in_y - corner->dyr * LINE_PERIOD;
507 if (down != corner){
508 cx += down->in_x - corner->dxd * LINE_PERIOD;
509 cy += down->in_y - corner->dyd * LINE_PERIOD;
510 cx >>= cc;
511 cy >>= cc;
513 /* if neither right nor down are there, this
514 corner can't be placed */
515 corner->in_x = cx;
516 corner->in_y = cy;
517 corner->used = MAX(right->used, down->used) + 1;
523 static void
524 find_corners(GstSparrow *sparrow, guint8 *in, sparrow_find_lines_t *fl){
525 find_corners_make_clusters(sparrow, in, fl);
526 if (sparrow->debug){
527 debug_clusters(sparrow, fl);
529 find_corners_make_corners(sparrow, in, fl);
530 find_corners_make_map(sparrow, in, fl);
531 if (sparrow->debug){
532 DEBUG_FIND_LINES(fl);
533 debug_corners_image(sparrow, fl);
538 /* With no line drawn (in our colour) look at the background noise. Any real
539 signal has to be stringer than this.
541 XXX looking for simple maximum -- maybe heap or histogram might be better,
542 so as to be less susceptible to wierd outliers (e.g., bad pixels). */
543 static void
544 look_for_threshold(GstSparrow *sparrow, guint8 *in, sparrow_find_lines_t *fl){
545 //DEBUG_FIND_LINES(fl);
546 int i;
547 guint32 colour;
548 guint32 cmask = sparrow->out.colours[sparrow->colour];
549 guint32 signal;
550 guint32 *in32 = (guint32 *)in;
551 guint32 highest = 0;
552 for (i = 0; i < (int)sparrow->in.pixcount; i++){
553 colour = in32[i] & cmask;
554 signal = ((colour >> fl->shift1) +
555 (colour >> fl->shift2)) & 0x1ff;
556 if (signal > highest){
557 highest = signal;
560 fl->threshold = highest + 1;
561 GST_DEBUG("found maximum noise of %d, using threshold %d\n", highest, fl->threshold);
565 static void
566 look_for_line(GstSparrow *sparrow, guint8 *in, sparrow_find_lines_t *fl,
567 sparrow_line_t *line){
568 guint i;
569 guint32 colour;
570 guint32 cmask = sparrow->out.colours[sparrow->colour];
571 int signal;
572 guint32 *in32 = (guint32 *)in;
573 for (i = 0; i < sparrow->in.pixcount; i++){
574 colour = in32[i] & cmask;
575 signal = ((colour >> fl->shift1) +
576 (colour >> fl->shift2)) & 0x1ff;
577 if (signal > fl->threshold){
578 if (fl->map[i].lines[line->dir]){
579 GST_DEBUG("HEY, expected point %d to be in line %d (dir %d)"
580 "and thus empty, but it is also in line %d\n",
581 i, line->index, line->dir, fl->map[i].lines[line->dir]);
583 fl->map[i].lines[line->dir] = line->index;
584 fl->map[i].signal[line->dir] = signal;
589 static void
590 debug_map_image(GstSparrow *sparrow, sparrow_find_lines_t *fl){
591 guint32 *data = (guint32*)fl->debug->imageData;
592 memset(data, 0, sparrow->in.size);
593 for (guint i = 0; i < sparrow->in.pixcount; i++){
594 data[i] |= fl->map[i].signal[SPARROW_HORIZONTAL] << sparrow->in.gshift;
595 data[i] |= fl->map[i].signal[SPARROW_VERTICAL] << sparrow->in.rshift;
597 MAYBE_DEBUG_IPL(fl->debug);
600 /* draw the line (in sparrow->colour) */
601 static inline void
602 draw_line(GstSparrow * sparrow, sparrow_line_t *line, guint8 *out){
603 guint32 *p = (guint32 *)out;
604 guint32 colour = sparrow->out.colours[sparrow->colour];
605 int i;
606 if (line->dir == SPARROW_HORIZONTAL){
607 p += line->offset * sparrow->out.width;
608 for (i = 0; i < sparrow->out.width; i++){
609 p[i] = colour;
612 else {
613 guint32 *p = (guint32 *)out;
614 p += line->offset;
615 for(i = 0; i < sparrow->out.height; i++){
616 *p = colour;
617 p += sparrow->out.width;
623 /* show each line for 2 frames, then wait sparrow->lag frames, leaving line on
624 until last one.
627 INVISIBLE sparrow_state
628 mode_find_edges(GstSparrow *sparrow, guint8 *in, guint8 *out){
629 sparrow_find_lines_t *fl = (sparrow_find_lines_t *)sparrow->helper_struct;
630 //DEBUG_FIND_LINES(fl);
631 if (fl->current == fl->n_lines){
632 goto done;
634 sparrow_line_t *line = fl->shuffled_lines[fl->current];
636 sparrow->countdown--;
637 memset(out, 0, sparrow->out.size);
638 if (sparrow->countdown){
639 /* show the line except on the first round, when we find a threshold*/
640 if (fl->threshold){
641 GST_DEBUG("current %d line %p\n", fl->current, line);
642 draw_line(sparrow, line, out);
645 else{
646 /*show nothing, look for result */
647 if (fl->threshold){
648 look_for_line(sparrow, in, fl, line);
649 fl->current++;
651 else {
652 look_for_threshold(sparrow, in, fl);
654 sparrow->countdown = sparrow->lag + 2;
656 if (sparrow->debug){
657 debug_map_image(sparrow, fl);
659 return SPARROW_STATUS_QUO;
660 done:
661 /*match up lines and find corners */
662 find_corners(sparrow, in, fl);
663 corners_to_lut(sparrow, fl);
664 return SPARROW_NEXT_STATE;
668 INVISIBLE void
669 finalise_find_edges(GstSparrow *sparrow){
670 sparrow_find_lines_t *fl = (sparrow_find_lines_t *)sparrow->helper_struct;
671 //DEBUG_FIND_LINES(fl);
672 if (sparrow->save){
673 dump_edges_info(sparrow, fl, sparrow->save);
675 if (sparrow->debug){
676 cvReleaseImage(&fl->debug);
678 free(fl->h_lines);
679 free(fl->shuffled_lines);
680 free(fl->map);
681 free(fl->mesh);
682 free(fl->clusters);
683 free(fl);
684 sparrow->helper_struct = NULL;
688 static void
689 setup_colour_shifts(GstSparrow *sparrow, sparrow_find_lines_t *fl){
690 switch (sparrow->colour){
691 case SPARROW_WHITE:
692 case SPARROW_GREEN:
693 fl->shift1 = sparrow->in.gshift;
694 fl->shift2 = sparrow->in.gshift;
695 break;
696 case SPARROW_MAGENTA:
697 fl->shift1 = sparrow->in.rshift;
698 fl->shift2 = sparrow->in.bshift;
699 break;
703 INVISIBLE void
704 init_find_edges(GstSparrow *sparrow){
705 gint32 w = sparrow->out.width;
706 gint32 h = sparrow->out.height;
707 gint i;
708 sparrow_find_lines_t *fl = zalloc_aligned_or_die(sizeof(sparrow_find_lines_t));
709 sparrow->helper_struct = (void *)fl;
711 gint h_lines = (h + LINE_PERIOD - 1) / LINE_PERIOD;
712 gint v_lines = (w + LINE_PERIOD - 1) / LINE_PERIOD;
713 gint n_lines = (h_lines + v_lines);
714 gint n_corners = (h_lines * v_lines);
715 fl->n_hlines = h_lines;
716 fl->n_vlines = v_lines;
717 fl->n_lines = n_lines;
719 fl->h_lines = malloc_aligned_or_die(sizeof(sparrow_line_t) * n_lines);
720 fl->shuffled_lines = malloc_aligned_or_die(sizeof(sparrow_line_t*) * n_lines);
721 GST_DEBUG("shuffled lines, malloced %p\n", fl->shuffled_lines);
723 if (sparrow->reload){
724 read_edges_info(sparrow, fl, sparrow->reload);
726 else {
727 fl->map = zalloc_aligned_or_die(sizeof(sparrow_intersect_t) * sparrow->in.pixcount);
728 fl->clusters = zalloc_or_die(n_corners * sizeof(sparrow_cluster_t));
729 fl->mesh = zalloc_aligned_or_die(n_corners * sizeof(sparrow_corner_t));
731 sparrow_line_t *line = fl->h_lines;
732 sparrow_line_t **sline = fl->shuffled_lines;
733 int offset = LINE_PERIOD / 2;
735 for (i = 0, offset = LINE_PERIOD / 2; offset < h;
736 i++, offset += LINE_PERIOD){
737 line->offset = offset;
738 line->dir = SPARROW_HORIZONTAL;
739 line->index = i;
740 *sline = line;
741 line++;
742 sline++;
745 /*now add the vertical lines */
746 fl->v_lines = line;
747 for (i = 0, offset = LINE_PERIOD / 2; offset < w;
748 i++, offset += LINE_PERIOD){
749 line->offset = offset;
750 line->dir = SPARROW_VERTICAL;
751 line->index = i;
752 *sline = line;
753 line++;
754 sline++;
756 //DEBUG_FIND_LINES(fl);
758 GST_DEBUG("allocated %d lines, used %d\n", n_lines, line - fl->h_lines);
760 /*now shuffle */
761 for (i = 0; i < n_lines - 1; i++){
762 int j = RANDINT(sparrow, 0, n_lines);
763 sparrow_line_t *tmp = fl->shuffled_lines[j];
764 fl->shuffled_lines[j] = fl->shuffled_lines[i];
765 fl->shuffled_lines[i] = tmp;
768 setup_colour_shifts(sparrow, fl);
770 if (sparrow->reload){
771 sparrow->countdown = 2;
773 else {
774 sparrow->countdown = sparrow->lag + 2;
777 //DEBUG_FIND_LINES(fl);
778 if (sparrow->debug){
779 CvSize size = {sparrow->in.width, sparrow->in.height};
780 fl->debug = cvCreateImage(size, IPL_DEPTH_8U, PIXSIZE);