This method is required for all derived classes of EffectBase, and returns a modified openshot::Frame object
| 45 | // This method is required for all derived classes of EffectBase, and returns a |
| 46 | // modified openshot::Frame object |
| 47 | std::shared_ptr<openshot::Frame> Shift::GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number) |
| 48 | { |
| 49 | // Get the frame's image |
| 50 | std::shared_ptr<QImage> frame_image = frame->GetImage(); |
| 51 | unsigned char *pixels = (unsigned char *) frame_image->bits(); |
| 52 | |
| 53 | // Get the current shift amount, and clamp to range (-1 to 1 range) |
| 54 | double x_shift = x.GetValue(frame_number); |
| 55 | double x_shift_limit = fmod(fabs(x_shift), 1.0); |
| 56 | double y_shift = y.GetValue(frame_number); |
| 57 | double y_shift_limit = fmod(fabs(y_shift), 1.0); |
| 58 | |
| 59 | // Declare temp arrays to hold pixels while we move things around |
| 60 | unsigned char *temp_row = new unsigned char[frame_image->width() * 4](); |
| 61 | |
| 62 | // X-SHIFT |
| 63 | // Loop through rows |
| 64 | for (int row = 0; row < frame_image->height(); row++) { |
| 65 | // Copy current row's pixels |
| 66 | int starting_row_pixel = row * frame_image->width(); |
| 67 | memcpy(temp_row, &pixels[starting_row_pixel * 4], sizeof(char) * frame_image->width() * 4); |
| 68 | |
| 69 | // Replace current row with left part of the pixels |
| 70 | if (x_shift > 0.0) { |
| 71 | // Move left side to the right |
| 72 | int relative_pixel_start = (int)round(frame_image->width() * x_shift_limit); |
| 73 | memcpy(&pixels[(starting_row_pixel + relative_pixel_start) * 4], &temp_row[0], sizeof(char) * (frame_image->width() - relative_pixel_start) * 4); |
| 74 | |
| 75 | // Move right side to the left |
| 76 | memcpy(&pixels[starting_row_pixel * 4], &temp_row[(frame_image->width() - relative_pixel_start) * 4], sizeof(char) * relative_pixel_start * 4); |
| 77 | } else if (x_shift < 0.0) { |
| 78 | // Move right side to the left |
| 79 | int relative_pixel_start = (int)round(frame_image->width() * x_shift_limit); |
| 80 | memcpy(&pixels[starting_row_pixel * 4], &temp_row[relative_pixel_start * 4], sizeof(char) * (frame_image->width() - relative_pixel_start) * 4); |
| 81 | |
| 82 | // Move left side to the right |
| 83 | memcpy(&pixels[(starting_row_pixel + (frame_image->width() - relative_pixel_start)) * 4], &temp_row[0], sizeof(char) * relative_pixel_start * 4); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // Make temp copy of pixels for Y-SHIFT |
| 88 | unsigned char *temp_image = new unsigned char[frame_image->width() * frame_image->height() * 4](); |
| 89 | memcpy(temp_image, pixels, sizeof(char) * frame_image->width() * frame_image->height() * 4); |
| 90 | |
| 91 | // Y-SHIFT |
| 92 | // Replace current row with left part of the pixels |
| 93 | if (y_shift > 0.0) { |
| 94 | // Move top side to bottom |
| 95 | int relative_pixel_start = frame_image->width() * (int)round(frame_image->height() * y_shift_limit); |
| 96 | memcpy(&pixels[relative_pixel_start * 4], temp_image, sizeof(char) * ((frame_image->width() * frame_image->height()) - relative_pixel_start) * 4); |
| 97 | |
| 98 | // Move bottom side to top |
| 99 | memcpy(pixels, &temp_image[((frame_image->width() * frame_image->height()) - relative_pixel_start) * 4], sizeof(char) * relative_pixel_start * 4); |
| 100 | |
| 101 | } else if (y_shift < 0.0) { |
| 102 | // Move bottom side to top |
| 103 | int relative_pixel_start = frame_image->width() * (int)round(frame_image->height() * y_shift_limit); |
| 104 | memcpy(pixels, &temp_image[relative_pixel_start * 4], sizeof(char) * ((frame_image->width() * frame_image->height()) - relative_pixel_start) * 4); |