26#include <QImageReader>
51 QSize default_svg_size;
54 if (path.toLower().endsWith(
".svg") || path.toLower().endsWith(
".svgz")) {
55 #if RESVG_VERSION_MIN(0, 11)
57 resvg_options.loadSystemFonts();
61 default_svg_size = load_svg_path(path);
62 if (!default_svg_size.isEmpty()) {
71 image = std::make_shared<QImage>();
72 QImageReader imgReader( path );
73 imgReader.setAutoTransform(
true );
74 imgReader.setDecideFormatFromContent(
true );
75 loaded = imgReader.read(image.get());
80 throw InvalidFile(
"File could not be opened.", path.toStdString());
87 #if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
94 if (!default_svg_size.isEmpty()) {
143 cached_image.reset();
155 throw ReaderClosed(
"The Image is closed. Call Open() before calling this method.", path.toStdString());
158 const std::lock_guard<std::recursive_mutex> lock(
getFrameMutex);
161 QSize current_max_size = calculate_max_size();
164 if (!cached_image || max_size != current_max_size) {
166 if (path.toLower().endsWith(
".svg") || path.toLower().endsWith(
".svgz")) {
172 cached_image = std::make_shared<QImage>(image->scaled(
174 Qt::KeepAspectRatio, Qt::SmoothTransformation));
177 max_size = current_max_size;
182 auto sz = cached_image->size();
185 auto image_frame = std::make_shared<Frame>(
186 requested_frame, sz.width(), sz.height(),
"#000000",
188 image_frame->AddImage(cached_image);
195QSize QtImageReader::calculate_max_size() {
199 if (max_width == 0 || max_height == 0) {
216 max_width = std::max(
float(max_width), max_width * max_scale_x);
217 max_height = std::max(
float(max_height), max_height * max_scale_y);
223 QSize width_size(max_width * max_scale_x,
226 max_height * max_scale_y);
228 if (width_size.width() >= max_width && width_size.height() >= max_height) {
229 max_width = std::max(max_width, width_size.width());
230 max_height = std::max(max_height, width_size.height());
232 max_width = std::max(max_width, height_size.width());
233 max_height = std::max(max_height, height_size.height());
239 float preview_ratio = 1.0;
246 max_width =
info.
width * max_scale_x * preview_ratio;
247 max_height =
info.
height * max_scale_y * preview_ratio;
255 return QSize(max_width, max_height);
259QSize QtImageReader::load_svg_path(QString) {
261 QSize default_size(0,0);
264 QSize current_max_size = calculate_max_size();
267#if RESVG_VERSION_MIN(0, 11)
268 ResvgRenderer renderer(path, resvg_options);
269 if (renderer.isValid()) {
270 default_size = renderer.defaultSize();
272 QSize svg_size = default_size.scaled(current_max_size, Qt::KeepAspectRatio);
273 auto qimage = renderer.renderToImage(svg_size);
274 image = std::make_shared<QImage>(
275 qimage.convertToFormat(QImage::Format_RGBA8888_Premultiplied));
278#elif RESVG_VERSION_MIN(0, 0)
279 ResvgRenderer renderer(path);
280 if (renderer.isValid()) {
281 default_size = renderer.defaultSize();
283 QSize svg_size = default_size.scaled(current_max_size, Qt::KeepAspectRatio);
285 image = std::make_shared<QImage>(svg_size,
286 QImage::Format_RGBA8888_Premultiplied);
287 image->fill(Qt::transparent);
288 QPainter p(image.get());
297 image = std::make_shared<QImage>();
298 loaded = image->load(path);
302 default_size.setWidth(image->width());
303 default_size.setHeight(image->height());
305 if (image->width() < current_max_size.width() || image->height() < current_max_size.height()) {
307 QSize svg_size = image->size().scaled(
308 current_max_size, Qt::KeepAspectRatio);
309 if (QCoreApplication::instance()) {
312 image = std::make_shared<QImage>(QIcon(path).pixmap(svg_size).toImage());
315 image = std::make_shared<QImage>(image->scaled(
316 svg_size, Qt::KeepAspectRatio, Qt::SmoothTransformation));
337 root[
"type"] =
"QtImageReader";
338 root[
"path"] = path.toStdString();
354 catch (
const std::exception& e)
357 throw InvalidJSON(
"JSON is invalid (missing keys or invalid data types)");
368 if (!root[
"path"].isNull())
369 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
The scale determines how a clip should be resized to fit its parent.
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.
double ToDouble() const
Return this fraction as a double (i.e. 1/2 = 0.5)
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.
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.
std::recursive_mutex getFrameMutex
Mutex for multiple threads.
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 class represents a 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)
bool has_single_image
Determines if this file only contains a single image.
float duration
Length of time (in seconds)
int width
The width of the video (in pixesl)
int channels
The number of audio channels used in the audio stream.
openshot::Fraction fps
Frames per second, as a fraction (i.e. 24/1 = 24 fps)
openshot::Fraction display_ratio
The ratio of width to height of the video stream (i.e. 640x480 has a ratio of 4/3)
int height
The height of the video (in pixels)
int64_t video_length
The number of frames in the video stream.
std::string acodec
The name of the audio codec used to encode / decode the video stream.
std::string vcodec
The name of the video codec used to encode / decode the video stream.
openshot::Fraction pixel_ratio
The pixel ratio of the video stream as a fraction (i.e. some pixels are not square)
bool has_video
Determines if this file has a video stream.
bool has_audio
Determines if this file has an audio stream.
openshot::Fraction video_timebase
The video timebase determines how long each frame stays on the screen.
int sample_rate
The number of audio samples per second (44100 is a common sample rate)
int64_t file_size
Size of file (in bytes)