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