| 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"cxx . . $ `<file>/include/frei0r.h`/ |
| 29 | #include "frei0r/cairo.h"cxx . . $ `<file>/include/frei0r/cairo.h`/ |
| 30 | |
| 31 | typedef struct cairo_blend_instancecxx . . $ cairo_blend_instance# |
| 32 | { |
| 33 | unsigned int widthcxx . . $ cairo_blend_instance#width.; |
| 34 | unsigned int heightcxx . . $ cairo_blend_instance#height.; |
| 35 | double opacitycxx . . $ cairo_blend_instance#opacity.; |
| 36 | char *blend_modecxx . . $ cairo_blend_instance#blend_mode.; |
| 37 | } cairo_blend_instance_tcxx . . $ cairo_blend_instance_t#; |
| 38 | |
| 39 | int f0r_initcxx . . $ f0r_init(b126dc7c1de90089).() |
| 40 | { |
| 41 | return 1; |
| 42 | } |
| 43 | |
| 44 | void f0r_deinitcxx . . $ f0r_deinit(49f6e7a06ebc5aa8).() |
| 45 | { |
| 46 | |
| 47 | } |
| 48 | |
| 49 | void f0r_get_plugin_infocxx . . $ f0r_get_plugin_info(35d753456c0ea59c).(f0r_plugin_info_tcxx . . $ f0r_plugin_info_t#* info) |
| 50 | { |
| 51 | info->namecxx . . $ f0r_plugin_info#name. = "cairoblend"; |
| 52 | info->authorcxx . . $ f0r_plugin_info#author. = "Janne Liljeblad"; |
| 53 | info->plugin_typecxx . . $ f0r_plugin_info#plugin_type. = F0R_PLUGIN_TYPE_MIXER2cxx . . $ `include/frei0r.h:251:9`!; |
| 54 | info->color_modelcxx . . $ f0r_plugin_info#color_model. = F0R_COLOR_MODEL_RGBA8888cxx . . $ `include/frei0r.h:308:9`!; |
| 55 | info->frei0r_versioncxx . . $ f0r_plugin_info#frei0r_version. = FREI0R_MAJOR_VERSIONcxx . . $ `include/frei0r.h:217:9`!; |
| 56 | info->major_versioncxx . . $ f0r_plugin_info#major_version. = 0; |
| 57 | info->minor_versioncxx . . $ f0r_plugin_info#minor_version. = 10; |
| 58 | info->num_paramscxx . . $ f0r_plugin_info#num_params. = 2; |
| 59 | info->explanationcxx . . $ f0r_plugin_info#explanation. = "Composites second input on the first input with user-defined blend mode and opacity."; |
| 60 | } |
| 61 | |
| 62 | void f0r_get_param_infocxx . . $ f0r_get_param_info(a9f9aa7954cc4a37).(f0r_param_info_tcxx . . $ f0r_param_info_t#* info, int param_index) |
| 63 | { |
| 64 | switch(param_index) { |
| 65 | case 0: |
| 66 | info->namecxx . . $ f0r_param_info#name. = "opacity"; |
| 67 | info->typecxx . . $ f0r_param_info#type. = F0R_PARAM_DOUBLEcxx . . $ `include/frei0r.h:383:9`!; |
| 68 | info->explanationcxx . . $ f0r_param_info#explanation. = "Opacity of composited image"; |
| 69 | break; |
| 70 | case 1: |
| 71 | info->namecxx . . $ f0r_param_info#name. = "blend mode"; |
| 72 | info->typecxx . . $ f0r_param_info#type. = F0R_PARAM_STRINGcxx . . $ `include/frei0r.h:400:9`!; |
| 73 | info->explanationcxx . . $ f0r_param_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_tcxx . . $ f0r_instance_t# f0r_constructcxx . . $ f0r_construct(da3f471cd007a696).(unsigned int width, unsigned int height) |
| 79 | { |
| 80 | // Validate inputs |
| 81 | if (width == 0 || height == 0) { |
| 82 | return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 83 | } |
| 84 | |
| 85 | cairo_blend_instance_tcxx . . $ cairo_blend_instance_t#* inst = (cairo_blend_instance_tcxx . . $ cairo_blend_instance_t#*)calloccxx . . $ calloc(63a7bf2941bfcd7a).(1, sizeof(*inst)); |
| 86 | if (!inst) { |
| 87 | return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 88 | } |
| 89 | |
| 90 | inst->widthcxx . . $ cairo_blend_instance#width. = width; |
| 91 | inst->heightcxx . . $ cairo_blend_instance#height. = height; |
| 92 | |
| 93 | inst->opacitycxx . . $ cairo_blend_instance#opacity. = 1.0; |
| 94 | |
| 95 | const char* blend_val = NORMALcxx . . $ `include/frei0r/cairo.h:36:9`!; |
| 96 | inst->blend_modecxx . . $ cairo_blend_instance#blend_mode. = (char*) malloccxx . . $ malloc(bfcfa4d6b7f7ef64). (strlencxx . . $ strlen(751346f8406fd082).(blend_val) + 1); |
| 97 | if (!inst->blend_modecxx . . $ cairo_blend_instance#blend_mode.) { |
| 98 | freecxx . . $ free(5d22bdacc48458e8).(inst); |
| 99 | return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 100 | } |
| 101 | strcpycxx . . $ strcpy(b7b3a83273496486). (inst->blend_modecxx . . $ cairo_blend_instance#blend_mode., blend_val); |
| 102 | |
| 103 | return (f0r_instance_tcxx . . $ f0r_instance_t#)inst; |
| 104 | } |
| 105 | |
| 106 | void f0r_destructcxx . . $ f0r_destruct(5d22bdacc48458e8).(f0r_instance_tcxx . . $ f0r_instance_t# instance) |
| 107 | { |
| 108 | if (!instance) { |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | cairo_blend_instance_tcxx . . $ cairo_blend_instance_t#* inst = (cairo_blend_instance_tcxx . . $ cairo_blend_instance_t#*)instance; |
| 113 | if (inst->blend_modecxx . . $ cairo_blend_instance#blend_mode.) { |
| 114 | freecxx . . $ free(5d22bdacc48458e8).(inst->blend_modecxx . . $ cairo_blend_instance#blend_mode.); |
| 115 | } |
| 116 | freecxx . . $ free(5d22bdacc48458e8).(instance); |
| 117 | } |
| 118 | |
| 119 | void f0r_set_param_valuecxx . . $ f0r_set_param_value(5be095a8278d2bf5).(f0r_instance_tcxx . . $ f0r_instance_t# instance, f0r_param_tcxx . . $ f0r_param_t# param, int param_index) |
| 120 | { |
| 121 | assertcxx . . $ `<external>/4398592474888995393/assert.h:77:11`!(instance); |
| 122 | cairo_blend_instance_tcxx . . $ cairo_blend_instance_t#* inst = (cairo_blend_instance_tcxx . . $ cairo_blend_instance_t#*) instance; |
| 123 | char* sval; |
| 124 | switch(param_index) { |
| 125 | case 0: |
| 126 | // Validate double parameter |
| 127 | if (param) { |
| 128 | inst->opacitycxx . . $ cairo_blend_instance#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_modecxx . . $ cairo_blend_instance#blend_mode. = (char*)realloccxx . . $ realloc(417ab160f6baadc6). (inst->blend_modecxx . . $ cairo_blend_instance#blend_mode., strlencxx . . $ strlen(751346f8406fd082).(sval) + 1); |
| 137 | if (inst->blend_modecxx . . $ cairo_blend_instance#blend_mode.) { |
| 138 | strcpycxx . . $ strcpy(b7b3a83273496486). (inst->blend_modecxx . . $ cairo_blend_instance#blend_mode., sval); |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | break; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | void f0r_get_param_valuecxx . . $ f0r_get_param_value(5be095a8278d2bf5).(f0r_instance_tcxx . . $ f0r_instance_t# instance, f0r_param_tcxx . . $ f0r_param_t# param, int param_index) |
| 147 | { |
| 148 | assertcxx . . $ `<external>/4398592474888995393/assert.h:77:11`!(instance); |
| 149 | cairo_blend_instance_tcxx . . $ cairo_blend_instance_t#* inst = (cairo_blend_instance_tcxx . . $ cairo_blend_instance_t#*)instance; |
| 150 | |
| 151 | switch(param_index) { |
| 152 | case 0: |
| 153 | if (param) { |
| 154 | *((double*)param) = inst->opacitycxx . . $ cairo_blend_instance#opacity.; |
| 155 | } |
| 156 | break; |
| 157 | case 1: |
| 158 | if (param && inst->blend_modecxx . . $ cairo_blend_instance#blend_mode.) { |
| 159 | *((f0r_param_stringcxx . . $ f0r_param_string# *)param) = inst->blend_modecxx . . $ cairo_blend_instance#blend_mode.; |
| 160 | } else if (param) { |
| 161 | *((f0r_param_stringcxx . . $ f0r_param_string# *)param) = ""; |
| 162 | } |
| 163 | break; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | void draw_compositecxx . . $ draw_composite(bbdfc263f65abf92).(cairo_blend_instance_tcxx . . $ cairo_blend_instance_t#* inst, unsigned char* out, unsigned char* src, double time) |
| 168 | { |
| 169 | int w = inst->widthcxx . . $ cairo_blend_instance#width.; |
| 170 | int h = inst->heightcxx . . $ cairo_blend_instance#height.; |
| 171 | int stride = cairo_format_stride_for_widthcxx . . $ cairo_format_stride_for_width(8ae00fa98af1f034). (CAIRO_FORMAT_ARGB32cxx . . $ CAIRO_FORMAT_ARGB32., w); |
| 172 | |
| 173 | // Validate inputs |
| 174 | if (!inst || !out || !src || w <= 0 || h <= 0) { |
| 175 | return; |
| 176 | } |
| 177 | |
| 178 | cairo_surface_tcxx . . $ cairo_surface_t#* out_image = cairo_image_surface_create_for_datacxx . . $ cairo_image_surface_create_for_data(ced8dd1547170d92). (out, |
| 179 | CAIRO_FORMAT_ARGB32cxx . . $ 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_tcxx . . $ cairo_t#* cr = cairo_createcxx . . $ cairo_create(b6f0039b687ec546). (out_image); |
| 189 | // Check if context creation succeeded |
| 190 | if (!cr) { |
| 191 | cairo_surface_destroycxx . . $ cairo_surface_destroy(2fa2b40fabeeffa2). (out_image); |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | cairo_surface_tcxx . . $ cairo_surface_t#* src_image = cairo_image_surface_create_for_datacxx . . $ cairo_image_surface_create_for_data(ced8dd1547170d92). ((unsigned char*)src, |
| 196 | CAIRO_FORMAT_ARGB32cxx . . $ CAIRO_FORMAT_ARGB32., |
| 197 | w, |
| 198 | h, |
| 199 | stride); |
| 200 | // Check if surface creation succeeded |
| 201 | if (!src_image) { |
| 202 | cairo_destroycxx . . $ cairo_destroy(21788b7825b29bf). (cr); |
| 203 | cairo_surface_destroycxx . . $ cairo_surface_destroy(2fa2b40fabeeffa2). (out_image); |
| 204 | return; |
| 205 | } |
| 206 | |
| 207 | // Validate blend mode string |
| 208 | if (inst->blend_modecxx . . $ cairo_blend_instance#blend_mode.) { |
| 209 | // Set source, blend mode and draw with current opacity |
| 210 | frei0r_cairo_set_operatorcxx . . $ frei0r_cairo_set_operator(53f89654c13f6bd9).(cr, inst->blend_modecxx . . $ cairo_blend_instance#blend_mode.); |
| 211 | } |
| 212 | |
| 213 | cairo_set_source_surfacecxx . . $ cairo_set_source_surface(7ccd196fd31e8c73). (cr, src_image, 0, 0); |
| 214 | cairo_paint_with_alphacxx . . $ cairo_paint_with_alpha(74853563bcf87c34). (cr, inst->opacitycxx . . $ cairo_blend_instance#opacity.); |
| 215 | |
| 216 | // Clean up in proper order |
| 217 | cairo_surface_destroycxx . . $ cairo_surface_destroy(2fa2b40fabeeffa2). (src_image); |
| 218 | cairo_destroycxx . . $ cairo_destroy(21788b7825b29bf). (cr); |
| 219 | cairo_surface_destroycxx . . $ cairo_surface_destroy(2fa2b40fabeeffa2). (out_image); |
| 220 | } |
| 221 | |
| 222 | void f0r_updatecxx . . $ f0r_update(96cd7b27f2fca8a7).(f0r_instance_tcxx . . $ f0r_instance_t# instance, double time, |
| 223 | const uint32_tcxx . . $ uint32_t#* inframe, uint32_tcxx . . $ uint32_t#* outframe) |
| 224 | { |
| 225 | // not impl. for mixers |
| 226 | } |
| 227 | |
| 228 | void f0r_update2cxx . . $ f0r_update2(1b234bc88d3943d5).(f0r_instance_tcxx . . $ f0r_instance_t# instance, double time, const uint32_tcxx . . $ uint32_t#* inframe1, |
| 229 | const uint32_tcxx . . $ uint32_t#* inframe2, const uint32_tcxx . . $ uint32_t#* inframe3, uint32_tcxx . . $ uint32_t#* outframe) |
| 230 | { |
| 231 | // Validate inputs |
| 232 | if (!instance || !inframe1 || !inframe2 || !outframe) { |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | assertcxx . . $ `<external>/4398592474888995393/assert.h:77:11`!(instance); |
| 237 | cairo_blend_instance_tcxx . . $ cairo_blend_instance_t#* inst = (cairo_blend_instance_tcxx . . $ 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->widthcxx . . $ cairo_blend_instance#width. * inst->heightcxx . . $ cairo_blend_instance#height.; |
| 243 | |
| 244 | // Validate dimensions |
| 245 | if (pixels <= 0) { |
| 246 | return; |
| 247 | } |
| 248 | |
| 249 | frei0r_cairo_premultiply_rgba2cxx . . $ frei0r_cairo_premultiply_rgba2(5da15ab08d52c577). (dst, out, pixels, -1); |
| 250 | frei0r_cairo_premultiply_rgbacxx . . $ frei0r_cairo_premultiply_rgba(49a5bbab55ea8abc). (src, pixels, -1); |
| 251 | draw_compositecxx . . $ draw_composite(bbdfc263f65abf92). (inst, out, src, time); |
| 252 | frei0r_cairo_unpremultiply_rgbacxx . . $ frei0r_cairo_unpremultiply_rgba(fc0f0e1646c32816). (out, pixels); |
| 253 | } |
| 254 |