Skip to content

Commit

Permalink
Frame: Reduce code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
ferdnyc committed Sep 13, 2020
1 parent 92d33a1 commit 0bcf1e4
Showing 1 changed file with 22 additions and 23 deletions.
45 changes: 22 additions & 23 deletions src/Frame.cpp
Expand Up @@ -762,31 +762,31 @@ void Frame::AddColor(int new_width, int new_height, std::string new_color)
}

// Add (or replace) pixel data to the frame
void Frame::AddImage(int new_width, int new_height, int bytes_per_pixel, QImage::Format type, const unsigned char *pixels_)
void Frame::AddImage(
int new_width, int new_height, int bytes_per_pixel,
QImage::Format type, const unsigned char *pixels_)
{
// Create new buffer
const GenericScopedLock<juce::CriticalSection> lock(addingImageSection);
int buffer_size = new_width * new_height * bytes_per_pixel;
qbuffer = new unsigned char[buffer_size]();

// Copy buffer data
memcpy((unsigned char*)qbuffer, pixels_, buffer_size);

// Create new image object, and fill with pixel data
#pragma omp critical (AddImage)
{
image = std::make_shared<QImage>(
qbuffer, new_width, new_height, new_width * bytes_per_pixel, type, (QImageCleanupFunction) &openshot::Frame::cleanUpBuffer, (void*) qbuffer);

// Always convert to RGBA8888 (if different)
if (image->format() != QImage::Format_RGBA8888)
*image = image->convertToFormat(QImage::Format_RGBA8888);

// Update height and width
width = image->width();
height = image->height();
has_image_data = true;
}
const GenericScopedLock<juce::CriticalSection> lock(addingImageSection);
int buffer_size = new_width * new_height * bytes_per_pixel;
qbuffer = new unsigned char[buffer_size]();

// Copy buffer data
memcpy((unsigned char*)qbuffer, pixels_, buffer_size);

} // Release addingImageSection lock

// Create new image object from pixel data
auto new_image = std::make_shared<QImage>(
qbuffer,
new_width, new_height,
new_width * bytes_per_pixel,
type,
(QImageCleanupFunction) &openshot::Frame::cleanUpBuffer,
(void*) qbuffer
);
AddImage(new_image);
}

// Add (or replace) pixel data to the frame
Expand Down Expand Up @@ -826,7 +826,6 @@ void Frame::AddImage(std::shared_ptr<QImage> new_image, bool only_odd_lines)
AddImage(new_image);

} else {

// Ignore image of different sizes or formats
bool ret=false;
#pragma omp critical (AddImage)
Expand Down

0 comments on commit 0bcf1e4

Please sign in to comment.