Debian Codegraph @

src/mixer2/cairoaffineblend/cairoaffineblend.c

raw symbols SCIP
1 /*
2 * cairoaffineblend.c
3 * Copyright 2012 Janne Liljeblad
4 *
5 * This file is a Frei0r plugin.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include <stdlib.h>
23 #include <assert.h>
24 #include <cairo.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <math.h>
28
29 #include "frei0r.h"
30 #include "frei0r/cairo.h"
31
32 double PI=3.14159265358979;
33
34 typedef struct cairo_affineblend_instance
35 {
36 unsigned int width;
37 unsigned int height;
38 double x;
39 double y;
40 double x_scale;
41 double y_scale;
42 double rotation;
43 double mix;
44 char *blend_mode;
45 double anchor_x;
46 double anchor_y;
47 } cairo_affineblend_instance_t;
48
49 int f0r_init()
50 {
51 return 1;
52 }
53
54 void f0r_deinit()
55 {
56
57 }
58
59 void f0r_get_plugin_info(f0r_plugin_info_t* info)
60 {
61 info->name = "cairoaffineblend";
62 info->author = "Janne Liljeblad";
63 info->plugin_type = F0R_PLUGIN_TYPE_MIXER2;
64 info->color_model = F0R_COLOR_MODEL_RGBA8888;
65 info->frei0r_version = FREI0R_MAJOR_VERSION;
66 info->major_version = 0;
67 info->minor_version = 10;
68 info->num_params = 9;
69 info->explanation = "Composites second input on first input applying user-defined transformation, opacity and blend mode";
70 }
71
72 void f0r_get_param_info(f0r_param_info_t* info, int param_index)
73 {
74 switch(param_index) {
75 case 0:
76 info->name = "x";
77 info->type = F0R_PARAM_DOUBLE;
78 info->explanation = "X position of second input, value interpreted as range -2*width - 3*width";
79 break;
80 case 1:
81 info->name = "y";
82 info->type = F0R_PARAM_DOUBLE;
83 info->explanation = "Y position of second input, value interpreted as range -2*height - 3*height";
84 break;
85 case 2:
86 info->name = "x scale";
87 info->type = F0R_PARAM_DOUBLE;
88 info->explanation = "X scale of second input, value interpreted as range 0 - 5";
89 break;
90 case 3:
91 info->name = "y scale";
92 info->type = F0R_PARAM_DOUBLE;
93 info->explanation = "Y scale of second input, value interpreted as range 0 - 5";
94 break;
95 case 4:
96 info->name = "rotation";
97 info->type = F0R_PARAM_DOUBLE;
98 info->explanation = "Rotation of second input, value interpreted as range 0 - 360";
99 break;
100 case 5:
101 info->name = "opacity";
102 info->type = F0R_PARAM_DOUBLE;
103 info->explanation = "Opacity of second input";
104 break;
105 case 6:
106 info->name = "blend mode";
107 info->type = F0R_PARAM_STRING;
108 info->explanation = "Blend mode used to compose image. Accepted values: 'normal', 'add', 'saturate', 'multiply', 'screen', 'overlay', 'darken', 'lighten', 'colordodge', 'colorburn', 'hardlight', 'softlight', 'difference', 'exclusion', 'hslhue', 'hslsaturation', 'hslcolor', 'hslluminosity'";
109 break;
110 case 7:
111 info->name = "anchor x";
112 info->type = F0R_PARAM_DOUBLE;
113 info->explanation = "X position of rotation center within the second input";
114 break;
115 case 8:
116 info->name = "anchor y";
117 info->type = F0R_PARAM_DOUBLE;
118 info->explanation ="Y position of rotation center within the second input";
119 break;
120 }
121 }
122
123 f0r_instance_t f0r_construct(unsigned int width, unsigned int height)
124 {
125 cairo_affineblend_instance_t* inst = (cairo_affineblend_instance_t*)calloc(1, sizeof(*inst));
126 inst->width = width;
127 inst->height = height;
128
129 inst->x = 0.4;
130 inst->y = 0.4;
131 inst->x_scale = 0.2;
132 inst->y_scale = 0.2;
133 inst->rotation = 0.0;
134 inst->mix = 1.0;
135
136 const char* blend_val = NORMAL;
137 {
138 size_t blen = strlen(blend_val) + 1;
139 inst->blend_mode = (char*) malloc(blen);
140 memcpy(inst->blend_mode, blend_val, blen);
141 }
142
143 return (f0r_instance_t)inst;
144 }
145
146 void f0r_destruct(f0r_instance_t instance)
147 {
148 cairo_affineblend_instance_t* inst = (cairo_affineblend_instance_t*)instance;
149 free(inst->blend_mode);
150 free(instance);
151 }
152
153 void f0r_set_param_value(f0r_instance_t instance, f0r_param_t param, int param_index)
154 {
155 assert(instance);
156 cairo_affineblend_instance_t* inst = (cairo_affineblend_instance_t*) instance;
157 char* sval;
158 switch(param_index) {
159 case 0:
160 inst->x = *((double*)param);
161 break;
162 case 1:
163 inst->y = *((double*)param);
164 break;
165 case 2:
166 inst->x_scale = *((double*)param);
167 break;
168 case 3:
169 inst->y_scale = *((double*)param);
170 break;
171 case 4:
172 inst->rotation = *((double*)param);
173 break;
174 case 5:
175 inst->mix = *((double*)param);
176 break;
177 case 6:
178 sval = (*(char**)param);
179 {
180 size_t slen = strlen(sval) + 1;
181 char *new_mode = (char*)realloc(inst->blend_mode, slen);
182 if (new_mode) {
183 inst->blend_mode = new_mode;
184 memcpy(inst->blend_mode, sval, slen);
185 }
186 }
187 break;
188 case 7:
189 inst->anchor_x = *((double*)param);
190 break;
191 case 8:
192 inst->anchor_y = *((double*)param);
193 break;
194 }
195 }
196
197 void f0r_get_param_value(f0r_instance_t instance, f0r_param_t param, int param_index)
198 {
199 assert(instance);
200 cairo_affineblend_instance_t* inst = (cairo_affineblend_instance_t*)instance;
201
202 switch(param_index) {
203 case 0:
204 *((double*)param) = inst->x;
205 break;
206 case 1:
207 *((double*)param) = inst->y;
208 break;
209 case 2:
210 *((double*)param) = inst->x_scale;
211 break;
212 case 3:
213 *((double*)param) = inst->y_scale;
214 break;
215 case 4:
216 *((double*)param) = inst->rotation;
217 break;
218 case 5:
219 *((double*)param) = inst->mix;
220 break;
221 case 6:
222 *((f0r_param_string *)param) = inst->blend_mode;
223 break;
224 case 7:
225 *((double*)param) = inst->anchor_x;
226 break;
227 case 8:
228 *((double*)param) = inst->anchor_y;
229 break;
230 }
231 }
232
233 void draw_composite(cairo_affineblend_instance_t* inst, unsigned char* out, unsigned char* dst, unsigned char* src, double time)
234 {
235 int w = inst->width;
236 int h = inst->height;
237 int stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32, w);
238
239 cairo_surface_t* out_image = cairo_image_surface_create_for_data (out,
240 CAIRO_FORMAT_ARGB32,
241 w,
242 h,
243 stride);
244 cairo_t* cr = cairo_create (out_image);
245
246 cairo_surface_t* dst_image = cairo_image_surface_create_for_data (dst,
247 CAIRO_FORMAT_ARGB32,
248 w,
249 h,
250 stride);
251 cairo_surface_t* src_image = cairo_image_surface_create_for_data ((unsigned char*)src,
252 CAIRO_FORMAT_ARGB32,
253 w,
254 h,
255 stride);
256
257 // Draw bg on surface
258 cairo_set_source_surface (cr, dst_image, 0, 0);
259 cairo_paint (cr);
260
261 double x_scale = frei0r_cairo_get_scale (inst->x_scale);
262 double y_scale = frei0r_cairo_get_scale (inst->y_scale);
263
264 //--- Get scaled and rotated anchor offsets.
265 double anchorX = -(x_scale * inst->anchor_x * inst->width);
266 double anchorY = -(y_scale * inst->anchor_y * inst->height);
267
268 double angleRad = inst->rotation * 360.0 * PI/180.0;
269 double sinVal = sin (angleRad);
270 double cosVal = cos (angleRad);
271
272 double anchor_rot_x = anchorX * cosVal - anchorY * sinVal;
273 double anchor_rot_y = anchorX * sinVal + anchorY * cosVal;
274
275 // Get interpreted x and y translation
276 double x_trans = frei0r_cairo_get_pixel_position (inst->x, inst->width);
277 double y_trans = frei0r_cairo_get_pixel_position (inst->y, inst->height);
278
279 //--- Get total translation to image tot left with scaling and rotation.
280 double x_trans_tot = x_trans + anchor_rot_x;
281 double y_trans_tot = y_trans + anchor_rot_y;
282
283 cairo_translate (cr, x_trans_tot, y_trans_tot);
284 cairo_rotate (cr, inst->rotation * 360.0 * PI/180.0);
285 cairo_scale (cr, x_scale, y_scale);
286 frei0r_cairo_set_operator(cr, inst->blend_mode);
287
288 // Set source and draw with current mix
289 cairo_set_source_surface (cr, src_image, 0, 0);
290 cairo_paint_with_alpha (cr, inst->mix);
291
292 cairo_surface_destroy (out_image);
293 cairo_surface_destroy (src_image);
294 cairo_surface_destroy (dst_image);
295 cairo_destroy (cr);
296 }
297
298 void f0r_update(f0r_instance_t instance, double time,
299 const uint32_t* inframe, uint32_t* outframe)
300 {
301 // not impl. for mixers
302 }
303
304 void f0r_update2(f0r_instance_t instance, double time, const uint32_t* inframe1,
305 const uint32_t* inframe2, const uint32_t* inframe3, uint32_t* outframe)
306 {
307 assert(instance);
308 cairo_affineblend_instance_t* inst = (cairo_affineblend_instance_t*) instance;
309
310 unsigned char* dst = (unsigned char*)inframe1;
311 unsigned char* src = (unsigned char*)inframe2;
312 unsigned char* out = (unsigned char*)outframe;
313 int pixels = inst->width * inst->height;
314
315 frei0r_cairo_premultiply_rgba (dst, pixels, -1);
316 frei0r_cairo_premultiply_rgba (src, pixels, -1);
317 draw_composite (inst, out, dst, src, time);
318 frei0r_cairo_unpremultiply_rgba (out, pixels);
319 }