OpenShot Library | libopenshot  0.7.0
Saturation.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 "Saturation.h"
14 #include "Exceptions.h"
15 #include <array>
16 #include <cmath>
17 
18 using namespace openshot;
19 
21 Saturation::Saturation() : saturation(1.0), saturation_R(1.0), saturation_G(1.0), saturation_B(1.0),
22  mask_mode(SATURATION_MASK_POST_BLEND) {
23  // Init effect properties
24  init_effect_details();
25 }
26 
27 // Default constructor
28 Saturation::Saturation(Keyframe saturation, Keyframe saturation_R, Keyframe saturation_G, Keyframe saturation_B) :
29  saturation(saturation), saturation_R(saturation_R), saturation_G(saturation_G), saturation_B(saturation_B),
31 {
32  // Init effect properties
33  init_effect_details();
34 }
35 
36 bool Saturation::UseCustomMaskBlend(int64_t frame_number) const {
37  (void) frame_number;
39 }
40 
41 void Saturation::ApplyCustomMaskBlend(std::shared_ptr<QImage> original_image, std::shared_ptr<QImage> effected_image,
42  std::shared_ptr<QImage> mask_image, int64_t frame_number) const {
43  (void) frame_number;
44  if (!original_image || !effected_image || !mask_image)
45  return;
46  if (original_image->size() != effected_image->size() || effected_image->size() != mask_image->size())
47  return;
48 
49  unsigned char* original_pixels = reinterpret_cast<unsigned char*>(original_image->bits());
50  unsigned char* effected_pixels = reinterpret_cast<unsigned char*>(effected_image->bits());
51  unsigned char* mask_pixels = reinterpret_cast<unsigned char*>(mask_image->bits());
52  const int width = effected_image->width();
53  const int height = effected_image->height();
54  const int original_stride = original_image->bytesPerLine();
55  const int effected_stride = effected_image->bytesPerLine();
56  const int mask_stride = mask_image->bytesPerLine();
57 
58  if (mask_invert) {
59  #pragma omp parallel for schedule(static)
60  for (int y = 0; y < height; ++y) {
61  unsigned char* original_row = original_pixels + y * original_stride;
62  unsigned char* effected_row = effected_pixels + y * effected_stride;
63  unsigned char* mask_row = mask_pixels + y * mask_stride;
64  for (int x = 0; x < width; ++x) {
65  const int idx = x * 4;
66  float factor = static_cast<float>(qGray(mask_row[idx], mask_row[idx + 1], mask_row[idx + 2])) / 255.0f;
67  factor = 1.0f - factor;
68  // Use a non-linear response curve for custom saturation drive mode.
69  factor = factor * factor;
70  const float inverse = 1.0f - factor;
71 
72  // Drive saturation strength with mask while preserving source alpha.
73  effected_row[idx] = static_cast<unsigned char>(
74  (original_row[idx] * inverse) + (effected_row[idx] * factor));
75  effected_row[idx + 1] = static_cast<unsigned char>(
76  (original_row[idx + 1] * inverse) + (effected_row[idx + 1] * factor));
77  effected_row[idx + 2] = static_cast<unsigned char>(
78  (original_row[idx + 2] * inverse) + (effected_row[idx + 2] * factor));
79  effected_row[idx + 3] = original_row[idx + 3];
80  }
81  }
82  } else {
83  #pragma omp parallel for schedule(static)
84  for (int y = 0; y < height; ++y) {
85  unsigned char* original_row = original_pixels + y * original_stride;
86  unsigned char* effected_row = effected_pixels + y * effected_stride;
87  unsigned char* mask_row = mask_pixels + y * mask_stride;
88  for (int x = 0; x < width; ++x) {
89  const int idx = x * 4;
90  float factor = static_cast<float>(qGray(mask_row[idx], mask_row[idx + 1], mask_row[idx + 2])) / 255.0f;
91  // Use a non-linear response curve for custom saturation drive mode.
92  factor = factor * factor;
93  const float inverse = 1.0f - factor;
94 
95  // Drive saturation strength with mask while preserving source alpha.
96  effected_row[idx] = static_cast<unsigned char>(
97  (original_row[idx] * inverse) + (effected_row[idx] * factor));
98  effected_row[idx + 1] = static_cast<unsigned char>(
99  (original_row[idx + 1] * inverse) + (effected_row[idx + 1] * factor));
100  effected_row[idx + 2] = static_cast<unsigned char>(
101  (original_row[idx + 2] * inverse) + (effected_row[idx + 2] * factor));
102  effected_row[idx + 3] = original_row[idx + 3];
103  }
104  }
105  }
106 }
107 
108 // Init effect settings
109 void Saturation::init_effect_details()
110 {
112  InitEffectInfo();
113 
115  info.class_name = "Saturation";
116  info.name = "Color Saturation";
117  info.description = "Adjust the color saturation.";
118  info.has_audio = false;
119  info.has_video = true;
120 }
121 
122 // This method is required for all derived classes of EffectBase, and returns a
123 // modified openshot::Frame object
124 std::shared_ptr<openshot::Frame> Saturation::GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number)
125 {
126  // Get the frame's image
127  std::shared_ptr<QImage> frame_image = frame->GetImage();
128 
129  if (!frame_image)
130  return frame;
131 
132  const int pixel_count = frame_image->width() * frame_image->height();
133 
134  // Get keyframe values for this frame
135  const float saturation_value = saturation.GetValue(frame_number);
136  const float saturation_value_R = saturation_R.GetValue(frame_number);
137  const float saturation_value_G = saturation_G.GetValue(frame_number);
138  const float saturation_value_B = saturation_B.GetValue(frame_number);
139 
140  // Constants used for color saturation formula
141  const float pR = 0.299f;
142  const float pG = 0.587f;
143  const float pB = 0.114f;
144  const float sqrt_pR = std::sqrt(pR);
145  const float sqrt_pG = std::sqrt(pG);
146  const float sqrt_pB = std::sqrt(pB);
147  // Rec.601 fixed-point luma weights used in many image/video pipelines.
148  // This avoids per-pixel sqrt() while keeping output stable.
149  static const std::array<float, 65026> sqrt_lut = [] {
150  std::array<float, 65026> lut{};
151  for (int i = 0; i <= 65025; ++i)
152  lut[i] = std::sqrt(static_cast<float>(i));
153  return lut;
154  }();
155 
156  // Loop through pixels
157  unsigned char *pixels = reinterpret_cast<unsigned char *>(frame_image->bits());
158  const int width = frame_image->width();
159  const int height = frame_image->height();
160  const int stride = frame_image->bytesPerLine();
161  // LUT for undoing premultiplication without a per-pixel divide.
162  static const std::array<float, 256> inv_alpha = [] {
163  std::array<float, 256> lut{};
164  lut[0] = 0.0f;
165  for (int i = 1; i < 256; ++i)
166  lut[i] = 255.0f / static_cast<float>(i);
167  return lut;
168  }();
169  const auto clamp_i = [](int value) -> int {
170  if (value < 0) return 0;
171  if (value > 255) return 255;
172  return value;
173  };
174 
175  const auto apply_saturation = [&](int &R, int &G, int &B) {
176  // Approximate sqrt(R^2*pR + G^2*pG + B^2*pB) with fixed-point weighted
177  // intensity and lookup table. 77/150/29 ~= 0.299/0.587/0.114.
178  const int weighted_sq = (77 * R * R + 150 * G * G + 29 * B * B + 128) >> 8;
179  const float p = sqrt_lut[weighted_sq];
180 
181  // Adjust common saturation
182  R = clamp_i(static_cast<int>(p + (R - p) * saturation_value));
183  G = clamp_i(static_cast<int>(p + (G - p) * saturation_value));
184  B = clamp_i(static_cast<int>(p + (B - p) * saturation_value));
185 
186  // Compute per-channel replacement brightness
187  const float p_r = R * sqrt_pR;
188  const float p_g = G * sqrt_pG;
189  const float p_b = B * sqrt_pB;
190 
191  // Adjust channel-separated saturation
192  const int Rr = static_cast<int>(p_r + (R - p_r) * saturation_value_R);
193  const int Gr = static_cast<int>(p_r - p_r * saturation_value_R);
194  const int Br = static_cast<int>(p_r - p_r * saturation_value_R);
195 
196  const int Rg = static_cast<int>(p_g - p_g * saturation_value_G);
197  const int Gg = static_cast<int>(p_g + (G - p_g) * saturation_value_G);
198  const int Bg = static_cast<int>(p_g - p_g * saturation_value_G);
199 
200  const int Rb = static_cast<int>(p_b - p_b * saturation_value_B);
201  const int Gb = static_cast<int>(p_b - p_b * saturation_value_B);
202  const int Bb = static_cast<int>(p_b + (B - p_b) * saturation_value_B);
203 
204  // Recombine and constrain values
205  R = clamp_i(Rr + Rg + Rb);
206  G = clamp_i(Gr + Gg + Gb);
207  B = clamp_i(Br + Bg + Bb);
208  };
209 
210  #pragma omp parallel for if(pixel_count >= 16384) schedule(static) shared (pixels)
211  for (int y = 0; y < height; ++y)
212  {
213  unsigned char* row = pixels + y * stride;
214  for (int x = 0; x < width; ++x) {
215  const int idx = x * 4;
216 
217  // Split hot paths by alpha to avoid unnecessary premultiply/unpremultiply work.
218  const int A = row[idx + 3];
219  if (A <= 0)
220  continue;
221  int R = 0;
222  int G = 0;
223  int B = 0;
224  if (A == 255) {
225  R = row[idx + 0];
226  G = row[idx + 1];
227  B = row[idx + 2];
228  apply_saturation(R, G, B);
229  row[idx + 0] = static_cast<unsigned char>(R);
230  row[idx + 1] = static_cast<unsigned char>(G);
231  row[idx + 2] = static_cast<unsigned char>(B);
232  } else {
233  const float alpha_percent = static_cast<float>(A) * (1.0f / 255.0f);
234  const float inv_alpha_percent = inv_alpha[A];
235 
236  // Get RGB values, and remove pre-multiplied alpha
237  R = static_cast<int>(row[idx + 0] * inv_alpha_percent);
238  G = static_cast<int>(row[idx + 1] * inv_alpha_percent);
239  B = static_cast<int>(row[idx + 2] * inv_alpha_percent);
240  apply_saturation(R, G, B);
241 
242  // Pre-multiply alpha back into color channels
243  row[idx + 0] = static_cast<unsigned char>(R * alpha_percent);
244  row[idx + 1] = static_cast<unsigned char>(G * alpha_percent);
245  row[idx + 2] = static_cast<unsigned char>(B * alpha_percent);
246  }
247  }
248  }
249 
250  // return the modified frame
251  return frame;
252 }
253 
254 // Generate JSON string of this object
255 std::string Saturation::Json() const {
256 
257  // Return formatted string
258  return JsonValue().toStyledString();
259 }
260 
261 // Generate Json::Value for this object
262 Json::Value Saturation::JsonValue() const {
263 
264  // Create root json object
265  Json::Value root = EffectBase::JsonValue(); // get parent properties
266  root["type"] = info.class_name;
267  root["saturation"] = saturation.JsonValue();
268  root["saturation_R"] = saturation_R.JsonValue();
269  root["saturation_G"] = saturation_G.JsonValue();
270  root["saturation_B"] = saturation_B.JsonValue();
271  root["mask_mode"] = mask_mode;
272 
273  // return JsonValue
274  return root;
275 }
276 
277 // Load JSON string into this object
278 void Saturation::SetJson(const std::string value) {
279 
280  // Parse JSON string into JSON objects
281  try
282  {
283  const Json::Value root = openshot::stringToJson(value);
284  // Set all values that match
285  SetJsonValue(root);
286  }
287  catch (const std::exception& e)
288  {
289  // Error parsing JSON (or missing keys)
290  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
291  }
292 }
293 
294 // Load Json::Value into this object
295 void Saturation::SetJsonValue(const Json::Value root) {
296 
297  // Set parent data
299 
300  // Set data from Json (if key is found)
301  if (!root["saturation"].isNull())
302  saturation.SetJsonValue(root["saturation"]);
303  if (!root["saturation_R"].isNull())
304  saturation_R.SetJsonValue(root["saturation_R"]);
305  if (!root["saturation_G"].isNull())
306  saturation_G.SetJsonValue(root["saturation_G"]);
307  if (!root["saturation_B"].isNull())
308  saturation_B.SetJsonValue(root["saturation_B"]);
309  if (!root["mask_mode"].isNull())
310  mask_mode = root["mask_mode"].asInt();
311 }
312 
313 // Get all properties for a specific frame
314 std::string Saturation::PropertiesJSON(int64_t requested_frame) const {
315 
316  // Generate JSON properties list
317  Json::Value root = BasePropertiesJSON(requested_frame);
318 
319  // Keyframes
320  root["saturation"] = add_property_json("Saturation", saturation.GetValue(requested_frame), "float", "", &saturation, 0.0, 4.0, false, requested_frame);
321  root["saturation_R"] = add_property_json("Saturation (Red)", saturation_R.GetValue(requested_frame), "float", "", &saturation_R, 0.0, 4.0, false, requested_frame);
322  root["saturation_G"] = add_property_json("Saturation (Green)", saturation_G.GetValue(requested_frame), "float", "", &saturation_G, 0.0, 4.0, false, requested_frame);
323  root["saturation_B"] = add_property_json("Saturation (Blue)", saturation_B.GetValue(requested_frame), "float", "", &saturation_B, 0.0, 4.0, false, requested_frame);
324  root["mask_mode"] = add_property_json("Mask Mode", mask_mode, "int", "", NULL, 0, 1, false, requested_frame);
325  root["mask_mode"]["choices"].append(add_property_choice_json("Limit to Mask", SATURATION_MASK_POST_BLEND, mask_mode));
326  root["mask_mode"]["choices"].append(add_property_choice_json("Vary Strength", SATURATION_MASK_DRIVE_AMOUNT, mask_mode));
327 
328  // Return formatted string
329  return root.toStyledString();
330 }
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::stringToJson
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:16
openshot::EffectBase::info
EffectInfoStruct info
Information about the current effect.
Definition: EffectBase.h:114
openshot::Saturation::Json
std::string Json() const override
Generate JSON string of this object.
Definition: Saturation.cpp:255
openshot::EffectBase::mask_invert
bool mask_invert
Invert grayscale mask values before blending.
Definition: EffectBase.h:115
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: AnimatedCurve.h:24
openshot::ClipBase::add_property_choice_json
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:132
openshot::EffectBase::JsonValue
virtual Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: EffectBase.cpp:102
openshot::Keyframe::SetJsonValue
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: KeyFrame.cpp:372
openshot::Saturation::mask_mode
int mask_mode
How to apply common masks to saturation (post-blend or drive-amount).
Definition: Saturation.h:62
openshot::Saturation::saturation_R
Keyframe saturation_R
Red color saturation.
Definition: Saturation.h:59
openshot::Saturation::SetJsonValue
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition: Saturation.cpp:295
openshot::Saturation::JsonValue
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition: Saturation.cpp:262
openshot::SATURATION_MASK_DRIVE_AMOUNT
@ SATURATION_MASK_DRIVE_AMOUNT
Definition: Saturation.h:37
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:257
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:223
openshot::EffectBase::InitEffectInfo
void InitEffectInfo()
Definition: EffectBase.cpp:42
openshot::EffectInfoStruct::has_audio
bool has_audio
Determines if this effect manipulates the audio of a frame.
Definition: EffectBase.h:44
openshot::EffectInfoStruct::class_name
std::string class_name
The class name of the effect.
Definition: EffectBase.h:39
openshot::EffectInfoStruct::description
std::string description
The description of this effect and what it does.
Definition: EffectBase.h:41
openshot::EffectInfoStruct::has_video
bool has_video
Determines if this effect manipulates the image of a frame.
Definition: EffectBase.h:43
openshot::Saturation::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: Saturation.h:81
openshot::Saturation::saturation_G
Keyframe saturation_G
Green color saturation.
Definition: Saturation.h:60
openshot::Saturation::saturation_B
Keyframe saturation_B
Blue color saturation.
Definition: Saturation.h:61
openshot::Saturation::UseCustomMaskBlend
bool UseCustomMaskBlend(int64_t frame_number) const override
Optional override for effects that need custom mask behavior.
Definition: Saturation.cpp:36
Saturation.h
Header file for Saturation class.
openshot::EffectInfoStruct::name
std::string name
The name of the effect.
Definition: EffectBase.h:40
openshot::Saturation::PropertiesJSON
std::string PropertiesJSON(int64_t requested_frame) const override
Definition: Saturation.cpp:314
openshot::Saturation::ApplyCustomMaskBlend
void ApplyCustomMaskBlend(std::shared_ptr< QImage > original_image, std::shared_ptr< QImage > effected_image, std::shared_ptr< QImage > mask_image, int64_t frame_number) const override
Optional override for effects with custom mask implementation.
Definition: Saturation.cpp:41
openshot::Saturation::Saturation
Saturation()
Blank constructor, useful when using Json to load the effect properties.
Definition: Saturation.cpp:21
openshot::SATURATION_MASK_POST_BLEND
@ SATURATION_MASK_POST_BLEND
Definition: Saturation.h:36
openshot::Saturation::SetJson
void SetJson(const std::string value) override
Load JSON string into this object.
Definition: Saturation.cpp:278
Exceptions.h
Header file for all Exception classes.
openshot::Saturation::saturation
Keyframe saturation
Overall color saturation: 0.0 = greyscale, 1.0 = normal, 2.0 = double saturation.
Definition: Saturation.h:58
openshot::EffectBase::SetJsonValue
virtual void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: EffectBase.cpp:146
openshot::Keyframe::GetValue
double GetValue(int64_t index) const
Get the value at a specific index.
Definition: KeyFrame.cpp:258