OpenShot Library | libopenshot  0.4.0
ColorShift.cpp
Go to the documentation of this file.
1 
9 // Copyright (c) 2008-2019 OpenShot Studios, LLC
10 //
11 // SPDX-License-Identifier: LGPL-3.0-or-later
12 
13 #include "ColorShift.h"
14 #include "Exceptions.h"
15 
16 using namespace openshot;
17 
19 ColorShift::ColorShift() : red_x(0.0), red_y(0.0), green_x(0.0), green_y(0.0), blue_x(0.0), blue_y(0.0), alpha_x(0.0), alpha_y(0.0) {
20  // Init effect properties
21  init_effect_details();
22 }
23 
24 // Default constructor
25 ColorShift::ColorShift(Keyframe red_x, Keyframe red_y, Keyframe green_x, Keyframe green_y, Keyframe blue_x, Keyframe blue_y, Keyframe alpha_x, Keyframe alpha_y) :
26  red_x(red_x), red_y(red_y), green_x(green_x), green_y(green_y), blue_x(blue_x), blue_y(blue_y), alpha_x(alpha_x), alpha_y(alpha_y)
27 {
28  // Init effect properties
29  init_effect_details();
30 }
31 
32 // Init effect settings
33 void ColorShift::init_effect_details()
34 {
37 
39  info.class_name = "ColorShift";
40  info.name = "Color Shift";
41  info.description = "Shift the colors of an image up, down, left, and right (with infinite wrapping).";
42  info.has_audio = false;
43  info.has_video = true;
44 }
45 
46 // This method is required for all derived classes of EffectBase, and returns a
47 // modified openshot::Frame object
48 std::shared_ptr<openshot::Frame> ColorShift::GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number)
49 {
50  // Get the frame's image
51  std::shared_ptr<QImage> frame_image = frame->GetImage();
52  unsigned char *pixels = (unsigned char *) frame_image->bits();
53 
54  // Get image size
55  int frame_image_width = frame_image->width();
56  int frame_image_height = frame_image->height();
57 
58  // Get the current shift amount, and clamp to range (-1 to 1 range)
59  // Red Keyframes
60  float red_x_shift = red_x.GetValue(frame_number);
61  int red_x_shift_limit = round(frame_image_width * fmod(fabs(red_x_shift), 1.0));
62  float red_y_shift = red_y.GetValue(frame_number);
63  int red_y_shift_limit = round(frame_image_height * fmod(fabs(red_y_shift), 1.0));
64  // Green Keyframes
65  float green_x_shift = green_x.GetValue(frame_number);
66  int green_x_shift_limit = round(frame_image_width * fmod(fabs(green_x_shift), 1.0));
67  float green_y_shift = green_y.GetValue(frame_number);
68  int green_y_shift_limit = round(frame_image_height * fmod(fabs(green_y_shift), 1.0));
69  // Blue Keyframes
70  float blue_x_shift = blue_x.GetValue(frame_number);
71  int blue_x_shift_limit = round(frame_image_width * fmod(fabs(blue_x_shift), 1.0));
72  float blue_y_shift = blue_y.GetValue(frame_number);
73  int blue_y_shift_limit = round(frame_image_height * fmod(fabs(blue_y_shift), 1.0));
74  // Alpha Keyframes
75  float alpha_x_shift = alpha_x.GetValue(frame_number);
76  int alpha_x_shift_limit = round(frame_image_width * fmod(fabs(alpha_x_shift), 1.0));
77  float alpha_y_shift = alpha_y.GetValue(frame_number);
78  int alpha_y_shift_limit = round(frame_image_height * fmod(fabs(alpha_y_shift), 1.0));
79 
80  // Make temp copy of pixels
81  unsigned char *temp_image = new unsigned char[frame_image_width * frame_image_height * 4]();
82  memcpy(temp_image, pixels, sizeof(char) * frame_image_width * frame_image_height * 4);
83 
84  // Init position of current row and pixel
85  int starting_row_index = 0;
86  int byte_index = 0;
87 
88  // Init RGBA values
89  unsigned char R = 0;
90  unsigned char G = 0;
91  unsigned char B = 0;
92  unsigned char A = 0;
93 
94  int red_starting_row_index = 0;
95  int green_starting_row_index = 0;
96  int blue_starting_row_index = 0;
97  int alpha_starting_row_index = 0;
98 
99  int red_pixel_offset = 0;
100  int green_pixel_offset = 0;
101  int blue_pixel_offset = 0;
102  int alpha_pixel_offset = 0;
103 
104  // Loop through rows of pixels
105  for (int row = 0; row < frame_image_height; row++) {
106  for (int col = 0; col < frame_image_width; col++) {
107  // Get position of current row and pixel
108  starting_row_index = row * frame_image_width * 4;
109  byte_index = starting_row_index + (col * 4);
110  red_starting_row_index = starting_row_index;
111  green_starting_row_index = starting_row_index;
112  blue_starting_row_index = starting_row_index;
113  alpha_starting_row_index = starting_row_index;
114 
115  red_pixel_offset = col;
116  green_pixel_offset = col;
117  blue_pixel_offset = col;
118  alpha_pixel_offset = col;
119 
120  // Get the RGBA value from each pixel (depending on offset)
121  R = temp_image[byte_index];
122  G = temp_image[byte_index + 1];
123  B = temp_image[byte_index + 2];
124  A = temp_image[byte_index + 3];
125 
126  // Shift X
127  if (red_x_shift > 0.0)
128  red_pixel_offset = (col + red_x_shift_limit) % frame_image_width;
129  if (red_x_shift < 0.0)
130  red_pixel_offset = (frame_image_width + col - red_x_shift_limit) % frame_image_width;
131  if (green_x_shift > 0.0)
132  green_pixel_offset = (col + green_x_shift_limit) % frame_image_width;
133  if (green_x_shift < 0.0)
134  green_pixel_offset = (frame_image_width + col - green_x_shift_limit) % frame_image_width;
135  if (blue_x_shift > 0.0)
136  blue_pixel_offset = (col + blue_x_shift_limit) % frame_image_width;
137  if (blue_x_shift < 0.0)
138  blue_pixel_offset = (frame_image_width + col - blue_x_shift_limit) % frame_image_width;
139  if (alpha_x_shift > 0.0)
140  alpha_pixel_offset = (col + alpha_x_shift_limit) % frame_image_width;
141  if (alpha_x_shift < 0.0)
142  alpha_pixel_offset = (frame_image_width + col - alpha_x_shift_limit) % frame_image_width;
143 
144  // Shift Y
145  if (red_y_shift > 0.0)
146  red_starting_row_index = ((row + red_y_shift_limit) % frame_image_height) * frame_image_width * 4;
147  if (red_y_shift < 0.0)
148  red_starting_row_index = ((frame_image_height + row - red_y_shift_limit) % frame_image_height) * frame_image_width * 4;
149  if (green_y_shift > 0.0)
150  green_starting_row_index = ((row + green_y_shift_limit) % frame_image_height) * frame_image_width * 4;
151  if (green_y_shift < 0.0)
152  green_starting_row_index = ((frame_image_height + row - green_y_shift_limit) % frame_image_height) * frame_image_width * 4;
153  if (blue_y_shift > 0.0)
154  blue_starting_row_index = ((row + blue_y_shift_limit) % frame_image_height) * frame_image_width * 4;
155  if (blue_y_shift < 0.0)
156  blue_starting_row_index = ((frame_image_height + row - blue_y_shift_limit) % frame_image_height) * frame_image_width * 4;
157  if (alpha_y_shift > 0.0)
158  alpha_starting_row_index = ((row + alpha_y_shift_limit) % frame_image_height) * frame_image_width * 4;
159  if (alpha_y_shift < 0.0)
160  alpha_starting_row_index = ((frame_image_height + row - alpha_y_shift_limit) % frame_image_height) * frame_image_width * 4;
161 
162  // Copy new values to this pixel
163  pixels[red_starting_row_index + 0 + (red_pixel_offset * 4)] = R;
164  pixels[green_starting_row_index + 1 + (green_pixel_offset * 4)] = G;
165  pixels[blue_starting_row_index + 2 + (blue_pixel_offset * 4)] = B;
166  pixels[alpha_starting_row_index + 3 + (alpha_pixel_offset * 4)] = A;
167  }
168  }
169 
170  // Delete arrays
171  delete[] temp_image;
172 
173  // return the modified frame
174  return frame;
175 }
176 
177 // Generate JSON string of this object
178 std::string ColorShift::Json() const {
179 
180  // Return formatted string
181  return JsonValue().toStyledString();
182 }
183 
184 // Generate Json::Value for this object
185 Json::Value ColorShift::JsonValue() const {
186 
187  // Create root json object
188  Json::Value root = EffectBase::JsonValue(); // get parent properties
189  root["type"] = info.class_name;
190  root["red_x"] = red_x.JsonValue();
191  root["red_y"] = red_y.JsonValue();
192  root["green_x"] = green_x.JsonValue();
193  root["green_y"] = green_y.JsonValue();
194  root["blue_x"] = blue_x.JsonValue();
195  root["blue_y"] = blue_y.JsonValue();
196  root["alpha_x"] = alpha_x.JsonValue();
197  root["alpha_y"] = alpha_y.JsonValue();
198 
199  // return JsonValue
200  return root;
201 }
202 
203 // Load JSON string into this object
204 void ColorShift::SetJson(const std::string value) {
205 
206  // Parse JSON string into JSON objects
207  try
208  {
209  const Json::Value root = openshot::stringToJson(value);
210  // Set all values that match
211  SetJsonValue(root);
212  }
213  catch (const std::exception& e)
214  {
215  // Error parsing JSON (or missing keys)
216  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
217  }
218 }
219 
220 // Load Json::Value into this object
221 void ColorShift::SetJsonValue(const Json::Value root) {
222 
223  // Set parent data
225 
226  // Set data from Json (if key is found)
227  if (!root["red_x"].isNull())
228  red_x.SetJsonValue(root["red_x"]);
229  if (!root["red_y"].isNull())
230  red_y.SetJsonValue(root["red_y"]);
231  if (!root["green_x"].isNull())
232  green_x.SetJsonValue(root["green_x"]);
233  if (!root["green_y"].isNull())
234  green_y.SetJsonValue(root["green_y"]);
235  if (!root["blue_x"].isNull())
236  blue_x.SetJsonValue(root["blue_x"]);
237  if (!root["blue_y"].isNull())
238  blue_y.SetJsonValue(root["blue_y"]);
239  if (!root["alpha_x"].isNull())
240  alpha_x.SetJsonValue(root["alpha_x"]);
241  if (!root["alpha_y"].isNull())
242  alpha_y.SetJsonValue(root["alpha_y"]);
243 }
244 
245 // Get all properties for a specific frame
246 std::string ColorShift::PropertiesJSON(int64_t requested_frame) const {
247 
248  // Generate JSON properties list
249  Json::Value root = BasePropertiesJSON(requested_frame);
250 
251  // Keyframes
252  root["red_x"] = add_property_json("Red X Shift", red_x.GetValue(requested_frame), "float", "", &red_x, -1, 1, false, requested_frame);
253  root["red_y"] = add_property_json("Red Y Shift", red_y.GetValue(requested_frame), "float", "", &red_y, -1, 1, false, requested_frame);
254  root["green_x"] = add_property_json("Green X Shift", green_x.GetValue(requested_frame), "float", "", &green_x, -1, 1, false, requested_frame);
255  root["green_y"] = add_property_json("Green Y Shift", green_y.GetValue(requested_frame), "float", "", &green_y, -1, 1, false, requested_frame);
256  root["blue_x"] = add_property_json("Blue X Shift", blue_x.GetValue(requested_frame), "float", "", &blue_x, -1, 1, false, requested_frame);
257  root["blue_y"] = add_property_json("Blue Y Shift", blue_y.GetValue(requested_frame), "float", "", &blue_y, -1, 1, false, requested_frame);
258  root["alpha_x"] = add_property_json("Alpha X Shift", alpha_x.GetValue(requested_frame), "float", "", &alpha_x, -1, 1, false, requested_frame);
259  root["alpha_y"] = add_property_json("Alpha Y Shift", alpha_y.GetValue(requested_frame), "float", "", &alpha_y, -1, 1, false, requested_frame);
260 
261  // Return formatted string
262  return root.toStyledString();
263 }
openshot::ClipBase::add_property_json
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:96
openshot::ColorShift::alpha_y
Keyframe alpha_y
Shift the Alpha Y coordinates (up or down)
Definition: ColorShift.h:49
openshot::stringToJson
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:16
openshot::ColorShift::red_x
Keyframe red_x
Shift the Red X coordinates (left or right)
Definition: ColorShift.h:42
openshot::EffectBase::info
EffectInfoStruct info
Information about the current effect.
Definition: EffectBase.h:69
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:28
openshot::ColorShift::ColorShift
ColorShift()
Blank constructor, useful when using Json to load the effect properties.
Definition: ColorShift.cpp:19
openshot::ColorShift::green_x
Keyframe green_x
Shift the Green X coordinates (left or right)
Definition: ColorShift.h:44
openshot::EffectBase::JsonValue
virtual Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: EffectBase.cpp:79
openshot::ColorShift::blue_x
Keyframe blue_x
Shift the Blue X coordinates (left or right)
Definition: ColorShift.h:46
openshot::Keyframe::SetJsonValue
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: KeyFrame.cpp:372
openshot::ColorShift::SetJson
void SetJson(const std::string value) override
Load JSON string into this object.
Definition: ColorShift.cpp:204
openshot::ColorShift::PropertiesJSON
std::string PropertiesJSON(int64_t requested_frame) const override
Definition: ColorShift.cpp:246
openshot::ColorShift::alpha_x
Keyframe alpha_x
Shift the Alpha X coordinates (left or right)
Definition: ColorShift.h:48
openshot::ColorShift::red_y
Keyframe red_y
Shift the Red Y coordinates (up or down)
Definition: ColorShift.h:43
openshot::Keyframe::JsonValue
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: KeyFrame.cpp:339
openshot::EffectBase::BasePropertiesJSON
Json::Value BasePropertiesJSON(int64_t requested_frame) const
Generate JSON object of base properties (recommended to be used by all effects)
Definition: EffectBase.cpp:179
openshot::ColorShift::green_y
Keyframe green_y
Shift the Green Y coordinates (up or down)
Definition: ColorShift.h:45
openshot::Keyframe
A Keyframe is a collection of Point instances, which is used to vary a number or property over time.
Definition: KeyFrame.h:53
openshot::InvalidJSON
Exception for invalid JSON.
Definition: Exceptions.h:217
openshot::EffectBase::InitEffectInfo
void InitEffectInfo()
Definition: EffectBase.cpp:24
openshot::EffectInfoStruct::has_audio
bool has_audio
Determines if this effect manipulates the audio of a frame.
Definition: EffectBase.h:41
openshot::ColorShift::SetJsonValue
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition: ColorShift.cpp:221
openshot::EffectInfoStruct::class_name
std::string class_name
The class name of the effect.
Definition: EffectBase.h:36
openshot::EffectInfoStruct::description
std::string description
The description of this effect and what it does.
Definition: EffectBase.h:38
openshot::ColorShift::JsonValue
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition: ColorShift.cpp:185
openshot::EffectInfoStruct::has_video
bool has_video
Determines if this effect manipulates the image of a frame.
Definition: EffectBase.h:40
openshot::ColorShift::Json
std::string Json() const override
Generate JSON string of this object.
Definition: ColorShift.cpp:178
openshot::EffectInfoStruct::name
std::string name
The name of the effect.
Definition: EffectBase.h:37
ColorShift.h
Header file for Color Shift effect class.
openshot::ColorShift::blue_y
Keyframe blue_y
Shift the Blue Y coordinates (up or down)
Definition: ColorShift.h:47
Exceptions.h
Header file for all Exception classes.
openshot::ColorShift::GetFrame
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: ColorShift.h:72
openshot::EffectBase::SetJsonValue
virtual void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: EffectBase.cpp:115
openshot::Keyframe::GetValue
double GetValue(int64_t index) const
Get the value at a specific index.
Definition: KeyFrame.cpp:258