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.
21 #include "gstsparrow.h"
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
);
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
);
75 int sort_median(int *a
, guint n
)
78 /*stupid sort, but n is very small*/
79 for (i
= 0; i
< n
; i
++){
80 for (j
= i
+ 1; j
< n
; j
++){
89 int answer
= a
[middle
];
92 answer
+= a
[middle
- 1];
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*/
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
;
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
;
139 p
->dx
= mesh_square
->dxr
;
140 p
->dy
= mesh_square
->dyr
;
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
);
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
;
155 p
->dx
= mesh_square
->dxr
;
156 p
->dy
= mesh_square
->dyr
;
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
);
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
){
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*/
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
;
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))
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
];
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
);
249 for (int j
= 1; j
< LINE_PERIOD
; j
++){
254 data
[INTXY(tyr
) * w
+ INTXY(txr
)] = 0x000088;
255 data
[INTXY(tyd
) * w
+ INTXY(txd
)] = 0x663300;
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;
268 data
[INTXY(y
) * w
+ INTXY(x
)] = colours
[MIN(c
->used
, 2)];
270 MAYBE_DEBUG_IPL(fl
->debug
);
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
288 find_corners_make_clusters(GstSparrow
*sparrow
, guint8
*in
, sparrow_find_lines_t
*fl
){
289 sparrow_cluster_t
*clusters
= fl
->clusters
;
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
)){
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
;
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
){
320 voters
[n
].signal
= signal
;
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
;
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,
348 discard_cluster_outliers(sparrow_cluster_t
*cluster
)
353 x_discard_cluster_outliers(sparrow_cluster_t
*cluster
)
355 sparrow_voter_t
*v
= cluster
->voters
;
358 guint32 touch
[CLUSTER_SIZE
];
361 guint32 all
= (1 << cluster
->n
) - 1;
362 for (i
= 0; i
< cluster
->n
; 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
];
375 if (touch
[cluster
->n
- 1] == all
){
378 /* something is wrong! somebody is disconnected! expel them!?
379 backpropagate connectedness, find the maximum popcount, discard the
384 for (i
= cluster
->n
- 1; i
>= 0; i
++){
385 if (bmask
!= touch
[i
] &&
386 bcount
< (int)popcount32(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){
396 for (i
= 0; i
< cluster
->n
; i
++){
397 if (touch
[i
] == bmask
){
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
;
420 for (y
= 0; y
< height
; y
++){
421 for (x
= 0; x
< width
; x
++, i
++){
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){
431 discard_cluster_outliers(cluster
);
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
;
453 GST_DEBUG("found corner %d at (%3f, %3f)\n",
454 i
, FLOATXY(xmean
), FLOATXY(ymean
));
460 find_corners_make_map(GstSparrow
*sparrow
, guint8
*in
, sparrow_find_lines_t
*fl
){
462 int width
= fl
->n_vlines
;
463 int height
= fl
->n_hlines
;
464 sparrow_corner_t
*mesh
= fl
->mesh
;
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
);
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
;
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
){
504 cx
= right
->in_x
- corner
->dxr
* LINE_PERIOD
;
505 cy
= right
->in_y
- corner
->dyr
* LINE_PERIOD
;
508 cx
+= down
->in_x
- corner
->dxd
* LINE_PERIOD
;
509 cy
+= down
->in_y
- corner
->dyd
* LINE_PERIOD
;
513 /* if neither right nor down are there, this
514 corner can't be placed */
517 corner
->used
= MAX(right
->used
, down
->used
) + 1;
524 find_corners(GstSparrow
*sparrow
, guint8
*in
, sparrow_find_lines_t
*fl
){
525 find_corners_make_clusters(sparrow
, in
, fl
);
527 debug_clusters(sparrow
, fl
);
529 find_corners_make_corners(sparrow
, in
, fl
);
530 find_corners_make_map(sparrow
, in
, fl
);
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). */
544 look_for_threshold(GstSparrow
*sparrow
, guint8
*in
, sparrow_find_lines_t
*fl
){
545 //DEBUG_FIND_LINES(fl);
548 guint32 cmask
= sparrow
->out
.colours
[sparrow
->colour
];
550 guint32
*in32
= (guint32
*)in
;
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
){
560 fl
->threshold
= highest
+ 1;
561 GST_DEBUG("found maximum noise of %d, using threshold %d\n", highest
, fl
->threshold
);
566 look_for_line(GstSparrow
*sparrow
, guint8
*in
, sparrow_find_lines_t
*fl
,
567 sparrow_line_t
*line
){
570 guint32 cmask
= sparrow
->out
.colours
[sparrow
->colour
];
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
;
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) */
602 draw_line(GstSparrow
* sparrow
, sparrow_line_t
*line
, guint8
*out
){
603 guint32
*p
= (guint32
*)out
;
604 guint32 colour
= sparrow
->out
.colours
[sparrow
->colour
];
606 if (line
->dir
== SPARROW_HORIZONTAL
){
607 p
+= line
->offset
* sparrow
->out
.width
;
608 for (i
= 0; i
< sparrow
->out
.width
; i
++){
613 guint32
*p
= (guint32
*)out
;
615 for(i
= 0; i
< sparrow
->out
.height
; i
++){
617 p
+= sparrow
->out
.width
;
623 /* show each line for 2 frames, then wait sparrow->lag frames, leaving line on
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
){
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*/
641 GST_DEBUG("current %d line %p\n", fl
->current
, line
);
642 draw_line(sparrow
, line
, out
);
646 /*show nothing, look for result */
648 look_for_line(sparrow
, in
, fl
, line
);
652 look_for_threshold(sparrow
, in
, fl
);
654 sparrow
->countdown
= sparrow
->lag
+ 2;
657 debug_map_image(sparrow
, fl
);
659 return SPARROW_STATUS_QUO
;
661 /*match up lines and find corners */
662 find_corners(sparrow
, in
, fl
);
663 corners_to_lut(sparrow
, fl
);
664 return SPARROW_NEXT_STATE
;
669 finalise_find_edges(GstSparrow
*sparrow
){
670 sparrow_find_lines_t
*fl
= (sparrow_find_lines_t
*)sparrow
->helper_struct
;
671 //DEBUG_FIND_LINES(fl);
673 dump_edges_info(sparrow
, fl
, sparrow
->save
);
676 cvReleaseImage(&fl
->debug
);
679 free(fl
->shuffled_lines
);
684 sparrow
->helper_struct
= NULL
;
689 setup_colour_shifts(GstSparrow
*sparrow
, sparrow_find_lines_t
*fl
){
690 switch (sparrow
->colour
){
693 fl
->shift1
= sparrow
->in
.gshift
;
694 fl
->shift2
= sparrow
->in
.gshift
;
696 case SPARROW_MAGENTA
:
697 fl
->shift1
= sparrow
->in
.rshift
;
698 fl
->shift2
= sparrow
->in
.bshift
;
704 init_find_edges(GstSparrow
*sparrow
){
705 gint32 w
= sparrow
->out
.width
;
706 gint32 h
= sparrow
->out
.height
;
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
);
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
;
745 /*now add the vertical lines */
747 for (i
= 0, offset
= LINE_PERIOD
/ 2; offset
< w
;
748 i
++, offset
+= LINE_PERIOD
){
749 line
->offset
= offset
;
750 line
->dir
= SPARROW_VERTICAL
;
756 //DEBUG_FIND_LINES(fl);
758 GST_DEBUG("allocated %d lines, used %d\n", n_lines
, line
- fl
->h_lines
);
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;
774 sparrow
->countdown
= sparrow
->lag
+ 2;
777 //DEBUG_FIND_LINES(fl);
779 CvSize size
= {sparrow
->in
.width
, sparrow
->in
.height
};
780 fl
->debug
= cvCreateImage(size
, IPL_DEPTH_8U
, PIXSIZE
);