Android algorithm Watermark

Source: Internet
Author: User

Basic knowledge:

Before explaining the code, let's review what we learned about water waves in physics classes in high school. Water waves have several properties: diffusion, attenuation, refraction, reflection, diffraction, etc.

diffusion : When you cast a stone into the water, you will see a circle of water waves formed by the point of the stone into the center, where you may be misled by the phenomenon that every point on the water waves is spread out in the direction of the point of the stone, which is wrong. In fact, any point on the water is spread around at all times by itself, and the reason for the formation of a circular wave is that the interior of the water waves is offset by the symmetry of the diffusion.

attenuation : Because the water is damp, otherwise, when you put stones in the pool, the waves will never stop the concussion.

refraction : Because the angle of inclination of different locations on the water waves is different, the bottom of the water we see from the observation point is not just below the observation point, but has a certain offset. This is why we can feel the shape of the waves without considering the reflection of the light at the top of the water.

Reflections : Water waves are reflected when they encounter obstacles.

diffraction : A rock is placed in the center of the pond, or a spacer with a slit in the middle, the diffraction of the water waves can be seen.

Algorithm derivation:

Well, with these features, and using math and geometry, we can simulate real water waves. However, if you've ever animated a wave in 3DMax, you'll know that it takes dozens of seconds to render a real-world water wave, and what we need now is a real-time rendering, with at least 20 frames per second to make the water waves appear smooth. Considering the speed of computer operation, we can not construct the water waves according to the sine function or the exact formula, we can not use the multiplication method, and can not use the trigonometric functions such as sin, cos and so on, only with a fast algorithm for approximate values, although there are some errors in this algorithm, we have to do so in order to meet the requirements of real-

First we will build two arrays of the same size as the pool image buf1[poolwidth * Poolheight] and Buf2[poolwidth * Poolheight] (poolwidth for the pixel width of the pool image, poolheight for the pool image Pixel height), which is used to hold the previous and next moment amplitude data for each point on the water, since amplitude also represents the energy of the wave, so in the following we call the two arrays to be the energetic buffers. The water surface is a plane at the initial state, and the amplitude of each point is 0, so the initial values of the two arrays are equal to 0.

The formula for calculating the amplitude is deduced below.

We assume that there is such a formula, at any moment according to a point around the front, back, left and right four points and the amplitude of the point itself to calculate the next moment of the amplitude of the point, then, we may use the inductive method to find any moment of the surface of the amplitude of any point. As shown on the left, as you can see, at some point, the amplitude of the X0 points is affected by the amplitude of the X0 point itself, at the same time by the influence from the front, back, left, and right four points (X1, X2, X3, X4) (in order to simplify, we ignore all other points), and the influence of these four points on the X0 point is will be equal. Then we can assume that this one-time formula is:

X0 ' = A * (X1 + X2 + X3 + X4) + b * X0 ( equation 1)

A, B is the undetermined coefficient, X0 ' is the amplitude of the next moment of the X0 point,

X0, X1, X2, X3, X4 are the amplitudes of the current moment.

Let's solve A and B.

Suppose the damping of water is 0. In this ideal condition, the total potential energy of the water will remain constant and the waves will always fluctuate. That is, at any moment, the amplitude of all points remains the same. Then you can get the following formula:

X0 ' + X1 ' + ... + Xn ' = X0 + X1 + ... + Xn

Replace each point with equation 1, substituting the above, to get:

(4a + b) * X0 + (4a + b) * X1 + ... (4a + b) * Xn = X0 + X1 + ... + Xn = > 4a + b = 1

Find a simple solution: a = 1/2, B =-1.

Since 1/2 can be done with the shift operator ">>" without the multiplication method, this group of solutions is the most applicable and the quickest. So the last formula that we get is:

X0 ' = (X1 + X2 + X3 + X4 )/2-x0

Well, with the approximate formula above, you can generalize to the following general conclusion: the amplitude at any point on the surface of a given moment is known, then, at the next moment, the amplitude of any point is equal to the amplitude of the front, back, left, and right four points adjacent to the point and divided by 2, minus the amplitude of that point.

It should be noted that water in practice is damped, otherwise, with the above formula, once you add a waves in the water, the water will never stop the shock. Therefore, the amplitude data also need to be attenuated processing, so that each point after a calculation, the amplitude is lower than the ideal value at a certain proportion. This attenuation rate has been tested, with 1/32 more appropriate, namely 1/2^5. Can be obtained quickly through the shift operation.

Here, the hardest part of the water-wave effect algorithm is clear, here is the code for calculating the amplitude data in the Android source program.

The amplitude algorithm for the next moment in a point is: the amplitude of the upper and lower left and right four points minus the current amplitude, i.e.

X0 ' = (X1 + X2 + X3 + X4)/2-x0

+----x3----+

//  +      | +

//  +      | +

X1---x0----x2

//  +      | +

//  +      | +

+----x4----+

//

void Ripplespread ()

{

int pixels = m_width * (m_height-1);

for ( int i = m_width; i < pixels; ++i) {

Wave wave diffusion: the amplitude of the upper and lower left and right four points minus the current amplitude

X0 ' = (X1 + X2 + X3 + X4)/2-x0

//

M_BUF2 [i] =

( short) (((m_buf1 [i-1] + m_buf1 [i + 1]+

M_BUF1 [I-m_width] + m_buf1 [i + m_width]) >> 1)

-M_buf2 [i]);

Wave attenuation 1/32

//

M_BUF2 [i]-= M_BUF2 [i] >> 5;

}

Exchanging wave energy data buffers

Short [] temp = M_BUF1;

M_BUF1 = M_buf2;

M_buf2 = temp;

}

Rendering:

We can then render the page based on the calculated amplitude data.

