Skip to content

Commit 4fc9e9d

Browse files
authored
Merge pull request #425 from ferdnyc/optimized-wave
Streamline and parallelize Wave effect
2 parents ce39a53 + 7d2ff3a commit 4fc9e9d

File tree

1 file changed

+16
-20
lines changed

1 file changed

+16
-20
lines changed

src/effects/Wave.cpp

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,10 @@ std::shared_ptr<Frame> Wave::GetFrame(std::shared_ptr<Frame> frame, int64_t fram
6868
// Get the frame's image
6969
std::shared_ptr<QImage> frame_image = frame->GetImage();
7070

71-
// Get pixels for frame image
71+
// Get original pixels for frame image, and also make a copy for editing
72+
const unsigned char *original_pixels = (unsigned char *) frame_image->constBits();
7273
unsigned char *pixels = (unsigned char *) frame_image->bits();
73-
74-
// Make temp copy of pixels before we start changing them
75-
unsigned char *temp_image = new unsigned char[frame_image->width() * frame_image->height() * 4]();
76-
memcpy(temp_image, pixels, sizeof(char) * frame_image->width() * frame_image->height() * 4);
74+
int pixel_count = frame_image->width() * frame_image->height();
7775

7876
// Get current keyframe values
7977
double time = frame_number;//abs(((frame_number + 255) % 510) - 255);
@@ -84,30 +82,28 @@ std::shared_ptr<Frame> Wave::GetFrame(std::shared_ptr<Frame> frame, int64_t fram
8482
double speed_y_value = speed_y.GetValue(frame_number);
8583

8684
// Loop through pixels
87-
for (int pixel = 0, byte_index=0; pixel < frame_image->width() * frame_image->height(); pixel++, byte_index+=4)
85+
#pragma omp parallel for
86+
for (int pixel = 0; pixel < pixel_count; ++pixel)
8887
{
89-
// Calculate X and Y pixel coordinates
88+
// Calculate pixel Y value
9089
int Y = pixel / frame_image->width();
9190

9291
// Calculate wave pixel offsets
93-
float noiseVal = (100 + Y * 0.001) * multiplier_value; // Time and time multiplier (to make the wave move)
94-
float noiseAmp = noiseVal * amplitude_value; // Apply amplitude / height of the wave
95-
float waveformVal = sin((Y * wavelength_value) + (time * speed_y_value)); // Waveform algorithm on y-axis
96-
float waveVal = (waveformVal + shift_x_value) * noiseAmp; // Shifts pixels on the x-axis
92+
float noiseVal = (100 + Y * 0.001) * multiplier_value; // Time and time multiplier (to make the wave move)
93+
float noiseAmp = noiseVal * amplitude_value; // Apply amplitude / height of the wave
94+
float waveformVal = sin((Y * wavelength_value) + (time * speed_y_value)); // Waveform algorithm on y-axis
95+
float waveVal = (waveformVal + shift_x_value) * noiseAmp; // Shifts pixels on the x-axis
9796

98-
long unsigned int source_X = round(pixel + waveVal) * 4;
99-
if (source_X < 0)
100-
source_X = 0;
101-
if (source_X > frame_image->width() * frame_image->height() * 4 * sizeof(char))
102-
source_X = (frame_image->width() * frame_image->height() * 4 * sizeof(char)) - (sizeof(char) * 4);
97+
long unsigned int source_px = round(pixel + waveVal);
98+
if (source_px < 0)
99+
source_px = 0;
100+
if (source_px >= pixel_count)
101+
source_px = pixel_count - 1;
103102

104103
// Calculate source array location, and target array location, and copy the 4 color values
105-
memcpy(&pixels[byte_index], &temp_image[source_X], sizeof(char) * 4);
104+
memcpy(&pixels[pixel * 4], &original_pixels[source_px * 4], sizeof(char) * 4);
106105
}
107106

108-
// Delete arrays
109-
delete[] temp_image;
110-
111107
// return the modified frame
112108
return frame;
113109
}

0 commit comments

Comments
 (0)