Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Frame: Fix interlaced AddImage #418

Merged
merged 1 commit into from
Feb 27, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 15 additions & 8 deletions src/Frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -818,16 +818,23 @@ void Frame::AddImage(std::shared_ptr<QImage> new_image, bool only_odd_lines)
// Ignore image of different sizes or formats
bool ret=false;
#pragma omp critical (AddImage)
if (image == new_image || image->size() != image->size() || image->format() != image->format())
ret=true;
if (ret)
{
if (image == new_image || image->size() != new_image->size()) {
ret = true;
}
else if (new_image->format() != image->format()) {
new_image = std::shared_ptr<QImage>(new QImage(new_image->convertToFormat(image->format())));
}
}
if (ret) {
return;

}

// Get the frame's image
const GenericScopedLock<juce::CriticalSection> lock(addingImageSection);
#pragma omp critical (AddImage)
{
const unsigned char *pixels = image->constBits();
unsigned char *pixels = image->bits();
Comment on lines -830 to +837
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consts are nice and all, when appropriate, but when you're modifying the pixels in the buffer I'm pretty sure we're not supposed to use constBits() since it suppresses Qt's copy-on-write handling for shared QImage buffers.

const unsigned char *new_pixels = new_image->constBits();

// Loop through the scanlines of the image (even or odd)
Expand All @@ -836,13 +843,13 @@ void Frame::AddImage(std::shared_ptr<QImage> new_image, bool only_odd_lines)
start = 1;

for (int row = start; row < image->height(); row += 2) {
memcpy((unsigned char *) pixels, new_pixels + (row * image->bytesPerLine()), image->bytesPerLine());
new_pixels += image->bytesPerLine();
int offset = row * image->bytesPerLine();
memcpy(pixels + offset, new_pixels + offset, image->bytesPerLine());
Comment on lines 845 to +847
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is where things really break.

The way that loop was previously written, it would go through every other row (odd or even) in new_pixels, and copy that row's data to the first row of pixels — every time! Nothing was ever offsetting the pixels location to advance it by the row stride, and nothing would have ever written to any part of pixels (and therefore image) beyond the first scanline/row.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Oh, yeah — the telltale cast on pixels — to cast away its const status, which it shouldn't have had in the first place — is also fun, in the previous code.)

}

// Update height and width
width = image->width();
height = image->height();
width = image->width();
has_image_data = true;
}
}
Expand Down