OpenShot Library | libopenshot  0.5.0
Sharpen.cpp
Go to the documentation of this file.
1 
9 // Copyright (c) 2008-2025 OpenShot Studios, LLC
10 //
11 // SPDX-License-Identifier: LGPL-3.0-or-later
12 
13 
14 #include "Sharpen.h"
15 #include "Exceptions.h"
16 #include <algorithm>
17 #include <cmath>
18 #include <vector>
19 #include <omp.h>
20 
21 using namespace openshot;
22 
23 // Constructor with default keyframes
25  : amount(10.0)
26  , radius(3.0)
27  , threshold(0.0)
28  , mode(0)
29  , channel(1)
30  , mask_mode(SHARPEN_MASK_LIMIT_TO_AREA)
31 {
32  init_effect_details();
33 }
34 
35 // Constructor from keyframes
37  : amount(a)
38  , radius(r)
39  , threshold(t)
40  , mode(0)
41  , channel(1)
42  , mask_mode(SHARPEN_MASK_LIMIT_TO_AREA)
43 {
44  init_effect_details();
45 }
46 
47 // Initialize effect metadata
48 void Sharpen::init_effect_details()
49 {
51  info.class_name = "Sharpen";
52  info.name = "Sharpen";
53  info.description = "Boost edge contrast to make video details look crisper.";
54  info.has_audio = false;
55  info.has_video = true;
56 }
57 
58 bool Sharpen::UseCustomMaskBlend(int64_t frame_number) const
59 {
60  (void) frame_number;
62 }
63 
64 void Sharpen::ApplyCustomMaskBlend(std::shared_ptr<QImage> original_image, std::shared_ptr<QImage> effected_image,
65  std::shared_ptr<QImage> mask_image, int64_t frame_number) const
66 {
67  (void) frame_number;
68  if (!original_image || !effected_image || !mask_image)
69  return;
70  if (original_image->size() != effected_image->size() || effected_image->size() != mask_image->size())
71  return;
72 
73  unsigned char* original_pixels = reinterpret_cast<unsigned char*>(original_image->bits());
74  unsigned char* effected_pixels = reinterpret_cast<unsigned char*>(effected_image->bits());
75  unsigned char* mask_pixels = reinterpret_cast<unsigned char*>(mask_image->bits());
76  const int pixel_count = effected_image->width() * effected_image->height();
77 
78  #pragma omp parallel for schedule(static)
79  for (int i = 0; i < pixel_count; ++i) {
80  const int idx = i * 4;
81  float factor = static_cast<float>(qGray(mask_pixels[idx], mask_pixels[idx + 1], mask_pixels[idx + 2])) / 255.0f;
82  if (mask_invert)
83  factor = 1.0f - factor;
84  factor = factor * factor;
85  const float inverse = 1.0f - factor;
86 
87  effected_pixels[idx] = static_cast<unsigned char>(
88  (original_pixels[idx] * inverse) + (effected_pixels[idx] * factor));
89  effected_pixels[idx + 1] = static_cast<unsigned char>(
90  (original_pixels[idx + 1] * inverse) + (effected_pixels[idx + 1] * factor));
91  effected_pixels[idx + 2] = static_cast<unsigned char>(
92  (original_pixels[idx + 2] * inverse) + (effected_pixels[idx + 2] * factor));
93  effected_pixels[idx + 3] = original_pixels[idx + 3];
94  }
95 }
96 
97 // Compute three box sizes to approximate a Gaussian of sigma
98 static void boxes_for_gauss(double sigma, int b[3])
99 {
100  const int n = 3;
101  double wi = std::sqrt((12.0 * sigma * sigma / n) + 1.0);
102  int wl = int(std::floor(wi));
103  if (!(wl & 1)) --wl;
104  int wu = wl + 2;
105  double mi = (12.0 * sigma * sigma - n*wl*wl - 4.0*n*wl - 3.0*n)
106  / (-4.0*wl - 4.0);
107  int m = int(std::round(mi));
108  for (int i = 0; i < n; ++i)
109  b[i] = i < m ? wl : wu;
110 }
111 
112 // Blur one axis with an edge-replicate sliding window
113 static void blur_axis(const QImage& src, QImage& dst, int r, bool vertical)
114 {
115  if (r <= 0) {
116  dst = src.copy();
117  return;
118  }
119 
120  int W = src.width();
121  int H = src.height();
122  int bpl = src.bytesPerLine();
123  const uchar* in = src.bits();
124  uchar* out = dst.bits();
125  int window = 2*r + 1;
126 
127  if (!vertical) {
128  #pragma omp parallel for
129  for (int y = 0; y < H; ++y) {
130  const uchar* rowIn = in + y*bpl;
131  uchar* rowOut = out + y*bpl;
132  double sB = rowIn[0]*(r+1), sG = rowIn[1]*(r+1),
133  sR = rowIn[2]*(r+1), sA = rowIn[3]*(r+1);
134  for (int x = 1; x <= r; ++x) {
135  const uchar* p = rowIn + std::min(x, W-1)*4;
136  sB += p[0]; sG += p[1]; sR += p[2]; sA += p[3];
137  }
138  for (int x = 0; x < W; ++x) {
139  uchar* o = rowOut + x*4;
140  o[0] = uchar(sB / window + 0.5);
141  o[1] = uchar(sG / window + 0.5);
142  o[2] = uchar(sR / window + 0.5);
143  o[3] = uchar(sA / window + 0.5);
144 
145  const uchar* addP = rowIn + std::min(x+r+1, W-1)*4;
146  const uchar* subP = rowIn + std::max(x-r, 0)*4;
147  sB += addP[0] - subP[0];
148  sG += addP[1] - subP[1];
149  sR += addP[2] - subP[2];
150  sA += addP[3] - subP[3];
151  }
152  }
153  }
154  else {
155  #pragma omp parallel for
156  for (int x = 0; x < W; ++x) {
157  double sB = 0, sG = 0, sR = 0, sA = 0;
158  const uchar* p0 = in + x*4;
159  sB = p0[0]*(r+1); sG = p0[1]*(r+1);
160  sR = p0[2]*(r+1); sA = p0[3]*(r+1);
161  for (int y = 1; y <= r; ++y) {
162  const uchar* p = in + std::min(y, H-1)*bpl + x*4;
163  sB += p[0]; sG += p[1]; sR += p[2]; sA += p[3];
164  }
165  for (int y = 0; y < H; ++y) {
166  uchar* o = out + y*bpl + x*4;
167  o[0] = uchar(sB / window + 0.5);
168  o[1] = uchar(sG / window + 0.5);
169  o[2] = uchar(sR / window + 0.5);
170  o[3] = uchar(sA / window + 0.5);
171 
172  const uchar* addP = in + std::min(y+r+1, H-1)*bpl + x*4;
173  const uchar* subP = in + std::max(y-r, 0)*bpl + x*4;
174  sB += addP[0] - subP[0];
175  sG += addP[1] - subP[1];
176  sR += addP[2] - subP[2];
177  sA += addP[3] - subP[3];
178  }
179  }
180  }
181 }
182 
183 // Wrapper to handle fractional radius by blending two integer passes
184 static void box_blur(const QImage& src, QImage& dst, double rf, bool vertical)
185 {
186  int r0 = int(std::floor(rf));
187  int r1 = r0 + 1;
188  double f = rf - r0;
189  if (f < 1e-4) {
190  blur_axis(src, dst, r0, vertical);
191  }
192  else {
193  QImage a(src.size(), QImage::Format_ARGB32);
194  QImage b(src.size(), QImage::Format_ARGB32);
195  blur_axis(src, a, r0, vertical);
196  blur_axis(src, b, r1, vertical);
197 
198  int pixels = src.width() * src.height();
199  const uchar* pa = a.bits();
200  const uchar* pb = b.bits();
201  uchar* pd = dst.bits();
202  #pragma omp parallel for
203  for (int i = 0; i < pixels; ++i) {
204  for (int c = 0; c < 4; ++c) {
205  pd[i*4+c] = uchar((1.0 - f) * pa[i*4+c]
206  + f * pb[i*4+c]
207  + 0.5);
208  }
209  }
210  }
211 }
212 
213 // Apply three sequential box blurs to approximate Gaussian
214 static void gauss_blur(const QImage& src, QImage& dst, double sigma)
215 {
216  int b[3];
217  boxes_for_gauss(sigma, b);
218  QImage t1(src.size(), QImage::Format_ARGB32);
219  QImage t2(src.size(), QImage::Format_ARGB32);
220 
221  double r = 0.5 * (b[0] - 1);
222  box_blur(src , t1, r, false);
223  box_blur(t1, t2, r, true);
224 
225  r = 0.5 * (b[1] - 1);
226  box_blur(t2, t1, r, false);
227  box_blur(t1, t2, r, true);
228 
229  r = 0.5 * (b[2] - 1);
230  box_blur(t2, t1, r, false);
231  box_blur(t1, dst, r, true);
232 }
233 
234 // Main frame processing
235 std::shared_ptr<Frame> Sharpen::GetFrame(
236  std::shared_ptr<Frame> frame, int64_t frame_number)
237 {
238  auto img = frame->GetImage();
239  if (!img || img->isNull())
240  return frame;
241  if (img->format() != QImage::Format_ARGB32)
242  *img = img->convertToFormat(QImage::Format_ARGB32);
243 
244  int W = img->width();
245  int H = img->height();
246  if (W <= 0 || H <= 0)
247  return frame;
248 
249  // Retrieve keyframe values
250  double amt = amount.GetValue(frame_number); // 0–40
251  double rpx = radius.GetValue(frame_number); // px
252  double thrUI = threshold.GetValue(frame_number); // 0–1
253 
254  // Sigma scaled against 720p reference
255  double sigma = std::max(0.1, rpx * H / 720.0);
256 
257  // Generate blurred image
258  QImage blur(W, H, QImage::Format_ARGB32);
259  gauss_blur(*img, blur, sigma);
260 
261  // Precompute maximum luma difference for adaptive threshold
262  int bplS = img->bytesPerLine();
263  int bplB = blur.bytesPerLine();
264  uchar* sBits = img->bits();
265  uchar* bBits = blur.bits();
266 
267  double maxDY = 0.0;
268  #pragma omp parallel for reduction(max:maxDY)
269  for (int y = 0; y < H; ++y) {
270  uchar* sRow = sBits + y * bplS;
271  uchar* bRow = bBits + y * bplB;
272  for (int x = 0; x < W; ++x) {
273  double dB = double(sRow[x*4+0]) - double(bRow[x*4+0]);
274  double dG = double(sRow[x*4+1]) - double(bRow[x*4+1]);
275  double dR = double(sRow[x*4+2]) - double(bRow[x*4+2]);
276  double dY = std::abs(0.114*dB + 0.587*dG + 0.299*dR);
277  maxDY = std::max(maxDY, dY);
278  }
279  }
280 
281  // Compute actual threshold in luma units
282  double thr = thrUI * maxDY;
283 
284  // Process pixels
285  #pragma omp parallel for
286  for (int y = 0; y < H; ++y) {
287  uchar* sRow = sBits + y * bplS;
288  uchar* bRow = bBits + y * bplB;
289  for (int x = 0; x < W; ++x) {
290  uchar* sp = sRow + x*4;
291  uchar* bp = bRow + x*4;
292 
293  // Detail per channel
294  double dB = double(sp[0]) - double(bp[0]);
295  double dG = double(sp[1]) - double(bp[1]);
296  double dR = double(sp[2]) - double(bp[2]);
297  double dY = 0.114*dB + 0.587*dG + 0.299*dR;
298 
299  // Skip if below adaptive threshold
300  if (std::abs(dY) < thr)
301  continue;
302 
303  // Halo limiter
304  auto halo = [](double d) {
305  return (255.0 - std::abs(d)) / 255.0;
306  };
307 
308  double outC[3];
309 
310  if (mode == 1) {
311  // HighPass: base = blurred image
312  // detail = original – blurred
313  // no halo limiter
314 
315  // precompute normalized luma weights
316  const double wB = 0.114, wG = 0.587, wR = 0.299;
317 
318  if (channel == 1) {
319  // Luma only: add back luma detail weighted per channel
320  double lumaInc = amt * dY;
321  outC[0] = bp[0] + lumaInc * wB;
322  outC[1] = bp[1] + lumaInc * wG;
323  outC[2] = bp[2] + lumaInc * wR;
324  }
325  else if (channel == 2) {
326  // Chroma only: subtract luma from detail, add chroma back
327  double lumaDetail = dY;
328  double chromaB = dB - lumaDetail * wB;
329  double chromaG = dG - lumaDetail * wG;
330  double chromaR = dR - lumaDetail * wR;
331  outC[0] = bp[0] + amt * chromaB;
332  outC[1] = bp[1] + amt * chromaG;
333  outC[2] = bp[2] + amt * chromaR;
334  }
335  else {
336  // All channels: add full per-channel detail
337  outC[0] = bp[0] + amt * dB;
338  outC[1] = bp[1] + amt * dG;
339  outC[2] = bp[2] + amt * dR;
340  }
341  }
342  else {
343  // Unsharp-Mask: base = original + amt * detail * halo(detail)
344  if (channel == 1) {
345  // Luma only
346  double inc = amt * dY * halo(dY);
347  for (int c = 0; c < 3; ++c)
348  outC[c] = sp[c] + inc;
349  }
350  else if (channel == 2) {
351  // Chroma only
352  double l = dY;
353  double chroma[3] = { dB - l, dG - l, dR - l };
354  for (int c = 0; c < 3; ++c)
355  outC[c] = sp[c] + amt * chroma[c] * halo(chroma[c]);
356  }
357  else {
358  // All channels
359  outC[0] = sp[0] + amt * dB * halo(dB);
360  outC[1] = sp[1] + amt * dG * halo(dG);
361  outC[2] = sp[2] + amt * dR * halo(dR);
362  }
363  }
364 
365  // Write back clamped
366  for (int c = 0; c < 3; ++c) {
367  sp[c] = uchar(std::clamp(outC[c], 0.0, 255.0) + 0.5);
368  }
369  }
370  }
371 
372  return frame;
373 }
374 
375 // JSON serialization
376 std::string Sharpen::Json() const
377 {
378  return JsonValue().toStyledString();
379 }
380 
381 Json::Value Sharpen::JsonValue() const
382 {
383  Json::Value root = EffectBase::JsonValue();
384  root["type"] = info.class_name;
385  root["amount"] = amount.JsonValue();
386  root["radius"] = radius.JsonValue();
387  root["threshold"] = threshold.JsonValue();
388  root["mode"] = mode;
389  root["channel"] = channel;
390  root["mask_mode"] = mask_mode;
391  return root;
392 }
393 
394 // JSON deserialization
395 void Sharpen::SetJson(std::string value)
396 {
397  auto root = openshot::stringToJson(value);
398  SetJsonValue(root);
399 }
400 
401 void Sharpen::SetJsonValue(Json::Value root)
402 {
404  if (!root["amount"].isNull())
405  amount.SetJsonValue(root["amount"]);
406  if (!root["radius"].isNull())
407  radius.SetJsonValue(root["radius"]);
408  if (!root["threshold"].isNull())
409  threshold.SetJsonValue(root["threshold"]);
410  if (!root["mode"].isNull())
411  mode = root["mode"].asInt();
412  if (!root["channel"].isNull())
413  channel = root["channel"].asInt();
414  if (!root["mask_mode"].isNull())
415  mask_mode = root["mask_mode"].asInt();
416 }
417 
418 // UI property definitions
419 std::string Sharpen::PropertiesJSON(int64_t t) const
420 {
421  Json::Value root = BasePropertiesJSON(t);
422  root["amount"] = add_property_json(
423  "Amount", amount.GetValue(t), "float", "", &amount, 0, 40, false, t);
424  root["radius"] = add_property_json(
425  "Radius", radius.GetValue(t), "float", "pixels", &radius, 0, 10, false, t);
426  root["threshold"] = add_property_json(
427  "Threshold", threshold.GetValue(t), "float", "ratio", &threshold, 0, 1, false, t);
428  root["mode"] = add_property_json(
429  "Mode", mode, "int", "", nullptr, 0, 1, false, t);
430  root["mode"]["choices"].append(add_property_choice_json("UnsharpMask", 0, mode));
431  root["mode"]["choices"].append(add_property_choice_json("HighPassBlend", 1, mode));
432  root["channel"] = add_property_json(
433  "Channel", channel, "int", "", nullptr, 0, 2, false, t);
434  root["channel"]["choices"].append(add_property_choice_json("All", 0, channel));
435  root["channel"]["choices"].append(add_property_choice_json("Luma", 1, channel));
436  root["channel"]["choices"].append(add_property_choice_json("Chroma", 2, channel));
437  root["mask_mode"] = add_property_json(
438  "Mask Mode", mask_mode, "int", "", nullptr, 0, 1, false, t);
439  root["mask_mode"]["choices"].append(add_property_choice_json("Limit to Mask", SHARPEN_MASK_LIMIT_TO_AREA, mask_mode));
440  root["mask_mode"]["choices"].append(add_property_choice_json("Vary Strength", SHARPEN_MASK_VARY_STRENGTH, mask_mode));
441  return root.toStyledString();
442 }
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::Sharpen::SetJson
void SetJson(const std::string value) override
Load JSON string into this object.
Definition: Sharpen.cpp:395
openshot::Sharpen::mode
int mode
Sharpening mode (0 = UnsharpMask, 1 = HighPassBlend)
Definition: Sharpen.h:57
openshot::SHARPEN_MASK_VARY_STRENGTH
@ SHARPEN_MASK_VARY_STRENGTH
Definition: Sharpen.h:27
openshot::EffectBase::info
EffectInfoStruct info
Information about the current effect.
Definition: EffectBase.h:110
openshot::EffectBase::mask_invert
bool mask_invert
Invert grayscale mask values before blending.
Definition: EffectBase.h:111
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:28
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:96
openshot::Sharpen::mask_mode
int mask_mode
Mask behavior mode for this effect.
Definition: Sharpen.h:63
openshot::Sharpen::PropertiesJSON
std::string PropertiesJSON(int64_t requested_frame) const override
Definition: Sharpen.cpp:419
openshot::Sharpen::Json
std::string Json() const override
Get and Set JSON methods.
Definition: Sharpen.cpp:376
openshot::Sharpen::radius
Keyframe radius
Radius of the blur used in sharpening (0 to 10 pixels for 1080p)
Definition: Sharpen.h:51
openshot::Sharpen::GetFrame
std::shared_ptr< Frame > GetFrame(std::shared_ptr< Frame > frame, int64_t frame_number) override
This method is required for all derived classes of EffectBase, and returns a modified openshot::Frame...
Definition: Sharpen.cpp:235
openshot::Keyframe::SetJsonValue
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: KeyFrame.cpp:372
openshot::Sharpen::channel
int channel
Channel to apply sharpening to (0 = All, 1 = Luma, 2 = Chroma)
Definition: Sharpen.h:60
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:236
Sharpen.h
Header file for Sharpen effect class.
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::Sharpen::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: Sharpen.cpp:64
openshot::EffectBase::InitEffectInfo
void InitEffectInfo()
Definition: EffectBase.cpp:37
openshot::EffectInfoStruct::has_audio
bool has_audio
Determines if this effect manipulates the audio of a frame.
Definition: EffectBase.h:44
openshot::SHARPEN_MASK_LIMIT_TO_AREA
@ SHARPEN_MASK_LIMIT_TO_AREA
Definition: Sharpen.h:26
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::Sharpen::JsonValue
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition: Sharpen.cpp:381
openshot::EffectInfoStruct::has_video
bool has_video
Determines if this effect manipulates the image of a frame.
Definition: EffectBase.h:43
openshot::Sharpen::SetJsonValue
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition: Sharpen.cpp:401
openshot::EffectInfoStruct::name
std::string name
The name of the effect.
Definition: EffectBase.h:40
openshot::Sharpen::Sharpen
Sharpen()
Default constructor.
Definition: Sharpen.cpp:24
openshot::Sharpen::threshold
Keyframe threshold
Threshold for applying sharpening (0 to 1)
Definition: Sharpen.h:54
openshot::Sharpen::amount
Keyframe amount
Amount of sharpening to apply (0 to 2)
Definition: Sharpen.h:48
openshot::Sharpen::UseCustomMaskBlend
bool UseCustomMaskBlend(int64_t frame_number) const override
Optional override for effects that need custom mask behavior.
Definition: Sharpen.cpp:58
Exceptions.h
Header file for all Exception classes.
openshot::EffectBase::SetJsonValue
virtual void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: EffectBase.cpp:139
openshot::Keyframe::GetValue
double GetValue(int64_t index) const
Get the value at a specific index.
Definition: KeyFrame.cpp:258