| 1 | /* glitch0r.c |
| 2 | * Copyright (C) 2016 IDENT Software ~ http://identsoft.org |
| 3 | * Inspired by the test video on frei0r.dune.org |
| 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 <string.h> |
| 25 | #include <time.h> |
| 26 | |
| 27 | #include "frei0r.h" |
| 28 | #include "frei0r/math.h" |
| 29 | |
| 30 | /* cheap & fast randomizer (by Fukuchi Kentarou) */ |
| 31 | static uint32_t randval; |
| 32 | inline static uint32_t fastrand() { return (randval=randval*1103515245+12345); }; |
| 33 | static void fastsrand(uint32_t seed) { randval = seed; }; |
| 34 | |
| 35 | struct glitch0r_state // helps to save time when allocating in a loop |
| 36 | { |
| 37 | unsigned int currentBlock; |
| 38 | unsigned int currentPos; |
| 39 | unsigned int currentY; |
| 40 | unsigned int blkShift; |
| 41 | |
| 42 | uint32_t distortionSeed1; |
| 43 | uint32_t distortionSeed2; |
| 44 | |
| 45 | short int howToDistort1; |
| 46 | short int howToDistort2; |
| 47 | short int passThisLine; |
| 48 | }; |
| 49 | |
| 50 | typedef struct glitch0r_instance |
| 51 | { |
| 52 | unsigned int width; |
| 53 | unsigned int height; |
| 54 | |
| 55 | unsigned int maxBlockSize; |
| 56 | unsigned int maxBlockShift; |
| 57 | |
| 58 | short int colorGlitchIntensity; |
| 59 | short int doColorDistortion; |
| 60 | short int glitchChance; |
| 61 | |
| 62 | struct glitch0r_state state; // Instance-specific state |
| 63 | } glitch0r_instance_t; |
| 64 | |
| 65 | |
| 66 | inline static unsigned int rnd (unsigned int min, unsigned int max) |
| 67 | { |
| 68 | return fastrand() % (max - min + 1) + min; |
| 69 | } |
| 70 | |
| 71 | inline static void glitch0r_state_reset(glitch0r_instance_t *inst) |
| 72 | { |
| 73 | inst->state.currentPos = 0; |
| 74 | inst->state.currentBlock = rnd(1, inst->maxBlockSize); |
| 75 | inst->state.blkShift = rnd(1, inst->maxBlockShift); |
| 76 | inst->state.passThisLine = (inst->glitchChance < rnd(1, 101)) ? 1 : 0; |
| 77 | |
| 78 | if (inst->doColorDistortion) |
| 79 | { |
| 80 | inst->state.distortionSeed1 = rnd(0x00000000, 0xfffffffe); |
| 81 | inst->state.distortionSeed2 = rnd(0x00000000, 0xfffffffe); |
| 82 | inst->state.howToDistort1 = rnd (0, inst->colorGlitchIntensity); |
| 83 | inst->state.howToDistort2 = rnd (0, inst->colorGlitchIntensity); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | inline static void glitch0r_pixel_dist0rt (uint32_t *pixel, |
| 88 | uint32_t distortionSeed, short int howToDistort) |
| 89 | { |
| 90 | |
| 91 | // Save alpha |
| 92 | uint8_t *px = (uint8_t *)pixel; |
| 93 | uint8_t alpha = px[3]; |
| 94 | |
| 95 | // Choose from five levels of madness: |
| 96 | switch (howToDistort) |
| 97 | { |
| 98 | case 0 : return; // ok, let this pixel live (just shift) |
| 99 | |
| 100 | case 1 : // lightest distortion: just invert |
| 101 | *(pixel) = ~*(pixel); |
| 102 | break; |
| 103 | |
| 104 | case 2 : // add some unneeded colors |
| 105 | *(pixel) = distortionSeed | *(pixel); |
| 106 | break; |
| 107 | |
| 108 | case 3 : // change some colors |
| 109 | *(pixel) = distortionSeed ^ *(pixel); |
| 110 | break; |
| 111 | |
| 112 | case 4 : // oh shi... |
| 113 | *(pixel) = distortionSeed & *(pixel); |
| 114 | break; |
| 115 | } |
| 116 | |
| 117 | // Restore alpha |
| 118 | px[3] = alpha; |
| 119 | } |
| 120 | |
| 121 | int f0r_init() |
| 122 | { |
| 123 | fastsrand((uint32_t)time(0)); |
| 124 | return 1; |
| 125 | } |
| 126 | |
| 127 | void f0r_deinit() |
| 128 | { /* no initialization required */ } |
| 129 | |
| 130 | void f0r_get_plugin_info(f0r_plugin_info_t* glitch0rInfo) |
| 131 | { |
| 132 | glitch0rInfo->name = "Glitch0r"; |
| 133 | glitch0rInfo->author = "IDENT Software"; |
| 134 | glitch0rInfo->plugin_type = F0R_PLUGIN_TYPE_FILTER; |
| 135 | glitch0rInfo->color_model = F0R_COLOR_MODEL_RGBA8888; |
| 136 | glitch0rInfo->frei0r_version = FREI0R_MAJOR_VERSION; |
| 137 | glitch0rInfo->major_version = 0; |
| 138 | glitch0rInfo->minor_version = 1; |
| 139 | glitch0rInfo->num_params = 4; |
| 140 | glitch0rInfo->explanation = "Adds glitches and block shifting"; |
| 141 | } |
| 142 | |
| 143 | void f0r_get_param_info(f0r_param_info_t* info, int param_index) |
| 144 | { |
| 145 | switch(param_index) { |
| 146 | case 0: |
| 147 | { |
| 148 | info->name = "Glitch frequency"; |
| 149 | info->type = F0R_PARAM_DOUBLE; |
| 150 | info->explanation = "How frequently the glitch should be applied"; |
| 151 | break; |
| 152 | } |
| 153 | |
| 154 | case 1: |
| 155 | { |
| 156 | info->name = "Block height"; |
| 157 | info->type = F0R_PARAM_DOUBLE; |
| 158 | info->explanation = "Height range of the block that will be shifted/glitched"; |
| 159 | break; |
| 160 | } |
| 161 | |
| 162 | case 2: |
| 163 | { |
| 164 | info->name = "Shift intensity"; |
| 165 | info->type = F0R_PARAM_DOUBLE; |
| 166 | info->explanation = "How much we should move blocks when glitching"; |
| 167 | break; |
| 168 | } |
| 169 | |
| 170 | case 3: |
| 171 | { |
| 172 | info->name = "Color glitching intensity"; |
| 173 | info->type = F0R_PARAM_DOUBLE; |
| 174 | info->explanation = "How intensive should be color distortion"; |
| 175 | break; |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | f0r_instance_t f0r_construct(unsigned int width, unsigned int height) |
| 181 | { |
| 182 | glitch0r_instance_t* inst = (glitch0r_instance_t*)calloc(1, sizeof(*inst)); |
| 183 | inst->width = width; inst->height = height; |
| 184 | inst->maxBlockSize = (unsigned int)MAX(1, inst->height / 2); |
| 185 | inst->maxBlockShift = (unsigned int)MAX(1, inst->width / 2); |
| 186 | inst->colorGlitchIntensity = 3; |
| 187 | inst->doColorDistortion = 1; |
| 188 | |
| 189 | glitch0r_state_reset(inst); |
| 190 | |
| 191 | return (f0r_instance_t)inst; |
| 192 | } |
| 193 | |
| 194 | void f0r_destruct(f0r_instance_t instance) |
| 195 | { |
| 196 | free(instance); |
| 197 | } |
| 198 | |
| 199 | void f0r_set_param_value(f0r_instance_t instance, |
| 200 | f0r_param_t param, int param_index) |
| 201 | { |
| 202 | assert(instance); |
| 203 | glitch0r_instance_t * inst = (glitch0r_instance_t*)instance; |
| 204 | |
| 205 | switch (param_index) |
| 206 | { |
| 207 | |
| 208 | case 0 : // glitch frequency |
| 209 | { |
| 210 | double glitchChance = *((double*)param); |
| 211 | // convert frei0r range [0, 1] to range between 0 and 100 |
| 212 | glitchChance = (100 * glitchChance); |
| 213 | inst->glitchChance = (short int)glitchChance; |
| 214 | break; |
| 215 | } |
| 216 | |
| 217 | case 1 : // block height |
| 218 | { |
| 219 | double mbHeight = *((double*)param); |
| 220 | // convert frei0r range [0, 1] to range between 1 and maximal height |
| 221 | mbHeight = (1 + (inst->height - 1) * mbHeight); |
| 222 | inst->maxBlockSize = (unsigned int)mbHeight; |
| 223 | |
| 224 | // zero size is a bad idea |
| 225 | if (inst->maxBlockSize == 0) |
| 226 | inst->maxBlockSize = (unsigned int)MAX(1, inst->height / 2); |
| 227 | break; |
| 228 | } |
| 229 | |
| 230 | case 2 : // shift intensity |
| 231 | { |
| 232 | double intensity = *((double*)param); |
| 233 | // convert frei0r range [0, 1] to range between 1 and maximal width |
| 234 | intensity = (1 + (inst->width - 1) * intensity); |
| 235 | inst->maxBlockShift = (unsigned int)intensity; |
| 236 | |
| 237 | // zero shift is a bad idea |
| 238 | if (inst->maxBlockShift == 0) |
| 239 | inst->maxBlockShift = (unsigned int)MAX(1, inst->width / 2); |
| 240 | break; |
| 241 | } |
| 242 | |
| 243 | case 3 : // color intensity |
| 244 | { |
| 245 | double intensity = *((double*)param); |
| 246 | // convert frei0r range [0, 1] to range between 0 and 5 |
| 247 | intensity = (5 * intensity); |
| 248 | inst->colorGlitchIntensity = (short int)intensity; |
| 249 | |
| 250 | if (inst->colorGlitchIntensity > 0) |
| 251 | { |
| 252 | inst->doColorDistortion = 1; |
| 253 | } |
| 254 | else |
| 255 | inst->doColorDistortion = 0; |
| 256 | |
| 257 | break; |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | void f0r_get_param_value(f0r_instance_t instance, |
| 263 | f0r_param_t param, int param_index) |
| 264 | { |
| 265 | |
| 266 | assert(instance); |
| 267 | glitch0r_instance_t * inst = (glitch0r_instance_t*)instance; |
| 268 | |
| 269 | switch (param_index) |
| 270 | { |
| 271 | case 0 : // glitch frequency |
| 272 | { |
| 273 | // convert plugin's param to frei0r range |
| 274 | *((double*)param) = (inst->glitchChance) / 100; |
| 275 | break; |
| 276 | } |
| 277 | |
| 278 | case 1 : // block height |
| 279 | { |
| 280 | // convert plugin's param to frei0r range |
| 281 | *((double*)param) = (inst->maxBlockSize - 1.0) / MAX(1, inst->height - 1.0); |
| 282 | break; |
| 283 | } |
| 284 | |
| 285 | case 2 : // block shift intensity |
| 286 | { |
| 287 | *((double*)param) = (inst->maxBlockShift - 1.0) / MAX(1, inst->width - 1.0); |
| 288 | break; |
| 289 | } |
| 290 | |
| 291 | case 3 : // color glitch intensity |
| 292 | { |
| 293 | *((double*)param) = (inst->colorGlitchIntensity) / 5; // 5 levels of madness |
| 294 | break; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | } |
| 299 | |
| 300 | void f0r_update(f0r_instance_t instance, double time, |
| 301 | const uint32_t* inframe, uint32_t* outframe) |
| 302 | { |
| 303 | assert(instance); |
| 304 | glitch0r_instance_t* inst = (glitch0r_instance_t*)instance; |
| 305 | unsigned int x, y; |
| 306 | |
| 307 | uint32_t* dst = outframe; |
| 308 | const uint32_t* src = inframe; |
| 309 | uint32_t *pixel; |
| 310 | |
| 311 | inst->state.currentBlock = rnd(1, inst->maxBlockSize); |
| 312 | |
| 313 | for (y = 0; y < inst->height; y++) |
| 314 | { |
| 315 | |
| 316 | if (inst->state.currentPos > inst->state.currentBlock) |
| 317 | { |
| 318 | glitch0r_state_reset(inst); |
| 319 | } |
| 320 | else |
| 321 | inst->state.currentPos++; |
| 322 | |
| 323 | inst->state.currentY = y*inst->width; |
| 324 | pixel = dst + inst->state.currentY; |
| 325 | |
| 326 | if (inst->state.passThisLine) |
| 327 | { |
| 328 | memcpy((uint32_t *)(dst + inst->state.currentY), |
| 329 | (uint32_t *)(src + inst->state.currentY), |
| 330 | (inst->width) * sizeof(uint32_t)); |
| 331 | continue; |
| 332 | } |
| 333 | |
| 334 | for (x = inst->state.blkShift; x < (inst->width); x++) |
| 335 | { |
| 336 | *(pixel) = *(src + inst->state.currentY + x); |
| 337 | |
| 338 | if (inst->doColorDistortion) |
| 339 | glitch0r_pixel_dist0rt(pixel, |
| 340 | inst->state.distortionSeed1, inst->state.howToDistort1); |
| 341 | |
| 342 | pixel++; |
| 343 | } |
| 344 | |
| 345 | for (x = 0; x < inst->state.blkShift; x++) |
| 346 | { |
| 347 | *(pixel) = *(src + inst->state.currentY + x); |
| 348 | |
| 349 | if (inst->doColorDistortion) |
| 350 | glitch0r_pixel_dist0rt(pixel, |
| 351 | inst->state.distortionSeed2, inst->state.howToDistort2); |
| 352 | |
| 353 | pixel++; |
| 354 | } |
| 355 | |
| 356 | } |
| 357 | } |
| 358 |