| 1 | /* |
| 2 | alphagrad.c |
| 3 | |
| 4 | This frei0r plugin fills alpha channel with a gradient |
| 5 | Version 0.1 aug 2010 |
| 6 | |
| 7 | Copyright (C) 2010 Marko Cebokli http://lea.hamradio.si/~s57uuu |
| 8 | |
| 9 | |
| 10 | This program is free software; you can redistribute it and/or modify |
| 11 | it under the terms of the GNU General Public License as published by |
| 12 | the Free Software Foundation; either version 2 of the License, or |
| 13 | (at your option) any later version. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program; if not, write to the Free Software |
| 22 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 23 | |
| 24 | */ |
| 25 | |
| 26 | |
| 27 | //compile: gcc -c -fPIC -Wall alphagrad.c -o alphagrad.o |
| 28 | //link: gcc -shared -o alphagrad.so alphagrad.o |
| 29 | |
| 30 | #include <stdio.h> |
| 31 | #include <frei0r.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <math.h> |
| 34 | #include <assert.h> |
| 35 | |
| 36 | |
| 37 | |
| 38 | //---------------------------------------- |
| 39 | //struktura za instanco efekta |
| 40 | typedef struct |
| 41 | { |
| 42 | int h; |
| 43 | int w; |
| 44 | |
| 45 | float poz,wdt,tilt,min,max; |
| 46 | uint32_t *gr8; |
| 47 | int op; |
| 48 | |
| 49 | } inst; |
| 50 | |
| 51 | //----------------------------------------------------- |
| 52 | //RGBA8888 little endian |
| 53 | void fill_grad(inst *in) |
| 54 | { |
| 55 | int i,j; |
| 56 | float st,ct,po,wd,d,a; |
| 57 | |
| 58 | st=sinf(in->tilt); |
| 59 | ct=cosf(in->tilt); |
| 60 | po=(-in->w/2.0+in->poz*in->w)*1.5; |
| 61 | wd=in->wdt*in->w; |
| 62 | |
| 63 | if (in->min==in->max) |
| 64 | { |
| 65 | for (i=0;i<in->h*in->w;i++) |
| 66 | in->gr8[i]=(((uint32_t)(in->min*255.0))<<24)&0xFF000000; |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | for (i=0;i<in->h;i++) |
| 71 | for (j=0;j<in->w;j++) |
| 72 | { |
| 73 | d=(i-in->h/2)*ct+(j-in->w/2)*st-po; |
| 74 | if (fabsf(d)>wd/2.0) |
| 75 | { |
| 76 | if (d>0.0) |
| 77 | a=in->min; |
| 78 | else |
| 79 | a=in->max; |
| 80 | } |
| 81 | else |
| 82 | { |
| 83 | if (d>wd/2.0) d=wd/2.0; |
| 84 | a = in->min+(wd/2.0-d) / wd*(in->max-in->min); |
| 85 | } |
| 86 | a=255.0*a; |
| 87 | in->gr8[i*in->w+j] = (((uint32_t)a)<<24)&0xFF000000; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | //----------------------------------------------------- |
| 92 | //stretch [0...1] to parameter range [min...max] linear |
| 93 | float map_value_forward(double v, float min, float max) |
| 94 | { |
| 95 | return min+(max-min)*v; |
| 96 | } |
| 97 | |
| 98 | //----------------------------------------------------- |
| 99 | //collapse from parameter range [min...max] to [0...1] linear |
| 100 | double map_value_backward(float v, float min, float max) |
| 101 | { |
| 102 | return (v-min)/(max-min); |
| 103 | } |
| 104 | |
| 105 | //*********************************************** |
| 106 | // OBVEZNE FREI0R FUNKCIJE |
| 107 | |
| 108 | //----------------------------------------------- |
| 109 | int f0r_init() |
| 110 | { |
| 111 | return 1; |
| 112 | } |
| 113 | |
| 114 | //------------------------------------------------ |
| 115 | void f0r_deinit() |
| 116 | { |
| 117 | } |
| 118 | |
| 119 | //----------------------------------------------- |
| 120 | void f0r_get_plugin_info(f0r_plugin_info_t* info) |
| 121 | { |
| 122 | |
| 123 | info->name="alphagrad"; |
| 124 | info->author="Marko Cebokli"; |
| 125 | info->plugin_type=F0R_PLUGIN_TYPE_FILTER; |
| 126 | info->color_model=F0R_COLOR_MODEL_RGBA8888; |
| 127 | info->frei0r_version=FREI0R_MAJOR_VERSION; |
| 128 | info->major_version=0; |
| 129 | info->minor_version=2; |
| 130 | info->num_params=6; |
| 131 | info->explanation="Fills alpha channel with a gradient"; |
| 132 | } |
| 133 | |
| 134 | //-------------------------------------------------- |
| 135 | void f0r_get_param_info(f0r_param_info_t* info, int param_index) |
| 136 | { |
| 137 | switch(param_index) |
| 138 | { |
| 139 | case 0: |
| 140 | info->name = "Position"; |
| 141 | info->type = F0R_PARAM_DOUBLE; |
| 142 | info->explanation = ""; |
| 143 | break; |
| 144 | case 1: |
| 145 | info->name = "Transition width"; |
| 146 | info->type = F0R_PARAM_DOUBLE; |
| 147 | info->explanation = ""; |
| 148 | break; |
| 149 | case 2: |
| 150 | info->name = "Tilt"; |
| 151 | info->type = F0R_PARAM_DOUBLE; |
| 152 | info->explanation = ""; |
| 153 | break; |
| 154 | case 3: |
| 155 | info->name = "Min"; |
| 156 | info->type = F0R_PARAM_DOUBLE; |
| 157 | info->explanation = ""; |
| 158 | break; |
| 159 | case 4: |
| 160 | info->name = "Max"; |
| 161 | info->type = F0R_PARAM_DOUBLE; |
| 162 | info->explanation = ""; |
| 163 | break; |
| 164 | case 5: |
| 165 | info->name = "Operation"; |
| 166 | info->type = F0R_PARAM_DOUBLE; |
| 167 | info->explanation = ""; |
| 168 | break; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | //---------------------------------------------- |
| 173 | f0r_instance_t f0r_construct(unsigned int width, unsigned int height) |
| 174 | { |
| 175 | inst *in; |
| 176 | |
| 177 | in=calloc(1,sizeof(inst)); |
| 178 | in->w=width; |
| 179 | in->h=height; |
| 180 | |
| 181 | in->poz=0.5; |
| 182 | in->wdt=0.5; |
| 183 | in->tilt=0.0; |
| 184 | in->min=0.0; |
| 185 | in->max=1.0; |
| 186 | in->op=0; |
| 187 | |
| 188 | in->gr8 = (uint32_t*)calloc(in->w*in->h, sizeof(uint32_t)); |
| 189 | |
| 190 | fill_grad(in); |
| 191 | |
| 192 | return (f0r_instance_t)in; |
| 193 | } |
| 194 | |
| 195 | //--------------------------------------------------- |
| 196 | void f0r_destruct(f0r_instance_t instance) |
| 197 | { |
| 198 | inst *in; |
| 199 | |
| 200 | in=(inst*)instance; |
| 201 | |
| 202 | free(in->gr8); |
| 203 | free(instance); |
| 204 | } |
| 205 | |
| 206 | //----------------------------------------------------- |
| 207 | void f0r_set_param_value(f0r_instance_t instance, f0r_param_t parm, int param_index) |
| 208 | { |
| 209 | inst *p; |
| 210 | double tmpf; |
| 211 | int tmpi,chg; |
| 212 | |
| 213 | p=(inst*)instance; |
| 214 | |
| 215 | chg=0; |
| 216 | switch(param_index) |
| 217 | { |
| 218 | case 0: |
| 219 | tmpf=*((double*)parm); |
| 220 | if (tmpf!=p->poz) chg=1; |
| 221 | p->poz=tmpf; |
| 222 | break; |
| 223 | case 1: |
| 224 | tmpf=*((double*)parm); |
| 225 | if (tmpf!=p->wdt) chg=1; |
| 226 | p->wdt=tmpf; |
| 227 | break; |
| 228 | case 2: |
| 229 | tmpf=map_value_forward(*((double*)parm), -3.15, 3.15); |
| 230 | if (tmpf!=p->tilt) chg=1; |
| 231 | p->tilt=tmpf; |
| 232 | break; |
| 233 | case 3: |
| 234 | tmpf=*((double*)parm); |
| 235 | if (tmpf!=p->min) chg=1; |
| 236 | p->min=tmpf; |
| 237 | break; |
| 238 | case 4: |
| 239 | tmpf=*((double*)parm); |
| 240 | if (tmpf!=p->max) chg=1; |
| 241 | p->max=tmpf; |
| 242 | break; |
| 243 | case 5: |
| 244 | tmpi=map_value_forward(*((double*)parm), 0.0, 4.9999); |
| 245 | if (p->op != tmpi) chg=1; |
| 246 | p->op=tmpi; |
| 247 | break; |
| 248 | } |
| 249 | |
| 250 | if (chg==0) return; |
| 251 | |
| 252 | fill_grad(p); |
| 253 | } |
| 254 | |
| 255 | //-------------------------------------------------- |
| 256 | void f0r_get_param_value(f0r_instance_t instance, f0r_param_t param, int param_index) |
| 257 | { |
| 258 | inst *p; |
| 259 | |
| 260 | p=(inst*)instance; |
| 261 | |
| 262 | switch(param_index) |
| 263 | { |
| 264 | case 0: |
| 265 | *((double*)param)=p->poz; |
| 266 | break; |
| 267 | case 1: |
| 268 | *((double*)param)=p->wdt; |
| 269 | break; |
| 270 | case 2: |
| 271 | *((double*)param)=map_value_backward(p->tilt, -3.15, 3.15); |
| 272 | break; |
| 273 | case 3: |
| 274 | *((double*)param)=p->min; |
| 275 | break; |
| 276 | case 4: |
| 277 | *((double*)param)=p->max; |
| 278 | break; |
| 279 | case 5: |
| 280 | *((double*)param)=map_value_backward(p->op, 0.0, 4.9999); |
| 281 | break; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | //------------------------------------------------- |
| 286 | //RGBA8888 little endian |
| 287 | void f0r_update(f0r_instance_t instance, double time, const uint32_t* inframe, uint32_t* outframe) |
| 288 | { |
| 289 | inst *in; |
| 290 | int i; |
| 291 | uint32_t t; |
| 292 | |
| 293 | assert(instance); |
| 294 | in=(inst*)instance; |
| 295 | |
| 296 | switch (in->op) |
| 297 | { |
| 298 | case 0: //write on clear |
| 299 | for (i=0;i<in->h*in->w;i++) |
| 300 | outframe[i] = (inframe[i]&0x00FFFFFF) | in->gr8[i]; |
| 301 | break; |
| 302 | case 1: //max |
| 303 | for (i=0;i<in->h*in->w;i++) |
| 304 | { |
| 305 | t=((inframe[i]&0xFF000000)>in->gr8[i]) ? inframe[i]&0xFF000000 : in->gr8[i]; |
| 306 | outframe[i] = (inframe[i]&0x00FFFFFF) | t; |
| 307 | } |
| 308 | break; |
| 309 | case 2: //min |
| 310 | for (i=0;i<in->h*in->w;i++) |
| 311 | { |
| 312 | t=((inframe[i]&0xFF000000)<in->gr8[i]) ? inframe[i]&0xFF000000 : in->gr8[i]; |
| 313 | outframe[i] = (inframe[i]&0x00FFFFFF) | t; |
| 314 | } |
| 315 | break; |
| 316 | case 3: //add |
| 317 | for (i=0;i<in->h*in->w;i++) |
| 318 | { |
| 319 | t=((inframe[i]&0xFF000000)>>1)+(in->gr8[i]>>1); |
| 320 | t = (t>0x7F800000) ? 0xFF000000 : t<<1; |
| 321 | outframe[i] = (inframe[i]&0x00FFFFFF) | t; |
| 322 | } |
| 323 | break; |
| 324 | case 4: //subtract |
| 325 | for (i=0;i<in->h*in->w;i++) |
| 326 | { |
| 327 | t= ((inframe[i]&0xFF000000)>in->gr8[i]) ? (inframe[i]&0xFF000000)-in->gr8[i] : 0; |
| 328 | outframe[i] = (inframe[i]&0x00FFFFFF) | t; |
| 329 | } |
| 330 | break; |
| 331 | default: |
| 332 | break; |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | //********************************************************** |