Advertisement
Guest User

Timedistortion

a guest
Feb 25th, 2014
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.25 KB | None | 0 0
  1. import processing.video.*;
  2. int w;
  3. int h;
  4. Movie movie;
  5. float[] time;
  6. int frame;
  7. int rate = 30;
  8. float fr = 1.0 / 30.0;
  9. int drawing = 0;
  10. PImage[] historyBuffer;
  11. int historyIndex;
  12.  
  13. int maxdelay = 128;
  14.  
  15. void setup()
  16. {
  17.   historyBuffer = new PImage[maxdelay + 8];
  18.   initHistory();
  19.  
  20.   movie = new Movie(this, "8.mp4");
  21.   movie.loop();
  22.   movie.pause();
  23.   frameRate(rate);
  24.  
  25.   w = movie.width;
  26.   h = movie.height;
  27.   if(w == 0) w = 640;
  28.   if(h == 0) h = 480;
  29.   size(w, h);
  30.  
  31.   time = new float[w * h];
  32.  
  33.   noLoop();
  34. }
  35.  
  36. void draw()
  37. {
  38. }
  39.  
  40. void mouseClicked()
  41. {
  42.   gen();
  43.   frame = 0;
  44.  
  45.   initHistory();
  46.  
  47.   print("Skipping...");
  48.   float o = random(movie.duration() - fr * 256.0);
  49.   movie.jump(o);
  50.   loop();
  51.   movie.play();
  52.  
  53.   print("...skipped");
  54. }
  55.  
  56. void initHistory()
  57. {  
  58.   historyIndex = 0;
  59.  
  60.   for(int i = 0; i < maxdelay + 8; i++)    
  61.     historyBuffer[i] = createImage(w, h, ARGB);
  62. }
  63.  
  64. void movieEvent(Movie m)
  65. {
  66.   m.read();
  67.   genFrame();  
  68.   frame++;
  69. }
  70.  
  71. void genFrame()
  72. {
  73.   PImage p = movie.get(0, 0, w, h);  
  74.  
  75.   historyBuffer[historyIndex] = p;
  76.   historyIndex = (historyIndex + 1) % maxdelay;
  77.  
  78.   int pi = 0;
  79.  
  80.   for(int y = 0; y < h; y++)
  81.   {
  82.     for(int x = 0; x < w; x++)
  83.     {
  84.       float baseDelay = time[x + y * w];      
  85.       int delay = (int)baseDelay;
  86.       float lerpFactor = baseDelay - delay;
  87.       int index = (historyIndex - delay) % maxdelay;
  88.       if(index < 0) index += maxdelay;      
  89.      
  90.       color pix1;
  91.       color pix2;
  92.       pix1 = historyBuffer[index].pixels[pi] & 0xffff0000;
  93.       pix1 += historyBuffer[(index + 1) % maxdelay].pixels[pi] & 0x0000ff00;
  94.       pix2 = historyBuffer[(index + 1) % maxdelay].pixels[pi] & 0xffff0000;
  95.       pix1 += historyBuffer[(index + 2) % maxdelay].pixels[pi] & 0x000000ff;
  96.       pix2 += historyBuffer[(index + 2) % maxdelay].pixels[pi] & 0x0000ff00;
  97.       pix2 += historyBuffer[(index + 3) % maxdelay].pixels[pi] & 0x000000ff;
  98.       pi++;
  99.       set(x, y, lerpColor(pix2, pix1, lerpFactor));
  100.     }
  101.   }
  102. }
  103.  
  104. color scale(color c, float s)
  105. {
  106.   return color(red(c) * s, green(c) * s, blue(c) * s);
  107. }
  108.  
  109. color add(color a, color b, float red, float grn, float blu)
  110. {
  111.   return color(red(a) * red + red(b), green(a) * grn + green(b), blue(a) * blu + blue(b));
  112. }
  113.  
  114. void genClear(int v)
  115. {
  116.   for(int i = 0; i < w * h; i++) time[i] = v;
  117. }
  118.  
  119. void genAddNoise(float a)
  120. {
  121.   for(int i = 0; i < w * h; i++) time[i] += random(a);
  122. }
  123.  
  124. void genPixelate(int sh, int sv)
  125. {
  126.   float v = 0;
  127.   int ly = 0;
  128.   sh += 1;
  129.   sv += 1;
  130.  
  131.   for(int y = 0; y < h; y++)
  132.   {
  133.     if(y % sh == 0) ly = y;
  134.    
  135.     for(int x = 0; x < w; x++)
  136.     {
  137.       if(x % sv == 0) v = time[x + ly * w];
  138.       time[x + y * w] = v;
  139.     }
  140.   }
  141. }
  142.  
  143. void genScale(float maxDist)
  144. {
  145.   float max = 0;
  146.   float min = maxDist;
  147.   for(int y = 10; y < h - 10; y++)
  148.   {    
  149.     for(int x = 10; x < w - 10; x++)
  150.     {
  151.       if(time[x + y * w] > max) max = time[x + y * w];
  152.       if(time[x + y * w] < min) min = time[x + y * w];
  153.     }  
  154.   }
  155.  
  156.   float scl = maxDist / (max - min);
  157.  
  158.   for(int i = 0; i < w * h; i++)
  159.   {
  160.      float v = (time[i] - min) * scl;
  161.      if(v > maxDist) v = maxDist;
  162.      if(v < 0) v = 0;
  163.      time[i] = v + 4;
  164.   }
  165. }
  166.  
  167. void genBlur(int radius)
  168. {
  169.   radius = radius / 2;
  170.   for(int i = 0; i < 3; i++)
  171.     genBoxBlur(radius);
  172. }
  173.  
  174. void genBoxBlur(int radius)
  175. {
  176.   float[] buff2 = new float[w * h];
  177.   float rec = 0.5 / radius;
  178.  
  179.   for(int y = 0; y < h; y++)
  180.   {
  181.     float total = 0;
  182.    
  183.     for(int x = -radius; x < w + radius; x++)
  184.     {
  185.       if(x + radius < w && x + radius >= 0) total += time[x + radius + y * w];
  186.       if(x - radius >= 0 && x - radius < w) total -= time[x - radius + y * w];
  187.       if(x >= 0 && x < w) buff2[x + y * w] = total * rec;    
  188.     }
  189.   }
  190.  
  191.   for(int x = 0; x < w; x++)
  192.   {
  193.     float total = 0;
  194.    
  195.     for(int y = -radius; y < h + radius; y++)
  196.     {
  197.       if(y + radius < h && y + radius >= 0) total += buff2[x + (y + radius) * w];
  198.       if(y - radius >= 0 && y - radius < h) total -= buff2[x + (y - radius) * w];
  199.       if(y >= 0 && y < h) time[x + y * w] = total * rec;
  200.     }
  201.   }
  202. }
  203.  
  204. int irand(int max)
  205. {
  206.   return ((int)random(max * 100)) % max;
  207. }
  208.  
  209. void gen()
  210. {
  211.   print("Generating map...");
  212.   genClear(0);
  213.  
  214.   int r = irand(6);  
  215.   switch(r)
  216.   {
  217.     case 0: genSines(); break;
  218.     case 1: genHSines(); break;
  219.     case 2: genVSines(); break;
  220.     case 3: genRandom(); break;
  221.     case 4: genSlitScanH(); break;
  222.     case 5: genSlitScanV(); break;
  223.   }
  224.  
  225.   r = irand(10);  
  226.   switch(r)
  227.   {
  228.     case 0: genPixelate(irand(32), irand(32)); break;
  229.     case 1: case 2: { int s = irand(32); genPixelate(s, s); break; }
  230.     case 3: genAddNoise(random(16.0)); break;
  231.   }
  232.  
  233.   if(irand(5) == 0) genBlur(irand(64) + 1);
  234.   genScale(random(maxdelay));
  235.  
  236.   print("...generated");
  237. }
  238.  
  239. void genSlitScanH()
  240. {
  241.   for(int y = 0; y < h; y++)
  242.   {
  243.     for(int x = 0; x < w; x++)
  244.     {
  245.       time[x + y * w] = y;
  246.     }
  247.   }
  248. }
  249.  
  250. void genSlitScanV()
  251. {
  252.   for(int y = 0; y < h; y++)
  253.   {
  254.     for(int x = 0; x < w; x++)
  255.     {
  256.       time[x + y * w] = x;
  257.     }
  258.   }
  259. }
  260.  
  261. void genRandom()
  262. {
  263.   for(int i = 0; i < w * h; i++)
  264.     time[i] = random(255);
  265. }
  266.  
  267. void genSines()
  268. {
  269.   genClear(128);
  270.   genVSines();
  271.   genHSines();
  272. }
  273.  
  274. void genVSines()
  275. {
  276.   int partials = irand(30);
  277.   float[] fs = new float[partials];
  278.   float[] ps = new float[partials];
  279.   float[] as = new float[partials];
  280.  
  281.   for(int i = 0; i < partials; i++)
  282.   {
  283.     fs[i] = random(0.05);
  284.     ps[i] = random(6.28);
  285.     as[i] = random(32.0);
  286.   }
  287.  
  288.   for(int i = 0; i < partials; i++)
  289.   {
  290.     for(int x = 0; x < w; x++)
  291.     {
  292.       ps[i] += fs[i];
  293.      
  294.       for(int y = 0; y < h; y++)
  295.       {
  296.         time[x + y * w] += sin(ps[i]) * as[i];
  297.       }
  298.     }
  299.   }
  300. }
  301.  
  302. void genHSines()
  303. {
  304.   float[] fs = new float[10];
  305.   float[] ps = new float[10];
  306.   float[] as = new float[10];
  307.  
  308.   for(int i = 0; i < 10; i++)
  309.   {
  310.     fs[i] = random(0.05);
  311.     ps[i] = random(6.28);
  312.     as[i] = random(32.0);
  313.   }
  314.  
  315.   for(int i = 0; i < 10; i++)
  316.   {
  317.     for(int y = 0; y < h; y++)
  318.     {
  319.       ps[i] += fs[i];
  320.      
  321.       for(int x = 0; x < w; x++)
  322.       {
  323.         time[x + y * w] += sin(ps[i]) * as[i];
  324.       }
  325.     }
  326.   }
  327. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement