开发者

Implement ripple effect in C given an array of pixels

开发者 https://www.devze.com 2023-04-03 20:10 出处:网络
I have to implement a ripple effect on an array of pixels. Each pixel is a 32 bit integer representing an ARGB color. Does anyone have any 开发者_StackOverflow社区suggestions on how to start?Generally

I have to implement a ripple effect on an array of pixels. Each pixel is a 32 bit integer representing an ARGB color. Does anyone have any 开发者_StackOverflow社区suggestions on how to start?


Generally, a ripple effect is some kind of distortion of the image where the amount of distortion varies from point to point in a "wavy" pattern. So the first task is to create a "depth mapping" producing a number for each pixel. Intuitively the depth map will represent the height of a rippled water surface above the original pixels. A starting point for experimentation could be

#define X_CENTER 100   // x pixel position of center
#define Y_CENTER 100   // y pixel position of center
#define RADIUS 70      // approximate radius of circular wave train, in pixels
#define WAVELENGTH 10  // wavelength of ripples, in pixels
#define TRAINWIDTH 3.4 // approximate width of wave train, in wavelengths
#define SUPERPHASE 1.5 // phase vel. / group vel. (irrelevant for stills)

// returns a number from -1.0 to 1.0
double depth(int x, int y) {
  double dx = x - X_CENTER ; // or int, if the center coords are ints
  double dy = y - Y_CENTER ;
  double r = (sqrt(dx*dx+dy*dy)-RADIUS)/WAVELENGTH ;
  double k = r - (1-SUPERPHASE)*RADIUS/WAVELENGTH ;
  double a = 1 / (1.0 + (r/TRAINWIDTH)*(r/TRAINWIDTH));
  return a * sin(k*2*PI);
}

When you have the depth map (which can be precomputed if all you want is to apply the same ripple to many different images), you have various options for what to do with it:

  • lighten or darken each pixel in proportion to its depth
  • shift the depth map a few pixels diagonally, and lighten or darken according to the difference between the shifted and unshifted map.
  • move each pixel slightly in the image, by a vector proportional to the difference between each pixel's depth and that of its (say) east and south neighbors. Some smoothing or oversampling may be necessary for a visually pleasing result especially if your base image contains crisp axis-parallel edges.

It may take some experimentation with combinations and variations of these to approximate your mental picture of the desired effect.

0

精彩评论

暂无评论...
验证码 换一张
取 消