| 1 | /* |
| 2 | Delay Grab video effect |
| 3 | |
| 4 | blockwise, controllable image delay |
| 5 | |
| 6 | Copyright (C) 1999/2000 A. Schiffler <[email protected]> |
| 7 | Copyright (C) 2001/2002 Denis Roio <[email protected]> |
| 8 | |
| 9 | original sourcecode is from libbgrab 2.1f |
| 10 | ported to FreeJ, successively modified |
| 11 | then ported to frei0r |
| 12 | |
| 13 | This program is free software; you can redistribute it and/or modify |
| 14 | it under the terms of the GNU General Public License as published by |
| 15 | the Free Software Foundation; either version 3 of the License, or |
| 16 | (at your option) any later version. |
| 17 | |
| 18 | This program is distributed in the hope that it will be useful, but |
| 19 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 21 | General Public License for more details. |
| 22 | |
| 23 | You should have received a copy of the GNU General Public License |
| 24 | along with this program; if not, write to the Free Software |
| 25 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 26 | |
| 27 | */ |
| 28 | |
| 29 | |
| 30 | #include <stdio.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <string.h> |
| 33 | #include <math.h> |
| 34 | #include <time.h> |
| 35 | #include <inttypes.h> |
| 36 | |
| 37 | #include <frei0r.hpp> |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | #define QUEUEDEPTH 71 /* was 76 */ |
| 43 | #define MODES 4 |
| 44 | |
| 45 | // freej compat facilitator |
| 46 | typedef struct { |
| 47 | int16_t x; ///< x axis position coordinate |
| 48 | int16_t y; ///< y axis position coordinate |
| 49 | uint16_t w; ///< width of frame in pixels |
| 50 | uint16_t h; ///< height of frame in pixels |
| 51 | uint8_t bpp; ///< bits per pixel |
| 52 | uint16_t pitch; ///< width of frame in bytes |
| 53 | uint32_t size; ///< size of the whole frame in bytes |
| 54 | } ScreenGeometry; |
| 55 | |
| 56 | class DelayGrab: public frei0r::filter { |
| 57 | |
| 58 | public: |
| 59 | DelayGrab(int wdt, int hgt); |
| 60 | ~DelayGrab(); |
| 61 | |
| 62 | virtual void update(double time, |
| 63 | uint32_t* out, |
| 64 | const uint32_t* in); |
| 65 | |
| 66 | |
| 67 | private: |
| 68 | |
| 69 | ScreenGeometry geo; |
| 70 | |
| 71 | void _init(int wdt, int hgt); |
| 72 | |
| 73 | void createDelaymap(int mode); |
| 74 | void set_blocksize(int bs); |
| 75 | int isqrt(unsigned int x); |
| 76 | |
| 77 | /* cheap & fast randomizer (by Fukuchi Kentarou) */ |
| 78 | uint32_t randval; |
| 79 | uint32_t fastrand() { return (randval=randval*1103515245+12345); }; |
| 80 | void fastsrand(uint32_t seed) { randval = seed; }; |
| 81 | |
| 82 | int x,y,i,xyoff,v; |
| 83 | uint8_t *imagequeue,*curqueue; |
| 84 | int curqueuenum; |
| 85 | uint32_t *curdelaymap; |
| 86 | uint8_t *curpos,*curimage; |
| 87 | int curposnum; |
| 88 | void *delaymap; |
| 89 | |
| 90 | /* initialized from the init */ |
| 91 | int delaymapwidth; /* width/blocksize */ |
| 92 | int delaymapheight; /* height/blocksize */ |
| 93 | int delaymapsize; /* delaymapheight*delaymapwidth */ |
| 94 | |
| 95 | int blocksize; |
| 96 | int block_per_pitch; |
| 97 | int block_per_bytespp; |
| 98 | int block_per_res; |
| 99 | |
| 100 | int current_mode; |
| 101 | |
| 102 | }; |
| 103 | |
| 104 | void DelayGrab::_init(int wdt, int hgt) { |
| 105 | geo.w = wdt; |
| 106 | geo.h = hgt; |
| 107 | geo.bpp = 32; |
| 108 | geo.size = geo.w*geo.h*(geo.bpp/8); |
| 109 | geo.pitch = geo.w*(geo.bpp/8); |
| 110 | } |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | DelayGrab::DelayGrab(int wdt, int hgt) { |
| 116 | |
| 117 | delaymap = NULL; |
| 118 | _init(wdt, hgt); |
| 119 | |
| 120 | imagequeue = (uint8_t *) malloc(QUEUEDEPTH*(geo.size)); |
| 121 | |
| 122 | /* starting mode */ |
| 123 | current_mode = 4; |
| 124 | /* starting blocksize */ |
| 125 | set_blocksize(2); |
| 126 | |
| 127 | curqueue=imagequeue; |
| 128 | curqueuenum=0; |
| 129 | |
| 130 | fastsrand(::time(NULL)); |
| 131 | } |
| 132 | |
| 133 | DelayGrab::~DelayGrab() { |
| 134 | free(delaymap); |
| 135 | free(imagequeue); |
| 136 | } |
| 137 | |
| 138 | |
| 139 | |
| 140 | void DelayGrab::update(double time, |
| 141 | uint32_t* out, |
| 142 | const uint32_t* in) { |
| 143 | |
| 144 | /* Update queue pointer */ |
| 145 | if (curqueuenum==0) { |
| 146 | curqueuenum=QUEUEDEPTH-1; |
| 147 | curqueue = imagequeue; |
| 148 | curqueue += (geo.size*(QUEUEDEPTH-1)); |
| 149 | } else { |
| 150 | curqueuenum--; |
| 151 | curqueue -= geo.size; |
| 152 | } |
| 153 | |
| 154 | /* Copy image to queue */ |
| 155 | memcpy(curqueue,in,geo.size); |
| 156 | |
| 157 | /* Copy image blockwise to screenbuffer */ |
| 158 | curdelaymap= (uint32_t *)delaymap; |
| 159 | for (y=0; y<delaymapheight; y++) { |
| 160 | for (x=0; x<delaymapwidth; x++) { |
| 161 | |
| 162 | curposnum=((curqueuenum + (*curdelaymap)) % QUEUEDEPTH); |
| 163 | |
| 164 | xyoff= (x*block_per_bytespp) + (y*block_per_pitch); |
| 165 | /* source */ |
| 166 | curpos= imagequeue; |
| 167 | curpos += (geo.size*curposnum); |
| 168 | curpos += xyoff; |
| 169 | /* target */ |
| 170 | curimage = (uint8_t *)out; |
| 171 | curimage += xyoff; |
| 172 | /* copy block */ |
| 173 | for (i=0; i<blocksize; i++) { |
| 174 | memcpy(curimage,curpos,block_per_res); |
| 175 | curpos += geo.pitch; |
| 176 | curimage += geo.pitch; |
| 177 | } |
| 178 | curdelaymap++; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | } |
| 183 | |
| 184 | // int kbd_input(char key) { |
| 185 | // int res = 1; |
| 186 | // switch(key) { |
| 187 | // case 'w': |
| 188 | // if(current_mode<MODES) createDelaymap(current_mode+1); |
| 189 | // break; |
| 190 | // case 'q': |
| 191 | // if(current_mode>1) createDelaymap(current_mode-1); |
| 192 | // break; |
| 193 | // case 's': |
| 194 | // set_blocksize(blocksize+1); |
| 195 | // break; |
| 196 | // case 'a': |
| 197 | // if(blocksize>2) set_blocksize(blocksize-1); |
| 198 | // break; |
| 199 | // default: |
| 200 | // res = 0; |
| 201 | // break; |
| 202 | // } |
| 203 | // return res; |
| 204 | // } |
| 205 | |
| 206 | void DelayGrab::createDelaymap(int mode) { |
| 207 | double d; |
| 208 | |
| 209 | curdelaymap=(uint32_t *)delaymap; |
| 210 | fastsrand(::time(NULL)); |
| 211 | |
| 212 | for (y=delaymapheight; y>0; y--) { |
| 213 | for (x=delaymapwidth; x>0; x--) { |
| 214 | switch (mode) { |
| 215 | case 1: |
| 216 | /* Random delay with square distribution */ |
| 217 | d = (double)fastrand()/(double)RAND_MAX; |
| 218 | *curdelaymap = (int)(d*d*16.0); |
| 219 | break; |
| 220 | case 2: |
| 221 | /* Vertical stripes of increasing delay outward from center */ |
| 222 | if (x<(delaymapwidth/2)) { |
| 223 | v=(delaymapwidth/2)-x; |
| 224 | } else if (x>(delaymapwidth/2)) { |
| 225 | v=x-(delaymapwidth/2); |
| 226 | } else { |
| 227 | v=0; |
| 228 | } |
| 229 | *curdelaymap=v/2; |
| 230 | break; |
| 231 | case 3: |
| 232 | /* Horizontal stripes of increasing delay outward from center */ |
| 233 | if(y<(delaymapheight/2)) { |
| 234 | v = (delaymapheight/2)-y; |
| 235 | } else if(y>(delaymapheight/2)) { |
| 236 | v = y-(delaymapheight/2); |
| 237 | } else { |
| 238 | v=0; |
| 239 | } |
| 240 | *curdelaymap=v/2; |
| 241 | break; |
| 242 | case 4: |
| 243 | /* Rings of increasing delay outward from center */ |
| 244 | v = (int)isqrt((unsigned int)((x-(delaymapwidth/2))* |
| 245 | (x-(delaymapwidth/2))+ |
| 246 | (y-(delaymapheight/2))* |
| 247 | (y-(delaymapheight/2)))); |
| 248 | *curdelaymap=v/2; |
| 249 | break; |
| 250 | } // switch |
| 251 | /* Clip values */ |
| 252 | if ((int)(*curdelaymap)<0) { |
| 253 | *curdelaymap=0; |
| 254 | } else if (*curdelaymap>(QUEUEDEPTH-1)) { |
| 255 | *curdelaymap=(QUEUEDEPTH-1); |
| 256 | } |
| 257 | curdelaymap++; |
| 258 | } |
| 259 | } |
| 260 | current_mode = mode; |
| 261 | } |
| 262 | |
| 263 | void DelayGrab::set_blocksize(int bs) { |
| 264 | |
| 265 | blocksize = bs; |
| 266 | block_per_pitch = blocksize*(geo.pitch); |
| 267 | block_per_bytespp = blocksize*(geo.bpp>>3); |
| 268 | block_per_res = blocksize<<(geo.bpp>>4); |
| 269 | |
| 270 | delaymapwidth = (geo.w)/blocksize; |
| 271 | delaymapheight = (geo.h)/blocksize; |
| 272 | delaymapsize = delaymapheight*delaymapwidth; |
| 273 | |
| 274 | free(delaymap); |
| 275 | delaymap = malloc(delaymapsize*4); |
| 276 | |
| 277 | createDelaymap(current_mode); |
| 278 | } |
| 279 | |
| 280 | /* i learned this on books // by jaromil */ |
| 281 | int DelayGrab::isqrt(unsigned int x) { |
| 282 | unsigned int m, y, b; m = 0x40000000; |
| 283 | y = 0; while(m != 0) { b = y | m; y = y>>1; |
| 284 | if(x>=b) { x=x-b; y=y|m; } |
| 285 | m=m>>2; } return y; |
| 286 | } |
| 287 | |
| 288 | |
| 289 | |
| 290 | frei0r::construct<DelayGrab> plugin("Delaygrab", |
| 291 | "delayed frame blitting mapped on a time bitmap", |
| 292 | "Bill Spinhover, Andreas Schiffler, Jaromil", |
| 293 | 3,1); |