]> git.sesse.net Git - vlc/blob - modules/video_filter/puzzle_pce.c
vdpau: eliminate dead code
[vlc] / modules / video_filter / puzzle_pce.c
1 /*****************************************************************************
2  * puzzle_pce.c : Puzzle game filter - pieces functions
3  *****************************************************************************
4  * Copyright (C) 2013 Vianney Boyer
5  * $Id$
6  *
7  * Author:  Vianney Boyer <vlcvboyer -at- gmail -dot- com>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31 #include <math.h>
32
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_filter.h>
36 #include <vlc_rand.h>
37
38 #include "filter_picture.h"
39
40 #include "puzzle_bezier.h"
41 #include "puzzle_lib.h"
42 #include "puzzle_pce.h"
43
44 #define SHAPES_QTY 20
45 #define PIECE_TYPE_NBR (4*2*(1+SHAPES_QTY))
46
47 /*****************************************************************************
48  * puzzle_bake_pieces_shapes: allocate and compute shapes
49  *****************************************************************************/
50 int puzzle_bake_pieces_shapes( filter_t *p_filter)
51 {
52 /* note:
53  *   piece_shape_t **ps_pieces_shapes;  * array [each piece type (PCE_TYPE_NBR  * 4 ( * negative ): top, left,right,btm)][each plane] of piece definition
54  *   0 => left border
55  *   1 => left border (negative, never used)
56  *   2 => top border
57  *   .....
58  *   8 => bezier left
59  *   9 => bezier left negative
60  *  10 => bezier top
61  *  11 => bezier top negative
62  *  12 => bezier btm
63  *  13 => bezier btm negative
64  *  14 => bezier right
65  *  15 => bezier right negative
66  *  .....
67  */
68
69     filter_sys_t *p_sys = p_filter->p_sys;
70
71     puzzle_free_ps_pieces_shapes(p_filter);
72     p_sys->ps_pieces_shapes = malloc( sizeof( piece_shape_t *) * PIECE_TYPE_NBR );
73     if( !p_sys->ps_pieces_shapes )
74         return VLC_ENOMEM;
75
76     for (int32_t i_piece = 0; i_piece < PIECE_TYPE_NBR; i_piece++) {
77         p_sys->ps_pieces_shapes[i_piece] = malloc( sizeof( piece_shape_t) * p_sys->s_allocated.i_planes );
78         if( !p_sys->ps_pieces_shapes[i_piece] )
79             return VLC_ENOMEM;
80         for (uint8_t i_plane = 0; i_plane < p_filter->p_sys->s_allocated.i_planes; i_plane++) {
81             p_sys->ps_pieces_shapes[i_piece][i_plane].i_row_nbr = 0;
82             p_sys->ps_pieces_shapes[i_piece][i_plane].ps_piece_shape_row = NULL;
83         }
84     }
85
86     int32_t i_currect_shape = 0;
87
88     for (uint8_t i_plane = 0; i_plane < p_filter->p_sys->s_allocated.i_planes; i_plane++) {
89         int i_ret;
90         i_ret = puzzle_generate_sect_border( p_filter, &p_sys->ps_pieces_shapes[i_currect_shape+0][i_plane], i_plane, puzzle_SHAPE_LEFT);
91         if (i_ret != VLC_SUCCESS) return i_ret;
92         i_ret = puzzle_generate_sect_border( p_filter, &p_sys->ps_pieces_shapes[i_currect_shape+1][i_plane], i_plane, puzzle_SHAPE_LEFT);
93         if (i_ret != VLC_SUCCESS) return i_ret;
94         i_ret = puzzle_generate_sect_border( p_filter, &p_sys->ps_pieces_shapes[i_currect_shape+2][i_plane], i_plane, puzzle_SHAPE_TOP);
95         if (i_ret != VLC_SUCCESS) return i_ret;
96         i_ret = puzzle_generate_sect_border( p_filter, &p_sys->ps_pieces_shapes[i_currect_shape+3][i_plane], i_plane, puzzle_SHAPE_TOP);
97         if (i_ret != VLC_SUCCESS) return i_ret;
98         i_ret = puzzle_generate_sect_border( p_filter, &p_sys->ps_pieces_shapes[i_currect_shape+4][i_plane], i_plane, puzzle_SHAPE_BTM);
99         if (i_ret != VLC_SUCCESS) return i_ret;
100         i_ret = puzzle_generate_sect_border( p_filter, &p_sys->ps_pieces_shapes[i_currect_shape+5][i_plane], i_plane, puzzle_SHAPE_BTM);
101         if (i_ret != VLC_SUCCESS) return i_ret;
102         i_ret = puzzle_generate_sect_border( p_filter, &p_sys->ps_pieces_shapes[i_currect_shape+6][i_plane], i_plane, puzzle_SHAPE_RIGHT);
103         if (i_ret != VLC_SUCCESS) return i_ret;
104         i_ret = puzzle_generate_sect_border( p_filter, &p_sys->ps_pieces_shapes[i_currect_shape+7][i_plane], i_plane, puzzle_SHAPE_RIGHT);
105         if (i_ret != VLC_SUCCESS) return i_ret;
106     }
107
108     i_currect_shape += 8;
109
110     int32_t i_width = p_sys->ps_desk_planes[0].i_pce_max_width;
111     int32_t i_lines = p_sys->ps_desk_planes[0].i_pce_max_lines;
112
113     for (int32_t i_shape = 0; i_shape<SHAPES_QTY; i_shape++) {
114
115         point_t *ps_scale_pts_H = puzzle_scale_curve_H(i_width, i_lines,     7, p_sys->ps_bezier_pts_H[i_shape], p_sys->s_allocated.i_shape_size);
116         point_t *ps_scale_pts_V = puzzle_H_2_scale_curve_V(i_width, i_lines, 7, p_sys->ps_bezier_pts_H[i_shape], p_sys->s_allocated.i_shape_size);
117         point_t *ps_neg_pts_H =   puzzle_curve_H_2_negative(7, ps_scale_pts_H);
118         point_t *ps_neg_pts_V =   puzzle_curve_V_2_negative(7, ps_scale_pts_V);
119
120         if (!ps_scale_pts_H || !ps_scale_pts_V || !ps_neg_pts_H || !ps_neg_pts_V)
121             return VLC_EGENERIC;
122
123         int i_ret;
124         for (uint8_t i_plane = 0; i_plane < p_filter->p_sys->s_allocated.i_planes; i_plane++) {
125             i_ret = puzzle_generate_sect_bezier( p_filter, &p_sys->ps_pieces_shapes[i_currect_shape][i_plane],   7, ps_scale_pts_V, i_plane, puzzle_SHAPE_LEFT);
126             if (i_ret != VLC_SUCCESS) break;
127             i_ret = puzzle_generate_sect_bezier( p_filter, &p_sys->ps_pieces_shapes[i_currect_shape+1][i_plane], 7, ps_neg_pts_V,   i_plane, puzzle_SHAPE_LEFT);
128             if (i_ret != VLC_SUCCESS) break;
129             i_ret = puzzle_generate_sect_bezier(  p_filter, &p_sys->ps_pieces_shapes[i_currect_shape+2][i_plane], 7, ps_scale_pts_H, i_plane, puzzle_SHAPE_TOP);
130             if (i_ret != VLC_SUCCESS) break;
131             i_ret = puzzle_generate_sect_bezier(  p_filter, &p_sys->ps_pieces_shapes[i_currect_shape+3][i_plane], 7, ps_neg_pts_H,   i_plane, puzzle_SHAPE_TOP);
132             if (i_ret != VLC_SUCCESS) break;
133
134             i_ret = puzzle_generate_sectTop2Btm( p_filter,    &p_sys->ps_pieces_shapes[i_currect_shape+4][i_plane], &p_sys->ps_pieces_shapes[i_currect_shape+2][i_plane], i_plane);
135             if (i_ret != VLC_SUCCESS) break;
136             i_ret = puzzle_generate_sectTop2Btm( p_filter,    &p_sys->ps_pieces_shapes[i_currect_shape+5][i_plane], &p_sys->ps_pieces_shapes[i_currect_shape+3][i_plane], i_plane);
137             if (i_ret != VLC_SUCCESS) break;
138             i_ret = puzzle_generate_sectLeft2Right( p_filter, &p_sys->ps_pieces_shapes[i_currect_shape+6][i_plane], &p_sys->ps_pieces_shapes[i_currect_shape][i_plane],   i_plane);
139             if (i_ret != VLC_SUCCESS) break;
140             i_ret = puzzle_generate_sectLeft2Right( p_filter, &p_sys->ps_pieces_shapes[i_currect_shape+7][i_plane], &p_sys->ps_pieces_shapes[i_currect_shape+1][i_plane], i_plane);
141             if (i_ret != VLC_SUCCESS) break;
142         }
143
144         free(ps_scale_pts_H);
145         free(ps_scale_pts_V);
146         free(ps_neg_pts_H);
147         free(ps_neg_pts_V);
148
149         if (i_ret != VLC_SUCCESS) return i_ret;
150
151         i_currect_shape += 8;
152     }
153
154     p_sys->b_shape_init = true;
155
156     return VLC_SUCCESS;
157 }
158
159 /* free allocated shapes data */
160 void puzzle_free_ps_pieces_shapes( filter_t *p_filter)
161 {
162     filter_sys_t *p_sys = p_filter->p_sys;
163
164     if (p_sys->ps_pieces_shapes == NULL)
165         return;
166
167     for (int32_t p = 0; p < p_sys->s_allocated.i_piece_types; p++) {
168         for (uint8_t i_plane = 0; i_plane < p_sys->s_allocated.i_planes; i_plane++) {
169             for (int32_t r = 0; r < p_sys->ps_pieces_shapes[p][i_plane].i_row_nbr; r++)
170                 free( p_sys->ps_pieces_shapes[p][i_plane].ps_piece_shape_row[r].ps_row_section );
171             free( p_sys->ps_pieces_shapes[p][i_plane].ps_piece_shape_row );
172         }
173         free( p_sys->ps_pieces_shapes[p] );
174     }
175     free( p_sys->ps_pieces_shapes );
176     p_sys->ps_pieces_shapes = NULL;
177 }
178
179 /*****************************************************************************
180  * puzzle_find_piece: use piece corners to find the piece selected
181  *                    by mouse cursor
182  *****************************************************************************/
183 int puzzle_find_piece( filter_t *p_filter, int32_t i_x, int32_t i_y, int32_t i_except) {
184     filter_sys_t *p_sys = p_filter->p_sys;
185
186     for (uint32_t i = 0; i < p_sys->s_allocated.i_pieces_nbr; i++) {
187         piece_t *ps_current_piece = &p_sys->ps_pieces[i];
188         if (( ps_current_piece->i_min_x <= i_x ) &&
189             ( ps_current_piece->i_max_x >= i_x ) &&
190             ( ps_current_piece->i_min_y <= i_y ) &&
191             ( ps_current_piece->i_max_y  >= i_y ) &&
192             ( (int32_t)i != i_except ) )
193         {
194             return i;
195         }
196     }
197     return -1;
198 }
199
200 /*****************************************************************************
201  * puzzle_calculate_corners: calculate corners location & regen geometry data
202  *****************************************************************************/
203 void puzzle_calculate_corners( filter_t *p_filter,  int32_t i_piece )
204 {
205     filter_sys_t *p_sys = p_filter->p_sys;
206     piece_t *ps_piece = &p_sys->ps_pieces[i_piece];
207
208     switch ( ps_piece->i_actual_angle)
209     {
210       case 0:
211         ps_piece->i_step_x_x = ps_piece->i_actual_mirror;
212         ps_piece->i_step_x_y = 0;
213         ps_piece->i_step_y_y = 1;
214         ps_piece->i_step_y_x = 0;
215         break;
216       case 1:
217         ps_piece->i_step_x_x = 0;
218         ps_piece->i_step_x_y = -ps_piece->i_actual_mirror; /* x offset on original pict creates negative y offset on desk */
219         ps_piece->i_step_y_y = 0;
220         ps_piece->i_step_y_x = 1;
221         break;
222       case 2:
223         ps_piece->i_step_x_x = -ps_piece->i_actual_mirror;
224         ps_piece->i_step_x_y = 0;
225         ps_piece->i_step_y_y = -1;
226         ps_piece->i_step_y_x = 0;
227         break;
228       case 3:
229         ps_piece->i_step_x_x = 0;
230         ps_piece->i_step_x_y = ps_piece->i_actual_mirror;
231         ps_piece->i_step_y_y = 0;
232         ps_piece->i_step_y_x = -1;
233         break;
234     }
235
236     /* regen geometry */
237     for (uint8_t i_plane = 1; i_plane < p_sys->s_allocated.i_planes; i_plane++) {
238         ps_piece->ps_piece_in_plane[i_plane].i_actual_x =
239             ps_piece->ps_piece_in_plane[0].i_actual_x * p_sys->ps_desk_planes[i_plane].i_width / p_sys->ps_desk_planes[0].i_width;
240         ps_piece->ps_piece_in_plane[i_plane].i_actual_y =
241             ps_piece->ps_piece_in_plane[0].i_actual_y * p_sys->ps_desk_planes[i_plane].i_lines / p_sys->ps_desk_planes[0].i_lines;
242     }
243
244     /* regen location of grabed piece's corners */
245     int32_t i_width = ps_piece->ps_piece_in_plane[0].i_width;
246     int32_t i_lines = ps_piece->ps_piece_in_plane[0].i_lines;
247
248     ps_piece->i_TLx = ps_piece->ps_piece_in_plane[0].i_actual_x;
249     ps_piece->i_TLy = ps_piece->ps_piece_in_plane[0].i_actual_y;
250     ps_piece->i_TRx = ps_piece->i_TLx + ( i_width - 1 ) * ps_piece->i_step_x_x;
251     ps_piece->i_TRy = ps_piece->i_TLy + ( i_width - 1 ) * ps_piece->i_step_x_y;
252     ps_piece->i_BRx = ps_piece->i_TLx + ( i_width - 1 ) * ps_piece->i_step_x_x + ( i_lines - 1 ) * ps_piece->i_step_y_x;
253     ps_piece->i_BRy = ps_piece->i_TLy + ( i_width - 1 ) * ps_piece->i_step_x_y + ( i_lines - 1 ) * ps_piece->i_step_y_y;
254     ps_piece->i_BLx = ps_piece->i_TLx + ( i_lines - 1 ) * ps_piece->i_step_y_x;
255     ps_piece->i_BLy = ps_piece->i_TLy + ( i_lines - 1 ) * ps_piece->i_step_y_y;
256
257     ps_piece->i_max_x = __MAX( __MAX( ps_piece->i_TLx, ps_piece->i_TRx ), __MAX( ps_piece->i_BLx, ps_piece->i_BRx ) );
258     ps_piece->i_min_x = __MIN( __MIN( ps_piece->i_TLx, ps_piece->i_TRx ), __MIN( ps_piece->i_BLx, ps_piece->i_BRx ) );
259     ps_piece->i_max_y = __MAX( __MAX( ps_piece->i_TLy, ps_piece->i_TRy ), __MAX( ps_piece->i_BLy, ps_piece->i_BRy ) );
260     ps_piece->i_min_y = __MIN( __MIN( ps_piece->i_TLy, ps_piece->i_TRy ), __MIN( ps_piece->i_BLy, ps_piece->i_BRy ) );
261
262     ps_piece->i_center_x = ( ps_piece->i_max_x + ps_piece->i_min_x ) / 2;
263     ps_piece->i_center_y = ( ps_piece->i_max_y + ps_piece->i_min_y ) / 2;
264
265     int32_t pce_overlap = puzzle_find_piece( p_filter, ps_piece->i_center_x, ps_piece->i_center_y, i_piece);
266
267     if ( ( pce_overlap != NO_PCE ) && ( p_sys->pi_group_qty[ps_piece->i_group_ID] == 1 ) )
268         ps_piece->b_overlap = true;
269 }
270
271 /*****************************************************************************
272  * rotate piece when user click on mouse
273  *****************************************************************************/
274 void puzzle_rotate_pce( filter_t *p_filter, int32_t i_piece, int8_t i_rotate_mirror, int32_t i_center_x, int32_t i_center_y, bool b_avoid_mirror )
275 {
276     filter_sys_t *p_sys = p_filter->p_sys;
277     piece_t *ps_piece = &p_sys->ps_pieces[i_piece];
278
279     if ( p_sys->s_current_param.i_rotate == 0 )
280         return;
281
282     if ( p_sys->s_current_param.i_rotate == 1 && (i_rotate_mirror != 2) )
283         return;
284
285     for ( uint8_t i=0; i < abs( i_rotate_mirror ); i++) {
286         int32_t i_tempx, i_tempy;
287
288         /* piece has to be rotated by 90° */
289         if ( i_rotate_mirror > 0 ) {
290             ps_piece->i_actual_angle++;
291             ps_piece->i_actual_angle &= 0x03;
292
293             i_tempx = -( i_center_y - ps_piece->ps_piece_in_plane[0].i_actual_y ) + i_center_x;
294             i_tempy = +( i_center_x - ps_piece->ps_piece_in_plane[0].i_actual_x ) + i_center_y;
295         }
296         else {
297             ps_piece->i_actual_angle--;
298             ps_piece->i_actual_angle &= 0x03;
299
300             i_tempx = +( i_center_y - ps_piece->ps_piece_in_plane[0].i_actual_y ) + i_center_x;
301             i_tempy = -( i_center_x - ps_piece->ps_piece_in_plane[0].i_actual_x ) + i_center_y;
302         }
303
304         ps_piece->ps_piece_in_plane[0].i_actual_x = i_tempx;
305         ps_piece->ps_piece_in_plane[0].i_actual_y = i_tempy;
306
307         if ( ps_piece->i_actual_angle == 0 && p_sys->s_current_param.i_rotate == 3 && !b_avoid_mirror ) {
308             ps_piece->ps_piece_in_plane[0].i_actual_x = 2 * i_center_x - ps_piece->ps_piece_in_plane[0].i_actual_x;
309             ps_piece->i_actual_mirror *= -1;
310         }
311         puzzle_calculate_corners( p_filter, i_piece );
312     }
313 }
314
315 /*****************************************************************************
316  * move group of joined pieces when user drag'n drop it with mouse
317  *****************************************************************************/
318 void puzzle_move_group( filter_t *p_filter, int32_t i_piece, int32_t i_dx, int32_t i_dy)
319 {
320     filter_sys_t *p_sys = p_filter->p_sys;
321     uint32_t i_group_ID = p_sys->ps_pieces[i_piece].i_group_ID;
322     for (uint32_t i = 0; i < p_sys->s_allocated.i_pieces_nbr; i++) {
323         piece_t *ps_piece = &p_sys->ps_pieces[i];
324         if (ps_piece->i_group_ID == i_group_ID) {
325             ps_piece->b_finished = false;
326             ps_piece->ps_piece_in_plane[0].i_actual_x += i_dx;
327             ps_piece->ps_piece_in_plane[0].i_actual_y += i_dy;
328
329             puzzle_calculate_corners( p_filter, i );
330         }
331     }
332 }
333
334 /*****************************************************************************
335  * draw straight rectangular piece in the specified plane
336  *****************************************************************************/
337 void puzzle_drw_basic_pce_in_plane( filter_t *p_filter, picture_t *p_pic_in, picture_t *p_pic_out, uint8_t i_plane, piece_t *ps_piece)
338 {
339     /* basic version rectangular & angle = 0 */
340     filter_sys_t *p_sys = p_filter->p_sys;
341
342     if ((p_sys->ps_puzzle_array == NULL) || (p_sys->ps_pieces == NULL) || (ps_piece == NULL))
343         return;
344
345     const int32_t i_src_pitch    = p_pic_in->p[i_plane].i_pitch;
346     const int32_t i_dst_pitch    = p_pic_out->p[i_plane].i_pitch;
347     const int32_t i_src_width    = p_pic_in->p[i_plane].i_pitch / p_pic_in->p[i_plane].i_pixel_pitch;
348     const int32_t i_dst_width    = p_pic_out->p[i_plane].i_pitch / p_pic_out->p[i_plane].i_pixel_pitch;
349     const int32_t i_pixel_pitch  = p_pic_out->p[i_plane].i_pixel_pitch;
350     const int32_t i_src_visible_lines    = p_pic_in->p[i_plane].i_visible_lines;
351     const int32_t i_dst_visible_lines    = p_pic_out->p[i_plane].i_visible_lines;
352     uint8_t *p_src = p_pic_in->p[i_plane].p_pixels;
353     uint8_t *p_dst = p_pic_out->p[i_plane].p_pixels;
354
355     const int32_t i_desk_start_x = ps_piece->ps_piece_in_plane[i_plane].i_actual_x;
356     const int32_t i_desk_start_y = ps_piece->ps_piece_in_plane[i_plane].i_actual_y;
357     const int32_t i_pic_start_x = ps_piece->ps_piece_in_plane[i_plane].i_original_x;
358     const int32_t i_pic_start_y = ps_piece->ps_piece_in_plane[i_plane].i_original_y;
359     const int32_t i_width = ps_piece->ps_piece_in_plane[i_plane].i_width;
360     const int32_t i_lines = ps_piece->ps_piece_in_plane[i_plane].i_lines;
361
362     const int32_t i_ofs_x   =           __MAX(0, __MAX(-i_desk_start_x,-i_pic_start_x));
363     const int32_t i_count_x = i_width - __MAX(0, __MAX(i_desk_start_x + i_width - i_dst_width, i_pic_start_x + i_width - i_src_width ));
364     const int32_t i_ofs_y   =           __MAX(0, __MAX(-i_desk_start_y,-i_pic_start_y));
365     const int32_t i_count_y = i_lines - __MAX(0, __MAX(i_desk_start_y + i_lines - i_dst_visible_lines, i_pic_start_y + i_lines - i_src_visible_lines ));
366
367     for (int32_t i_y = i_ofs_y; i_y < i_count_y; i_y++) {
368         memcpy( p_dst + (i_desk_start_y + i_y) * i_dst_pitch + ( i_desk_start_x + i_ofs_x ) * i_pixel_pitch,
369             p_src + (i_pic_start_y + i_y) * i_src_pitch + ( i_pic_start_x + i_ofs_x ) * i_pixel_pitch,
370             ( i_count_x - i_ofs_x ) * i_pixel_pitch );
371     }
372
373     return;
374 }
375
376 /*****************************************************************************
377  * draw oriented rectangular piece in the specified plane
378  *****************************************************************************/
379 void puzzle_drw_adv_pce_in_plane( filter_t *p_filter, picture_t *p_pic_in, picture_t *p_pic_out, uint8_t i_plane, piece_t *ps_piece)
380 {
381     /* here we still have rectangular shape but angle is not 0 */
382     filter_sys_t *p_sys = p_filter->p_sys;
383
384     if ((p_sys->ps_puzzle_array == NULL) || (p_sys->ps_pieces == NULL) || (ps_piece == NULL))
385         return;
386
387     const int32_t i_src_pitch    = p_pic_in->p[i_plane].i_pitch;
388     const int32_t i_dst_pitch    = p_pic_out->p[i_plane].i_pitch;
389     const int32_t i_src_width    = p_pic_in->p[i_plane].i_pitch / p_pic_in->p[i_plane].i_pixel_pitch;
390     const int32_t i_dst_width    = p_pic_out->p[i_plane].i_pitch / p_pic_out->p[i_plane].i_pixel_pitch;
391     const int32_t i_pixel_pitch  = p_pic_out->p[i_plane].i_pixel_pitch;
392     const int32_t i_src_visible_lines    = p_pic_in->p[i_plane].i_visible_lines;
393     const int32_t i_dst_visible_lines    = p_pic_out->p[i_plane].i_visible_lines;
394     uint8_t *p_src = p_pic_in->p[i_plane].p_pixels;
395     uint8_t *p_dst = p_pic_out->p[i_plane].p_pixels;
396
397     const int32_t i_desk_start_x = ps_piece->ps_piece_in_plane[i_plane].i_actual_x;
398     const int32_t i_desk_start_y = ps_piece->ps_piece_in_plane[i_plane].i_actual_y;
399     const int32_t i_pic_start_x = ps_piece->ps_piece_in_plane[i_plane].i_original_x;
400     const int32_t i_pic_start_y = ps_piece->ps_piece_in_plane[i_plane].i_original_y;
401     const int32_t i_width = ps_piece->ps_piece_in_plane[i_plane].i_width;
402     const int32_t i_lines = ps_piece->ps_piece_in_plane[i_plane].i_lines;
403
404     for (int32_t i_y = 0; i_y < i_lines; i_y++) {
405         int32_t i_current_src_y = i_pic_start_y + i_y;
406
407         if ( ( i_current_src_y >= 0 ) && ( i_current_src_y < i_src_visible_lines ) ) {
408             for (int32_t i_x = 0; i_x < i_width; i_x++) {
409                 int32_t i_current_dst_x = i_desk_start_x + i_x * ps_piece->i_step_x_x + i_y * ps_piece->i_step_y_x;
410                 int32_t i_current_dst_y = i_desk_start_y + i_x * ps_piece->i_step_x_y + i_y * ps_piece->i_step_y_y;
411                 int32_t i_current_src_x = i_pic_start_x + i_x;
412
413                 if ( ( i_current_dst_x  >= 0 ) && ( i_current_src_x >= 0 )
414                         && ( i_current_dst_x  < i_dst_width ) && ( i_current_src_x < i_src_width )
415                         && ( i_current_dst_y >= 0 ) && ( i_current_dst_y < i_dst_visible_lines ) )
416                 {
417                     memcpy( p_dst + i_current_dst_y * i_dst_pitch + i_current_dst_x * i_pixel_pitch,
418                             p_src + i_current_src_y * i_src_pitch + i_current_src_x * i_pixel_pitch,
419                            i_pixel_pitch );
420                 }
421             }
422         }
423     }
424
425     return;
426 }
427
428 /*****************************************************************************
429  * draw complex shape in the specified plane
430  *****************************************************************************/
431 void puzzle_drw_complex_pce_in_plane( filter_t *p_filter, picture_t *p_pic_in, picture_t *p_pic_out, uint8_t i_plane, piece_t *ps_piece, uint32_t i_pce)
432 {
433     /* "puzzle" shape and maybe angle != 0 */
434     filter_sys_t *p_sys = p_filter->p_sys;
435
436     if ((p_sys->ps_puzzle_array == NULL) || (p_sys->ps_pieces == NULL) || (ps_piece == NULL))
437         return;
438
439     const int32_t i_src_pitch    = p_pic_in->p[i_plane].i_pitch;
440     const int32_t i_dst_pitch    = p_pic_out->p[i_plane].i_pitch;
441     const int32_t i_src_width    = p_pic_in->p[i_plane].i_pitch / p_pic_in->p[i_plane].i_pixel_pitch;
442     const int32_t i_dst_width    = p_pic_out->p[i_plane].i_pitch / p_pic_out->p[i_plane].i_pixel_pitch;
443     const int32_t i_pixel_pitch  = p_pic_out->p[i_plane].i_pixel_pitch;
444     const int32_t i_src_visible_lines    = p_pic_in->p[i_plane].i_visible_lines;
445     const int32_t i_dst_visible_lines    = p_pic_out->p[i_plane].i_visible_lines;
446     uint8_t *p_src = p_pic_in->p[i_plane].p_pixels;
447     uint8_t *p_dst = p_pic_out->p[i_plane].p_pixels;
448
449     const int32_t i_desk_start_x = ps_piece->ps_piece_in_plane[i_plane].i_actual_x;
450     const int32_t i_desk_start_y = ps_piece->ps_piece_in_plane[i_plane].i_actual_y;
451     const int32_t i_pic_start_x = ps_piece->ps_piece_in_plane[i_plane].i_original_x;
452     const int32_t i_pic_start_y = ps_piece->ps_piece_in_plane[i_plane].i_original_y;
453
454     piece_shape_t *ps_top_shape =   &p_sys->ps_pieces_shapes[ps_piece->i_top_shape][i_plane];
455     piece_shape_t *ps_btm_shape =   &p_sys->ps_pieces_shapes[ps_piece->i_btm_shape][i_plane];
456     piece_shape_t *ps_right_shape = &p_sys->ps_pieces_shapes[ps_piece->i_right_shape][i_plane];
457     piece_shape_t *ps_left_shape =  &p_sys->ps_pieces_shapes[ps_piece->i_left_shape][i_plane];
458     piece_shape_t *ps_shape;
459
460     int32_t i_min_y = ps_top_shape->i_first_row_offset;
461     int32_t i_max_y = ps_btm_shape->i_first_row_offset + ps_btm_shape->i_row_nbr - 1;
462
463     for (int32_t i_y = i_min_y; i_y <= i_max_y; i_y++) {
464         int32_t i_current_src_y = i_pic_start_y + i_y;
465
466         if ( ( i_current_src_y >= 0 ) && ( i_current_src_y < i_src_visible_lines ) ) {
467             int32_t i_sect_start_x = 0;
468
469             /* process each sub shape (each quarter) */
470             for (int8_t i_shape=0; i_shape < 4; i_shape++) {
471                 switch ( i_shape )
472                 {
473                   case 0:
474                     ps_shape = ps_left_shape;
475                     break;
476                   case 1:
477                     ps_shape = ps_top_shape;
478                     break;
479                   case 2:
480                     ps_shape = ps_btm_shape;
481                     break;
482                   case 3:
483                     ps_shape = ps_right_shape;
484                     break;
485                 }
486
487                 int32_t i_r = i_y - ps_shape->i_first_row_offset;
488
489                 if (i_r <0 || i_r >= ps_shape->i_row_nbr)
490                     continue;
491
492                 piece_shape_row_t *ps_piece_shape_row = &ps_shape->ps_piece_shape_row[i_r];
493
494                 for (int32_t i_s = 0; i_s < ps_piece_shape_row->i_section_nbr; i_s++) {
495                     uint8_t i_type = ps_piece_shape_row->ps_row_section[i_s].i_type;
496                     int32_t i_width = ps_piece_shape_row->ps_row_section[i_s].i_width;
497                     if (i_type == 0) {
498                         /* copy pixel line from input image to puzzle desk */
499                         for (int32_t i_x = 0; i_x < i_width; i_x++) {
500                             int32_t i_current_dst_x = i_desk_start_x + (i_sect_start_x + i_x) * ps_piece->i_step_x_x + i_y * ps_piece->i_step_y_x;
501                             int32_t i_current_dst_y = i_desk_start_y + (i_sect_start_x + i_x) * ps_piece->i_step_x_y + i_y * ps_piece->i_step_y_y;
502                             int32_t i_current_src_x = i_pic_start_x + (i_sect_start_x + i_x);
503
504                             if (    i_current_dst_x < 0 || i_current_dst_x >= i_dst_width
505                                  || i_current_src_x < 0 || i_current_src_x >= i_src_width
506                                  || i_current_dst_y < 0 || i_current_dst_y >= i_dst_visible_lines )
507                                 continue;
508
509                             memcpy( p_dst + i_current_dst_y * i_dst_pitch + i_current_dst_x * i_pixel_pitch,
510                                     p_src + i_current_src_y * i_src_pitch + i_current_src_x * i_pixel_pitch,
511                                     i_pixel_pitch );
512
513                             /* Check if mouse pointer is over this pixel
514                              * Yes: set i_pointed_pce = current drawn piece
515                              */
516                             if ((i_plane == 0)  && (p_sys->i_mouse_x == i_current_dst_x )
517                                                 && (p_sys->i_mouse_y == i_current_dst_y ))
518                                 p_sys->i_pointed_pce = i_pce;
519                         }
520                     }
521                     i_sect_start_x += i_width;
522                 }
523             }
524         }
525     }
526
527     return;
528 }
529
530 /*****************************************************************************
531  * draw all puzzle pieces on the desk
532  *****************************************************************************/
533 void puzzle_draw_pieces( filter_t *p_filter, picture_t *p_pic_in, picture_t *p_pic_out)
534 {
535     filter_sys_t *p_sys = p_filter->p_sys;
536
537     if ((p_sys->ps_puzzle_array == NULL) || (p_sys->ps_pieces == NULL))
538         return;
539
540     for( uint8_t i_plane = 0; i_plane < p_pic_out->i_planes; i_plane++ ) {
541         for ( int32_t i = p_sys->s_allocated.i_pieces_nbr-1; i >= 0 ; i-- ) {
542             piece_t *ps_piece = &p_sys->ps_pieces[i];
543
544             if (!p_sys->s_current_param.b_advanced
545                     || (ps_piece->i_actual_mirror == 1 && ps_piece->i_actual_angle == 0
546                     && p_sys->s_current_param.i_shape_size == 0))
547             {
548                 puzzle_drw_basic_pce_in_plane(p_filter, p_pic_in, p_pic_out, i_plane, ps_piece);
549             }
550             else if ( ( p_sys->s_current_param.i_shape_size == 0)  || !p_sys->b_shape_init
551                     || (p_sys->ps_pieces_shapes == NULL) || (!p_sys->b_shape_init) )
552             {
553                 puzzle_drw_adv_pce_in_plane(p_filter, p_pic_in, p_pic_out, i_plane, ps_piece);
554             }
555             else {
556                 puzzle_drw_complex_pce_in_plane(p_filter, p_pic_in, p_pic_out, i_plane, ps_piece, i);
557             }
558         }
559     }
560
561     return;
562 }
563
564 /*****************************************************************************
565  * when generating shape data: determine limit between sectors to be drawn
566  *****************************************************************************/
567 int32_t puzzle_diagonal_limit( filter_t *p_filter, int32_t i_y, bool b_left, uint8_t i_plane )
568 {
569     filter_sys_t *p_sys = p_filter->p_sys;
570
571     if (b_left ^ (i_y >= p_sys->ps_desk_planes[i_plane].i_pce_max_lines / 2))
572         return ( i_y * p_sys->ps_desk_planes[i_plane].i_pce_max_width) / p_sys->ps_desk_planes[i_plane].i_pce_max_lines;
573     else
574         return p_sys->ps_desk_planes[i_plane].i_pce_max_width - ( ( i_y * p_sys->ps_desk_planes[i_plane].i_pce_max_width) / p_sys->ps_desk_planes[i_plane].i_pce_max_lines);
575 }
576
577 #define MAX_SECT 10
578
579 /*****************************************************************************
580  * generate data which will be used to draw each line of a piece sector with
581  *       flat border
582  *****************************************************************************/
583 int puzzle_generate_sect_border( filter_t *p_filter, piece_shape_t *ps_piece_shape, uint8_t i_plane, uint8_t i_border)
584 {
585     /* generate data required to draw a sector of border puzzle piece */
586     if (!ps_piece_shape)
587         return VLC_EGENERIC;
588
589     filter_sys_t *p_sys = p_filter->p_sys;
590
591     int32_t i_width = p_sys->ps_desk_planes[i_plane].i_pce_max_width;
592     int32_t i_lines = p_sys->ps_desk_planes[i_plane].i_pce_max_lines;
593
594     /* process each horizontal pixel lines */
595     int32_t i_min_y = (i_border != puzzle_SHAPE_BTM) ? 0 : floor( i_lines / 2 );
596
597     int32_t i_nb_y = (i_border != puzzle_SHAPE_TOP)?
598                         (i_lines - i_min_y) : (i_lines /2 - i_min_y);
599
600     /* allocate memory */
601     ps_piece_shape->i_row_nbr = i_nb_y;
602     ps_piece_shape->i_first_row_offset = i_min_y;
603     ps_piece_shape->ps_piece_shape_row = malloc( sizeof( piece_shape_row_t ) * i_nb_y );
604     if (!ps_piece_shape->ps_piece_shape_row)
605         return VLC_ENOMEM;
606
607     for (int32_t i_y = i_min_y; i_y < i_nb_y + i_min_y; i_y++) {
608         uint8_t i_sect = 0;
609         int32_t pi_sects[MAX_SECT];
610         int32_t i_row = i_y - i_min_y;
611
612         /* ...fill from border to next junction */
613         switch (i_border)
614         {
615           case puzzle_SHAPE_TOP:
616           case puzzle_SHAPE_BTM:
617             pi_sects[i_sect] = puzzle_diagonal_limit( p_filter, i_y, false, i_plane ) - 1
618                             - (puzzle_diagonal_limit( p_filter, i_y, true, i_plane ) - 1);
619             break;
620           case puzzle_SHAPE_RIGHT:
621             pi_sects[i_sect] = i_width - puzzle_diagonal_limit( p_filter, i_y, false, i_plane );
622             break;
623           case puzzle_SHAPE_LEFT:
624           default:
625             pi_sects[i_sect] = puzzle_diagonal_limit( p_filter, i_y, true, i_plane );
626         }
627         i_sect++;
628
629         /* ...allocate memory and copy final values */
630         ps_piece_shape->ps_piece_shape_row[i_row].i_section_nbr = i_sect;
631         ps_piece_shape->ps_piece_shape_row[i_row].ps_row_section = malloc ( sizeof(row_section_t) * i_sect);
632         if (!ps_piece_shape->ps_piece_shape_row[i_row].ps_row_section) {
633             for (uint8_t i=0; i<i_row;i++)
634                 free(ps_piece_shape->ps_piece_shape_row[i].ps_row_section);
635             free(ps_piece_shape->ps_piece_shape_row);
636             ps_piece_shape->ps_piece_shape_row = NULL;
637             return VLC_ENOMEM;
638         }
639
640         for (uint8_t i=0; i < i_sect; i++) {
641             ps_piece_shape->ps_piece_shape_row[i_row].ps_row_section[i].i_type = i % 2; /* 0 = fill ; 1 = offset */
642             ps_piece_shape->ps_piece_shape_row[i_row].ps_row_section[i].i_width = pi_sects[i];
643         }
644     }
645     return VLC_SUCCESS;
646 }
647
648 /*****************************************************************************
649  * generate data which will be used to draw each line of a piece sector based
650  *       on bezier curve
651  *****************************************************************************/
652 int puzzle_generate_sect_bezier( filter_t *p_filter, piece_shape_t *ps_piece_shape, uint8_t i_pts_nbr, point_t *ps_pt, uint8_t i_plane, uint8_t i_border)
653 {
654     /* generate data required to draw a sector of puzzle piece using bezier shape */
655     if ((!ps_pt) || (!ps_piece_shape))
656         return VLC_EGENERIC;
657
658     filter_sys_t *p_sys = p_filter->p_sys;
659
660     int32_t i_width = p_sys->ps_desk_planes[i_plane].i_pce_max_width;
661     int32_t i_lines = p_sys->ps_desk_planes[i_plane].i_pce_max_lines;
662     int32_t i_size_x_0 = p_sys->ps_desk_planes[0].i_pce_max_width;
663     int32_t i_size_y_0 = p_sys->ps_desk_planes[0].i_pce_max_lines;
664
665     float f_x_ratio =  ((float) i_width) / ((float) i_size_x_0);
666     float f_y_ratio = ((float) i_lines) / ((float) i_size_y_0);
667
668     /* first: get min x and min y */
669     float f_min_curve_x, f_min_curve_y;
670     puzzle_get_min_bezier(&f_min_curve_x, &f_min_curve_y, f_x_ratio, f_y_ratio, ps_pt, i_pts_nbr);
671
672     f_min_curve_y = __MIN(0,floor(f_min_curve_y));
673     f_min_curve_x = __MIN(0,floor(f_min_curve_x));
674
675     /* next: process each horizontal pixel lines */
676     int32_t i_min_y = (i_border==puzzle_SHAPE_TOP)?floor(f_min_curve_y):0;
677     int32_t i_nb_y = (i_border==puzzle_SHAPE_TOP)?(i_lines / 2 - i_min_y):i_lines;
678
679     /* allocate memory */
680     ps_piece_shape->i_row_nbr = i_nb_y;
681     ps_piece_shape->i_first_row_offset = i_min_y;
682     ps_piece_shape->ps_piece_shape_row = malloc( sizeof( piece_shape_row_t ) * ps_piece_shape->i_row_nbr );
683     if (!ps_piece_shape->ps_piece_shape_row)
684         return VLC_ENOMEM;
685
686     return puzzle_generate_shape_lines(p_filter, ps_piece_shape, i_min_y, i_nb_y, f_x_ratio, f_y_ratio, ps_pt, i_pts_nbr, i_border, i_plane);
687 }
688
689 /*****************************************************************************
690  * when generating shape data: determine minimum bezier value
691  *****************************************************************************/
692 void puzzle_get_min_bezier(float *f_min_curve_x, float *f_min_curve_y, float f_x_ratio, float f_y_ratio, point_t *ps_pt, uint8_t i_pts_nbr)
693 {
694     *f_min_curve_y = ps_pt[0].f_y * f_y_ratio;
695     *f_min_curve_x = ps_pt[0].f_x * f_x_ratio;
696
697     for (float f_t = 0; f_t <= i_pts_nbr - 1; f_t += 0.1 ) {
698         int8_t i_main_t = floor(f_t);
699         if ( i_main_t == i_pts_nbr - 1 )
700             i_main_t = i_pts_nbr - 2;
701         float f_sub_t = f_t - i_main_t;
702
703         *f_min_curve_x = __MIN(*f_min_curve_x,bezier_val(ps_pt,f_sub_t,i_main_t,x) * f_x_ratio);
704         *f_min_curve_y = __MIN(*f_min_curve_y,bezier_val(ps_pt,f_sub_t,i_main_t,y) * f_y_ratio);
705     }
706 }
707
708 /*****************************************************************************
709  * proceed with each line in order to generate data which will be used
710  *     to draw each line of a piece sector
711  *****************************************************************************/
712 int puzzle_generate_shape_lines( filter_t *p_filter, piece_shape_t *ps_piece_shape, int32_t i_min_y, int32_t i_nb_y, float f_x_ratio, float f_y_ratio, point_t *ps_pt, uint8_t i_pts_nbr, uint8_t i_border, uint8_t i_plane)
713 {
714     /* generate data required to draw a line of a piece sector */
715     for (int32_t i_y = i_min_y; i_y < i_nb_y + i_min_y; i_y++) {
716         int32_t i_row = i_y - i_min_y;
717
718         int32_t pi_sects[MAX_SECT];
719
720         uint8_t i_sect = puzzle_detect_curve( p_filter, i_y, f_x_ratio, f_y_ratio, ps_pt, i_pts_nbr, i_border, i_plane, pi_sects);
721
722         /* ...we have to convert absolute values to offsets and take into account min_curve_x */
723         int8_t i_s = 0;
724         int32_t i_last_x = (i_border==puzzle_SHAPE_TOP && (i_y>=0))?puzzle_diagonal_limit( p_filter, i_y, true, i_plane ):0;
725
726         for (i_s = 0; i_s<i_sect; i_s++) {
727             int32_t i_current_x = pi_sects[i_s];
728             int32_t i_delta = i_current_x - i_last_x;
729             pi_sects[i_s] = i_delta;
730
731             i_last_x = i_current_x;
732         }
733
734         switch (i_border)
735         {
736           case puzzle_SHAPE_TOP:
737             /* ...allocate memory and copy final values */
738             /* note for y > 0 we have to ignore the first offset as it is included in "Left" piece shape */
739             if ( i_y >= 0 ) {
740                 ps_piece_shape->ps_piece_shape_row[i_row].i_section_nbr = i_sect;
741                 ps_piece_shape->ps_piece_shape_row[i_row].ps_row_section = malloc (  sizeof(row_section_t) * i_sect);
742                 if (!ps_piece_shape->ps_piece_shape_row[i_row].ps_row_section) {
743                     for (uint8_t i=0; i<i_row;i++)
744                         free(ps_piece_shape->ps_piece_shape_row[i].ps_row_section);
745                     free(ps_piece_shape->ps_piece_shape_row);
746                     ps_piece_shape->ps_piece_shape_row = NULL;
747                     return VLC_ENOMEM;
748                 }
749                 for (uint8_t i=0; i < i_sect; i++) {
750                     ps_piece_shape->ps_piece_shape_row[i_row].ps_row_section[i].i_type = i % 2; /* 0 = fill ; 1 = offset */
751                     ps_piece_shape->ps_piece_shape_row[i_row].ps_row_section[i].i_width = pi_sects[i];
752                 }
753             }
754             else {
755                 ps_piece_shape->ps_piece_shape_row[i_row].i_section_nbr = i_sect;
756                 ps_piece_shape->ps_piece_shape_row[i_row].ps_row_section = malloc (  sizeof(row_section_t) * i_sect);
757                 if (!ps_piece_shape->ps_piece_shape_row[i_row].ps_row_section) {
758                     for (uint8_t i=0; i<i_row;i++)
759                         free(ps_piece_shape->ps_piece_shape_row[i].ps_row_section);
760                     free(ps_piece_shape->ps_piece_shape_row);
761                     ps_piece_shape->ps_piece_shape_row = NULL;
762                     return VLC_ENOMEM;
763                 }
764                 for (uint8_t i=0; i < i_sect; i++) {
765                     ps_piece_shape->ps_piece_shape_row[i_row].ps_row_section[i].i_type = (i + 1) % 2; /* 0 = fill ; 1 = offset */
766                     ps_piece_shape->ps_piece_shape_row[i_row].ps_row_section[i].i_width = pi_sects[i];
767                 }
768             }
769             break;
770           case puzzle_SHAPE_LEFT:
771             /* ...allocate memory and copy final values */
772             /* note for y > 0 we have to ignore the first offset as it is included in "Left" piece shape */
773             ps_piece_shape->ps_piece_shape_row[i_row].i_section_nbr = i_sect;
774             ps_piece_shape->ps_piece_shape_row[i_row].ps_row_section = malloc (  sizeof(row_section_t) * i_sect);
775             if (!ps_piece_shape->ps_piece_shape_row[i_row].ps_row_section) {
776                 for (uint8_t i=0; i<i_row;i++)
777                     free(ps_piece_shape->ps_piece_shape_row[i].ps_row_section);
778                 free(ps_piece_shape->ps_piece_shape_row);
779                 ps_piece_shape->ps_piece_shape_row = NULL;
780                 return VLC_ENOMEM;
781             }
782             for (uint8_t i=0; i < i_sect; i++) {
783                 ps_piece_shape->ps_piece_shape_row[i_row].ps_row_section[i].i_type = (i+1) % 2; /* 0 = fill ; 1 = offset */
784                 ps_piece_shape->ps_piece_shape_row[i_row].ps_row_section[i].i_width = pi_sects[i];
785             }
786         }
787     }
788     return VLC_SUCCESS;
789 }
790
791 /*****************************************************************************
792  * when generating shape data: detect all bezier curve intersections with
793  * current line
794  *****************************************************************************/
795 int puzzle_detect_curve( filter_t *p_filter, int32_t i_y, float f_x_ratio, float f_y_ratio, point_t *ps_pt, uint8_t i_pts_nbr, uint8_t i_border, uint8_t i_plane, int32_t *pi_sects)
796 {
797     int8_t i_main_t = 0;
798     float f_xd, f_yd;
799     float f_xo = ps_pt[0].f_x * f_x_ratio;
800     float f_yo = ps_pt[0].f_y * f_y_ratio;
801     int8_t i_sect = 0;
802
803     for (float f_t = 0; f_t <= i_pts_nbr - 1; f_t += 0.1 ) {
804         i_main_t = floor(f_t);
805         if ( i_main_t == i_pts_nbr - 1 )
806             i_main_t = i_pts_nbr - 2;
807         float f_sub_t = f_t - i_main_t;
808
809         f_xd = bezier_val(ps_pt,f_sub_t,i_main_t,x) * f_x_ratio;
810         f_yd = bezier_val(ps_pt,f_sub_t,i_main_t,y) * f_y_ratio;
811
812         if ((f_yo < (float)i_y+0.5 && f_yd >= (float)i_y+0.5) || (f_yo > (float)i_y+0.5 && f_yd <= (float)i_y+0.5)) {
813             pi_sects[i_sect] = floor(((float)i_y+0.5 - f_yo) * (f_xd - f_xo) / (f_yd - f_yo) + f_xo);
814             if (i_sect < MAX_SECT - 1)
815                 i_sect++;
816         }
817
818         f_xo = f_xd;
819         f_yo = f_yd;
820     }
821     f_xd = ps_pt[i_pts_nbr - 1].f_x * f_x_ratio;
822     f_yd = ps_pt[i_pts_nbr - 1].f_y * f_y_ratio;
823
824     /* ...fill from this junction to next junction */
825     if ( i_y >= 0 ) {
826         /* last diagonal intersection */
827         pi_sects[i_sect] = (i_border==puzzle_SHAPE_TOP)?puzzle_diagonal_limit( p_filter, i_y, false, i_plane )
828                                                        :puzzle_diagonal_limit( p_filter, i_y, true,  i_plane );
829         if (i_sect < MAX_SECT - 1)
830             i_sect++;
831     }
832
833     /* ...reorder the list of intersection */
834     int32_t i_s = 0;
835
836     while (i_s < (i_sect - 1)) {
837         if (pi_sects[i_s] > pi_sects[i_s+1]) {
838             uint32_t i_temp = pi_sects[i_s];
839             pi_sects[i_s] = pi_sects[i_s+1];
840             pi_sects[i_s+1] = i_temp;
841             i_s = 0;
842         }
843         else {
844             i_s++;
845         }
846     }
847
848     return i_sect;
849 }
850
851 /*****************************************************************************
852  * generate Right shape data from Left shape data
853  *****************************************************************************/
854 int puzzle_generate_sectLeft2Right( filter_t *p_filter, piece_shape_t *ps_piece_shape, piece_shape_t *ps_left_piece_shape, uint8_t i_plane)
855 {
856     if ((!ps_piece_shape) || (!ps_left_piece_shape))
857         return VLC_EGENERIC;
858
859     filter_sys_t *p_sys = p_filter->p_sys;
860
861     int32_t i_min_y = ps_left_piece_shape->i_first_row_offset;
862     int32_t i_nb_y = ps_left_piece_shape->i_row_nbr;
863
864     /* allocate memory */
865     ps_piece_shape->i_row_nbr = i_nb_y;
866     ps_piece_shape->i_first_row_offset = i_min_y;
867     ps_piece_shape->ps_piece_shape_row = malloc( sizeof( piece_shape_row_t ) * i_nb_y );
868     if (!ps_piece_shape->ps_piece_shape_row)
869         return VLC_ENOMEM;
870
871     for (int32_t i_y = i_min_y; i_y < i_nb_y + i_min_y; i_y++) {
872         int32_t i_row = i_y - i_min_y;
873
874         int32_t i_width = p_sys->ps_desk_planes[i_plane].i_pce_max_width;
875         int32_t i_left_width = puzzle_diagonal_limit( p_filter, i_y, true, i_plane  );
876         int32_t i_right_width = i_width - puzzle_diagonal_limit( p_filter, i_y, false, i_plane );
877         int16_t i_section_nbr = ps_left_piece_shape->ps_piece_shape_row[i_row].i_section_nbr;
878
879         ps_piece_shape->ps_piece_shape_row[i_row].i_section_nbr = i_section_nbr;
880         ps_piece_shape->ps_piece_shape_row[i_row].ps_row_section = malloc (  sizeof(row_section_t) * i_section_nbr);
881         if (!ps_piece_shape->ps_piece_shape_row[i_row].ps_row_section) {
882             for (uint8_t i=0; i<i_row;i++)
883                 free(ps_piece_shape->ps_piece_shape_row[i].ps_row_section);
884             free(ps_piece_shape->ps_piece_shape_row);
885             ps_piece_shape->ps_piece_shape_row = NULL;
886             return VLC_ENOMEM;
887         }
888
889         ps_piece_shape->ps_piece_shape_row[i_row].ps_row_section[0].i_type =
890                 ps_left_piece_shape->ps_piece_shape_row[i_row].ps_row_section[0].i_type;
891         ps_piece_shape->ps_piece_shape_row[i_row].ps_row_section[0].i_width =
892                 ps_left_piece_shape->ps_piece_shape_row[i_row].ps_row_section[0].i_width + i_right_width - i_left_width;
893
894         for (int8_t i_s=0; i_s<i_section_nbr;i_s++) {
895             ps_piece_shape->ps_piece_shape_row[i_row].ps_row_section[i_s].i_type =
896                     ps_left_piece_shape->ps_piece_shape_row[i_row].ps_row_section[i_section_nbr - 1 - i_s].i_type;
897             ps_piece_shape->ps_piece_shape_row[i_row].ps_row_section[i_s].i_width =
898                     ps_left_piece_shape->ps_piece_shape_row[i_row].ps_row_section[i_section_nbr - 1 - i_s].i_width
899                     + (i_s == 0 ? i_right_width - i_left_width : 0);
900         }
901     }
902     return VLC_SUCCESS;
903 }
904
905 /*****************************************************************************
906  * generates Bottom shape data from Top shape data
907  *****************************************************************************/
908 int puzzle_generate_sectTop2Btm( filter_t *p_filter, piece_shape_t *ps_piece_shape, piece_shape_t *ps_top_piece_shape, uint8_t i_plane)
909 {
910     if ((!ps_piece_shape) || (!ps_top_piece_shape))
911         return VLC_EGENERIC;
912
913     filter_sys_t *p_sys = p_filter->p_sys;
914
915     int32_t i_top_min_y = ps_top_piece_shape->i_first_row_offset;
916     int32_t i_top_nb_y = ps_top_piece_shape->i_row_nbr;
917     int32_t i_lines = p_sys->ps_desk_planes[i_plane].i_pce_max_lines;
918     int32_t i_max_y = p_sys->ps_desk_planes[i_plane].i_pce_max_lines - i_top_min_y;
919
920     int32_t i_min_y = i_lines / 2;
921     int32_t i_nb_y = i_max_y - i_min_y;
922
923     /* allocate memory */
924     ps_piece_shape->i_row_nbr = i_nb_y;
925     ps_piece_shape->i_first_row_offset = i_min_y;
926     ps_piece_shape->ps_piece_shape_row = malloc( sizeof( piece_shape_row_t ) * i_nb_y );
927     if (!ps_piece_shape->ps_piece_shape_row)
928         return VLC_ENOMEM;
929
930     for (int32_t i_y = i_min_y; i_y < i_nb_y + i_min_y; i_y++) {
931         int32_t i_top_y = 2 * i_min_y - i_y + (i_nb_y - i_top_nb_y);
932         int32_t i_row = i_y - i_min_y;
933         int32_t i_top_row = i_top_y - i_top_min_y;
934
935         if ( i_top_row < 0 || i_top_row >= i_top_nb_y ) { /* the line does not exist in top */
936             ps_piece_shape->ps_piece_shape_row[i_row].i_section_nbr = 1;
937             ps_piece_shape->ps_piece_shape_row[i_row].ps_row_section = malloc (  sizeof(row_section_t) * 1);
938             if (!ps_piece_shape->ps_piece_shape_row[i_row].ps_row_section) {
939                 for (uint8_t i=0; i<i_row;i++)
940                     free(ps_piece_shape->ps_piece_shape_row[i].ps_row_section);
941                 free(ps_piece_shape->ps_piece_shape_row);
942                 ps_piece_shape->ps_piece_shape_row = NULL;
943                 return VLC_ENOMEM;
944             }
945             ps_piece_shape->ps_piece_shape_row[i_row].ps_row_section[0].i_type = 0;
946             ps_piece_shape->ps_piece_shape_row[i_row].ps_row_section[0].i_width =
947                 puzzle_diagonal_limit( p_filter, i_y, false, i_plane ) - 1 - (puzzle_diagonal_limit( p_filter, i_y, true, i_plane ) - 1);
948         }
949         else { /* copy the line from TopShape */
950             int32_t i_top_width =
951                 puzzle_diagonal_limit( p_filter, i_top_y, false, i_plane ) - 1 - (puzzle_diagonal_limit( p_filter, i_top_y, true, i_plane ) - 1);
952             int32_t i_width =
953                 puzzle_diagonal_limit( p_filter, i_y, false, i_plane ) - 1 - (puzzle_diagonal_limit( p_filter, i_y, true, i_plane ) - 1);
954             int32_t i_left_adjust = ( i_width - i_top_width ) / 2;
955             int32_t i_right_adjust = ( i_width - i_top_width ) - i_left_adjust;
956
957             int8_t i_section_nbr = ps_top_piece_shape->ps_piece_shape_row[i_top_row].i_section_nbr;
958             ps_piece_shape->ps_piece_shape_row[i_row].i_section_nbr = i_section_nbr;
959             ps_piece_shape->ps_piece_shape_row[i_row].ps_row_section = malloc (  sizeof(row_section_t) * i_section_nbr);
960             if (!ps_piece_shape->ps_piece_shape_row[i_row].ps_row_section) {
961                 for (uint8_t i=0; i<i_row;i++)
962                     free(ps_piece_shape->ps_piece_shape_row[i].ps_row_section);
963                 free(ps_piece_shape->ps_piece_shape_row);
964                 ps_piece_shape->ps_piece_shape_row = NULL;
965                 return VLC_ENOMEM;
966             }
967
968             for (int8_t i_s=0; i_s<i_section_nbr; i_s++) {
969                 ps_piece_shape->ps_piece_shape_row[i_row].ps_row_section[i_s].i_type =
970                         ps_top_piece_shape->ps_piece_shape_row[i_top_row].ps_row_section[i_s].i_type;
971                 ps_piece_shape->ps_piece_shape_row[i_row].ps_row_section[i_s].i_width =
972                         ps_top_piece_shape->ps_piece_shape_row[i_top_row].ps_row_section[i_s].i_width
973                         + (i_s == 0 ? i_left_adjust : (i_s == i_section_nbr-1 ? i_right_adjust : 0));
974             }
975         }
976     }
977     return VLC_SUCCESS;
978 }