Debian Codegraph @

src/mixer2/cairoblend/cairoblend.c

raw symbols SCIP
1 /*
2 * cairoblend.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
28 #include "frei0r.h"
29 #include "frei0r/cairo.h"
30
31 typedef struct cairo_blend_instance
32 {
33 unsigned int width;
34 unsigned int height;
35 double opacity;
36 char *blend_mode;
37 } cairo_blend_instance_t;
38
39 int f0r_init()
40 {
41 return 1;
42 }
43
44 void f0r_deinit()
45 {
46
47 }
48
49 void f0r_get_plugin_info(f0r_plugin_info_t* info)
50 {
51 info->name = "cairoblend";
52 info->author = "Janne Liljeblad";
53 info->plugin_type = F0R_PLUGIN_TYPE_MIXER2;
54 info->color_model = F0R_COLOR_MODEL_RGBA8888;
55 info->frei0r_version = FREI0R_MAJOR_VERSION;
56 info->major_version = 0;
57 info->minor_version = 10;
58 info->num_params = 2;
59 info->explanation = "Composites second input on the first input with user-defined blend mode and opacity.";
60 }
61
62 void f0r_get_param_info(f0r_param_info_t* info, int param_index)
63 {
64 switch(param_index) {
65 case 0:
66 info->name = "opacity";
67 info->type = F0R_PARAM_DOUBLE;
68 info->explanation = "Opacity of composited image";
69 break;
70 case 1:
71 info->name = "blend mode";
72 info->type = F0R_PARAM_STRING;
73 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'";
74 break;
75 }
76 }
77
78 f0r_instance_t f0r_construct(unsigned int width, unsigned int height)
79 {
80 // Validate inputs
81 if (width == 0 || height == 0) {
82 return NULL;
83 }
84
85 cairo_blend_instance_t* inst = (cairo_blend_instance_t*)calloc(1, sizeof(*inst));
86 if (!inst) {
87 return NULL;
88 }
89
90 inst->width = width;
91 inst->height = height;
92
93 inst->opacity = 1.0;
94
95 const char* blend_val = NORMAL;
96 inst->blend_mode = (char*) malloc (strlen(blend_val) + 1);
97 if (!inst->blend_mode) {
98 free(inst);
99 return NULL;
100 }
101 strcpy (inst->blend_mode, blend_val);
102
103 return (f0r_instance_t)inst;
104 }
105
106 void f0r_destruct(f0r_instance_t instance)
107 {
108 if (!instance) {
109 return;
110 }
111
112 cairo_blend_instance_t* inst = (cairo_blend_instance_t*)instance;
113 if (inst->blend_mode) {
114 free(inst->blend_mode);
115 }
116 free(instance);
117 }
118
119 void f0r_set_param_value(f0r_instance_t instance, f0r_param_t param, int param_index)
120 {
121 assert(instance);
122 cairo_blend_instance_t* inst = (cairo_blend_instance_t*) instance;
123 char* sval;
124 switch(param_index) {
125 case 0:
126 // Validate double parameter
127 if (param) {
128 inst->opacity = *((double*)param);
129 }
130 break;
131 case 1:
132 // Validate string parameter
133 if (param) {
134 sval = (*(char**)param);
135 if (sval) {
136 inst->blend_mode = (char*)realloc (inst->blend_mode, strlen(sval) + 1);
137 if (inst->blend_mode) {
138 strcpy (inst->blend_mode, sval);
139 }
140 }
141 }
142 break;
143 }
144 }
145
146 void f0r_get_param_value(f0r_instance_t instance, f0r_param_t param, int param_index)
147 {
148 assert(instance);
149 cairo_blend_instance_t* inst = (cairo_blend_instance_t*)instance;
150
151 switch(param_index) {
152 case 0:
153 if (param) {
154 *((double*)param) = inst->opacity;
155 }
156 break;
157 case 1:
158 if (param && inst->blend_mode) {
159 *((f0r_param_string *)param) = inst->blend_mode;
160 } else if (param) {
161 *((f0r_param_string *)param) = "";
162 }
163 break;
164 }
165 }
166
167 void draw_composite(cairo_blend_instance_t* inst, unsigned char* out, unsigned char* src, double time)
168 {
169 int w = inst->width;
170 int h = inst->height;
171 int stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32, w);
172
173 // Validate inputs
174 if (!inst || !out || !src || w <= 0 || h <= 0) {
175 return;
176 }
177
178 cairo_surface_t* out_image = cairo_image_surface_create_for_data (out,
179 CAIRO_FORMAT_ARGB32,
180 w,
181 h,
182 stride);
183 // Check if surface creation succeeded
184 if (!out_image) {
185 return;
186 }
187
188 cairo_t* cr = cairo_create (out_image);
189 // Check if context creation succeeded
190 if (!cr) {
191 cairo_surface_destroy (out_image);
192 return;
193 }
194
195 cairo_surface_t* src_image = cairo_image_surface_create_for_data ((unsigned char*)src,
196 CAIRO_FORMAT_ARGB32,
197 w,
198 h,
199 stride);
200 // Check if surface creation succeeded
201 if (!src_image) {
202 cairo_destroy (cr);
203 cairo_surface_destroy (out_image);
204 return;
205 }
206
207 // Validate blend mode string
208 if (inst->blend_mode) {
209 // Set source, blend mode and draw with current opacity
210 frei0r_cairo_set_operator(cr, inst->blend_mode);
211 }
212
213 cairo_set_source_surface (cr, src_image, 0, 0);
214 cairo_paint_with_alpha (cr, inst->opacity);
215
216 // Clean up in proper order
217 cairo_surface_destroy (src_image);
218 cairo_destroy (cr);
219 cairo_surface_destroy (out_image);
220 }
221
222 void f0r_update(f0r_instance_t instance, double time,
223 const uint32_t* inframe, uint32_t* outframe)
224 {
225 // not impl. for mixers
226 }
227
228 void f0r_update2(f0r_instance_t instance, double time, const uint32_t* inframe1,
229 const uint32_t* inframe2, const uint32_t* inframe3, uint32_t* outframe)
230 {
231 // Validate inputs
232 if (!instance || !inframe1 || !inframe2 || !outframe) {
233 return;
234 }
235
236 assert(instance);
237 cairo_blend_instance_t* inst = (cairo_blend_instance_t*) instance;
238
239 unsigned char* dst = (unsigned char*)inframe1;
240 unsigned char* src = (unsigned char*)inframe2;
241 unsigned char* out = (unsigned char*)outframe;
242 int pixels = inst->width * inst->height;
243
244 // Validate dimensions
245 if (pixels <= 0) {
246 return;
247 }
248
249 frei0r_cairo_premultiply_rgba2 (dst, out, pixels, -1);
250 frei0r_cairo_premultiply_rgba (src, pixels, -1);
251 draw_composite (inst, out, src, time);
252 frei0r_cairo_unpremultiply_rgba (out, pixels);
253 }
254