OpenShot Library | libopenshot  0.1.9
TextReader.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Source file for TextReader 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/TextReader.h"
29 
30 using namespace openshot;
31 
32 /// Default constructor (blank text)
33 TextReader::TextReader() : width(1024), height(768), x_offset(0), y_offset(0), text(""), font("Arial"), size(10.0), text_color("#ffffff"), background_color("#000000"), is_open(false), gravity(GRAVITY_CENTER) {
34 
35  // Open and Close the reader, to populate it's attributes (such as height, width, etc...)
36  Open();
37  Close();
38 }
39 
40 TextReader::TextReader(int width, int height, int x_offset, int y_offset, GravityType gravity, string text, string font, double size, string text_color, string background_color)
41 : width(width), height(height), x_offset(x_offset), y_offset(y_offset), text(text), font(font), size(size), text_color(text_color), background_color(background_color), is_open(false), gravity(gravity)
42 {
43  // Open and Close the reader, to populate it's attributes (such as height, width, etc...)
44  Open();
45  Close();
46 }
47 
48 // Open reader
50 {
51  // Open reader if not already open
52  if (!is_open)
53  {
54  // create image
55  image = std::shared_ptr<Magick::Image>(new Magick::Image(Magick::Geometry(width,height), Magick::Color(background_color)));
56 
57  // Give image a transparent background color
58  image->backgroundColor(Magick::Color("none"));
59 
60  // Set gravity (map between OpenShot and ImageMagick)
61  switch (gravity)
62  {
63  case GRAVITY_TOP_LEFT:
64  lines.push_back(Magick::DrawableGravity(Magick::NorthWestGravity));
65  break;
66  case GRAVITY_TOP:
67  lines.push_back(Magick::DrawableGravity(Magick::NorthGravity));
68  break;
69  case GRAVITY_TOP_RIGHT:
70  lines.push_back(Magick::DrawableGravity(Magick::NorthEastGravity));
71  break;
72  case GRAVITY_LEFT:
73  lines.push_back(Magick::DrawableGravity(Magick::WestGravity));
74  break;
75  case GRAVITY_CENTER:
76  lines.push_back(Magick::DrawableGravity(Magick::CenterGravity));
77  break;
78  case GRAVITY_RIGHT:
79  lines.push_back(Magick::DrawableGravity(Magick::EastGravity));
80  break;
82  lines.push_back(Magick::DrawableGravity(Magick::SouthWestGravity));
83  break;
84  case GRAVITY_BOTTOM:
85  lines.push_back(Magick::DrawableGravity(Magick::SouthGravity));
86  break;
88  lines.push_back(Magick::DrawableGravity(Magick::SouthEastGravity));
89  break;
90  }
91 
92  // Set stroke properties
93  lines.push_back(Magick::DrawableStrokeColor(Magick::Color("none")));
94  lines.push_back(Magick::DrawableStrokeWidth(0.0));
95  lines.push_back(Magick::DrawableFillColor(text_color));
96  lines.push_back(Magick::DrawableFont(font));
97  lines.push_back(Magick::DrawablePointSize(size));
98  lines.push_back(Magick::DrawableText(x_offset, y_offset, text));
99 
100  // Draw image
101  image->draw(lines);
102 
103  // Update image properties
104  info.has_audio = false;
105  info.has_video = true;
106  info.file_size = image->fileSize();
107  info.vcodec = image->format();
108  info.width = image->size().width();
109  info.height = image->size().height();
110  info.pixel_ratio.num = 1;
111  info.pixel_ratio.den = 1;
112  info.duration = 60 * 60 * 24; // 24 hour duration
113  info.fps.num = 30;
114  info.fps.den = 1;
115  info.video_timebase.num = 1;
116  info.video_timebase.den = 30;
118 
119  // Calculate the DAR (display aspect ratio)
121 
122  // Reduce size fraction
123  size.Reduce();
124 
125  // Set the ratio based on the reduced fraction
126  info.display_ratio.num = size.num;
127  info.display_ratio.den = size.den;
128 
129  // Mark as "open"
130  is_open = true;
131  }
132 }
133 
134 // Close reader
136 {
137  // Close all objects, if reader is 'open'
138  if (is_open)
139  {
140  // Mark as "closed"
141  is_open = false;
142  }
143 }
144 
145 // Get an openshot::Frame object for a specific frame number of this reader.
146 std::shared_ptr<Frame> TextReader::GetFrame(int64_t requested_frame)
147 {
148  if (image)
149  {
150  // Create or get frame object
151  std::shared_ptr<Frame> image_frame(new Frame(requested_frame, image->size().width(), image->size().height(), "#000000", 0, 2));
152 
153  // Add Image data to frame
154  std::shared_ptr<Magick::Image> copy_image(new Magick::Image(*image.get()));
155  copy_image->modifyImage(); // actually copy the image data to this object
156  //TODO: Reimplement this with QImage
157  //image_frame->AddImage(copy_image);
158 
159  // return frame object
160  return image_frame;
161  } else {
162  // return empty frame
163  std::shared_ptr<Frame> image_frame(new Frame(1, 640, 480, "#000000", 0, 2));
164 
165  // return frame object
166  return image_frame;
167  }
168 
169 }
170 
171 // Generate JSON string of this object
173 
174  // Return formatted string
175  return JsonValue().toStyledString();
176 }
177 
178 // Generate Json::JsonValue for this object
179 Json::Value TextReader::JsonValue() {
180 
181  // Create root json object
182  Json::Value root = ReaderBase::JsonValue(); // get parent properties
183  root["type"] = "TextReader";
184  root["width"] = width;
185  root["height"] = height;
186  root["x_offset"] = x_offset;
187  root["y_offset"] = y_offset;
188  root["text"] = text;
189  root["font"] = font;
190  root["size"] = size;
191  root["text_color"] = text_color;
192  root["background_color"] = background_color;
193  root["gravity"] = gravity;
194 
195  // return JsonValue
196  return root;
197 }
198 
199 // Load JSON string into this object
200 void TextReader::SetJson(string value) {
201 
202  // Parse JSON string into JSON objects
203  Json::Value root;
204  Json::Reader reader;
205  bool success = reader.parse( value, root );
206  if (!success)
207  // Raise exception
208  throw InvalidJSON("JSON could not be parsed (or is invalid)", "");
209 
210  try
211  {
212  // Set all values that match
213  SetJsonValue(root);
214  }
215  catch (exception e)
216  {
217  // Error parsing JSON (or missing keys)
218  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)", "");
219  }
220 }
221 
222 // Load Json::JsonValue into this object
223 void TextReader::SetJsonValue(Json::Value root) {
224 
225  // Set parent data
227 
228  // Set data from Json (if key is found)
229  if (!root["width"].isNull())
230  width = root["width"].asInt();
231  if (!root["height"].isNull())
232  height = root["height"].asInt();
233  if (!root["x_offset"].isNull())
234  x_offset = root["x_offset"].asInt();
235  if (!root["y_offset"].isNull())
236  y_offset = root["y_offset"].asInt();
237  if (!root["text"].isNull())
238  text = root["text"].asString();
239  if (!root["font"].isNull())
240  font = root["font"].asString();
241  if (!root["size"].isNull())
242  size = root["size"].asDouble();
243  if (!root["text_color"].isNull())
244  text_color = root["text_color"].asString();
245  if (!root["background_color"].isNull())
246  background_color = root["background_color"].asString();
247  if (!root["gravity"].isNull())
248  gravity = (GravityType) root["gravity"].asInt();
249 
250  // Re-Open path, and re-init everything (if needed)
251  if (is_open)
252  {
253  Close();
254  Open();
255  }
256 }
int num
Numerator for the fraction.
Definition: Fraction.h:44
Align clip to the right of its parent (middle aligned)
Definition: Enums.h:42
Align clip to the bottom right of its parent.
Definition: Enums.h:45
string Json()
Get and Set JSON methods.
Definition: TextReader.cpp:172
void Close()
Close Reader.
Definition: TextReader.cpp:135
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
Json::Value JsonValue()
Generate Json::JsonValue for this object.
Definition: TextReader.cpp:179
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
Align clip to the top right of its parent.
Definition: Enums.h:39
Align clip to the bottom left of its parent.
Definition: Enums.h:43
void SetJsonValue(Json::Value root)
Load Json::JsonValue into this object.
Definition: TextReader.cpp:223
std::shared_ptr< Frame > GetFrame(int64_t requested_frame)
Definition: TextReader.cpp:146
bool has_audio
Determines if this file has an audio stream.
Definition: ReaderBase.h:62
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
Align clip to the bottom center of its parent.
Definition: Enums.h:44
Align clip to the top left of its parent.
Definition: Enums.h:37
This class represents a fraction.
Definition: Fraction.h:42
Align clip to the left of its parent (middle aligned)
Definition: Enums.h:40
TextReader()
Default constructor (blank text)
Definition: TextReader.cpp:33
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
Align clip to the center of its parent (middle aligned)
Definition: Enums.h:41
Fraction pixel_ratio
The pixel ratio of the video stream as a fraction (i.e. some pixels are not square) ...
Definition: ReaderBase.h:71
This namespace is the default namespace for all code in the openshot library.
Exception for invalid JSON.
Definition: Exceptions.h:152
string vcodec
The name of the video codec used to encode / decode the video stream.
Definition: ReaderBase.h:73
void SetJson(string value)
Load JSON string into this object.
Definition: TextReader.cpp:200
Align clip to the top center of its parent.
Definition: Enums.h:38
int den
Denominator for the fraction.
Definition: Fraction.h:45
void Open()
Open Reader - which is called by the constructor automatically.
Definition: TextReader.cpp:49
GravityType
This enumeration determines how clips are aligned to their parent container.
Definition: Enums.h:35
double ToDouble()
Return this fraction as a double (i.e. 1/2 = 0.5)
Definition: Fraction.cpp:46