| 1 | /* |
| 2 | * cairogradient.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 <string.h> |
| 24 | #include <assert.h> |
| 25 | #include <cairo.h> |
| 26 | #include <stdio.h> |
| 27 | #include <math.h> |
| 28 | |
| 29 | #include "frei0r.h" |
| 30 | #include "frei0r/cairo.h" |
| 31 | |
| 32 | typedef struct cairo_gradient_instance |
| 33 | { |
| 34 | unsigned int width; |
| 35 | unsigned int height; |
| 36 | char *pattern; |
| 37 | f0r_param_color_t start_color; |
| 38 | double start_opacity; |
| 39 | f0r_param_color_t end_color; |
| 40 | double end_opacity; |
| 41 | double start_x; |
| 42 | double start_y; |
| 43 | double end_x; |
| 44 | double end_y; |
| 45 | char *end_point; |
| 46 | double offset; |
| 47 | char *blend_mode; |
| 48 | } cairo_gradient_instance_t; |
| 49 | |
| 50 | int f0r_init() |
| 51 | { |
| 52 | return 1; |
| 53 | } |
| 54 | |
| 55 | void f0r_deinit() |
| 56 | { |
| 57 | |
| 58 | } |
| 59 | |
| 60 | void f0r_get_plugin_info(f0r_plugin_info_t* info) |
| 61 | { |
| 62 | info->name = "cairogradient"; |
| 63 | info->author = "Janne Liljeblad"; |
| 64 | info->plugin_type = F0R_PLUGIN_TYPE_FILTER; |
| 65 | info->color_model = F0R_COLOR_MODEL_RGBA8888; |
| 66 | info->frei0r_version = FREI0R_MAJOR_VERSION; |
| 67 | info->major_version = 1; |
| 68 | info->minor_version = 0; |
| 69 | info->num_params = 11; |
| 70 | info->explanation = "Draws a gradient on top of image. Filter is given gradient start and end points, colors and opacities."; |
| 71 | } |
| 72 | |
| 73 | void f0r_get_param_info(f0r_param_info_t* info, int param_index) |
| 74 | { |
| 75 | switch(param_index) { |
| 76 | case 0: |
| 77 | info->name = "pattern"; |
| 78 | info->type = F0R_PARAM_STRING; |
| 79 | info->explanation = "Linear or radial gradient";// Accepted values: 'gradient_linear' and 'gradient_radial |
| 80 | break; |
| 81 | case 1: |
| 82 | info->name = "start color"; |
| 83 | info->type = F0R_PARAM_COLOR; |
| 84 | info->explanation = "First color of the gradient"; |
| 85 | break; |
| 86 | case 2: |
| 87 | info->name = "start opacity"; |
| 88 | info->type = F0R_PARAM_DOUBLE; |
| 89 | info->explanation = "Opacity of the first color of the gradient"; |
| 90 | break; |
| 91 | case 3: |
| 92 | info->name = "end color"; |
| 93 | info->type = F0R_PARAM_COLOR; |
| 94 | info->explanation = "Second color of the gradient"; |
| 95 | break; |
| 96 | case 4: |
| 97 | info->name = "end opacity"; |
| 98 | info->type = F0R_PARAM_DOUBLE; |
| 99 | info->explanation = "Opacity of the second color of the gradient"; |
| 100 | break; |
| 101 | case 5: |
| 102 | info->name = "start x"; |
| 103 | info->type = F0R_PARAM_DOUBLE; |
| 104 | info->explanation = "X position of the start point of the gradient"; |
| 105 | break; |
| 106 | case 6: |
| 107 | info->name = "start y"; |
| 108 | info->type = F0R_PARAM_DOUBLE; |
| 109 | info->explanation = "Y position of the start point of the gradient"; |
| 110 | break; |
| 111 | case 7: |
| 112 | info->name = "end x"; |
| 113 | info->type = F0R_PARAM_DOUBLE; |
| 114 | info->explanation = "X position of the end point of the gradient"; |
| 115 | break; |
| 116 | case 8: |
| 117 | info->name = "end y"; |
| 118 | info->type = F0R_PARAM_DOUBLE; |
| 119 | info->explanation = "Y position of the end point of the gradient"; |
| 120 | break; |
| 121 | case 9: |
| 122 | info->name = "offset"; |
| 123 | info->type = F0R_PARAM_DOUBLE; |
| 124 | info->explanation = "Position of first color in the line connecting gradient ends, really useful only for radial gradient"; |
| 125 | break; |
| 126 | case 10: |
| 127 | info->name = "blend mode"; |
| 128 | info->type = F0R_PARAM_STRING; |
| 129 | info->explanation = "Blend mode used to compose gradient on image. Accepted values: 'normal', 'add', 'saturate', 'multiply', 'screen', 'overlay', 'darken', 'lighten', 'colordodge', 'colorburn', 'hardlight', 'softlight', 'difference', 'exclusion', 'hslhue', 'hslsaturation', 'hslcolor', 'hslluminosity'"; |
| 130 | break; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | f0r_instance_t f0r_construct(unsigned int width, unsigned int height) |
| 135 | { |
| 136 | cairo_gradient_instance_t* inst = (cairo_gradient_instance_t*) calloc (1, sizeof(*inst)); |
| 137 | inst->width = width; |
| 138 | inst->height = height; |
| 139 | |
| 140 | const char* pattern_val = GRADIENT_LINEAR; |
| 141 | inst->pattern = (char*) malloc (strlen(pattern_val) + 1); |
| 142 | strcpy (inst->pattern, pattern_val); |
| 143 | |
| 144 | inst->start_color.r = 0.0; |
| 145 | inst->start_color.g = 0.0; |
| 146 | inst->start_color.b = 0.0; |
| 147 | inst->start_opacity = 0.5; |
| 148 | inst->end_color.r = 1.0; |
| 149 | inst->end_color.g = 1.0; |
| 150 | inst->end_color.b = 1.0; |
| 151 | inst->end_opacity = 0.5; |
| 152 | inst->start_x = 0.5; |
| 153 | inst->start_y = 0.0; |
| 154 | inst->end_x = 0.5; |
| 155 | inst->end_y = 1.0; |
| 156 | inst->offset = 0.0; |
| 157 | |
| 158 | const char* blend_val = NORMAL; |
| 159 | inst->blend_mode = (char*) malloc (strlen(blend_val) + 1 ); |
| 160 | strcpy (inst->blend_mode, blend_val); |
| 161 | |
| 162 | return (f0r_instance_t)inst; |
| 163 | } |
| 164 | |
| 165 | void f0r_destruct(f0r_instance_t instance) |
| 166 | { |
| 167 | cairo_gradient_instance_t* inst = (cairo_gradient_instance_t*)instance; |
| 168 | free(inst->blend_mode); |
| 169 | free(inst->pattern); |
| 170 | free(instance); |
| 171 | } |
| 172 | |
| 173 | void f0r_set_param_value(f0r_instance_t instance, f0r_param_t param, int param_index) |
| 174 | { |
| 175 | assert(instance); |
| 176 | cairo_gradient_instance_t* inst = (cairo_gradient_instance_t*)instance; |
| 177 | char* sval; |
| 178 | switch(param_index) { |
| 179 | case 0: |
| 180 | sval = (*(char**)param); |
| 181 | inst->pattern = (char*)realloc (inst->pattern, strlen(sval) + 1); |
| 182 | strcpy( inst->pattern, sval ); |
| 183 | break; |
| 184 | case 1: |
| 185 | inst->start_color = *((f0r_param_color_t*)param); |
| 186 | break; |
| 187 | case 2: |
| 188 | inst->start_opacity = *((double*)param); |
| 189 | break; |
| 190 | case 3: |
| 191 | inst->end_color = *((f0r_param_color_t*)param); |
| 192 | break; |
| 193 | case 4: |
| 194 | inst->end_opacity = *((double*)param); |
| 195 | break; |
| 196 | case 5: |
| 197 | inst->start_x = *((double*)param); |
| 198 | break; |
| 199 | case 6: |
| 200 | inst->start_y = *((double*)param); |
| 201 | break; |
| 202 | case 7: |
| 203 | inst->end_x = *((double*)param); |
| 204 | break; |
| 205 | case 8: |
| 206 | inst->end_y = *((double*)param); |
| 207 | break; |
| 208 | case 9: |
| 209 | inst->offset = *((double*)param); |
| 210 | break; |
| 211 | case 10: |
| 212 | sval = (*(char**)param); |
| 213 | inst->blend_mode = (char*) realloc (inst->blend_mode, strlen(sval) + 1); |
| 214 | strcpy (inst->blend_mode, sval); |
| 215 | break; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | void f0r_get_param_value(f0r_instance_t instance, f0r_param_t param, int param_index) |
| 220 | { |
| 221 | assert(instance); |
| 222 | cairo_gradient_instance_t* inst = (cairo_gradient_instance_t*)instance; |
| 223 | |
| 224 | switch(param_index) { |
| 225 | case 0: |
| 226 | *((f0r_param_string *)param) = inst->pattern; |
| 227 | break; |
| 228 | case 1: |
| 229 | *((f0r_param_color_t*)param) = inst->start_color; |
| 230 | break; |
| 231 | case 2: |
| 232 | *((double*)param) = inst->start_opacity; |
| 233 | break; |
| 234 | case 3: |
| 235 | *((f0r_param_color_t*)param) = inst->end_color; |
| 236 | break; |
| 237 | case 4: |
| 238 | *((double*)param) = inst->end_opacity; |
| 239 | break; |
| 240 | case 5: |
| 241 | *((double*)param) = inst->start_x; |
| 242 | break; |
| 243 | case 6: |
| 244 | *((double*)param) = inst->start_y; |
| 245 | break; |
| 246 | case 7: |
| 247 | *((double*)param) = inst->end_x; |
| 248 | break; |
| 249 | case 8: |
| 250 | *((double*)param) = inst->end_y; |
| 251 | break; |
| 252 | case 9: |
| 253 | *((double*)param) = inst->offset; |
| 254 | break; |
| 255 | case 10: |
| 256 | *((f0r_param_string *)param) = inst->blend_mode; |
| 257 | break; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | void draw_gradient(cairo_gradient_instance_t* inst, unsigned char* dst, const unsigned char* src, double time) |
| 262 | { |
| 263 | int stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32, inst->width); |
| 264 | cairo_surface_t *surface = cairo_image_surface_create_for_data (dst, CAIRO_FORMAT_ARGB32, inst->width, inst->height, stride); |
| 265 | cairo_t *cr = cairo_create (surface); |
| 266 | |
| 267 | cairo_surface_t* src_image = cairo_image_surface_create_for_data ((unsigned char*)src, |
| 268 | CAIRO_FORMAT_ARGB32, inst->width, inst->height, stride); |
| 269 | |
| 270 | cairo_set_source_surface (cr, src_image, 0, 0); |
| 271 | cairo_paint (cr); |
| 272 | |
| 273 | cairo_pattern_t *pat; |
| 274 | double sx = inst->start_x; |
| 275 | double sy = inst->start_y; |
| 276 | double ex = inst->end_x; |
| 277 | double ey = inst->end_y; |
| 278 | if (strcmp(inst->pattern, GRADIENT_RADIAL) == 0) |
| 279 | { |
| 280 | |
| 281 | |
| 282 | double distance_x = (sx - ex) * inst->width; |
| 283 | double distance_y = (sy - ey) * inst->height; |
| 284 | double distance = sqrt( (distance_x * distance_x) + (distance_y * distance_y)); |
| 285 | pat = cairo_pattern_create_radial (sx * inst->width, |
| 286 | sy * inst->height, |
| 287 | 0.0, |
| 288 | sx * inst->width, |
| 289 | sy * inst->height, |
| 290 | distance); |
| 291 | } |
| 292 | else |
| 293 | { |
| 294 | pat = cairo_pattern_create_linear ( sx * inst->width, |
| 295 | sy * inst->height, |
| 296 | ex * inst->width, |
| 297 | ey * inst->height); |
| 298 | } |
| 299 | |
| 300 | freior_cairo_set_color_stop_rgba_LITTLE_ENDIAN (pat, |
| 301 | 1.0, |
| 302 | inst->start_color.r, |
| 303 | inst->start_color.g, |
| 304 | inst->start_color.b, |
| 305 | inst->start_opacity); |
| 306 | freior_cairo_set_color_stop_rgba_LITTLE_ENDIAN (pat, |
| 307 | inst->offset, |
| 308 | inst->end_color.r, |
| 309 | inst->end_color.g, |
| 310 | inst->end_color.b, |
| 311 | inst->end_opacity); |
| 312 | |
| 313 | |
| 314 | cairo_set_source (cr, pat); |
| 315 | frei0r_cairo_set_operator(cr, inst->blend_mode); |
| 316 | cairo_rectangle (cr, 0, 0, inst->width, inst->height); |
| 317 | |
| 318 | cairo_fill (cr); |
| 319 | |
| 320 | cairo_pattern_destroy (pat); |
| 321 | cairo_destroy (cr); |
| 322 | cairo_surface_destroy (surface); |
| 323 | cairo_surface_destroy (src_image); |
| 324 | } |
| 325 | |
| 326 | void f0r_update(f0r_instance_t instance, double time, |
| 327 | const uint32_t* inframe, uint32_t* outframe) |
| 328 | { |
| 329 | assert(instance); |
| 330 | cairo_gradient_instance_t* inst = (cairo_gradient_instance_t*)instance; |
| 331 | |
| 332 | unsigned char* dst = (unsigned char*)outframe; |
| 333 | unsigned char* src = (unsigned char*)inframe; |
| 334 | int pixels = inst->width * inst->height; |
| 335 | |
| 336 | // Clear the destination |
| 337 | memset(dst, 0, pixels * sizeof(uint32_t)); |
| 338 | |
| 339 | frei0r_cairo_premultiply_rgba (src, pixels, -1); |
| 340 | draw_gradient(inst, dst, src, time); |
| 341 | frei0r_cairo_unpremultiply_rgba (dst, pixels); |
| 342 | } |
| 343 |