OpenShot Library | libopenshot 0.5.0
Loading...
Searching...
No Matches
ImageReader.cpp
Go to the documentation of this file.
1
8
9// Copyright (c) 2008-2019 OpenShot Studios, LLC
10//
11// SPDX-License-Identifier: LGPL-3.0-or-later
12
13// Require ImageMagick support
14#ifdef USE_IMAGEMAGICK
15
16#include "MagickUtilities.h"
17#include "QtUtilities.h"
18
19#include "ImageReader.h"
20#include "Exceptions.h"
21#include "Frame.h"
22
23using namespace openshot;
24
25ImageReader::ImageReader(const std::string& path, bool inspect_reader) : path(path), is_open(false)
26{
27 // Open and Close the reader, to populate its attributes (such as height, width, etc...)
28 if (inspect_reader) {
29 Open();
30 Close();
31 }
32}
33
34// Open image file
36{
37 // Open reader if not already open
38 if (!is_open)
39 {
40 // Attempt to open file
41 try
42 {
43 // load image
44 image = std::make_shared<Magick::Image>(path);
45
46 // Give image a transparent background color
47 image->backgroundColor(Magick::Color("none"));
48 MAGICK_IMAGE_ALPHA(image, true);
49 }
50 catch (const Magick::Exception& e) {
51 // raise exception
52 throw InvalidFile("File could not be opened.", path);
53 }
54
55 // Update image properties
56 info.has_audio = false;
57 info.has_video = true;
58 info.has_single_image = true;
59 info.file_size = image->fileSize();
60 info.vcodec = image->format();
61 info.width = image->size().width();
62 info.height = image->size().height();
63 info.pixel_ratio = openshot::Fraction(1, 1);
64 info.fps = openshot::Fraction(30, 1);
65 info.video_timebase = info.fps.Reciprocal();
66 // Default still-image duration: 1 hour, aligned to fps
67 info.video_length = 60 * 60 * info.fps.num; // 3600 seconds * 30 fps
68 info.duration = static_cast<float>(info.video_length / info.fps.ToDouble());
69
70 // Calculate the DAR (display aspect ratio)
71 Fraction dar(
72 info.width * info.pixel_ratio.num,
73 info.height * info.pixel_ratio.den);
74
75 // Reduce DAR fraction & set ratio
76 dar.Reduce();
77 info.display_ratio = dar;
78
79 // Mark as "open"
80 is_open = true;
81 }
82}
83
85{
86 if (is_open)
87 {
88 is_open = false;
89 // Delete the image
90 image.reset();
91 }
92}
93
94// Get an openshot::Frame object for a specific frame number of this reader.
95std::shared_ptr<Frame> ImageReader::GetFrame(int64_t requested_frame)
96{
97 if (!is_open) {
98 throw ReaderClosed(
99 "The ImageReader is closed. "
100 "Call Open() before calling this method.", path);
101 }
102
103 // Create or get frame object
104 auto image_frame = std::make_shared<Frame>(
105 requested_frame,
106 image->size().width(), image->size().height(),
107 "#000000", 0, 2);
108
109 // Add Image data to frame
110 auto qimage = openshot::Magick2QImage(image);
111 image_frame->AddImage(qimage);
112 return image_frame;
113}
114
115// Generate JSON string of this object
116std::string ImageReader::Json() const {
117
118 // Return formatted string
119 return JsonValue().toStyledString();
120}
121
122// Generate Json::Value for this object
123Json::Value ImageReader::JsonValue() const {
124
125 // get parent properties
126 Json::Value root = ReaderBase::JsonValue();
127
128 root["type"] = "ImageReader";
129 root["path"] = path;
130 return root;
131}
132
133// Load JSON string into this object
134void ImageReader::SetJson(const std::string value) {
135
136 // Parse JSON string into JSON objects
137 try
138 {
139 const Json::Value root = openshot::stringToJson(value);
140 // Set all values that match
141 SetJsonValue(root);
142 }
143 catch (const std::exception& e)
144 {
145 throw InvalidJSON(
146 "JSON is invalid (missing keys or invalid data types)");
147 }
148}
149
150// Load Json::Value into this object
151void ImageReader::SetJsonValue(const Json::Value root) {
152
153 // Set parent data
155
156 // Set data from Json (if key is found)
157 if (!root["path"].isNull())
158 path = root["path"].asString();
159
160 if (is_open) {
161 Close();
162 Open();
163 }
164}
165
166#endif //USE_IMAGEMAGICK
Header file for all Exception classes.
Header file for Frame class.
Header file for ImageReader class.
Header file for MagickUtilities (IM6/IM7 compatibility overlay).
#define MAGICK_IMAGE_ALPHA(im, a)
Header file for QtUtilities (compatibiity overlay).
This class represents a fraction.
Definition Fraction.h:30
void Reduce()
Reduce this fraction (i.e. 640/480 = 4/3).
Definition Fraction.cpp:65
void Open() override
Open File - which is called by the constructor automatically.
std::shared_ptr< Frame > GetFrame(int64_t requested_frame) override
ImageReader(const std::string &path, bool inspect_reader=true)
Constructor for ImageReader.
void SetJson(const std::string value) override
Load JSON string into this object.
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Json::Value JsonValue() const override
Generate Json::Value for this object.
std::string Json() const override
Generate JSON string of this object.
void Close() override
Close File.
Exception for files that can not be found or opened.
Definition Exceptions.h:188
Exception for invalid JSON.
Definition Exceptions.h:218
openshot::ReaderInfo info
Information about the current media file.
Definition ReaderBase.h:88
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.
Exception when a reader is closed, and a frame is requested.
Definition Exceptions.h:364
This namespace is the default namespace for all code in the openshot library.
Definition Compressor.h:29
std::shared_ptr< QImage > Magick2QImage(std::shared_ptr< Magick::Image >)
Convert Magick::Image to QImage.
const Json::Value stringToJson(const std::string value)
Definition Json.cpp:16