]> git.sesse.net Git - mlt/blob - src/modules/plus/filter_charcoal.c
e99981a56add6ddeb096bb95e24eb8574224f39b
[mlt] / src / modules / plus / filter_charcoal.c
1 /*
2  * filter_charcoal.c -- charcoal filter
3  * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4  * Author: Charles Yates <charles.yates@pandora.be>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library 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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <framework/mlt_filter.h>
22 #include <framework/mlt_frame.h>
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <math.h>
27
28 static inline int get_Y( uint8_t *pixels, int width, int height, int x, int y )
29 {
30         if ( x < 0 || x >= width || y < 0 || y >= height )
31         {
32                 return 235;
33         }
34         else
35         {
36                 uint8_t *pixel = pixels + y * ( width << 1 ) + ( x << 1 );
37                 return *pixel;
38         }
39 }
40
41 static inline int sqrti( int n )
42 {
43         int p = 0;
44         int q = 1;
45         int r = n;
46         int h = 0;
47
48         while( q <= n )
49                 q = q << 2;
50
51         while( q != 1 )
52         {
53                 q = q >> 2;
54                 h = p + q;
55                 p = p >> 1;
56                 if ( r >= h )
57                 {
58                         p = p + q;
59                         r = r - h;
60                 }
61         }
62
63         return p;
64 }
65
66 /** Do it :-).
67 */
68
69 static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
70 {
71         // Get the filter
72         mlt_filter filter = mlt_frame_pop_service( this );
73
74         // Get the image
75         int error = mlt_frame_get_image( this, image, format, width, height, 1 );
76
77         // Only process if we have no error and a valid colour space
78         if ( error == 0 && *format == mlt_image_yuv422 )
79         {
80                 // Get the charcoal scatter value
81                 int x_scatter = mlt_properties_get_double( MLT_FILTER_PROPERTIES( filter ), "x_scatter" );
82                 int y_scatter = mlt_properties_get_double( MLT_FILTER_PROPERTIES( filter ), "y_scatter" );
83                 float scale = mlt_properties_get_double( MLT_FILTER_PROPERTIES( filter ), "scale" );
84                 float mix = mlt_properties_get_double( MLT_FILTER_PROPERTIES( filter ), "mix" );
85                 int invert = mlt_properties_get_int( MLT_FILTER_PROPERTIES( filter ), "invert" );
86
87                 // We'll process pixel by pixel
88                 int x = 0;
89                 int y = 0;
90
91                 // We need to create a new frame as this effect modifies the input
92                 uint8_t *temp = mlt_pool_alloc( *width * *height * 2 );
93                 uint8_t *p = temp;
94                 uint8_t *q = *image;
95
96                 // Calculations are carried out on a 3x3 matrix
97                 int matrix[ 3 ][ 3 ];
98
99                 // Used to carry out the matrix calculations
100                 int sum1;
101                 int sum2;
102                 float sum;
103                 int val;
104
105                 // Loop for each row
106                 for ( y = 0; y < *height; y ++ )
107                 {
108                         // Loop for each pixel
109                         for ( x = 0; x < *width; x ++ )
110                         {
111                                 // Populate the matrix
112                                 matrix[ 0 ][ 0 ] = get_Y( *image, *width, *height, x - x_scatter, y - y_scatter );
113                                 matrix[ 0 ][ 1 ] = get_Y( *image, *width, *height, x            , y - y_scatter );
114                                 matrix[ 0 ][ 2 ] = get_Y( *image, *width, *height, x + x_scatter, y - y_scatter );
115                                 matrix[ 1 ][ 0 ] = get_Y( *image, *width, *height, x - x_scatter, y             );
116                                 matrix[ 1 ][ 2 ] = get_Y( *image, *width, *height, x + x_scatter, y             );
117                                 matrix[ 2 ][ 0 ] = get_Y( *image, *width, *height, x - x_scatter, y + y_scatter );
118                                 matrix[ 2 ][ 1 ] = get_Y( *image, *width, *height, x            , y + y_scatter );
119                                 matrix[ 2 ][ 2 ] = get_Y( *image, *width, *height, x + x_scatter, y + y_scatter );
120
121                                 // Do calculations
122                                 sum1 = (matrix[2][0] - matrix[0][0]) + ( (matrix[2][1] - matrix[0][1]) << 1 ) + (matrix[2][2] - matrix[2][0]);
123                                 sum2 = (matrix[0][2] - matrix[0][0]) + ( (matrix[1][2] - matrix[1][0]) << 1 ) + (matrix[2][2] - matrix[2][0]);
124                                 sum = scale * sqrti( sum1 * sum1 + sum2 * sum2 );
125
126                                 // Assign value
127                                 *p ++ = !invert ? ( sum >= 16 && sum <= 235 ? 251 - sum : sum < 16 ? 235 : 16 ) :
128                                                                   ( sum >= 16 && sum <= 235 ? sum : sum < 16 ? 16 : 235 );
129                                 q ++;
130                                 val = 128 + mix * ( *q ++ - 128 );
131                                 val = val < 16 ? 16 : val > 240 ? 240 : val;
132                                 *p ++ = val;
133                         }
134                 }
135
136                 // Return the created image
137                 *image = temp;
138
139                 // Store new and destroy old
140                 mlt_properties_set_data( MLT_FRAME_PROPERTIES( this ), "image", *image, *width * *height * 2, mlt_pool_release, NULL );
141         }
142
143         return error;
144 }
145
146 /** Filter processing.
147 */
148
149 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
150 {
151         // Push the frame filter
152         mlt_frame_push_service( frame, this );
153         mlt_frame_push_get_image( frame, filter_get_image );
154
155         return frame;
156 }
157
158 /** Constructor for the filter.
159 */
160
161 mlt_filter filter_charcoal_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
162 {
163         mlt_filter this = mlt_filter_new( );
164         if ( this != NULL )
165         {
166                 this->process = filter_process;
167                 mlt_properties_set( MLT_FILTER_PROPERTIES( this ), "x_scatter", "1" );
168                 mlt_properties_set( MLT_FILTER_PROPERTIES( this ), "y_scatter", "1" );
169                 mlt_properties_set( MLT_FILTER_PROPERTIES( this ), "scale", "1.5" );
170                 mlt_properties_set( MLT_FILTER_PROPERTIES( this ), "mix", "0" );
171         }
172         return this;
173 }
174