@@ -71,7 +71,7 @@ void QtImageReader::Open()
7171 if (!is_open)
7272 {
7373 bool success = true ;
74- image = std::shared_ptr<QImage>( new QImage ()) ;
74+ bool loaded = false ;
7575
7676#if USE_RESVG == 1
7777 // If defined and found in CMake, utilize the libresvg for parsing
@@ -80,38 +80,32 @@ void QtImageReader::Open()
8080 if (path.toLower ().endsWith (" .svg" ) || path.toLower ().endsWith (" .svgz" )) {
8181
8282 ResvgRenderer renderer (path);
83- if (!renderer.isValid ()) {
84- // Attempt to open file (old method using Qt5 limited SVG parsing)
85- success = image->load (path);
86- if (success) {
87- image = std::shared_ptr<QImage>(new QImage (image->convertToFormat (QImage::Format_RGBA8888)));
88- }
89- } else {
90-
91- image = std::shared_ptr<QImage>(new QImage (renderer.defaultSize (), QImage::Format_RGBA8888));
83+ if (renderer.isValid ()) {
84+
85+ image = std::shared_ptr<QImage>(new QImage (renderer.defaultSize (), QImage::Format_ARGB32_Premultiplied));
9286 image->fill (Qt::transparent);
9387
9488 QPainter p (image.get ());
9589 renderer.render (&p);
9690 p.end ();
91+ loaded = true ;
9792 }
93+ }
94+ #endif
9895
99- } else {
100- // Attempt to open file (old method)
96+ if (!loaded) {
97+ // Attempt to open file using Qt's build in image processing capabilities
98+ image = std::shared_ptr<QImage>(new QImage ());
10199 success = image->load (path);
102- if (success)
103- image = std::shared_ptr<QImage>(new QImage (image->convertToFormat (QImage::Format_RGBA8888)));
104100 }
105- #else
106- // Attempt to open file using Qt's build in image processing capabilities
107- success = image->load (path);
108- if (success)
109- image = std::shared_ptr<QImage>(new QImage (image->convertToFormat (QImage::Format_RGBA8888)));
110- #endif
111101
112- if (!success)
102+ if (!success) {
113103 // raise exception
114104 throw InvalidFile (" File could not be opened." , path.toStdString ());
105+ }
106+
107+ // Convert to proper format
108+ image = std::shared_ptr<QImage>(new QImage (image->convertToFormat (QImage::Format_RGBA8888)));
115109
116110 // Update image properties
117111 info.has_audio = false ;
@@ -224,42 +218,40 @@ std::shared_ptr<Frame> QtImageReader::GetFrame(int64_t requested_frame)
224218
225219 // Scale image smaller (or use a previous scaled image)
226220 if (!cached_image || (max_size.width () != max_width || max_size.height () != max_height)) {
221+
222+ bool rendered = false ;
227223#if USE_RESVG == 1
228224 // If defined and found in CMake, utilize the libresvg for parsing
229225 // SVG files and rasterizing them to QImages.
230226 // Only use resvg for files ending in '.svg' or '.svgz'
231227 if (path.toLower ().endsWith (" .svg" ) || path.toLower ().endsWith (" .svgz" )) {
228+
232229 ResvgRenderer renderer (path);
233230 if (renderer.isValid ()) {
234231 // Scale SVG size to keep aspect ratio, and fill the max_size as best as possible
235232 QSize svg_size (renderer.defaultSize ().width (), renderer.defaultSize ().height ());
236233 svg_size.scale (max_width, max_height, Qt::KeepAspectRatio);
237234
238235 // Create empty QImage
239- cached_image = std::shared_ptr<QImage>(new QImage (QSize (svg_size.width (), svg_size.height ()), QImage::Format_RGBA8888 ));
236+ cached_image = std::shared_ptr<QImage>(new QImage (QSize (svg_size.width (), svg_size.height ()), QImage::Format_ARGB32_Premultiplied ));
240237 cached_image->fill (Qt::transparent);
241238
242239 // Render SVG into QImage
243240 QPainter p (cached_image.get ());
244241 renderer.render (&p);
245242 p.end ();
246- } else {
247- // Resize current rasterized SVG (since we failed to parse original SVG file with resvg)
248- cached_image = std::shared_ptr<QImage>(new QImage (image->scaled (max_width, max_height, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
249- cached_image = std::shared_ptr<QImage>(new QImage (cached_image->convertToFormat (QImage::Format_RGBA8888)));
243+ rendered = true ;
250244 }
251- } else {
245+ }
246+ #endif
247+
248+ if (!rendered) {
252249 // We need to resize the original image to a smaller image (for performance reasons)
253250 // Only do this once, to prevent tons of unneeded scaling operations
254251 cached_image = std::shared_ptr<QImage>(new QImage (image->scaled (max_width, max_height, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
255- cached_image = std::shared_ptr<QImage>(new QImage (cached_image->convertToFormat (QImage::Format_RGBA8888)));
256252 }
257- #else
258- // We need to resize the original image to a smaller image (for performance reasons)
259- // Only do this once, to prevent tons of unneeded scaling operations
260- cached_image = std::shared_ptr<QImage>(new QImage (image->scaled (max_width, max_height, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
253+
261254 cached_image = std::shared_ptr<QImage>(new QImage (cached_image->convertToFormat (QImage::Format_RGBA8888)));
262- #endif
263255
264256 // Set max size (to later determine if max_size is changed)
265257 max_size.setWidth (max_width);
0 commit comments