OpenShot Library | libopenshot  0.2.6
ImageReader.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Source file for ImageReader class
4  * @author Jonathan Thomas <jonathan@openshot.org>
5  *
6  * @ref License
7  */
8 
9 /* LICENSE
10  *
11  * Copyright (c) 2008-2019 OpenShot Studios, LLC
12  * <http://www.openshotstudios.com/>. This file is part of
13  * OpenShot Library (libopenshot), an open-source project dedicated to
14  * delivering high quality video editing and animation solutions to the
15  * world. For more information visit <http://www.openshot.org/>.
16  *
17  * OpenShot Library (libopenshot) is free software: you can redistribute it
18  * and/or modify it under the terms of the GNU Lesser General Public License
19  * as published by the Free Software Foundation, either version 3 of the
20  * License, or (at your option) any later version.
21  *
22  * OpenShot Library (libopenshot) is distributed in the hope that it will be
23  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU Lesser General Public License for more details.
26  *
27  * You should have received a copy of the GNU Lesser General Public License
28  * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
29  */
30 
31 // Require ImageMagick support
32 #ifdef USE_IMAGEMAGICK
33 
34 #include "ImageReader.h"
35 #include "Exceptions.h"
36 
37 using namespace openshot;
38 
39 ImageReader::ImageReader(const std::string& path, bool inspect_reader) : path(path), is_open(false)
40 {
41  // Open and Close the reader, to populate its attributes (such as height, width, etc...)
42  if (inspect_reader) {
43  Open();
44  Close();
45  }
46 }
47 
48 // Open image file
50 {
51  // Open reader if not already open
52  if (!is_open)
53  {
54  // Attempt to open file
55  try
56  {
57  // load image
58  image = std::make_shared<Magick::Image>(path);
59 
60  // Give image a transparent background color
61  image->backgroundColor(Magick::Color("none"));
62  MAGICK_IMAGE_ALPHA(image, true);
63  }
64  catch (const Magick::Exception& e) {
65  // raise exception
66  throw InvalidFile("File could not be opened.", path);
67  }
68 
69  // Update image properties
70  info.has_audio = false;
71  info.has_video = true;
72  info.has_single_image = true;
73  info.file_size = image->fileSize();
74  info.vcodec = image->format();
75  info.width = image->size().width();
76  info.height = image->size().height();
77  info.pixel_ratio.num = 1;
78  info.pixel_ratio.den = 1;
79  info.duration = 60 * 60 * 1; // 1 hour duration
80  info.fps.num = 30;
81  info.fps.den = 1;
83  info.video_timebase.den = 30;
85 
86  // Calculate the DAR (display aspect ratio)
88 
89  // Reduce size fraction
90  size.Reduce();
91 
92  // Set the ratio based on the reduced fraction
93  info.display_ratio.num = size.num;
94  info.display_ratio.den = size.den;
95 
96  // Mark as "open"
97  is_open = true;
98  }
99 }
100 
101 // Close image file
103 {
104  // Close all objects, if reader is 'open'
105  if (is_open)
106  {
107  // Mark as "closed"
108  is_open = false;
109 
110  // Delete the image
111  image.reset();
112  }
113 }
114 
115 // Get an openshot::Frame object for a specific frame number of this reader.
116 std::shared_ptr<Frame> ImageReader::GetFrame(int64_t requested_frame)
117 {
118  // Check for open reader (or throw exception)
119  if (!is_open)
120  throw ReaderClosed("The FFmpegReader is closed. Call Open() before calling this method.", path);
121 
122  // Create or get frame object
123  auto image_frame = std::make_shared<Frame>(
124  requested_frame, image->size().width(), image->size().height(),
125  "#000000", 0, 2);
126 
127  // Add Image data to frame
128  image_frame->AddMagickImage(image);
129 
130  // return frame object
131  return image_frame;
132 }
133 
134 // Generate JSON string of this object
135 std::string ImageReader::Json() const {
136 
137  // Return formatted string
138  return JsonValue().toStyledString();
139 }
140 
141 // Generate Json::Value for this object
142 Json::Value ImageReader::JsonValue() const {
143 
144  // Create root json object
145  Json::Value root = ReaderBase::JsonValue(); // get parent properties
146  root["type"] = "ImageReader";
147  root["path"] = path;
148 
149  // return JsonValue
150  return root;
151 }
152 
153 // Load JSON string into this object
154 void ImageReader::SetJson(const std::string value) {
155 
156  // Parse JSON string into JSON objects
157  try
158  {
159  const Json::Value root = openshot::stringToJson(value);
160  // Set all values that match
161  SetJsonValue(root);
162  }
163  catch (const std::exception& e)
164  {
165  // Error parsing JSON (or missing keys)
166  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
167  }
168 }
169 
170 // Load Json::Value into this object
171 void ImageReader::SetJsonValue(const Json::Value root) {
172 
173  // Set parent data
175 
176  // Set data from Json (if key is found)
177  if (!root["path"].isNull())
178  path = root["path"].asString();
179 
180  // Re-Open path, and re-init everything (if needed)
181  if (is_open)
182  {
183  Close();
184  Open();
185  }
186 }
187 
188 #endif //USE_IMAGEMAGICK
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
int num
Numerator for the fraction.
Definition: Fraction.h:50
void Close() override
Close File.
int width
The width of the video (in pixesl)
Definition: ReaderBase.h:68
#define MAGICK_IMAGE_ALPHA(im, a)
std::shared_ptr< Frame > GetFrame(int64_t requested_frame) override
float duration
Length of time (in seconds)
Definition: ReaderBase.h:65
double ToDouble() const
Return this fraction as a double (i.e. 1/2 = 0.5)
Definition: Fraction.cpp:59
void SetJson(const std::string value) override
Load JSON string into this object.
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:34
Exception when a reader is closed, and a frame is requested.
Definition: Exceptions.h:337
bool has_video
Determines if this file has a video stream.
Definition: ReaderBase.h:62
int64_t file_size
Size of file (in bytes)
Definition: ReaderBase.h:66
Header file for all Exception classes.
bool has_audio
Determines if this file has an audio stream.
Definition: ReaderBase.h:63
virtual Json::Value JsonValue() const =0
Generate Json::Value for this object.
Definition: ReaderBase.cpp:116
int64_t video_length
The number of frames in the video stream.
Definition: ReaderBase.h:75
int height
The height of the video (in pixels)
Definition: ReaderBase.h:67
openshot::Fraction video_timebase
The video timebase determines how long each frame stays on the screen.
Definition: ReaderBase.h:77
Exception for files that can not be found or opened.
Definition: Exceptions.h:173
This class represents a fraction.
Definition: Fraction.h:48
bool has_single_image
Determines if this file only contains a single image.
Definition: ReaderBase.h:64
void Open() override
Open File - which is called by the constructor automatically.
Definition: ImageReader.cpp:49
virtual void SetJsonValue(const Json::Value root)=0
Load Json::Value into this object.
Definition: ReaderBase.cpp:171
openshot::ReaderInfo info
Information about the current media file.
Definition: ReaderBase.h:111
std::string vcodec
The name of the video codec used to encode / decode the video stream.
Definition: ReaderBase.h:74
Json::Value JsonValue() const override
Generate Json::Value for this object.
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:46
Exception for invalid JSON.
Definition: Exceptions.h:205
openshot::Fraction display_ratio
The ratio of width to height of the video stream (i.e. 640x480 has a ratio of 4/3) ...
Definition: ReaderBase.h:73
openshot::Fraction pixel_ratio
The pixel ratio of the video stream as a fraction (i.e. some pixels are not square) ...
Definition: ReaderBase.h:72
ImageReader(const std::string &path, bool inspect_reader=true)
Constructor for ImageReader.
Definition: ImageReader.cpp:39
Header file for ImageReader class.
int den
Denominator for the fraction.
Definition: Fraction.h:51
openshot::Fraction fps
Frames per second, as a fraction (i.e. 24/1 = 24 fps)
Definition: ReaderBase.h:70
std::string Json() const override
Generate JSON string of this object.