Loading...
Searching...
No Matches
CropHelpers.cpp
Go to the documentation of this file.
// Copyright (c) 2008-2025 OpenShot Studios, LLC
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#include "CropHelpers.h"
#include <algorithm>
#include <cmath>
#include <limits>
#include "../Clip.h"
#include "Crop.h"
namespace openshot {
const Crop* FindResizingCropEffect(Clip* clip) {
if (!clip) {
return nullptr;
}
for (auto effect : clip->Effects()) {
if (auto crop_effect = dynamic_cast<Crop*>(effect)) {
if (crop_effect->resize) {
return crop_effect;
}
}
}
return nullptr;
}
void ApplyCropResizeScale(Clip* clip, int source_width, int source_height, int& max_width, int& max_height) {
const Crop* crop_effect = FindResizingCropEffect(clip);
if (!crop_effect) {
return;
}
const float max_left = crop_effect->left.GetMaxPoint().co.Y;
const float max_right = crop_effect->right.GetMaxPoint().co.Y;
const float max_top = crop_effect->top.GetMaxPoint().co.Y;
const float max_bottom = crop_effect->bottom.GetMaxPoint().co.Y;
const float visible_width = std::max(0.01f, 1.0f - max_left - max_right);
const float visible_height = std::max(0.01f, 1.0f - max_top - max_bottom);
const double scaled_width = std::ceil(max_width / visible_width);
const double scaled_height = std::ceil(max_height / visible_height);
const double clamped_width = std::min<double>(source_width, scaled_width);
const double clamped_height = std::min<double>(source_height, scaled_height);
max_width = static_cast<int>(std::min<double>(std::numeric_limits<int>::max(), clamped_width));
max_height = static_cast<int>(std::min<double>(std::numeric_limits<int>::max(), clamped_height));
}
} // namespace openshot
Clip.h
Header file for Clip class.
CropHelpers.h
Shared helpers for Crop effect scaling logic.
Crop.h
Header file for Crop effect class.
openshot::Clip
This class represents a clip (used to arrange readers on the timeline).
Definition Clip.h:89
openshot::Clip::Effects
std::list< openshot::EffectBase * > Effects()
Return the list of effects on the timeline.
Definition Clip.h:243
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
This class crops a frame image (from any side), and can be animated with openshot::Keyframe curves ov...
Definition Crop.h:38
openshot::Crop::right
Keyframe right
Size of right bar.
Definition Crop.h:47
openshot::Crop::left
Keyframe left
Size of left bar.
Definition Crop.h:45
openshot::Crop::bottom
Keyframe bottom
Size of bottom bar.
Definition Crop.h:48
openshot::Crop::top
Keyframe top
Size of top bar.
Definition Crop.h:46
openshot::Keyframe::GetMaxPoint
Point GetMaxPoint() const
Get max point (by Y coordinate).
Definition KeyFrame.cpp:245
openshot::Point::co
Coordinate co
This is the primary coordinate.
Definition Point.h:66
openshot
This namespace is the default namespace for all code in the openshot library.
Definition Compressor.h:29
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::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