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());
87 info.has_audio =
false;
88 info.has_video =
true;
89 info.has_single_image =
true;
90 #if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
92 info.file_size = image->sizeInBytes();
94 info.file_size = image->byteCount();
96 info.vcodec =
"QImage";
97 if (!default_svg_size.isEmpty()) {
99 info.width = default_svg_size.width();
100 info.height = default_svg_size.height();
103 info.width = image->width();
104 info.height = image->height();
106 info.pixel_ratio.num = 1;
107 info.pixel_ratio.den = 1;
110 info.video_timebase.num = 1;
111 info.video_timebase.den = 30;
113 info.video_length = 60 * 60 *
info.fps.num;
114 info.duration =
static_cast<float>(
info.video_length /
info.fps.ToDouble());
123 info.display_ratio.num = size.
num;
124 info.display_ratio.den = size.
den;
127 max_size.setWidth(
info.width);
128 max_size.setHeight(
info.height);
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;
184 requested_frame,
info.fps,
info.sample_rate,
info.channels);
185 auto sz = cached_image->size();
188 auto image_frame = std::make_shared<Frame>(
189 requested_frame, sz.width(), sz.height(),
"#000000",
190 sample_count,
info.channels);
191 image_frame->AddImage(cached_image);
198QSize 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,
227 round(max_width / (
float(
info.width) /
float(
info.height))));
228 QSize height_size(round(max_height / (
float(
info.height) /
float(
info.width))),
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 const QString lower_path = path.toLower();
261 const bool is_svg = lower_path.endsWith(
".svg") || lower_path.endsWith(
".svgz");
263 if (is_svg && !parent) {
267 bounded_size.scale(max_decode_size, Qt::KeepAspectRatio);
268 max_width = bounded_size.width();
269 max_height = bounded_size.height();
270 }
else if (bounded_size.width() > max_decode_size.width() ||
271 bounded_size.height() > max_decode_size.height()) {
272 bounded_size.scale(max_decode_size, Qt::KeepAspectRatio);
273 max_width = bounded_size.width();
274 max_height = bounded_size.height();
279 return QSize(max_width, max_height);
283QSize QtImageReader::load_svg_path(QString) {
285 QSize default_size(0,0);
288 QSize current_max_size = calculate_max_size();
291#if RESVG_VERSION_MIN(0, 11)
292 ResvgRenderer renderer(path, resvg_options);
293 if (renderer.isValid()) {
294 default_size = renderer.defaultSize();
296 QSize svg_size = default_size.scaled(current_max_size, Qt::KeepAspectRatio);
297 auto qimage = renderer.renderToImage(svg_size);
298 image = std::make_shared<QImage>(
299 qimage.convertToFormat(QImage::Format_RGBA8888_Premultiplied));
302#elif RESVG_VERSION_MIN(0, 0)
303 ResvgRenderer renderer(path);
304 if (renderer.isValid()) {
305 default_size = renderer.defaultSize();
307 QSize svg_size = default_size.scaled(current_max_size, Qt::KeepAspectRatio);
309 image = std::make_shared<QImage>(svg_size,
310 QImage::Format_RGBA8888_Premultiplied);
311 image->fill(Qt::transparent);
312 QPainter p(image.get());
321 image = std::make_shared<QImage>();
322 loaded = image->load(path);
326 default_size.setWidth(image->width());
327 default_size.setHeight(image->height());
329 if (image->width() < current_max_size.width() || image->height() < current_max_size.height()) {
331 QSize svg_size = image->size().scaled(
332 current_max_size, Qt::KeepAspectRatio);
333 if (QCoreApplication::instance()) {
338 QSvgRenderer renderer(path);
339 if (renderer.isValid()) {
340 image = std::make_shared<QImage>(
341 svg_size, QImage::Format_RGBA8888_Premultiplied);
342 image->fill(Qt::transparent);
343 QPainter p(image.get());
347 image = std::make_shared<QImage>(image->scaled(
348 svg_size, Qt::KeepAspectRatio, Qt::SmoothTransformation));
351 image = std::make_shared<QImage>(image->scaled(
352 svg_size, Qt::KeepAspectRatio, Qt::SmoothTransformation));
356 image = std::make_shared<QImage>(image->scaled(
357 svg_size, Qt::KeepAspectRatio, Qt::SmoothTransformation));
378 root[
"type"] =
"QtImageReader";
379 root[
"path"] = path.toStdString();
395 catch (
const std::exception& e)
398 throw InvalidJSON(
"JSON is invalid (missing keys or invalid data types)");
409 if (!root[
"path"].isNull())
410 path = QString::fromStdString(root[
"path"].asString());
Header file for CacheMemory class.
Header file for Clip class.
Shared helpers for Crop effect scaling logic.
Header file for all Exception classes.
Header file for QtImageReader class.
Header file for Timeline class.
This class represents a clip (used to arrange readers on the timeline).
openshot::Keyframe scale_x
Curve representing the horizontal scaling in percent (0 to 1).
openshot::TimelineBase * ParentTimeline() override
Get the associated Timeline pointer (if any).
openshot::Keyframe scale_y
Curve representing the vertical scaling in percent (0 to 1).
openshot::ScaleType scale
double Y
The Y value of the coordinate (usually representing the value of the property being animated).
This class represents a fraction.
int num
Numerator for the fraction.
void Reduce()
Reduce this fraction (i.e. 640/480 = 4/3).
int den
Denominator for the fraction.
int GetSamplesPerFrame(openshot::Fraction fps, int sample_rate, int channels)
Calculate the # of samples per video frame (for the current frame number).
Exception for files that can not be found or opened.
Exception for invalid JSON.
Point GetMaxPoint() const
Get max point (by Y coordinate).
Coordinate co
This is the primary coordinate.
Json::Value JsonValue() const override
Generate Json::Value for this object.
QtImageReader(std::string path, bool inspect_reader=true)
Constructor for QtImageReader.
std::string Json() const override
Generate JSON string of this object.
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
void Open() override
Open File - which is called by the constructor automatically.
void SetJson(const std::string value) override
Load JSON string into this object.
void Close() override
Close File.
std::shared_ptr< Frame > GetFrame(int64_t requested_frame) override
openshot::ReaderInfo info
Information about the current media file.
int MaxDecodeWidth() const
Return the current maximum decoded frame width (0 when unlimited).
virtual void SetJsonValue(const Json::Value root)=0
Load Json::Value into this object.
virtual Json::Value JsonValue() const =0
Generate Json::Value for this object.
int MaxDecodeHeight() const
Return the current maximum decoded frame height (0 when unlimited).
std::recursive_mutex getFrameMutex
Mutex for multiple threads.
bool HasMaxDecodeSize() const
Return true when a maximum decoded frame size is active.
openshot::ClipBase * ParentClip()
Parent clip object of this reader (which can be unparented and NULL).
Exception when a reader is closed, and a frame is requested.
int preview_height
Optional preview width of timeline image. If your preview window is smaller than the timeline,...
int preview_width
Optional preview width of timeline image. If your preview window is smaller than the timeline,...
This namespace is the default namespace for all code in the openshot library.
@ SCALE_FIT
Scale the clip until either height or width fills the canvas (with no cropping).
@ SCALE_STRETCH
Scale the clip until both height and width fill the canvas (distort to fit).
@ SCALE_CROP
Scale the clip until both height and width fill the canvas (cropping the overlap).
@ SCALE_NONE
Do not scale the clip.
void ApplyCropResizeScale(Clip *clip, int source_width, int source_height, int &max_width, int &max_height)
Scale the requested max_width / max_height based on the Crop resize amount, capped by source size.
const Json::Value stringToJson(const std::string value)
int width
The width of the video (in pixesl).
int height
The height of the video (in pixels).