]> git.sesse.net Git - mlt/blob - src/modules/core/filter_mirror.c
Noise and mirrors
[mlt] / src / modules / core / filter_mirror.c
1 /*
2  * filter_mirror.c -- mirror filter
3  * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4  * Author: Charles Yates <charles.yates@pandora.be>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 #include "filter_mirror.h"
22
23 #include <framework/mlt_frame.h>
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 /** Do it :-).
30 */
31
32 static int filter_get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
33 {
34         // Pop the mirror filter from the stack
35         mlt_filter this = mlt_frame_pop_service( frame );
36
37         // Get the mirror type
38         mlt_properties properties = mlt_filter_properties( this );
39
40         // Get the properties
41         char *mirror = mlt_properties_get( properties, "mirror" );
42
43         // Determine if reverse is required
44         int reverse = mlt_properties_get_int( properties, "reverse" );
45
46         // Get the image
47         int error = mlt_frame_get_image( frame, image, format, width, height, 1 );
48
49         // If we have an image of the right colour space
50         if ( error == 0 && *format == mlt_image_yuv422 )
51         {
52                 // We'll KISS here
53                 int hh = *height / 2;
54
55                 if ( !strcmp( mirror, "horizontal" ) )
56                 {
57                         uint8_t *p = NULL;
58                         uint8_t *q = NULL;
59                         int i;
60                         for ( i = 0; i < *height; i ++ )
61                         {
62                                 p = ( uint8_t * )*image + i * *width * 2;
63                                 q = p + *width * 2;
64                                 if ( !reverse )
65                                 {
66                                         while ( p != q )
67                                         {
68                                                 *p ++ = *( q - 2 );
69                                                 *p ++ = *( q - 3 );
70                                                 *p ++ = *( q - 4 );
71                                                 *p ++ = *( q - 1 );
72                                                 q -= 4;
73                                         }
74                                 }
75                                 else
76                                 {
77                                         while ( p != q )
78                                         {
79                                                 *( q - 2 ) = *p ++;
80                                                 *( q - 3 ) = *p ++;
81                                                 *( q - 4 ) = *p ++;
82                                                 *( q - 1 ) = *p ++;
83                                                 q -= 4;
84                                         }
85                                 }
86                         }
87                 }
88                 else if ( !strcmp( mirror, "vertical" ) )
89                 {
90                         uint16_t *end = ( uint16_t *)*image + *width * *height;
91                         uint16_t *p = NULL;
92                         uint16_t *q = NULL;
93                         int i;
94                         int j;
95                         for ( i = 0; i < hh; i ++ )
96                         {
97                                 p = ( uint16_t * )*image + i * *width;
98                                 q = end - i * *width;
99                                 j = *width;
100                                 if ( !reverse )
101                                 {
102                                         while ( j -- )
103                                                 *p ++ = *q ++;
104                                 }
105                                 else
106                                 {
107                                         while ( j -- )
108                                                 *q ++ = *p ++;
109                                 }
110                         }
111                 }
112                 else if ( !strcmp( mirror, "diagonal" ) )
113                 {
114                         uint8_t *end = ( uint8_t *)*image + *width * *height * 2;
115                         uint8_t *p = NULL;
116                         uint8_t *q = NULL;
117                         int i;
118                         int j;
119                         for ( i = 0; i < *height; i ++ )
120                         {
121                                 p = ( uint8_t * )*image + i * *width * 2;
122                                 q = end - i * *width * 2;
123                                 j = ( ( *width * ( *height - i ) ) / *height ) / 2;
124                                 if ( !reverse )
125                                 {
126                                         while ( j -- )
127                                         {
128                                                 *p ++ = *( q - 2 );
129                                                 *p ++ = *( q - 3 );
130                                                 *p ++ = *( q - 4 );
131                                                 *p ++ = *( q - 1 );
132                                                 q -= 4;
133                                         }
134                                 }
135                                 else
136                                 {
137                                         while ( j -- )
138                                         {
139                                                 *( q - 2 ) = *p ++;
140                                                 *( q - 3 ) = *p ++;
141                                                 *( q - 4 ) = *p ++;
142                                                 *( q - 1 ) = *p ++;
143                                                 q -= 4;
144                                         }
145                                 }
146                         }
147                 }
148                 else if ( !strcmp( mirror, "flip" ) )
149                 {
150                         uint8_t t[ 4 ];
151                         uint8_t *p = NULL;
152                         uint8_t *q = NULL;
153                         int i;
154                         for ( i = 0; i < *height; i ++ )
155                         {
156                                 p = ( uint8_t * )*image + i * *width * 2;
157                                 q = p + *width * 2;
158                                 while ( p != q )
159                                 {
160                                         t[ 0 ] = p[ 0 ];
161                                         t[ 1 ] = p[ 1 ];
162                                         t[ 2 ] = p[ 2 ];
163                                         t[ 3 ] = p[ 3 ];
164                                         *p ++ = *( q - 2 );
165                                         *p ++ = *( q - 3 );
166                                         *p ++ = *( q - 4 );
167                                         *p ++ = *( q - 1 );
168                                         *( -- q ) = t[ 3 ];
169                                         *( -- q ) = t[ 0 ];
170                                         *( -- q ) = t[ 1 ];
171                                         *( -- q ) = t[ 2 ];
172                                 }
173                         }
174                 }
175                 else if ( !strcmp( mirror, "flop" ) )
176                 {
177                         uint16_t *end = ( uint16_t *)*image + *width * *height;
178                         uint16_t *p = NULL;
179                         uint16_t *q = NULL;
180                         uint16_t t;
181                         int i;
182                         int j;
183                         for ( i = 0; i < hh; i ++ )
184                         {
185                                 p = ( uint16_t * )*image + i * *width;
186                                 q = end - i * *width;
187                                 j = *width;
188                                 while ( j -- )
189                                 {
190                                         t = *p;
191                                         *p ++ = *q;
192                                         *q ++ = t;
193                                 }
194                         }
195                 }
196         }
197
198         // Return the error
199         return error;
200 }
201
202 /** Filter processing.
203 */
204
205 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
206 {
207         // Push the service on to the stack
208         mlt_frame_push_service( frame, this );
209
210         // Push the filter method on to the stack
211         mlt_frame_push_service( frame, filter_get_image );
212
213         return frame;
214 }
215
216 /** Constructor for the filter.
217 */
218
219 mlt_filter filter_mirror_init( void *arg )
220 {
221         // Construct a new filter
222         mlt_filter this = mlt_filter_new( );
223
224         // If we have a filter, initialise it
225         if ( this != NULL )
226         {
227                 // Get the properties
228                 mlt_properties properties = mlt_filter_properties( this );
229
230                 // Set the default mirror type
231                 mlt_properties_set_or_default( properties, "mirror", arg, "horizontal" );
232
233                 // Assign the process method
234                 this->process = filter_process;
235         }
236
237         // Return the filter
238         return this;
239 }
240