Minor optimisation
[melted] / 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 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_charcoal.h"
22
23 #include <framework/mlt_frame.h>
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <math.h>
28
29 static inline int get_Y( uint8_t *pixels, int width, int height, int x, int y )
30 {
31 if ( x < 0 || x >= width || y < 0 || y >= height )
32 {
33 return 235;
34 }
35 else
36 {
37 uint8_t *pixel = pixels + y * ( width << 1 ) + ( x << 1 );
38 return *pixel;
39 }
40 }
41
42 static inline int sqrti( int n )
43 {
44 int p = 0;
45 int q = 1;
46 int r = n;
47 int h = 0;
48
49 while( q <= n )
50 q = q << 2;
51
52 while( q != 1 )
53 {
54 q = q >> 2;
55 h = p + q;
56 p = p >> 1;
57 if ( r >= h )
58 {
59 p = p + q;
60 r = r - h;
61 }
62 }
63
64 return p;
65 }
66
67 /** Do it :-).
68 */
69
70 static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
71 {
72 // Get the filter
73 mlt_filter filter = mlt_frame_pop_service( this );
74
75 // Get the image
76 int error = mlt_frame_get_image( this, image, format, width, height, 1 );
77
78 // Only process if we have no error and a valid colour space
79 if ( error == 0 && *format == mlt_image_yuv422 )
80 {
81 // Get the charcoal scatter value
82 int x_scatter = mlt_properties_get_double( mlt_filter_properties( filter ), "x_scatter" );
83 int y_scatter = mlt_properties_get_double( mlt_filter_properties( filter ), "y_scatter" );
84 float scale = mlt_properties_get_double( mlt_filter_properties( filter ), "scale" );
85 float mix = mlt_properties_get_double( mlt_filter_properties( filter ), "mix" );
86 int invert = mlt_properties_get_int( mlt_filter_properties( filter ), "invert" );
87
88 // We'll process pixel by pixel
89 int x = 0;
90 int y = 0;
91
92 // We need to create a new frame as this effect modifies the input
93 uint8_t *temp = mlt_pool_alloc( *width * *height * 2 );
94 uint8_t *p = temp;
95 uint8_t *q = *image;
96
97 // Calculations are carried out on a 3x3 matrix
98 int matrix[ 3 ][ 3 ];
99
100 // Used to carry out the matrix calculations
101 int sum1;
102 int sum2;
103 float sum;
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 *p ++ = 128 + mix * ( *q ++ - 128 );
131 }
132 }
133
134 // Return the created image
135 *image = temp;
136
137 // Store new and destroy old
138 mlt_properties_set_data( mlt_frame_properties( this ), "image", *image, *width * *height * 2, mlt_pool_release, NULL );
139 }
140
141 return error;
142 }
143
144 /** Filter processing.
145 */
146
147 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
148 {
149 // Push the frame filter
150 mlt_frame_push_service( frame, this );
151 mlt_frame_push_get_image( frame, filter_get_image );
152
153 return frame;
154 }
155
156 /** Constructor for the filter.
157 */
158
159 mlt_filter filter_charcoal_init( char *arg )
160 {
161 mlt_filter this = mlt_filter_new( );
162 if ( this != NULL )
163 {
164 this->process = filter_process;
165 mlt_properties_set( mlt_filter_properties( this ), "x_scatter", "1" );
166 mlt_properties_set( mlt_filter_properties( this ), "y_scatter", "1" );
167 mlt_properties_set( mlt_filter_properties( this ), "scale", "1.5" );
168 mlt_properties_set( mlt_filter_properties( this ), "mix", "0" );
169 }
170 return this;
171 }
172