Skip to content

Commit 028bafc

Browse files
authored
Merge pull request #410 from OpenShot/resvg-image-format
Fix Resvg image format
2 parents c8be335 + 63baee1 commit 028bafc

File tree

3 files changed

+42
-37
lines changed

3 files changed

+42
-37
lines changed

cmake/Modules/FindRESVG.cmake

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,23 @@ find_package_handle_standard_args(RESVG
9494
# Export target
9595
if(RESVG_FOUND AND NOT TARGET RESVG::resvg)
9696
message(STATUS "Creating IMPORTED target RESVG::resvg")
97-
add_library(RESVG::resvg UNKNOWN IMPORTED)
97+
if (WIN32)
98+
# Windows mis-links SHARED library targets
99+
add_library(RESVG::resvg UNKNOWN IMPORTED)
100+
message(STATUS " UNKNOWN IMPORTED target for Win32")
101+
else()
102+
# Linux needs SHARED to link because libresvg has no SONAME
103+
add_library(RESVG::resvg SHARED IMPORTED)
104+
set_property(TARGET RESVG::resvg APPEND PROPERTY
105+
IMPORTED_NO_SONAME TRUE)
106+
message(STATUS " SHARED IMPORTED target with IMPORTED_NO_SONAME")
107+
endif()
108+
109+
message(STATUS " INCLUDE_DIRECTORIES: ${RESVG_INCLUDE_DIRS}")
110+
message(STATUS " COMPILE_DEFINITIONS: ${RESVG_DEFINITIONS}")
111+
message(STATUS " IMPORTED_LOCATION: ${RESVG_LIBRARIES}")
98112

99-
set_target_properties(RESVG::resvg PROPERTIES
113+
set_property(TARGET RESVG::resvg APPEND PROPERTY
100114
INTERFACE_INCLUDE_DIRECTORIES "${RESVG_INCLUDE_DIRS}")
101115

102116
set_property(TARGET RESVG::resvg APPEND PROPERTY

src/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,7 @@ if (TARGET RESVG::resvg)
360360
#include_directories(${RESVG_INCLUDE_DIRS})
361361
target_link_libraries(openshot PUBLIC RESVG::resvg)
362362

363-
# define a global var (used in the C++)
364-
add_definitions( -DUSE_RESVG=1 )
363+
target_compile_definitions(openshot PUBLIC "-DUSE_RESVG=1")
365364
set(CMAKE_SWIG_FLAGS "-DUSE_RESVG=1")
366365
endif()
367366

src/QtImageReader.cpp

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)