~mbranton/libopenshot/alpha-channel-fix

« back to all changes in this revision

Viewing changes to src/QtImageReader.cpp

  • Committer: Jonathan Thomas
  • Date: 2016-09-14 09:11:12 UTC
  • Revision ID: jonathan@openshot.org-20160914091112-mcxg747f4zniwscx
Added SetMaxSize for image optimizations in QImageReader and FFmpegReader, which lets the timeline pass down the max size to all clips and readers, so they can optionally optimize the size of images (especially useful for optimizing preview performance). Removed convoluted image scaling code in FFmpegReader, and replaced with simpler version. Also, fixed a few regressions from the new Caching code, primarily a crash when reaching the end of the last clip on the timeline.

Show diffs side-by-side

added added

removed removed

Lines of Context:
109
109
        if (!is_open)
110
110
                throw ReaderClosed("The Image is closed.  Call Open() before calling this method.", path);
111
111
 
112
 
        // Create or get frame object
113
 
        tr1::shared_ptr<Frame> image_frame(new Frame(requested_frame, info.width, info.height, "#000000", Frame::GetSamplesPerFrame(requested_frame, info.fps, info.sample_rate, info.channels), info.channels));
114
 
 
115
 
        // Add Image data to frame
116
 
        image_frame->AddImage(image);
117
 
 
118
 
        // return frame object
119
 
        return image_frame;
 
112
        // Determine if we need to scale the image (for performance reasons)
 
113
        // The timeline passes its size to the clips, which pass their size to the readers, and eventually here
 
114
        // A max_width/max_height = 0 means do not scale (probably because we are scaling the image larger than 100%)
 
115
        if (max_width != 0 && max_height != 0 && max_width < info.width && max_height < info.height)
 
116
        {
 
117
                // Scale image smaller (or use a previous scaled image)
 
118
                if (!cached_image) {
 
119
                        // Create a scoped lock, allowing only a single thread to run the following code at one time
 
120
                        const GenericScopedLock<CriticalSection> lock(getFrameCriticalSection);
 
121
 
 
122
                        // We need to resize the original image to a smaller image (for performance reasons)
 
123
                        // Only do this once, to prevent tons of unneeded scaling operations
 
124
                        cached_image = tr1::shared_ptr<QImage>(new QImage(image->scaled(max_width, max_height, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
 
125
                        cached_image = tr1::shared_ptr<QImage>(new QImage(cached_image->convertToFormat(QImage::Format_RGBA8888)));
 
126
                }
 
127
 
 
128
                // Create or get frame object
 
129
                tr1::shared_ptr<Frame> image_frame(new Frame(requested_frame, cached_image->width(), cached_image->height(), "#000000", Frame::GetSamplesPerFrame(requested_frame, info.fps, info.sample_rate, info.channels), info.channels));
 
130
 
 
131
                // Add Image data to frame
 
132
                image_frame->AddImage(cached_image);
 
133
 
 
134
                // return frame object
 
135
                return image_frame;
 
136
 
 
137
        } else {
 
138
                // Use original image (higher quality but slower)
 
139
                // Create or get frame object
 
140
                tr1::shared_ptr<Frame> image_frame(new Frame(requested_frame, info.width, info.height, "#000000", Frame::GetSamplesPerFrame(requested_frame, info.fps, info.sample_rate, info.channels), info.channels));
 
141
 
 
142
                // Add Image data to frame
 
143
                image_frame->AddImage(image);
 
144
 
 
145
                // return frame object
 
146
                return image_frame;
 
147
        }
120
148
}
121
149
 
122
150
// Generate JSON string of this object