OpenShot Library | libopenshot  0.2.6
Hue.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Source file for Hue effect 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 "Hue.h"
32 #include "Exceptions.h"
33 
34 using namespace openshot;
35 
36 /// Blank constructor, useful when using Json to load the effect properties
37 Hue::Hue() : Hue(0.0) {
38  // Init effect properties
39  init_effect_details();
40 }
41 
42 // Default constructor
43 Hue::Hue(Keyframe hue): hue(hue)
44 {
45  // Init effect properties
46  init_effect_details();
47 }
48 
49 // Init effect settings
50 void Hue::init_effect_details()
51 {
52  /// Initialize the values of the EffectInfo struct.
54 
55  /// Set the effect info
56  info.class_name = "Hue";
57  info.name = "Hue";
58  info.description = "Adjust the hue / color of the frame's image.";
59  info.has_audio = false;
60  info.has_video = true;
61 }
62 
63 // This method is required for all derived classes of EffectBase, and returns a
64 // modified openshot::Frame object
65 std::shared_ptr<openshot::Frame> Hue::GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number)
66 {
67  // Get the frame's image
68  std::shared_ptr<QImage> frame_image = frame->GetImage();
69 
70  int pixel_count = frame_image->width() * frame_image->height();
71 
72  // Get the current hue percentage shift amount, and convert to degrees
73  double degrees = 360.0 * hue.GetValue(frame_number);
74  float cosA = cos(degrees*3.14159265f/180);
75  float sinA = sin(degrees*3.14159265f/180);
76 
77  // Calculate a rotation matrix for the RGB colorspace (based on the current hue shift keyframe value)
78  float matrix[3] = {
79  cosA + (1.0f - cosA) / 3.0f,
80  1.0f/3.0f * (1.0f - cosA) - sqrtf(1.0f/3.0f) * sinA,
81  1.0f/3.0f * (1.0f - cosA) + sqrtf(1.0f/3.0f) * sinA
82  };
83 
84  // Loop through pixels
85  unsigned char *pixels = (unsigned char *) frame_image->bits();
86 
87  #pragma omp parallel for shared (pixels)
88  for (int pixel = 0; pixel < pixel_count; ++pixel)
89  {
90  // Get the RGB values from the pixel (ignore the alpha channel)
91  int R = pixels[pixel * 4];
92  int G = pixels[pixel * 4 + 1];
93  int B = pixels[pixel * 4 + 2];
94 
95  // Multiply each color by the hue rotation matrix
96  pixels[pixel * 4] = constrain(R * matrix[0] + G * matrix[1] + B * matrix[2]);
97  pixels[pixel * 4 + 1] = constrain(R * matrix[2] + G * matrix[0] + B * matrix[1]);
98  pixels[pixel * 4 + 2] = constrain(R * matrix[1] + G * matrix[2] + B * matrix[0]);
99  }
100 
101  // return the modified frame
102  return frame;
103 }
104 
105 // Generate JSON string of this object
106 std::string Hue::Json() const {
107 
108  // Return formatted string
109  return JsonValue().toStyledString();
110 }
111 
112 // Generate Json::Value for this object
113 Json::Value Hue::JsonValue() const {
114 
115  // Create root json object
116  Json::Value root = EffectBase::JsonValue(); // get parent properties
117  root["type"] = info.class_name;
118  root["hue"] = hue.JsonValue();
119 
120  // return JsonValue
121  return root;
122 }
123 
124 // Load JSON string into this object
125 void Hue::SetJson(const std::string value) {
126 
127  // Parse JSON string into JSON objects
128  try
129  {
130  const Json::Value root = openshot::stringToJson(value);
131  // Set all values that match
132  SetJsonValue(root);
133  }
134  catch (const std::exception& e)
135  {
136  // Error parsing JSON (or missing keys)
137  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
138  }
139 }
140 
141 // Load Json::Value into this object
142 void Hue::SetJsonValue(const Json::Value root) {
143 
144  // Set parent data
146 
147  // Set data from Json (if key is found)
148  if (!root["hue"].isNull())
149  hue.SetJsonValue(root["hue"]);
150 }
151 
152 // Get all properties for a specific frame
153 std::string Hue::PropertiesJSON(int64_t requested_frame) const {
154 
155  // Generate JSON properties list
156  Json::Value root;
157  root["id"] = add_property_json("ID", 0.0, "string", Id(), NULL, -1, -1, true, requested_frame);
158  root["position"] = add_property_json("Position", Position(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
159  root["layer"] = add_property_json("Track", Layer(), "int", "", NULL, 0, 20, false, requested_frame);
160  root["start"] = add_property_json("Start", Start(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
161  root["end"] = add_property_json("End", End(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
162  root["duration"] = add_property_json("Duration", Duration(), "float", "", NULL, 0, 1000 * 60 * 30, true, requested_frame);
163 
164  // Keyframes
165  root["hue"] = add_property_json("Hue", hue.GetValue(requested_frame), "float", "", &hue, 0.0, 1.0, false, requested_frame);
166 
167  // Set the parent effect which properties this effect will inherit
168  root["parent_effect_id"] = add_property_json("Parent", 0.0, "string", info.parent_effect_id, NULL, -1, -1, false, requested_frame);
169 
170  // Return formatted string
171  return root.toStyledString();
172 }
Keyframe hue
Shift the hue coordinates (left or right)
Definition: Hue.h:61
std::string PropertiesJSON(int64_t requested_frame) const override
Definition: Hue.cpp:153
void SetJson(const std::string value) override
Load JSON string into this object.
Definition: Hue.cpp:125
std::string Id() const
Get the Id of this clip object.
Definition: ClipBase.h:107
Hue()
Default constructor, useful when using Json to load the effect properties.
Definition: Hue.cpp:37
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
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:34
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
Header file for all Exception classes.
This class shifts the hue of an image, and can be animated with openshot::Keyframe curves over time...
Definition: Hue.h:53
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition: Hue.cpp:113
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
std::string Json() const override
Generate JSON string of this object.
Definition: Hue.cpp:106
Header file for Hue effect class.
float Duration() const
Get the length of this clip (in seconds)
Definition: ClipBase.h:112
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
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: Hue.h:77
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
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
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
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
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition: Hue.cpp:142
EffectInfoStruct info
Information about the current effect.
Definition: EffectBase.h:87