OpenShot Library | libopenshot 0.5.0
Loading...
Searching...
No Matches
CropHelpers.cpp
Go to the documentation of this file.
1
8
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
22namespace 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
40void 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
Header file for Clip class.
Shared helpers for Crop effect scaling logic.
Header file for Crop effect class.
This class represents a clip (used to arrange readers on the timeline).
Definition Clip.h:89
std::list< openshot::EffectBase * > Effects()
Return the list of effects on the timeline.
Definition Clip.h:243
double Y
The Y value of the coordinate (usually representing the value of the property being animated).
Definition Coordinate.h:41
This class crops a frame image (from any side), and can be animated with openshot::Keyframe curves ov...
Definition Crop.h:38
Keyframe right
Size of right bar.
Definition Crop.h:47
Keyframe left
Size of left bar.
Definition Crop.h:45
Keyframe bottom
Size of bottom bar.
Definition Crop.h:48
Keyframe top
Size of top bar.
Definition Crop.h:46
Point GetMaxPoint() const
Get max point (by Y coordinate).
Definition KeyFrame.cpp:245
Coordinate co
This is the primary coordinate.
Definition Point.h:66
This namespace is the default namespace for all code in the openshot library.
Definition Compressor.h:29
const Crop * FindResizingCropEffect(Clip *clip)
Return the first Crop effect on this clip that has resize enabled (if any).
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.