OpenShot Library | libopenshot  0.5.0
CropHelpers.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 #include "CropHelpers.h"
14 
15 #include <algorithm>
16 #include <cmath>
17 #include <limits>
18 
19 #include "../Clip.h"
20 #include "Crop.h"
21 
22 namespace openshot {
23 
25  if (!clip) {
26  return nullptr;
27  }
28 
29  for (auto effect : clip->Effects()) {
30  if (auto crop_effect = dynamic_cast<Crop*>(effect)) {
31  if (crop_effect->resize) {
32  return crop_effect;
33  }
34  }
35  }
36 
37  return nullptr;
38 }
39 
40 void ApplyCropResizeScale(Clip* clip, int source_width, int source_height, int& max_width, int& max_height) {
41  const Crop* crop_effect = FindResizingCropEffect(clip);
42  if (!crop_effect) {
43  return;
44  }
45 
46  const float max_left = crop_effect->left.GetMaxPoint().co.Y;
47  const float max_right = crop_effect->right.GetMaxPoint().co.Y;
48  const float max_top = crop_effect->top.GetMaxPoint().co.Y;
49  const float max_bottom = crop_effect->bottom.GetMaxPoint().co.Y;
50 
51  const float visible_width = std::max(0.01f, 1.0f - max_left - max_right);
52  const float visible_height = std::max(0.01f, 1.0f - max_top - max_bottom);
53 
54  const double scaled_width = std::ceil(max_width / visible_width);
55  const double scaled_height = std::ceil(max_height / visible_height);
56 
57  const double clamped_width = std::min<double>(source_width, scaled_width);
58  const double clamped_height = std::min<double>(source_height, scaled_height);
59 
60  max_width = static_cast<int>(std::min<double>(std::numeric_limits<int>::max(), clamped_width));
61  max_height = static_cast<int>(std::min<double>(std::numeric_limits<int>::max(), clamped_height));
62 }
63 
64 } // namespace openshot
openshot::Coordinate::Y
double Y
The Y value of the coordinate (usually representing the value of the property being animated)
Definition: Coordinate.h:41
openshot::Crop::right
Keyframe right
Size of right bar.
Definition: Crop.h:47
openshot::FindResizingCropEffect
const Crop * FindResizingCropEffect(Clip *clip)
Return the first Crop effect on this clip that has resize enabled (if any)
Definition: CropHelpers.cpp:24
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:28
openshot::Point::co
Coordinate co
This is the primary coordinate.
Definition: Point.h:66
openshot::Crop::bottom
Keyframe bottom
Size of bottom bar.
Definition: Crop.h:48
openshot::Clip
This class represents a clip (used to arrange readers on the timeline)
Definition: Clip.h:89
openshot::Keyframe::GetMaxPoint
Point GetMaxPoint() const
Get max point (by Y coordinate)
Definition: KeyFrame.cpp:245
openshot::Crop::left
Keyframe left
Size of left bar.
Definition: Crop.h:45
openshot::Clip::Effects
std::list< openshot::EffectBase * > Effects()
Return the list of effects on the timeline.
Definition: Clip.h:243
openshot::Crop
This class crops a frame image (from any side), and can be animated with openshot::Keyframe curves ov...
Definition: Crop.h:37
CropHelpers.h
Shared helpers for Crop effect scaling logic.
Crop.h
Header file for Crop effect class.
openshot::ApplyCropResizeScale
void ApplyCropResizeScale(Clip *clip, int source_width, int source_height, int &max_width, int &max_height)
Scale the requested max_width / max_height based on the Crop resize amount, capped by source size.
Definition: CropHelpers.cpp:40
openshot::Crop::top
Keyframe top
Size of top bar.
Definition: Crop.h:46