Because of the refraction of water, when the surface of the water is not perpendicular to our vision, we see the underwater scenery is not at the point of observation is directly below, and there is a certain offset. The degree of migration is related to the slope of the water wave, the refractive index of the waters and the depth of water, which is obviously unrealistic if accurate calculations are to be made. Again, we just need to do a linear approximation of the process. Because the more inclined the water surface, the more we see the underwater scenery offset, so, we can approximate the surface of a point on the front and back, left and right two points of the amplitude difference to represent the observed underwater scenery offset.

In the program, the original image is loaded with one page, and another page is used for rendering. Get a pointer to two page memory areas, SRC and DST, and then copy each pixel on the original image to the rendered page, based on the offset. The code for page rendering is as follows:

void Ripplerender ()

{

int offset;

int i = m_width;

int length = m_width * m_height;

for ( int y = 1; y < m_height-1; ++y) {

for ( int x = 0; x < m_width; ++x, ++i) {

Calculates the memory address offset of the offset pixel and the original pixel:

Offset = width * Yoffset + xoffset

offset = (M_width * (M_BUF1 [i-m_width]-m_buf1 [i + m_width]) + (M_BUF1 [i-1]-M_BUF1 [i + 1]);

Determine if the coordinates are within range

if (i + offset > 0 && i + offset < length) {

M_BITMAP2 [i] = m_bitmap1 [i + offset];

}

Else {

M_BITMAP2 [i] = m_bitmap1 [i];

}

}

}

}

Add Waves:

As the saying goes: Smoke, in order to form waves, we have to add waves to the pool, you can imagine into the water into the stone, the formation of the waves size and energy and the radius of the stone and you throw the force of the stone is related. Knowing this, well, we just need to modify the wave data buffer buf, let it in the stone into the water place to a negative "sharp pulse", that is, let buf[x,y] =-N. After experiments, the range of n is more appropriate between (32 ~ 128).

Control waves radius Well done, you just take the stone into the center of the water point, draw a radius of the stone radius of the circle, so that all the points in the circle so a negative "sharp pulse" on it (here also do approximate treatment).

The code for adding waves is as follows:

Stonesize: Waves Radius

Stoneweight: Waves Energy

//

void dropstone ( int x, int y, int stonesize, int stoneweight)

{

Determine if the coordinates are within range

if ((x + stonesize) > M_width | | (y + stonesize) > M_height

|| (x-stonesize) < 0 | | (Y-stonesize) < 0) {

return ;

}

int value = stonesize * stonesize;

Short weight = ( short)-stoneweight;

for    ( int posx = x-stonesize; posx < x + stonesize; ++posx) {

for       ( int posy = y-stonesize; posy < y + stonesize; ++posy) {

if ((posx-x) * (posx-x) + (posy-y) * (posy-y)

< value)

{

M_BUF1 [m_width * posy + posx] = weight;

}

}

}

}

If we want to simulate the ripple effect that occurs when the water is being crossed, we also need to add new algorithm function Breasenhamdrop.

void dropstoneline ( int x, int y, int stonesize, int stoneweight) {

Determine if the coordinates are within the screen range

if ((x + stonesize) > M_width | | (y + stonesize) > M_height

|| (x-stonesize) < 0 | | (Y-stonesize) < 0) {

return ;

}

for    ( int posx = x-stonesize; posx < x + stonesize; ++posx) {

for       ( int posy = y-stonesize; posy < y + stonesize; ++posy) {

M_BUF1 [m_width * posy + posx] =-40;

}

}

}

XS, Ys: Starting point, Xe, ye: End point

Size: Waves radius, Weight: waves Energy

void breasenhamdrop ( int xs, int ys, int XE, int ye, int size, intweight)

{

int dx = xe-xs;

int dy = ye-ys;

DX = (dx >= 0)? DX:-DX;

dy = (dy >= 0)? DY:-dy;

if (dx = = 0 && dy = = 0) {

Dropstoneline (XS, YS, size, weight);

}

Else if (dx = = 0) {

int yinc = (Ye-ys! = 0)? 1:-1;

for ( int i = 0; i < dy; ++i) {

Dropstoneline (XS, YS, size, weight);

ys + = Yinc;

}

}

Else if (dy = = 0) {

int xinc = (Xe-xs! = 0)? 1:-1;

for ( int i = 0; i < DX; ++i) {

Dropstoneline (XS, YS, size, weight);

xs + = Xinc;

}

}

Else if (dx > Dy) {

int p = (dy << 1)-DX;

int inc1 = (dy << 1);

int inc2 = ((DY-DX) << 1);

int xinc = (Xe-xs! = 0)? 1:-1;

int yinc = (Ye-ys! = 0)? 1:-1;

for ( int i = 0; i < DX; ++i) {

Dropstoneline (XS, YS, size, weight);

xs + = Xinc;

if (P < 0) {

p + = inc1;

}

Else {

ys + = Yinc;

p + = INC2;

}

}

}

Else {

int p = (dx << 1)-dy;

int inc1 = (dx << 1);

int inc2 = ((dx-dy) << 1);

int xinc = (Xe-xs! = 0)? 1:-1;

int yinc = (Ye-ys! = 0)? 1:-1;

for ( int i = 0; i < dy; ++i) {

Dropstoneline (XS, YS, size, weight);

ys + = Yinc;

if (P < 0) {

p + = inc1;

}

Else {

xs + = Xinc;

p + = INC2;

}

}

}

}

:


Ripple effect across the water

Raindrops drip water effects

Conclusion:

This data buffer to the image of the water wave processing method, there is one of the biggest advantage is that the program operation and display speed and the complexity of the water is not related, regardless of the surface is calm or choppy, the program's FPS has remained unchanged, this point you study the program should be able to see

Android algorithm Watermark

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.