Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import processing.video.*;
- int w;
- int h;
- Movie movie;
- float[] time;
- int frame;
- int rate = 30;
- float fr = 1.0 / 30.0;
- int drawing = 0;
- PImage[] historyBuffer;
- int historyIndex;
- int maxdelay = 128;
- void setup()
- {
- historyBuffer = new PImage[maxdelay + 8];
- initHistory();
- movie = new Movie(this, "8.mp4");
- movie.loop();
- movie.pause();
- frameRate(rate);
- w = movie.width;
- h = movie.height;
- if(w == 0) w = 640;
- if(h == 0) h = 480;
- size(w, h);
- time = new float[w * h];
- noLoop();
- }
- void draw()
- {
- }
- void mouseClicked()
- {
- gen();
- frame = 0;
- initHistory();
- print("Skipping...");
- float o = random(movie.duration() - fr * 256.0);
- movie.jump(o);
- loop();
- movie.play();
- print("...skipped");
- }
- void initHistory()
- {
- historyIndex = 0;
- for(int i = 0; i < maxdelay + 8; i++)
- historyBuffer[i] = createImage(w, h, ARGB);
- }
- void movieEvent(Movie m)
- {
- m.read();
- genFrame();
- frame++;
- }
- void genFrame()
- {
- PImage p = movie.get(0, 0, w, h);
- historyBuffer[historyIndex] = p;
- historyIndex = (historyIndex + 1) % maxdelay;
- int pi = 0;
- for(int y = 0; y < h; y++)
- {
- for(int x = 0; x < w; x++)
- {
- float baseDelay = time[x + y * w];
- int delay = (int)baseDelay;
- float lerpFactor = baseDelay - delay;
- int index = (historyIndex - delay) % maxdelay;
- if(index < 0) index += maxdelay;
- color pix1;
- color pix2;
- pix1 = historyBuffer[index].pixels[pi] & 0xffff0000;
- pix1 += historyBuffer[(index + 1) % maxdelay].pixels[pi] & 0x0000ff00;
- pix2 = historyBuffer[(index + 1) % maxdelay].pixels[pi] & 0xffff0000;
- pix1 += historyBuffer[(index + 2) % maxdelay].pixels[pi] & 0x000000ff;
- pix2 += historyBuffer[(index + 2) % maxdelay].pixels[pi] & 0x0000ff00;
- pix2 += historyBuffer[(index + 3) % maxdelay].pixels[pi] & 0x000000ff;
- pi++;
- set(x, y, lerpColor(pix2, pix1, lerpFactor));
- }
- }
- }
- color scale(color c, float s)
- {
- return color(red(c) * s, green(c) * s, blue(c) * s);
- }
- color add(color a, color b, float red, float grn, float blu)
- {
- return color(red(a) * red + red(b), green(a) * grn + green(b), blue(a) * blu + blue(b));
- }
- void genClear(int v)
- {
- for(int i = 0; i < w * h; i++) time[i] = v;
- }
- void genAddNoise(float a)
- {
- for(int i = 0; i < w * h; i++) time[i] += random(a);
- }
- void genPixelate(int sh, int sv)
- {
- float v = 0;
- int ly = 0;
- sh += 1;
- sv += 1;
- for(int y = 0; y < h; y++)
- {
- if(y % sh == 0) ly = y;
- for(int x = 0; x < w; x++)
- {
- if(x % sv == 0) v = time[x + ly * w];
- time[x + y * w] = v;
- }
- }
- }
- void genScale(float maxDist)
- {
- float max = 0;
- float min = maxDist;
- for(int y = 10; y < h - 10; y++)
- {
- for(int x = 10; x < w - 10; x++)
- {
- if(time[x + y * w] > max) max = time[x + y * w];
- if(time[x + y * w] < min) min = time[x + y * w];
- }
- }
- float scl = maxDist / (max - min);
- for(int i = 0; i < w * h; i++)
- {
- float v = (time[i] - min) * scl;
- if(v > maxDist) v = maxDist;
- if(v < 0) v = 0;
- time[i] = v + 4;
- }
- }
- void genBlur(int radius)
- {
- radius = radius / 2;
- for(int i = 0; i < 3; i++)
- genBoxBlur(radius);
- }
- void genBoxBlur(int radius)
- {
- float[] buff2 = new float[w * h];
- float rec = 0.5 / radius;
- for(int y = 0; y < h; y++)
- {
- float total = 0;
- for(int x = -radius; x < w + radius; x++)
- {
- if(x + radius < w && x + radius >= 0) total += time[x + radius + y * w];
- if(x - radius >= 0 && x - radius < w) total -= time[x - radius + y * w];
- if(x >= 0 && x < w) buff2[x + y * w] = total * rec;
- }
- }
- for(int x = 0; x < w; x++)
- {
- float total = 0;
- for(int y = -radius; y < h + radius; y++)
- {
- if(y + radius < h && y + radius >= 0) total += buff2[x + (y + radius) * w];
- if(y - radius >= 0 && y - radius < h) total -= buff2[x + (y - radius) * w];
- if(y >= 0 && y < h) time[x + y * w] = total * rec;
- }
- }
- }
- int irand(int max)
- {
- return ((int)random(max * 100)) % max;
- }
- void gen()
- {
- print("Generating map...");
- genClear(0);
- int r = irand(6);
- switch(r)
- {
- case 0: genSines(); break;
- case 1: genHSines(); break;
- case 2: genVSines(); break;
- case 3: genRandom(); break;
- case 4: genSlitScanH(); break;
- case 5: genSlitScanV(); break;
- }
- r = irand(10);
- switch(r)
- {
- case 0: genPixelate(irand(32), irand(32)); break;
- case 1: case 2: { int s = irand(32); genPixelate(s, s); break; }
- case 3: genAddNoise(random(16.0)); break;
- }
- if(irand(5) == 0) genBlur(irand(64) + 1);
- genScale(random(maxdelay));
- print("...generated");
- }
- void genSlitScanH()
- {
- for(int y = 0; y < h; y++)
- {
- for(int x = 0; x < w; x++)
- {
- time[x + y * w] = y;
- }
- }
- }
- void genSlitScanV()
- {
- for(int y = 0; y < h; y++)
- {
- for(int x = 0; x < w; x++)
- {
- time[x + y * w] = x;
- }
- }
- }
- void genRandom()
- {
- for(int i = 0; i < w * h; i++)
- time[i] = random(255);
- }
- void genSines()
- {
- genClear(128);
- genVSines();
- genHSines();
- }
- void genVSines()
- {
- int partials = irand(30);
- float[] fs = new float[partials];
- float[] ps = new float[partials];
- float[] as = new float[partials];
- for(int i = 0; i < partials; i++)
- {
- fs[i] = random(0.05);
- ps[i] = random(6.28);
- as[i] = random(32.0);
- }
- for(int i = 0; i < partials; i++)
- {
- for(int x = 0; x < w; x++)
- {
- ps[i] += fs[i];
- for(int y = 0; y < h; y++)
- {
- time[x + y * w] += sin(ps[i]) * as[i];
- }
- }
- }
- }
- void genHSines()
- {
- float[] fs = new float[10];
- float[] ps = new float[10];
- float[] as = new float[10];
- for(int i = 0; i < 10; i++)
- {
- fs[i] = random(0.05);
- ps[i] = random(6.28);
- as[i] = random(32.0);
- }
- for(int i = 0; i < 10; i++)
- {
- for(int y = 0; y < h; y++)
- {
- ps[i] += fs[i];
- for(int x = 0; x < w; x++)
- {
- time[x + y * w] += sin(ps[i]) * as[i];
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement