Big modification - switch to macros for parent class access
[melted] / 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 + 1 ) * *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, "xdiagonal" ) )
149 {
150 uint8_t *end = ( uint8_t *)*image + *width * *height * 2;
151 uint8_t *p = NULL;
152 uint8_t *q = NULL;
153 int i;
154 int j;
155 for ( i = 0; i < *height; i ++ )
156 {
157 p = ( uint8_t * )*image + ( i + 1 ) * *width * 2;
158 q = end - ( i + 1 ) * *width * 2;
159 j = ( ( *width * ( *height - i ) ) / *height ) / 2;
160 if ( !reverse )
161 {
162 while ( j -- )
163 {
164 *q ++ = *( p - 2 );
165 *q ++ = *( p - 3 );
166 *q ++ = *( p - 4 );
167 *q ++ = *( p - 1 );
168 p -= 4;
169 }
170 }
171 else
172 {
173 while ( j -- )
174 {
175 *( p - 2 ) = *q ++;
176 *( p - 3 ) = *q ++;
177 *( p - 4 ) = *q ++;
178 *( p - 1 ) = *q ++;
179 p -= 4;
180 }
181 }
182 }
183 }
184 else if ( !strcmp( mirror, "flip" ) )
185 {
186 uint8_t t[ 4 ];
187 uint8_t *p = NULL;
188 uint8_t *q = NULL;
189 int i;
190 for ( i = 0; i < *height; i ++ )
191 {
192 p = ( uint8_t * )*image + i * *width * 2;
193 q = p + *width * 2;
194 while ( p < q )
195 {
196 t[ 0 ] = p[ 0 ];
197 t[ 1 ] = p[ 1 ];
198 t[ 2 ] = p[ 2 ];
199 t[ 3 ] = p[ 3 ];
200 *p ++ = *( q - 2 );
201 *p ++ = *( q - 3 );
202 *p ++ = *( q - 4 );
203 *p ++ = *( q - 1 );
204 *( -- q ) = t[ 3 ];
205 *( -- q ) = t[ 0 ];
206 *( -- q ) = t[ 1 ];
207 *( -- q ) = t[ 2 ];
208 }
209 }
210 }
211 else if ( !strcmp( mirror, "flop" ) )
212 {
213 uint16_t *end = ( uint16_t *)*image + *width * *height;
214 uint16_t *p = NULL;
215 uint16_t *q = NULL;
216 uint16_t t;
217 int i;
218 int j;
219 for ( i = 0; i < hh; i ++ )
220 {
221 p = ( uint16_t * )*image + i * *width;
222 q = end - ( i + 1 ) * *width;
223 j = *width;
224 while ( j -- )
225 {
226 t = *p;
227 *p ++ = *q;
228 *q ++ = t;
229 }
230 }
231 }
232 }
233
234 // Return the error
235 return error;
236 }
237
238 /** Filter processing.
239 */
240
241 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
242 {
243 // Push the service on to the stack
244 mlt_frame_push_service( frame, this );
245
246 // Push the filter method on to the stack
247 mlt_frame_push_service( frame, filter_get_image );
248
249 return frame;
250 }
251
252 /** Constructor for the filter.
253 */
254
255 mlt_filter filter_mirror_init( void *arg )
256 {
257 // Construct a new filter
258 mlt_filter this = mlt_filter_new( );
259
260 // If we have a filter, initialise it
261 if ( this != NULL )
262 {
263 // Get the properties
264 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
265
266 // Set the default mirror type
267 mlt_properties_set_or_default( properties, "mirror", arg, "horizontal" );
268
269 // Assign the process method
270 this->process = filter_process;
271 }
272
273 // Return the filter
274 return this;
275 }
276