|
3 | 3 | #include <hyperion/Grabber.h> |
4 | 4 | #include <HyperionConfig.h> |
5 | 5 |
|
| 6 | +// std |
| 7 | +#include <cstring> |
| 8 | + |
6 | 9 | // utils includes |
7 | 10 | #include <utils/GlobalSignals.h> |
8 | 11 | #include <events/EventHandler.h> |
@@ -263,6 +266,34 @@ void GrabberWrapper::setCropping(int cropLeft, int cropRight, int cropTop, int c |
263 | 266 | _ggrabber->setCropping(cropLeft, cropRight, cropTop, cropBottom); |
264 | 267 | } |
265 | 268 |
|
| 269 | +void GrabberWrapper::applyAspectRatio() |
| 270 | +{ |
| 271 | + if (_targetAspectRatio <= 0.0f) |
| 272 | + return; |
| 273 | + |
| 274 | + const int srcW = _image.width(); |
| 275 | + const int srcH = _image.height(); |
| 276 | + // Round to nearest even number to keep stride well-aligned |
| 277 | + const int dstW = (static_cast<int>(srcH * _targetAspectRatio) + 1) & ~1; |
| 278 | + |
| 279 | + if (dstW <= srcW) |
| 280 | + return; |
| 281 | + |
| 282 | + if (_paddedImage.width() != dstW || _paddedImage.height() != srcH) |
| 283 | + _paddedImage.resize(dstW, srcH); |
| 284 | + |
| 285 | + _paddedImage.clear(ColorRgb::BLACK); |
| 286 | + |
| 287 | + const int offsetX = (dstW - srcW) / 2; |
| 288 | + const ColorRgb* src = _image.memptr(); |
| 289 | + ColorRgb* dst = _paddedImage.memptr(); |
| 290 | + |
| 291 | + for (int y = 0; y < srcH; ++y) |
| 292 | + memcpy(dst + y * dstW + offsetX, src + y * srcW, static_cast<size_t>(srcW) * sizeof(ColorRgb)); |
| 293 | + |
| 294 | + _image.swap(_paddedImage); |
| 295 | +} |
| 296 | + |
266 | 297 | void GrabberWrapper::updateTimer(int interval) |
267 | 298 | { |
268 | 299 | if(_updateInterval_ms != interval) |
@@ -316,6 +347,16 @@ void GrabberWrapper::handleSettingsUpdate(settings::type type, const QJsonDocume |
316 | 347 | obj["cropBottom"].toInt(0)); |
317 | 348 |
|
318 | 349 | _ggrabber->setFramerate(obj["fps"].toInt(DEFAULT_RATE_HZ)); |
| 350 | + |
| 351 | + // aspect ratio padding |
| 352 | + const QString ar = obj["aspectRatio"].toString("disabled"); |
| 353 | + if (ar == "4:3-to-16:9") |
| 354 | + _targetAspectRatio = 16.0f / 9.0f; |
| 355 | + else if (ar == "16:9-to-21:9") |
| 356 | + _targetAspectRatio = 21.0f / 9.0f; |
| 357 | + else |
| 358 | + _targetAspectRatio = 0.0f; |
| 359 | + |
319 | 360 | // eval new update time |
320 | 361 | updateTimer(_ggrabber->getUpdateInterval()); |
321 | 362 |
|
|
0 commit comments