OpenShot Library | libopenshot  0.1.9
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  * @section LICENSE
7  *
8  * Copyright (c) 2008-2014 OpenShot Studios, LLC
9  * <http://www.openshotstudios.com/>. This file is part of
10  * OpenShot Library (libopenshot), an open-source project dedicated to
11  * delivering high quality video editing and animation solutions to the
12  * world. For more information visit <http://www.openshot.org/>.
13  *
14  * OpenShot Library (libopenshot) is free software: you can redistribute it
15  * and/or modify it under the terms of the GNU Lesser General Public License
16  * as published by the Free Software Foundation, either version 3 of the
17  * License, or (at your option) any later version.
18  *
19  * OpenShot Library (libopenshot) is distributed in the hope that it will be
20  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public License
25  * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
26  */
27 
28 #include "../include/ImageReader.h"
29 
30 using namespace openshot;
31 
32 ImageReader::ImageReader(string path) : path(path), is_open(false)
33 {
34  // Open and Close the reader, to populate it's attributes (such as height, width, etc...)
35  Open();
36  Close();
37 }
38 
39 ImageReader::ImageReader(string path, bool inspect_reader) : path(path), is_open(false)
40 {
41  // Open and Close the reader, to populate it's 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::shared_ptr<Magick::Image>(new Magick::Image(path));
59 
60  // Give image a transparent background color
61  image->backgroundColor(Magick::Color("none"));
62  image->matte(true);
63  }
64  catch (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 * 24; // 24 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  std::shared_ptr<Frame> image_frame(new Frame(requested_frame, image->size().width(), image->size().height(), "#000000", 0, 2));
124 
125  // Add Image data to frame
126  image_frame->AddMagickImage(image);
127 
128  // return frame object
129  return image_frame;
130 }
131 
132 // Generate JSON string of this object
134 
135  // Return formatted string
136  return JsonValue().toStyledString();
137 }
138 
139 // Generate Json::JsonValue for this object
140 Json::Value ImageReader::JsonValue() {
141 
142  // Create root json object
143  Json::Value root = ReaderBase::JsonValue(); // get parent properties
144  root["type"] = "ImageReader";
145  root["path"] = path;
146 
147  // return JsonValue
148  return root;
149 }
150 
151 // Load JSON string into this object
152 void ImageReader::SetJson(string value) {
153 
154  // Parse JSON string into JSON objects
155  Json::Value root;
156  Json::Reader reader;
157  bool success = reader.parse( value, root );
158  if (!success)
159  // Raise exception
160  throw InvalidJSON("JSON could not be parsed (or is invalid)", "");
161 
162  try
163  {
164  // Set all values that match
165  SetJsonValue(root);
166  }
167  catch (exception e)
168  {
169  // Error parsing JSON (or missing keys)
170  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)", "");
171  }
172 }
173 
174 // Load Json::JsonValue into this object
175 void ImageReader::SetJsonValue(Json::Value root) {
176 
177  // Set parent data
179 
180  // Set data from Json (if key is found)
181  if (!root["path"].isNull())
182  path = root["path"].asString();
183 
184  // Re-Open path, and re-init everything (if needed)
185  if (is_open)
186  {
187  Close();
188  Open();
189  }
190 }
int num
Numerator for the fraction.
Definition: Fraction.h:44
int width
The width of the video (in pixesl)
Definition: ReaderBase.h:67
This class represents a single frame of video (i.e. image & audio data)
Definition: Frame.h:115
float duration
Length of time (in seconds)
Definition: ReaderBase.h:64
Exception when a reader is closed, and a frame is requested.
Definition: Exceptions.h:234
bool has_video
Determines if this file has a video stream.
Definition: ReaderBase.h:61
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:72
int64_t file_size
Size of file (in bytes)
Definition: ReaderBase.h:65
bool has_audio
Determines if this file has an audio stream.
Definition: ReaderBase.h:62
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:74
int height
The height of the video (in pixels)
Definition: ReaderBase.h:66
std::shared_ptr< Frame > GetFrame(int64_t requested_frame)
Exception for files that can not be found or opened.
Definition: Exceptions.h:132
void Close()
Close File.
string Json()
Get and Set JSON methods.
void SetJson(string value)
Load JSON string into this object.
void Open()
Open File - which is called by the constructor automatically.
Definition: ImageReader.cpp:49
This class represents a fraction.
Definition: Fraction.h:42
bool has_single_image
Determines if this file only contains a single image.
Definition: ReaderBase.h:63
virtual Json::Value JsonValue()=0
Generate Json::JsonValue for this object.
Definition: ReaderBase.cpp:106
virtual void SetJsonValue(Json::Value root)=0
Load Json::JsonValue into this object.
Definition: ReaderBase.cpp:155
ReaderInfo info
Information about the current media file.
Definition: ReaderBase.h:111
Fraction fps
Frames per second, as a fraction (i.e. 24/1 = 24 fps)
Definition: ReaderBase.h:69
Fraction video_timebase
The video timebase determines how long each frame stays on the screen.
Definition: ReaderBase.h:76
Fraction pixel_ratio
The pixel ratio of the video stream as a fraction (i.e. some pixels are not square) ...
Definition: ReaderBase.h:71
ImageReader(string path)
Definition: ImageReader.cpp:32
This namespace is the default namespace for all code in the openshot library.
Exception for invalid JSON.
Definition: Exceptions.h:152
Json::Value JsonValue()
Generate Json::JsonValue for this object.
string vcodec
The name of the video codec used to encode / decode the video stream.
Definition: ReaderBase.h:73
int den
Denominator for the fraction.
Definition: Fraction.h:45
double ToDouble()
Return this fraction as a double (i.e. 1/2 = 0.5)
Definition: Fraction.cpp:46