| 1 | /* |
| 2 | Copyright (C) 2013 Vadim Druzhin <[email protected]> |
| 3 | |
| 4 | This program is free software: you can redistribute it and/or modify |
| 5 | it under the terms of the GNU Lesser General Public License as published by |
| 6 | the Free Software Foundation, either version 3 of the License, or |
| 7 | (at your option) any later version. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU Lesser General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public License |
| 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
| 18 | #include <stdlib.h> |
| 19 | #include <stdint.h> |
| 20 | #include <string.h> |
| 21 | #define _USE_MATH_DEFINES |
| 22 | #include <math.h> |
| 23 | #include <frei0r.h> |
| 24 | |
| 25 | typedef struct instance_s |
| 26 | { |
| 27 | int w; |
| 28 | int h; |
| 29 | double pos; |
| 30 | int r; |
| 31 | int bw; |
| 32 | int bw_scale; |
| 33 | int *bwk; |
| 34 | } instance_t; |
| 35 | |
| 36 | int f0r_init(void) |
| 37 | { |
| 38 | return 1; |
| 39 | } |
| 40 | |
| 41 | void f0r_deinit(void) {} |
| 42 | |
| 43 | void f0r_get_plugin_info(f0r_plugin_info_t *info) |
| 44 | { |
| 45 | info->name = "wipe-circle"; |
| 46 | info->author = "Vadim Druzhin"; |
| 47 | info->plugin_type = F0R_PLUGIN_TYPE_MIXER2; |
| 48 | info->color_model = F0R_COLOR_MODEL_RGBA8888; |
| 49 | info->frei0r_version = FREI0R_MAJOR_VERSION; |
| 50 | info->major_version = 0; |
| 51 | info->minor_version = 1; |
| 52 | info->num_params = 1; |
| 53 | info->explanation = "Circle wipe"; |
| 54 | } |
| 55 | |
| 56 | void f0r_get_param_info(f0r_param_info_t *info, int index) |
| 57 | { |
| 58 | if(0 == index) |
| 59 | { |
| 60 | info->name = "position"; |
| 61 | info->type = F0R_PARAM_DOUBLE; |
| 62 | info->explanation = "Circle size"; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | f0r_instance_t f0r_construct(unsigned width, unsigned height) |
| 67 | { |
| 68 | instance_t *inst; |
| 69 | int r; |
| 70 | int bw; |
| 71 | int i; |
| 72 | |
| 73 | r = (int)(hypotf((float)height, (float)width) / 2.0f + 0.5f); |
| 74 | |
| 75 | bw = r / 16; |
| 76 | |
| 77 | inst = malloc(sizeof(*inst) + bw * sizeof(*inst->bwk)); |
| 78 | if(NULL == inst) |
| 79 | return NULL; |
| 80 | |
| 81 | inst->w = width; |
| 82 | inst->h = height; |
| 83 | inst-> pos = 0.0; |
| 84 | inst->r = r; |
| 85 | inst->bw = bw; |
| 86 | inst->bw_scale = bw * bw; |
| 87 | inst->bwk = (int *)(inst + 1); |
| 88 | |
| 89 | for(i = 0; i < bw; ++i) |
| 90 | { |
| 91 | if(i < bw / 2) |
| 92 | inst->bwk[i] = i * i * 2; |
| 93 | else |
| 94 | inst->bwk[i] = inst->bw_scale - (bw - i) * (bw - i) * 2; |
| 95 | } |
| 96 | |
| 97 | return inst; |
| 98 | } |
| 99 | |
| 100 | void f0r_destruct(f0r_instance_t inst) |
| 101 | { |
| 102 | free(inst); |
| 103 | } |
| 104 | |
| 105 | void f0r_set_param_value(f0r_instance_t instance, f0r_param_t param, int param_index) |
| 106 | { |
| 107 | instance_t *inst = instance; |
| 108 | |
| 109 | if(0 == param_index) |
| 110 | inst->pos = *(f0r_param_double *)param; |
| 111 | } |
| 112 | |
| 113 | void f0r_get_param_value(f0r_instance_t instance, f0r_param_t param, int param_index) |
| 114 | { |
| 115 | instance_t *inst = instance; |
| 116 | |
| 117 | if(0 == param_index) |
| 118 | *(f0r_param_double *)param = inst->pos; |
| 119 | } |
| 120 | |
| 121 | void f0r_update2( |
| 122 | f0r_instance_t instance, |
| 123 | double time, |
| 124 | const uint32_t* inframe1, |
| 125 | const uint32_t* inframe2, |
| 126 | const uint32_t* inframe3, |
| 127 | uint32_t* outframe |
| 128 | ) |
| 129 | { |
| 130 | instance_t *inst = instance; |
| 131 | int x0, y0, r0, r1; |
| 132 | int y, x, dy, dx, ox, oy; |
| 133 | |
| 134 | (void)time; /* Unused */ |
| 135 | (void)inframe3; /* Unused */ |
| 136 | |
| 137 | r1 = (int)((inst->r + inst->bw) * inst->pos + 0.5); |
| 138 | r0 = r1 - inst->bw; |
| 139 | |
| 140 | x0 = inst->w / 2; |
| 141 | y0 = inst->h / 2; |
| 142 | |
| 143 | dy = inst->h / 2 - r1; |
| 144 | dx = inst->w / 2 - r1; |
| 145 | |
| 146 | if(r0 > 0) |
| 147 | { |
| 148 | ox = (int)((float)r0 * (float)M_SQRT1_2 + 0.5f); |
| 149 | oy = ox; |
| 150 | |
| 151 | if(ox > inst->w / 2) |
| 152 | ox = inst->w / 2; |
| 153 | |
| 154 | if(oy > inst->h / 2) |
| 155 | oy = inst->h / 2; |
| 156 | } |
| 157 | else |
| 158 | { |
| 159 | ox = 0; |
| 160 | oy = 0; |
| 161 | } |
| 162 | |
| 163 | if(ox > 0 && oy > 0) |
| 164 | { |
| 165 | for(y = y0 - oy; y < y0 + oy; ++y) |
| 166 | memcpy(outframe + inst->w * y + x0 - ox, |
| 167 | inframe2 + inst->w * y + x0 - ox, ox * 2 * sizeof(*outframe)); |
| 168 | } |
| 169 | |
| 170 | if(dy > 0) |
| 171 | { |
| 172 | memcpy(outframe, inframe1, inst->w * dy * sizeof(*outframe)); |
| 173 | |
| 174 | memcpy(outframe + inst->w * (y0 + r1), inframe1 + inst->w * (y0 + r1), |
| 175 | inst->w * dy * sizeof(*outframe)); |
| 176 | |
| 177 | inframe1 += dy * inst->w; |
| 178 | inframe2 += dy * inst->w; |
| 179 | outframe += dy * inst->w; |
| 180 | } |
| 181 | else |
| 182 | dy = 0; |
| 183 | |
| 184 | if(dx > 0) |
| 185 | { |
| 186 | for(y = 0; y < inst->h - dy * 2; ++y) |
| 187 | { |
| 188 | memcpy(outframe + inst->w * y, inframe1 + inst->w * y, dx * sizeof(*outframe)); |
| 189 | |
| 190 | memcpy(outframe + inst->w * y + inst->w - dx, |
| 191 | inframe1 + inst->w * y + inst->w - dx, dx * sizeof(*outframe)); |
| 192 | } |
| 193 | |
| 194 | inframe1 += dx; |
| 195 | inframe2 += dx; |
| 196 | outframe += dx; |
| 197 | } |
| 198 | else |
| 199 | dx = 0; |
| 200 | |
| 201 | for(y = dy; y < inst->h - dy; ++y) |
| 202 | { |
| 203 | for(x = dx; x < inst->w - dx; ++x) |
| 204 | { |
| 205 | if(y < y0 - oy || y >= y0 + oy || x < x0 - ox || x >= x0 + ox) |
| 206 | { |
| 207 | int r = (int)(hypotf((float)(x - x0), (float)(y - y0)) + 0.5f); |
| 208 | |
| 209 | if(r >= r1) |
| 210 | *outframe = *inframe1; |
| 211 | else if(r < r0) |
| 212 | *outframe = *inframe2; |
| 213 | else |
| 214 | { |
| 215 | int k1 = inst->bwk[r - r0]; |
| 216 | int k2 = inst->bw_scale - k1; |
| 217 | uint8_t *c1 = (uint8_t *)inframe1; |
| 218 | uint8_t *c2 = (uint8_t *)inframe2; |
| 219 | uint8_t *co = (uint8_t *)outframe; |
| 220 | |
| 221 | co[0] = (c1[0] * k1 + c2[0] * k2 + inst->bw_scale / 2) / inst->bw_scale; |
| 222 | co[1] = (c1[1] * k1 + c2[1] * k2 + inst->bw_scale / 2) / inst->bw_scale; |
| 223 | co[2] = (c1[2] * k1 + c2[2] * k2 + inst->bw_scale / 2) / inst->bw_scale; |
| 224 | co[3] = (c1[3] * k1 + c2[3] * k2 + inst->bw_scale / 2) / inst->bw_scale; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | ++inframe1; |
| 229 | ++inframe2; |
| 230 | ++outframe; |
| 231 | } |
| 232 | |
| 233 | inframe1 += dx * 2; |
| 234 | inframe2 += dx * 2; |
| 235 | outframe += dx * 2; |
| 236 | } |
| 237 | } |
| 238 |