Skip to content

Commit

Permalink
Merge pull request #410 from OpenShot/resvg-image-format
Browse files Browse the repository at this point in the history
Fix Resvg image format
  • Loading branch information
ferdnyc authored Jan 12, 2020
2 parents c8be335 + 63baee1 commit 028bafc
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 37 deletions.
18 changes: 16 additions & 2 deletions cmake/Modules/FindRESVG.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,23 @@ find_package_handle_standard_args(RESVG
# Export target
if(RESVG_FOUND AND NOT TARGET RESVG::resvg)
message(STATUS "Creating IMPORTED target RESVG::resvg")
add_library(RESVG::resvg UNKNOWN IMPORTED)
if (WIN32)
# Windows mis-links SHARED library targets
add_library(RESVG::resvg UNKNOWN IMPORTED)
message(STATUS " UNKNOWN IMPORTED target for Win32")
else()
# Linux needs SHARED to link because libresvg has no SONAME
add_library(RESVG::resvg SHARED IMPORTED)
set_property(TARGET RESVG::resvg APPEND PROPERTY
IMPORTED_NO_SONAME TRUE)
message(STATUS " SHARED IMPORTED target with IMPORTED_NO_SONAME")
endif()

message(STATUS " INCLUDE_DIRECTORIES: ${RESVG_INCLUDE_DIRS}")
message(STATUS " COMPILE_DEFINITIONS: ${RESVG_DEFINITIONS}")
message(STATUS " IMPORTED_LOCATION: ${RESVG_LIBRARIES}")

set_target_properties(RESVG::resvg PROPERTIES
set_property(TARGET RESVG::resvg APPEND PROPERTY
INTERFACE_INCLUDE_DIRECTORIES "${RESVG_INCLUDE_DIRS}")

set_property(TARGET RESVG::resvg APPEND PROPERTY
Expand Down
3 changes: 1 addition & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,7 @@ if (TARGET RESVG::resvg)
#include_directories(${RESVG_INCLUDE_DIRS})
target_link_libraries(openshot PUBLIC RESVG::resvg)

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

Expand Down
58 changes: 25 additions & 33 deletions src/QtImageReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void QtImageReader::Open()
if (!is_open)
{
bool success = true;
image = std::shared_ptr<QImage>(new QImage());
bool loaded = false;

#if USE_RESVG == 1
// If defined and found in CMake, utilize the libresvg for parsing
Expand All @@ -80,38 +80,32 @@ void QtImageReader::Open()
if (path.toLower().endsWith(".svg") || path.toLower().endsWith(".svgz")) {

ResvgRenderer renderer(path);
if (!renderer.isValid()) {
// Attempt to open file (old method using Qt5 limited SVG parsing)
success = image->load(path);
if (success) {
image = std::shared_ptr<QImage>(new QImage(image->convertToFormat(QImage::Format_RGBA8888)));
}
} else {

image = std::shared_ptr<QImage>(new QImage(renderer.defaultSize(), QImage::Format_RGBA8888));
if (renderer.isValid()) {

image = std::shared_ptr<QImage>(new QImage(renderer.defaultSize(), QImage::Format_ARGB32_Premultiplied));
image->fill(Qt::transparent);

QPainter p(image.get());
renderer.render(&p);
p.end();
loaded = true;
}
}
#endif

} else {
// Attempt to open file (old method)
if (!loaded) {
// Attempt to open file using Qt's build in image processing capabilities
image = std::shared_ptr<QImage>(new QImage());
success = image->load(path);
if (success)
image = std::shared_ptr<QImage>(new QImage(image->convertToFormat(QImage::Format_RGBA8888)));
}
#else
// Attempt to open file using Qt's build in image processing capabilities
success = image->load(path);
if (success)
image = std::shared_ptr<QImage>(new QImage(image->convertToFormat(QImage::Format_RGBA8888)));
#endif

if (!success)
if (!success) {
// raise exception
throw InvalidFile("File could not be opened.", path.toStdString());
}

// Convert to proper format
image = std::shared_ptr<QImage>(new QImage(image->convertToFormat(QImage::Format_RGBA8888)));

// Update image properties
info.has_audio = false;
Expand Down Expand Up @@ -224,42 +218,40 @@ std::shared_ptr<Frame> QtImageReader::GetFrame(int64_t requested_frame)

// Scale image smaller (or use a previous scaled image)
if (!cached_image || (max_size.width() != max_width || max_size.height() != max_height)) {

bool rendered = false;
#if USE_RESVG == 1
// If defined and found in CMake, utilize the libresvg for parsing
// SVG files and rasterizing them to QImages.
// Only use resvg for files ending in '.svg' or '.svgz'
if (path.toLower().endsWith(".svg") || path.toLower().endsWith(".svgz")) {

ResvgRenderer renderer(path);
if (renderer.isValid()) {
// Scale SVG size to keep aspect ratio, and fill the max_size as best as possible
QSize svg_size(renderer.defaultSize().width(), renderer.defaultSize().height());
svg_size.scale(max_width, max_height, Qt::KeepAspectRatio);

// Create empty QImage
cached_image = std::shared_ptr<QImage>(new QImage(QSize(svg_size.width(), svg_size.height()), QImage::Format_RGBA8888));
cached_image = std::shared_ptr<QImage>(new QImage(QSize(svg_size.width(), svg_size.height()), QImage::Format_ARGB32_Premultiplied));
cached_image->fill(Qt::transparent);

// Render SVG into QImage
QPainter p(cached_image.get());
renderer.render(&p);
p.end();
} else {
// Resize current rasterized SVG (since we failed to parse original SVG file with resvg)
cached_image = std::shared_ptr<QImage>(new QImage(image->scaled(max_width, max_height, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
cached_image = std::shared_ptr<QImage>(new QImage(cached_image->convertToFormat(QImage::Format_RGBA8888)));
rendered = true;
}
} else {
}
#endif

if (!rendered) {
// We need to resize the original image to a smaller image (for performance reasons)
// Only do this once, to prevent tons of unneeded scaling operations
cached_image = std::shared_ptr<QImage>(new QImage(image->scaled(max_width, max_height, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
cached_image = std::shared_ptr<QImage>(new QImage(cached_image->convertToFormat(QImage::Format_RGBA8888)));
}
#else
// We need to resize the original image to a smaller image (for performance reasons)
// Only do this once, to prevent tons of unneeded scaling operations
cached_image = std::shared_ptr<QImage>(new QImage(image->scaled(max_width, max_height, Qt::KeepAspectRatio, Qt::SmoothTransformation)));

cached_image = std::shared_ptr<QImage>(new QImage(cached_image->convertToFormat(QImage::Format_RGBA8888)));
#endif

// Set max size (to later determine if max_size is changed)
max_size.setWidth(max_width);
Expand Down

0 comments on commit 028bafc

Please sign in to comment.