OpenShot Library | libopenshot  0.1.2
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) throw(InvalidFile) : 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 // Open image file
41 {
42  // Open reader if not already open
43  if (!is_open)
44  {
45  // Attempt to open file
46  try
47  {
48  // load image
49  image = tr1::shared_ptr<Magick::Image>(new Magick::Image(path));
50 
51  // Give image a transparent background color
52  image->backgroundColor(Magick::Color("none"));
53  image->matte(true);
54  }
55  catch (Magick::Exception e) {
56  // raise exception
57  throw InvalidFile("File could not be opened.", path);
58  }
59 
60  // Update image properties
61  info.has_audio = false;
62  info.has_video = true;
63  info.has_single_image = true;
64  info.file_size = image->fileSize();
65  info.vcodec = image->format();
66  info.width = image->size().width();
67  info.height = image->size().height();
68  info.pixel_ratio.num = 1;
69  info.pixel_ratio.den = 1;
70  info.duration = 60 * 60 * 24; // 24 hour duration
71  info.fps.num = 30;
72  info.fps.den = 1;
74  info.video_timebase.den = 30;
76 
77  // Calculate the DAR (display aspect ratio)
79 
80  // Reduce size fraction
81  size.Reduce();
82 
83  // Set the ratio based on the reduced fraction
84  info.display_ratio.num = size.num;
85  info.display_ratio.den = size.den;
86 
87  // Mark as "open"
88  is_open = true;
89  }
90 }
91 
92 // Close image file
94 {
95  // Close all objects, if reader is 'open'
96  if (is_open)
97  {
98  // Mark as "closed"
99  is_open = false;
100 
101  // Delete the image
102  image.reset();
103  }
104 }
105 
106 // Get an openshot::Frame object for a specific frame number of this reader.
107 tr1::shared_ptr<Frame> ImageReader::GetFrame(long int requested_frame) throw(ReaderClosed)
108 {
109  // Check for open reader (or throw exception)
110  if (!is_open)
111  throw ReaderClosed("The FFmpegReader is closed. Call Open() before calling this method.", path);
112 
113  // Create or get frame object
114  tr1::shared_ptr<Frame> image_frame(new Frame(requested_frame, image->size().width(), image->size().height(), "#000000", 0, 2));
115 
116  // Add Image data to frame
117  image_frame->AddMagickImage(image);
118 
119  // return frame object
120  return image_frame;
121 }
122 
123 // Generate JSON string of this object
125 
126  // Return formatted string
127  return JsonValue().toStyledString();
128 }
129 
130 // Generate Json::JsonValue for this object
131 Json::Value ImageReader::JsonValue() {
132 
133  // Create root json object
134  Json::Value root = ReaderBase::JsonValue(); // get parent properties
135  root["type"] = "ImageReader";
136  root["path"] = path;
137 
138  // return JsonValue
139  return root;
140 }
141 
142 // Load JSON string into this object
143 void ImageReader::SetJson(string value) throw(InvalidJSON) {
144 
145  // Parse JSON string into JSON objects
146  Json::Value root;
147  Json::Reader reader;
148  bool success = reader.parse( value, root );
149  if (!success)
150  // Raise exception
151  throw InvalidJSON("JSON could not be parsed (or is invalid)", "");
152 
153  try
154  {
155  // Set all values that match
156  SetJsonValue(root);
157  }
158  catch (exception e)
159  {
160  // Error parsing JSON (or missing keys)
161  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)", "");
162  }
163 }
164 
165 // Load Json::JsonValue into this object
166 void ImageReader::SetJsonValue(Json::Value root) throw(InvalidFile) {
167 
168  // Set parent data
170 
171  // Set data from Json (if key is found)
172  if (!root["path"].isNull())
173  path = root["path"].asString();
174 
175  // Re-Open path, and re-init everything (if needed)
176  if (is_open)
177  {
178  Close();
179  Open();
180  }
181 }
long long file_size
Size of file (in bytes)
Definition: ReaderBase.h:65
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
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.
int height
The height of the video (in pixels)
Definition: ReaderBase.h:66
Exception for files that can not be found or opened.
Definition: Exceptions.h:132
void Close()
Close File.
Definition: ImageReader.cpp:93
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:40
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:104
virtual void SetJsonValue(Json::Value root)=0
Load Json::JsonValue into this object.
Definition: ReaderBase.cpp:153
ReaderInfo info
Information about the current media file.
Definition: ReaderBase.h:108
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
tr1::shared_ptr< Frame > GetFrame(long int requested_frame)
long int video_length
The number of frames in the video stream.
Definition: ReaderBase.h:74
double ToDouble()
Return this fraction as a double (i.e. 1/2 = 0.5)
Definition: Fraction.cpp:46