Recently I've been looking at Povray, pyprocessing, and cfdg (version 3.0) as tools for creating digital images. I have branched two separate blogs where I mainly explore jruby + processing and processing.py

Sunday, 8 March 2009

Simple Image Warping

Here is something I tried in processing earlier a bit of image warping, reminds me a bit of Uri Gellers spoon bending!!!
(guts of code stolen from processing discourse board attributed to dpgc)
Code Revised 11 March now that I'm using image as background image size must match frame size:-
   1:PImage source, destination;
2:
3:void setup()
4:{
5: size(220,300);
6: source = loadImage("warping.jpg"); // fill in your own image here
7: frameRate(3);
8:}
9:
10:void draw()
11:{
12: if (frameCount < 10)
13: {
14: background(source) ;
15: }
16: if (frameCount > 200)
17: {
18: exit();
19: }
20: else{
21: destination = warp(source, frameCount);
22: background(destination);
23: }
24:}
25:
26:// implement a simple vertical wave warp.
27:PImage warp(PImage source, int count)
28:{
29: float waveAmplitude = count/5, // pixels
30: numWaves = count % 5; // how many full wave cycles to run down the image
31: int w = source.width, h = source.height;
32: PImage destination = new PImage(w,h);
33: source.loadPixels();
34: destination.loadPixels();
35:
36: float yToPhase = 2*PI*numWaves / h; // conversion factor from y values to radians.
37:
38: for(int x = 0; x < w; x++)
39: for(int y = 0; y < h; y++)
40: {
41: int newX, newY;
42: newX = int(x + waveAmplitude*sin(y * yToPhase));
43: newY = y;
44: color c;
45: if(newX >= w || newX < 0 ||
46: newY >= h || newY < 0)
47: c = color(0,0,0);
48: else
49: c = source.pixels[newY*w + newX];
50: destination.pixels[y*w+x] = c;
51: }
52: return destination;
53:}

No comments:

Post a Comment

Followers

Blog Archive

About Me

My photo
Pembrokeshire, United Kingdom
I have developed JRubyArt and propane new versions of ruby-processing for JRuby-9.1.5.0 and processing-3.2.2