Skip to content

Commit ce39a53

Browse files
authored
Merge pull request #426 from ferdnyc/pixelate-code
Rewrite of Pixelate effect
2 parents 1e8e2a2 + 423f0ce commit ce39a53

File tree

1 file changed

+14
-28
lines changed

1 file changed

+14
-28
lines changed

src/effects/Pixelate.cpp

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -75,34 +75,20 @@ std::shared_ptr<Frame> Pixelate::GetFrame(std::shared_ptr<Frame> frame, int64_t
7575
double bottom_value = bottom.GetValue(frame_number);
7676

7777
if (pixelization_value > 0.0) {
78-
// Resize frame image smaller (based on pixelization value)
79-
std::shared_ptr<QImage> smaller_frame_image = std::shared_ptr<QImage>(new QImage(frame_image->scaledToWidth(std::max(frame_image->width() * pixelization_value, 2.0), Qt::SmoothTransformation)));
80-
81-
// Resize image back to original size (with no smoothing to create pixelated image)
82-
std::shared_ptr<QImage> pixelated_image = std::shared_ptr<QImage>(new QImage(smaller_frame_image->scaledToWidth(frame_image->width(), Qt::FastTransformation).convertToFormat(QImage::Format_RGBA8888)));
83-
84-
// Get pixel array pointer
85-
unsigned char *pixels = (unsigned char *) frame_image->bits();
86-
unsigned char *pixelated_pixels = (unsigned char *) pixelated_image->bits();
87-
88-
// Get pixels sizes of all margins
89-
int top_bar_height = top_value * frame_image->height();
90-
int bottom_bar_height = bottom_value * frame_image->height();
91-
int left_bar_width = left_value * frame_image->width();
92-
int right_bar_width = right_value * frame_image->width();
93-
94-
// Loop through rows
95-
for (int row = 0; row < frame_image->height(); row++) {
96-
97-
// Copy pixelated pixels into original frame image (where needed)
98-
if ((row >= top_bar_height) && (row <= frame_image->height() - bottom_bar_height)) {
99-
memcpy(&pixels[(row * frame_image->width() + left_bar_width) * 4], &pixelated_pixels[(row * frame_image->width() + left_bar_width) * 4], sizeof(char) * (frame_image->width() - left_bar_width - right_bar_width) * 4);
100-
}
101-
}
102-
103-
// Cleanup temp images
104-
smaller_frame_image.reset();
105-
pixelated_image.reset();
78+
int w = frame_image->width();
79+
int h = frame_image->height();
80+
81+
// Define area we're working on in terms of a QRect with QMargins applied
82+
QRect area(QPoint(0,0), frame_image->size());
83+
area = area.marginsRemoved({int(left_value * w), int(top_value * h), int(right_value * w), int(bottom_value * h)});
84+
85+
// Copy and scale pixels in area to be pixelated
86+
auto frame_scaled = frame_image->copy(area).scaledToWidth(area.width() * pixelization_value, Qt::SmoothTransformation);
87+
88+
// Draw pixelated image back over original
89+
QPainter painter(frame_image.get());
90+
painter.drawImage(area, frame_scaled);
91+
painter.end();
10692
}
10793

10894
// return the modified frame

0 commit comments

Comments
 (0)