Debian Codegraph @

src/filter/mask0mate/mask0mate.c

raw symbols SCIP
1 /* mask0mate.c
2 * Copyright (C) 2007 Richard Spindler ([email protected])
3 * This file is a Frei0r plugin.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <math.h>
21 #include "frei0r.h"
22 #include <stdlib.h>
23 #include "frei0r/blur.h"
24
25 typedef struct mask0mate_instance {
26 double left, top, right, bottom;
27 double blur;
28 int invert;
29 int w, h;
30 uint32_t* mask;
31 uint32_t* mask_blurred;
32 f0r_instance_t* blur_instance;
33 } mask0mate_instance_t;
34
35 void update_mask( mask0mate_instance_t* i ){
36 int l, r, t, b;
37 l = (int)( i->left * i->w );
38 r = (int)( i->w - ( i->right * i->w ) );
39 t = (int)( i->top * i->h );
40 b = (int)( i->h - ( i->bottom * i->h ) );
41
42 if ( l < 0 ) { l = 0; }
43 if ( r < 0 ) { r = 0; }
44 if ( t < 0 ) { t = 0; }
45 if ( b < 0 ) { b = 0; }
46
47 if ( l > i->w ) { l = i->w; }
48 if ( r > i->w ) { r = i->w; }
49 if ( t > i->h ) { t = i->h; }
50 if ( b > i->h ) { b = i->h; }
51
52 if ( l > r ) { int c = l; l = r; r = c; }
53 if ( t > b ) { int c = t; t = b; b = c; }
54
55 int len = i->w * i->h;
56 int j;
57 uint32_t v;
58 if ( i->invert ) {
59 v = 0x00ffffff;
60 } else {
61 v = 0xffffffff;
62 }
63 for ( j = 0; j < len; j++ ) {
64 i->mask[j] = v;
65 }
66 if ( !i->invert ) {
67 v = 0x00ffffff;
68 } else {
69 v = 0xffffffff;
70 }
71 int y, x;
72 for ( y = t; y < b; y++ ) {
73 for ( x = l; x < r; x++ ) {
74 i->mask[y*i->w + x] = v;
75 }
76 }
77
78 blur_set_param_value(i->blur_instance, &i->blur, 0 );
79 blur_update(i->blur_instance, 0.0, i->mask, i->mask_blurred);
80 }
81 int f0r_init()
82 {
83 return 1;
84 }
85 void f0r_deinit()
86 { /* empty */ }
87
88 void f0r_get_plugin_info( f0r_plugin_info_t* info )
89 {
90 info->name = "Mask0Mate";
91 info->author = "Richard Spindler";
92 info->plugin_type = F0R_PLUGIN_TYPE_FILTER;
93 info->color_model = F0R_COLOR_MODEL_RGBA8888;
94 info->frei0r_version = FREI0R_MAJOR_VERSION;
95 info->major_version = 0;
96 info->minor_version = 1;
97 info->num_params = 6;
98 info->explanation = "Creates an square alpha-channel mask";
99
100 }
101 void f0r_get_param_info( f0r_param_info_t* info, int param_index )
102 {
103 switch ( param_index ) {
104 case 0:
105 info->name = "Left";
106 info->type = F0R_PARAM_DOUBLE;
107 info->explanation = "";
108 break;
109 case 1:
110 info->name = "Right";
111 info->type = F0R_PARAM_DOUBLE;
112 info->explanation = "";
113 break;
114 case 2:
115 info->name = "Top";
116 info->type = F0R_PARAM_DOUBLE;
117 info->explanation = "";
118 break;
119 case 3:
120 info->name = "Bottom";
121 info->type = F0R_PARAM_DOUBLE;
122 info->explanation = "";
123 break;
124 case 4:
125 info->name = "Invert";
126 info->type = F0R_PARAM_BOOL;
127 info->explanation = "Invert the mask, creates a hole in the frame.";
128 break;
129 case 5:
130 info->name = "Blur";
131 info->type = F0R_PARAM_DOUBLE;
132 info->explanation = "Blur the outline of the mask";
133 break;
134 }
135 }
136
137 f0r_instance_t f0r_construct(unsigned int width, unsigned int height)
138 {
139 mask0mate_instance_t* inst = (mask0mate_instance_t*)calloc(1, sizeof(*inst));
140 inst->w = width;
141 inst->h = height;
142 inst->left = 0.2;
143 inst->right = 0.2;
144 inst->top = 0.2;
145 inst->bottom = 0.2;
146 inst->mask = (uint32_t*)malloc( width * height * sizeof(uint32_t) );
147 inst->mask_blurred = (uint32_t*)malloc( width * height * sizeof(uint32_t) );
148 inst->blur_instance = (f0r_instance_t*)blur_construct( width, height );
149 update_mask( inst );
150 return (f0r_instance_t)inst;
151 }
152 void f0r_destruct(f0r_instance_t instance)
153 {
154 mask0mate_instance_t* inst = (mask0mate_instance_t*)instance;
155 blur_destruct(inst->blur_instance);
156 free(inst->mask);
157 free(inst->mask_blurred);
158 free(instance);
159 }
160 void f0r_set_param_value(f0r_instance_t instance,
161 f0r_param_t param, int param_index)
162 {
163 mask0mate_instance_t* inst = (mask0mate_instance_t*)instance;
164 switch ( param_index ) {
165 case 0:
166 inst->left = *((double*)param);
167 break;
168 case 1:
169 inst->right = *((double*)param);
170 break;
171 case 2:
172 inst->top = *((double*)param);
173 break;
174 case 3:
175 inst->bottom = *((double*)param);
176 break;
177 case 4:
178 if ( *((double*)param) < 0.5 ) {
179 inst->invert = 0;
180 } else {
181 inst->invert = 1;
182 }
183 break;
184 case 5:
185 inst->blur = *((double*)param);
186 break;
187 }
188 update_mask( inst );
189 }
190 void f0r_get_param_value(f0r_instance_t instance,
191 f0r_param_t param, int param_index)
192 {
193 mask0mate_instance_t* inst = (mask0mate_instance_t*)instance;
194 switch ( param_index ) {
195 case 0:
196 *((double*)param) = inst->left;
197 break;
198 case 1:
199 *((double*)param) = inst->right;
200 break;
201 case 2:
202 *((double*)param) = inst->top;
203 break;
204 case 3:
205 *((double*)param) = inst->bottom;
206 break;
207 case 4:
208 *((double*)param) = (double)inst->invert;
209 break;
210 case 5:
211 *((double*)param) = inst->blur;
212 break;
213 }
214 }
215 void f0r_update(f0r_instance_t instance, double time,
216 const uint32_t* inframe, uint32_t* outframe)
217 {
218 mask0mate_instance_t* inst = (mask0mate_instance_t*)instance;
219
220 uint32_t* dst = outframe;
221 const uint32_t* src = inframe;
222 const uint32_t* alpha = inst->mask_blurred;
223
224 int len = inst->w * inst->h;
225
226 int i;
227 for ( i = 0; i < len; i++ ) {
228 *dst = *src & (*alpha | 0x00ffffff);
229 dst++;
230 src++;
231 alpha++;
232 }
233
234 }
235