OpenShot Library | libopenshot  0.2.4
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 "../include/ImageReader.h"
35 
36 using namespace openshot;
37 
38 ImageReader::ImageReader(std::string path) : path(path), is_open(false)
39 {
40  // Open and Close the reader, to populate its attributes (such as height, width, etc...)
41  Open();
42  Close();
43 }
44 
45 ImageReader::ImageReader(std::string path, bool inspect_reader) : path(path), is_open(false)
46 {
47  // Open and Close the reader, to populate its attributes (such as height, width, etc...)
48  if (inspect_reader) {
49  Open();
50  Close();
51  }
52 }
53 
54 // Open image file
56 {
57  // Open reader if not already open
58  if (!is_open)
59  {
60  // Attempt to open file
61  try
62  {
63  // load image
64  image = std::shared_ptr<Magick::Image>(new Magick::Image(path));
65 
66  // Give image a transparent background color
67  image->backgroundColor(Magick::Color("none"));
68  MAGICK_IMAGE_ALPHA(image, true);
69  }
70  catch (const Magick::Exception& e) {
71  // raise exception
72  throw InvalidFile("File could not be opened.", path);
73  }
74 
75  // Update image properties
76  info.has_audio = false;
77  info.has_video = true;
78  info.has_single_image = true;
79  info.file_size = image->fileSize();
80  info.vcodec = image->format();
81  info.width = image->size().width();
82  info.height = image->size().height();
83  info.pixel_ratio.num = 1;
84  info.pixel_ratio.den = 1;
85  info.duration = 60 * 60 * 1; // 1 hour duration
86  info.fps.num = 30;
87  info.fps.den = 1;
89  info.video_timebase.den = 30;
91 
92  // Calculate the DAR (display aspect ratio)
94 
95  // Reduce size fraction
96  size.Reduce();
97 
98  // Set the ratio based on the reduced fraction
99  info.display_ratio.num = size.num;
100  info.display_ratio.den = size.den;
101 
102  // Mark as "open"
103  is_open = true;
104  }
105 }
106 
107 // Close image file
109 {
110  // Close all objects, if reader is 'open'
111  if (is_open)
112  {
113  // Mark as "closed"
114  is_open = false;
115 
116  // Delete the image
117  image.reset();
118  }
119 }
120 
121 // Get an openshot::Frame object for a specific frame number of this reader.
122 std::shared_ptr<Frame> ImageReader::GetFrame(int64_t requested_frame)
123 {
124  // Check for open reader (or throw exception)
125  if (!is_open)
126  throw ReaderClosed("The FFmpegReader is closed. Call Open() before calling this method.", path);
127 
128  // Create or get frame object
129  std::shared_ptr<Frame> image_frame(new Frame(requested_frame, image->size().width(), image->size().height(), "#000000", 0, 2));
130 
131  // Add Image data to frame
132  image_frame->AddMagickImage(image);
133 
134  // return frame object
135  return image_frame;
136 }
137 
138 // Generate JSON string of this object
139 std::string ImageReader::Json() {
140 
141  // Return formatted string
142  return JsonValue().toStyledString();
143 }
144 
145 // Generate Json::JsonValue for this object
146 Json::Value ImageReader::JsonValue() {
147 
148  // Create root json object
149  Json::Value root = ReaderBase::JsonValue(); // get parent properties
150  root["type"] = "ImageReader";
151  root["path"] = path;
152 
153  // return JsonValue
154  return root;
155 }
156 
157 // Load JSON string into this object
158 void ImageReader::SetJson(std::string value) {
159 
160  // Parse JSON string into JSON objects
161  Json::Value root;
162  Json::CharReaderBuilder rbuilder;
163  Json::CharReader* reader(rbuilder.newCharReader());
164 
165  std::string errors;
166  bool success = reader->parse( value.c_str(),
167  value.c_str() + value.size(), &root, &errors );
168  delete reader;
169 
170  if (!success)
171  // Raise exception
172  throw InvalidJSON("JSON could not be parsed (or is invalid)");
173 
174  try
175  {
176  // Set all values that match
177  SetJsonValue(root);
178  }
179  catch (const std::exception& e)
180  {
181  // Error parsing JSON (or missing keys)
182  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
183  }
184 }
185 
186 // Load Json::JsonValue into this object
187 void ImageReader::SetJsonValue(Json::Value root) {
188 
189  // Set parent data
191 
192  // Set data from Json (if key is found)
193  if (!root["path"].isNull())
194  path = root["path"].asString();
195 
196  // Re-Open path, and re-init everything (if needed)
197  if (is_open)
198  {
199  Close();
200  Open();
201  }
202 }
203 
204 #endif //USE_IMAGEMAGICK
int num
Numerator for the fraction.
Definition: Fraction.h:47
ImageReader(std::string path)
Definition: ImageReader.cpp:38
int width
The width of the video (in pixesl)
Definition: ReaderBase.h:68
#define MAGICK_IMAGE_ALPHA(im, a)
This class represents a single frame of video (i.e. image & audio data)
Definition: Frame.h:106
float duration
Length of time (in seconds)
Definition: ReaderBase.h:65
std::string Json()
Get and Set JSON methods.
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
void SetJson(std::string value)
Load JSON string into this object.
bool has_audio
Determines if this file has an audio stream.
Definition: ReaderBase.h:63
void SetJsonValue(Json::Value root)
Load Json::JsonValue into this object.
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
std::shared_ptr< Frame > GetFrame(int64_t requested_frame)
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
void Close()
Close File.
void Open()
Open File - which is called by the constructor automatically.
Definition: ImageReader.cpp:55
This class represents a fraction.
Definition: Fraction.h:45
bool has_single_image
Determines if this file only contains a single image.
Definition: ReaderBase.h:64
virtual Json::Value JsonValue()=0
Generate Json::JsonValue for this object.
Definition: ReaderBase.cpp:116
virtual void SetJsonValue(Json::Value root)=0
Load Json::JsonValue into this object.
Definition: ReaderBase.cpp:170
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
This namespace is the default namespace for all code in the openshot library.
Exception for invalid JSON.
Definition: Exceptions.h:205
Json::Value JsonValue()
Generate Json::JsonValue for this object.
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
int den
Denominator for the fraction.
Definition: Fraction.h:48
openshot::Fraction fps
Frames per second, as a fraction (i.e. 24/1 = 24 fps)
Definition: ReaderBase.h:70
double ToDouble()
Return this fraction as a double (i.e. 1/2 = 0.5)
Definition: Fraction.cpp:49