From 63ee36ddfe0fa454450cd9ae0a66a8673fdfc9dc Mon Sep 17 00:00:00 2001 From: Michael Kirsch Date: Sun, 10 Dec 2023 21:13:50 +0100 Subject: [PATCH] Enable Area Selection in ScreenshotHelper class --- src/macro-core/macro-action-screenshot.cpp | 2 +- .../video/macro-condition-video.cpp | 18 ++++++------- src/macro-external/video/preview-dialog.cpp | 13 +++++----- src/utils/screenshot-helper.cpp | 26 ++++++++++++++----- src/utils/screenshot-helper.hpp | 7 ++--- 5 files changed, 41 insertions(+), 25 deletions(-) diff --git a/src/macro-core/macro-action-screenshot.cpp b/src/macro-core/macro-action-screenshot.cpp index aa1565aa..b6e5f5d4 100644 --- a/src/macro-core/macro-action-screenshot.cpp +++ b/src/macro-core/macro-action-screenshot.cpp @@ -32,7 +32,7 @@ void MacroActionScreenshot::CustomScreenshot(OBSWeakSource &source) } auto s = obs_weak_source_get_source(source); _screenshot.~ScreenshotHelper(); - new (&_screenshot) ScreenshotHelper(s, false, 0, true, _path); + new (&_screenshot) ScreenshotHelper(s, QRect(), false, 0, true, _path); obs_source_release(s); } diff --git a/src/macro-external/video/macro-condition-video.cpp b/src/macro-external/video/macro-condition-video.cpp index c4909d51..a6c5a4de 100644 --- a/src/macro-external/video/macro-condition-video.cpp +++ b/src/macro-external/video/macro-condition-video.cpp @@ -215,8 +215,15 @@ void MacroConditionVideo::GetScreenshot(bool blocking) { auto source = obs_weak_source_get_source(_video.GetVideo()); _screenshotData.~ScreenshotHelper(); - new (&_screenshotData) - ScreenshotHelper(source, blocking, GetSwitcher()->interval); + QRect screenshotArea; + if (_areaParameters.enable && _condition != VideoCondition::NO_IMAGE) { + screenshotArea.setRect(_areaParameters.area.x, + _areaParameters.area.y, + _areaParameters.area.width, + _areaParameters.area.height); + } + new (&_screenshotData) ScreenshotHelper( + source, screenshotArea, blocking, GetSwitcher()->interval); obs_source_release(source); _getNextScreenshot = false; } @@ -361,13 +368,6 @@ bool MacroConditionVideo::CheckColor() bool MacroConditionVideo::Compare() { - if (_areaParameters.enable && _condition != VideoCondition::NO_IMAGE) { - _screenshotData.image = _screenshotData.image.copy( - _areaParameters.area.x, _areaParameters.area.y, - _areaParameters.area.width, - _areaParameters.area.height); - } - if (_condition != VideoCondition::OCR) { SetVariableValue(""); } diff --git a/src/macro-external/video/preview-dialog.cpp b/src/macro-external/video/preview-dialog.cpp index 1443d888..e8b2c8ae 100644 --- a/src/macro-external/video/preview-dialog.cpp +++ b/src/macro-external/video/preview-dialog.cpp @@ -255,7 +255,13 @@ void PreviewImage::CreateImage(const VideoInput &video, PreviewType type, VideoCondition condition) { auto source = obs_weak_source_get_source(video.GetVideo()); - ScreenshotHelper screenshot(source, true); + QRect screenshotArea; + if (areaParams.enable && type == PreviewType::SHOW_MATCH) { + screenshotArea.setRect(areaParams.area.x, areaParams.area.y, + areaParams.area.width, + areaParams.area.height); + } + ScreenshotHelper screenshot(source, screenshotArea, true); obs_source_release(source); if (!video.ValidSelection() || !screenshot.done) { @@ -274,11 +280,6 @@ void PreviewImage::CreateImage(const VideoInput &video, PreviewType type, if (type == PreviewType::SHOW_MATCH) { std::unique_lock lock(_mtx); - if (areaParams.enable) { - screenshot.image = screenshot.image.copy( - areaParams.area.x, areaParams.area.y, - areaParams.area.width, areaParams.area.height); - } // Will emit status label update MarkMatch(screenshot.image, patternMatchParams, patternImageData, objDetectParams, ocrParams, diff --git a/src/utils/screenshot-helper.cpp b/src/utils/screenshot-helper.cpp index 90e0003c..75de17f5 100644 --- a/src/utils/screenshot-helper.cpp +++ b/src/utils/screenshot-helper.cpp @@ -7,10 +7,11 @@ namespace advss { static void ScreenshotTick(void *param, float); -ScreenshotHelper::ScreenshotHelper(obs_source_t *source, bool blocking, - int timeout, bool saveToFile, +ScreenshotHelper::ScreenshotHelper(obs_source_t *source, const QRect &subarea, + bool blocking, int timeout, bool saveToFile, std::string path) : weakSource(OBSGetWeakRef(source)), + _subarea(subarea), _blocking(blocking), _saveToFile(saveToFile), _path(path) @@ -62,7 +63,12 @@ void ScreenshotHelper::Screenshot() cy = ovi.base_height; } - if (!cx || !cy) { + QRect renderArea(0, 0, cx, cy); + if (!_subarea.isEmpty()) { + renderArea &= _subarea; + } + + if (renderArea.isEmpty()) { vblog(LOG_WARNING, "Cannot screenshot \"%s\", invalid target size", obs_source_get_name(source)); @@ -71,16 +77,24 @@ void ScreenshotHelper::Screenshot() return; } + cx = renderArea.width(); + cy = renderArea.height(); + texrender = gs_texrender_create(GS_RGBA, GS_ZS_NONE); - stagesurf = gs_stagesurface_create(cx, cy, GS_RGBA); + stagesurf = gs_stagesurface_create(renderArea.width(), + renderArea.height(), GS_RGBA); gs_texrender_reset(texrender); - if (gs_texrender_begin(texrender, cx, cy)) { + if (gs_texrender_begin(texrender, renderArea.width(), + renderArea.height())) { vec4 zero; vec4_zero(&zero); gs_clear(GS_CLEAR_COLOR, &zero, 0.0f, 0); - gs_ortho(0.0f, (float)cx, 0.0f, (float)cy, -100.0f, 100.0f); + gs_ortho((float)(renderArea.left()), + (float)(renderArea.right() + 1), + (float)(renderArea.top()), + (float)(renderArea.bottom() + 1), -100.0f, 100.0f); gs_blend_state_push(); gs_blend_function(GS_BLEND_ONE, GS_BLEND_ZERO); diff --git a/src/utils/screenshot-helper.hpp b/src/utils/screenshot-helper.hpp index 19cd8797..ee86742b 100644 --- a/src/utils/screenshot-helper.hpp +++ b/src/utils/screenshot-helper.hpp @@ -12,9 +12,9 @@ namespace advss { class ScreenshotHelper { public: ScreenshotHelper() = default; - ScreenshotHelper(obs_source_t *source, bool blocking = false, - int timeout = 1000, bool saveToFile = false, - std::string path = ""); + ScreenshotHelper(obs_source_t *source, const QRect &subarea = QRect(), + bool blocking = false, int timeout = 1000, + bool saveToFile = false, std::string path = ""); ScreenshotHelper &operator=(const ScreenshotHelper &) = delete; ScreenshotHelper(const ScreenshotHelper &) = delete; ~ScreenshotHelper(); @@ -39,6 +39,7 @@ public: private: std::atomic_bool _initDone = false; + QRect _subarea = QRect(); bool _blocking = false; std::thread _saveThread; bool _saveToFile = false;