OpenShot Library | libopenshot  0.2.6
Mask.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Source file for Mask 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 #include "Mask.h"
32 #include "Exceptions.h"
33 #include "FFmpegReader.h"
34 #ifdef USE_IMAGEMAGICK
35  #include "ImageReader.h"
36 #endif
37 #include "ReaderBase.h"
38 
39 using namespace openshot;
40 
41 /// Blank constructor, useful when using Json to load the effect properties
42 Mask::Mask() : reader(NULL), replace_image(false), needs_refresh(true) {
43  // Init effect properties
44  init_effect_details();
45 }
46 
47 // Default constructor
48 Mask::Mask(ReaderBase *mask_reader, Keyframe mask_brightness, Keyframe mask_contrast) :
49  reader(mask_reader), brightness(mask_brightness), contrast(mask_contrast), replace_image(false), needs_refresh(true)
50 {
51  // Init effect properties
52  init_effect_details();
53 }
54 
55 // Init effect settings
56 void Mask::init_effect_details()
57 {
58  /// Initialize the values of the EffectInfo struct.
60 
61  /// Set the effect info
62  info.class_name = "Mask";
63  info.name = "Alpha Mask / Wipe Transition";
64  info.description = "Uses a grayscale mask image to gradually wipe / transition between 2 images.";
65  info.has_audio = false;
66  info.has_video = true;
67 }
68 
69 // This method is required for all derived classes of EffectBase, and returns a
70 // modified openshot::Frame object
71 std::shared_ptr<openshot::Frame> Mask::GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number) {
72  // Get the mask image (from the mask reader)
73  std::shared_ptr<QImage> frame_image = frame->GetImage();
74 
75  // Check if mask reader is open
76  #pragma omp critical (open_mask_reader)
77  {
78  if (reader && !reader->IsOpen())
79  reader->Open();
80  }
81 
82  // No reader (bail on applying the mask)
83  if (!reader)
84  return frame;
85 
86  // Get mask image (if missing or different size than frame image)
87  #pragma omp critical (open_mask_reader)
88  {
89  if (!original_mask || !reader->info.has_single_image || needs_refresh ||
90  (original_mask && original_mask->size() != frame_image->size())) {
91 
92  // Only get mask if needed
93  auto mask_without_sizing = std::make_shared<QImage>(
94  *reader->GetFrame(frame_number)->GetImage());
95 
96  // Resize mask image to match frame size
97  original_mask = std::make_shared<QImage>(
98  mask_without_sizing->scaled(
99  frame_image->width(), frame_image->height(),
100  Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
101  }
102  }
103 
104  // Refresh no longer needed
105  needs_refresh = false;
106 
107  // Get pixel arrays
108  unsigned char *pixels = (unsigned char *) frame_image->bits();
109  unsigned char *mask_pixels = (unsigned char *) original_mask->bits();
110 
111  double contrast_value = (contrast.GetValue(frame_number));
112  double brightness_value = (brightness.GetValue(frame_number));
113 
114  // Loop through mask pixels, and apply average gray value to frame alpha channel
115  for (int pixel = 0, byte_index=0; pixel < original_mask->width() * original_mask->height(); pixel++, byte_index+=4)
116  {
117  // Get the RGB values from the pixel
118  int R = mask_pixels[byte_index];
119  int G = mask_pixels[byte_index + 1];
120  int B = mask_pixels[byte_index + 2];
121  int A = mask_pixels[byte_index + 3];
122 
123  // Get the average luminosity
124  int gray_value = qGray(R, G, B);
125 
126  // Adjust the contrast
127  float factor = (259 * (contrast_value + 255)) / (255 * (259 - contrast_value));
128  gray_value = constrain((factor * (gray_value - 128)) + 128);
129 
130  // Adjust the brightness
131  gray_value += (255 * brightness_value);
132 
133  // Constrain the value from 0 to 255
134  gray_value = constrain(gray_value);
135 
136  // Calculate the % change in alpha
137  float alpha_percent = float(constrain(A - gray_value)) / 255.0;
138 
139  // Set the alpha channel to the gray value
140  if (replace_image) {
141  // Replace frame pixels with gray value (including alpha channel)
142  pixels[byte_index + 0] = gray_value;
143  pixels[byte_index + 1] = gray_value;
144  pixels[byte_index + 2] = gray_value;
145  pixels[byte_index + 3] = gray_value;
146  } else {
147  // Mulitply new alpha value with all the colors (since we are using a premultiplied
148  // alpha format)
149  pixels[byte_index + 0] *= alpha_percent;
150  pixels[byte_index + 1] *= alpha_percent;
151  pixels[byte_index + 2] *= alpha_percent;
152  pixels[byte_index + 3] *= alpha_percent;
153  }
154 
155  }
156 
157  // return the modified frame
158  return frame;
159 }
160 
161 // Generate JSON string of this object
162 std::string Mask::Json() const {
163 
164  // Return formatted string
165  return JsonValue().toStyledString();
166 }
167 
168 // Generate Json::Value for this object
169 Json::Value Mask::JsonValue() const {
170 
171  // Create root json object
172  Json::Value root = EffectBase::JsonValue(); // get parent properties
173  root["type"] = info.class_name;
174  root["brightness"] = brightness.JsonValue();
175  root["contrast"] = contrast.JsonValue();
176  if (reader)
177  root["reader"] = reader->JsonValue();
178  else
179  root["reader"] = Json::objectValue;
180  root["replace_image"] = replace_image;
181 
182  // return JsonValue
183  return root;
184 }
185 
186 // Load JSON string into this object
187 void Mask::SetJson(const std::string value) {
188 
189  // Parse JSON string into JSON objects
190  try
191  {
192  const Json::Value root = openshot::stringToJson(value);
193  // Set all values that match
194  SetJsonValue(root);
195  }
196  catch (const std::exception& e)
197  {
198  // Error parsing JSON (or missing keys)
199  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
200  }
201 }
202 
203 // Load Json::Value into this object
204 void Mask::SetJsonValue(const Json::Value root) {
205 
206  // Set parent data
208 
209  // Set data from Json (if key is found)
210  if (!root["replace_image"].isNull())
211  replace_image = root["replace_image"].asBool();
212  if (!root["brightness"].isNull())
213  brightness.SetJsonValue(root["brightness"]);
214  if (!root["contrast"].isNull())
215  contrast.SetJsonValue(root["contrast"]);
216  if (!root["reader"].isNull()) // does Json contain a reader?
217  {
218  #pragma omp critical (open_mask_reader)
219  {
220  // This reader has changed, so refresh cached assets
221  needs_refresh = true;
222 
223  if (!root["reader"]["type"].isNull()) // does the reader Json contain a 'type'?
224  {
225  // Close previous reader (if any)
226  if (reader) {
227  // Close and delete existing reader (if any)
228  reader->Close();
229  delete reader;
230  reader = NULL;
231  }
232 
233  // Create new reader (and load properties)
234  std::string type = root["reader"]["type"].asString();
235 
236  if (type == "FFmpegReader") {
237 
238  // Create new reader
239  reader = new FFmpegReader(root["reader"]["path"].asString());
240  reader->SetJsonValue(root["reader"]);
241 
242  #ifdef USE_IMAGEMAGICK
243  } else if (type == "ImageReader") {
244 
245  // Create new reader
246  reader = new ImageReader(root["reader"]["path"].asString());
247  reader->SetJsonValue(root["reader"]);
248  #endif
249 
250  } else if (type == "QtImageReader") {
251 
252  // Create new reader
253  reader = new QtImageReader(root["reader"]["path"].asString());
254  reader->SetJsonValue(root["reader"]);
255 
256  } else if (type == "ChunkReader") {
257 
258  // Create new reader
259  reader = new ChunkReader(root["reader"]["path"].asString(), (ChunkVersion) root["reader"]["chunk_version"].asInt());
260  reader->SetJsonValue(root["reader"]);
261 
262  }
263  }
264 
265  }
266  }
267 
268 }
269 
270 // Get all properties for a specific frame
271 std::string Mask::PropertiesJSON(int64_t requested_frame) const {
272 
273  // Generate JSON properties list
274  Json::Value root;
275  root["id"] = add_property_json("ID", 0.0, "string", Id(), NULL, -1, -1, true, requested_frame);
276  root["position"] = add_property_json("Position", Position(), "float", "", NULL, 0, 30 * 60 * 60 * 48, false, requested_frame);
277  root["layer"] = add_property_json("Track", Layer(), "int", "", NULL, 0, 20, false, requested_frame);
278  root["start"] = add_property_json("Start", Start(), "float", "", NULL, 0, 30 * 60 * 60 * 48, false, requested_frame);
279  root["end"] = add_property_json("End", End(), "float", "", NULL, 0, 30 * 60 * 60 * 48, false, requested_frame);
280  root["duration"] = add_property_json("Duration", Duration(), "float", "", NULL, 0, 30 * 60 * 60 * 48, true, requested_frame);
281  root["replace_image"] = add_property_json("Replace Image", replace_image, "int", "", NULL, 0, 1, false, requested_frame);
282 
283  // Add replace_image choices (dropdown style)
284  root["replace_image"]["choices"].append(add_property_choice_json("Yes", true, replace_image));
285  root["replace_image"]["choices"].append(add_property_choice_json("No", false, replace_image));
286 
287  // Keyframes
288  root["brightness"] = add_property_json("Brightness", brightness.GetValue(requested_frame), "float", "", &brightness, -1.0, 1.0, false, requested_frame);
289  root["contrast"] = add_property_json("Contrast", contrast.GetValue(requested_frame), "float", "", &contrast, 0, 20, false, requested_frame);
290 
291  if (reader)
292  root["reader"] = add_property_json("Source", 0.0, "reader", reader->Json(), NULL, 0, 1, false, requested_frame);
293  else
294  root["reader"] = add_property_json("Source", 0.0, "reader", "{}", NULL, 0, 1, false, requested_frame);
295 
296  // Set the parent effect which properties this effect will inherit
297  root["parent_effect_id"] = add_property_json("Parent", 0.0, "string", info.parent_effect_id, NULL, -1, -1, false, requested_frame);
298 
299  // Return formatted string
300  return root.toStyledString();
301 }
This class reads a special chunk-formatted file, which can be easily shared in a distributed environm...
Definition: ChunkReader.h:97
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition: Mask.cpp:169
bool replace_image
Replace the frame image with a grayscale image representing the mask. Great for debugging a mask...
Definition: Mask.h:65
std::string Id() const
Get the Id of this clip object.
Definition: ClipBase.h:107
Header file for ReaderBase class.
float Start() const
Get start position (in seconds) of clip (trim start of video)
Definition: ClipBase.h:110
virtual void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: EffectBase.cpp:127
Header file for FFmpegReader class.
Header file for Mask class.
virtual void Close()=0
Close the reader (and any resources it was consuming)
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:34
This abstract class is the base class, used by all readers in libopenshot.
Definition: ReaderBase.h:97
virtual Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: EffectBase.cpp:92
bool has_audio
Determines if this effect manipulates the audio of a frame.
Definition: EffectBase.h:59
virtual std::shared_ptr< openshot::Frame > GetFrame(int64_t number)=0
This class uses the ImageMagick++ libraries, to open image files, and return openshot::Frame objects ...
Definition: ImageReader.h:73
Header file for all Exception classes.
virtual Json::Value JsonValue() const =0
Generate Json::Value for this object.
Definition: ReaderBase.cpp:116
This class uses the FFmpeg libraries, to open video files and audio files, and return openshot::Frame...
Definition: FFmpegReader.h:93
Keyframe contrast
Contrast keyframe to control the hardness of the wipe effect / mask.
Definition: Mask.h:67
virtual std::string Json() const =0
Generate JSON string of this object.
std::shared_ptr< openshot::Frame > GetFrame(int64_t frame_number) override
This method is required for all derived classes of ClipBase, and returns a new openshot::Frame object...
Definition: Mask.h:87
Json::Value add_property_choice_json(std::string name, int value, int selected_value) const
Generate JSON choice for a property (dropdown properties)
Definition: ClipBase.cpp:104
bool has_single_image
Determines if this file only contains a single image.
Definition: ReaderBase.h:64
ChunkVersion
This enumeration allows the user to choose which version of the chunk they would like (low...
Definition: ChunkReader.h:68
Mask()
Blank constructor, useful when using Json to load the effect properties.
Definition: Mask.cpp:42
std::string class_name
The class name of the effect.
Definition: EffectBase.h:54
std::string name
The name of the effect.
Definition: EffectBase.h:55
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 PropertiesJSON(int64_t requested_frame) const override
Definition: Mask.cpp:271
float Duration() const
Get the length of this clip (in seconds)
Definition: ClipBase.h:112
void SetJson(const std::string value) override
Load JSON string into this object.
Definition: Mask.cpp:187
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: KeyFrame.cpp:368
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:46
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition: Mask.cpp:204
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: KeyFrame.cpp:335
std::string description
The description of this effect and what it does.
Definition: EffectBase.h:56
Keyframe brightness
Brightness keyframe to control the wipe / mask effect. A constant value here will prevent animation...
Definition: Mask.h:66
bool has_video
Determines if this effect manipulates the image of a frame.
Definition: EffectBase.h:58
Exception for invalid JSON.
Definition: Exceptions.h:205
double GetValue(int64_t index) const
Get the value at a specific index.
Definition: KeyFrame.cpp:268
int constrain(int color_value)
Constrain a color value from 0 to 255.
Definition: EffectBase.cpp:73
std::string parent_effect_id
Id of the parent effect (if there is one)
Definition: EffectBase.h:57
std::string Json() const override
Generate JSON string of this object.
Definition: Mask.cpp:162
Header file for ImageReader class.
float Position() const
Get position on timeline (in seconds)
Definition: ClipBase.h:108
float End() const
Get end position (in seconds) of clip (trim end of video)
Definition: ClipBase.h:111
This class uses the Qt library, to open image files, and return openshot::Frame objects containing th...
Definition: QtImageReader.h:67
A Keyframe is a collection of Point instances, which is used to vary a number or property over time...
Definition: KeyFrame.h:72
Json::Value add_property_json(std::string name, float value, std::string type, std::string memo, const Keyframe *keyframe, float min_value, float max_value, bool readonly, int64_t requested_frame) const
Generate JSON for a property.
Definition: ClipBase.cpp:68
int Layer() const
Get layer of clip on timeline (lower number is covered by higher numbers)
Definition: ClipBase.h:109
virtual void Open()=0
Open the reader (and start consuming resources, such as images or video files)
EffectInfoStruct info
Information about the current effect.
Definition: EffectBase.h:87
virtual bool IsOpen()=0
Determine if reader is open or closed.