26 #include <QImageReader>
28 #include <QSvgRenderer>
54 QSize default_svg_size;
57 if (path.toLower().endsWith(
".svg") || path.toLower().endsWith(
".svgz")) {
58 #if RESVG_VERSION_MIN(0, 11)
60 resvg_options.loadSystemFonts();
64 default_svg_size = load_svg_path(path);
65 if (!default_svg_size.isEmpty()) {
74 image = std::make_shared<QImage>();
75 QImageReader imgReader( path );
76 imgReader.setAutoTransform(
true );
77 imgReader.setDecideFormatFromContent(
true );
78 loaded = imgReader.read(image.get());
83 throw InvalidFile(
"QtImageReader could not open image file.", path.toStdString());
90 #if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
97 if (!default_svg_size.isEmpty()) {
146 cached_image.reset();
158 throw ReaderClosed(
"The Image is closed. Call Open() before calling this method.", path.toStdString());
161 const std::lock_guard<std::recursive_mutex> lock(
getFrameMutex);
164 QSize current_max_size = calculate_max_size();
167 if (!cached_image || max_size != current_max_size) {
169 if (path.toLower().endsWith(
".svg") || path.toLower().endsWith(
".svgz")) {
175 cached_image = std::make_shared<QImage>(image->scaled(
177 Qt::KeepAspectRatio, Qt::SmoothTransformation));
180 max_size = current_max_size;
185 auto sz = cached_image->size();
188 auto image_frame = std::make_shared<Frame>(
189 requested_frame, sz.width(), sz.height(),
"#000000",
191 image_frame->AddImage(cached_image);
198 QSize QtImageReader::calculate_max_size() {
202 if (max_width == 0 || max_height == 0) {
219 max_width = std::max(
float(max_width), max_width * max_scale_x);
220 max_height = std::max(
float(max_height), max_height * max_scale_y);
226 QSize width_size(max_width * max_scale_x,
229 max_height * max_scale_y);
231 if (width_size.width() >= max_width && width_size.height() >= max_height) {
232 max_width = std::max(max_width, width_size.width());
233 max_height = std::max(max_height, width_size.height());
235 max_width = std::max(max_width, height_size.width());
236 max_height = std::max(max_height, height_size.height());
242 float preview_ratio = 1.0;
249 max_width =
info.
width * max_scale_x * preview_ratio;
250 max_height =
info.
height * max_scale_y * preview_ratio;
258 QSize bounded_size(max_width, max_height);
260 if (bounded_size.width() > max_decode_size.width() ||
261 bounded_size.height() > max_decode_size.height()) {
262 bounded_size.scale(max_decode_size, Qt::KeepAspectRatio);
263 max_width = bounded_size.width();
264 max_height = bounded_size.height();
269 return QSize(max_width, max_height);
273 QSize QtImageReader::load_svg_path(QString) {
275 QSize default_size(0,0);
278 QSize current_max_size = calculate_max_size();
281 #if RESVG_VERSION_MIN(0, 11)
282 ResvgRenderer renderer(path, resvg_options);
283 if (renderer.isValid()) {
284 default_size = renderer.defaultSize();
286 QSize svg_size = default_size.scaled(current_max_size, Qt::KeepAspectRatio);
287 auto qimage = renderer.renderToImage(svg_size);
288 image = std::make_shared<QImage>(
289 qimage.convertToFormat(QImage::Format_RGBA8888_Premultiplied));
292 #elif RESVG_VERSION_MIN(0, 0)
293 ResvgRenderer renderer(path);
294 if (renderer.isValid()) {
295 default_size = renderer.defaultSize();
297 QSize svg_size = default_size.scaled(current_max_size, Qt::KeepAspectRatio);
299 image = std::make_shared<QImage>(svg_size,
300 QImage::Format_RGBA8888_Premultiplied);
301 image->fill(Qt::transparent);
302 QPainter p(image.get());
311 image = std::make_shared<QImage>();
312 loaded = image->load(path);
316 default_size.setWidth(image->width());
317 default_size.setHeight(image->height());
319 if (image->width() < current_max_size.width() || image->height() < current_max_size.height()) {
321 QSize svg_size = image->size().scaled(
322 current_max_size, Qt::KeepAspectRatio);
323 if (QCoreApplication::instance()) {
328 QSvgRenderer renderer(path);
329 if (renderer.isValid()) {
330 image = std::make_shared<QImage>(
331 svg_size, QImage::Format_RGBA8888_Premultiplied);
332 image->fill(Qt::transparent);
333 QPainter p(image.get());
337 image = std::make_shared<QImage>(image->scaled(
338 svg_size, Qt::KeepAspectRatio, Qt::SmoothTransformation));
341 image = std::make_shared<QImage>(image->scaled(
342 svg_size, Qt::KeepAspectRatio, Qt::SmoothTransformation));
346 image = std::make_shared<QImage>(image->scaled(
347 svg_size, Qt::KeepAspectRatio, Qt::SmoothTransformation));
368 root[
"type"] =
"QtImageReader";
369 root[
"path"] = path.toStdString();
385 catch (
const std::exception& e)
388 throw InvalidJSON(
"JSON is invalid (missing keys or invalid data types)");
399 if (!root[
"path"].isNull())
400 path = QString::fromStdString(root[
"path"].asString());