From 5cd91a9c973727ba1d13b1e040dfb9eceece16e7 Mon Sep 17 00:00:00 2001 From: Jonathan Thomas Date: Wed, 12 Mar 2025 16:52:15 -0500 Subject: [PATCH] Fixing a RGB to BGR color issue with the Outline effect, and adding in unit tests for the Outline effect. --- src/effects/Outline.cpp | 8 +++--- src/effects/Outline.h | 14 +++++----- tests/CMakeLists.txt | 1 + tests/CVOutline.cpp | 62 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 11 deletions(-) create mode 100644 tests/CVOutline.cpp diff --git a/src/effects/Outline.cpp b/src/effects/Outline.cpp index 80999f89..307d3c4d 100644 --- a/src/effects/Outline.cpp +++ b/src/effects/Outline.cpp @@ -119,10 +119,10 @@ cv::Mat Outline::QImageToBGRACvMat(std::shared_ptr& qimage) { } std::shared_ptr Outline::BGRACvMatToQImage(cv::Mat img) { - cv::Mat final_img; - cv::cvtColor(img, final_img, cv::COLOR_RGBA2BGRA); - QImage qimage(final_img.data, final_img.cols, final_img.rows, final_img.step, QImage::Format_ARGB32); - std::shared_ptr imgIn = std::make_shared(qimage.convertToFormat(QImage::Format_RGBA8888_Premultiplied)); + // Directly wrap the cv::Mat data in a QImage + QImage qimage(img.data, img.cols, img.rows, img.step, QImage::Format_ARGB32); + std::shared_ptr imgIn = std::make_shared( + qimage.convertToFormat(QImage::Format_RGBA8888_Premultiplied)); return imgIn; } diff --git a/src/effects/Outline.h b/src/effects/Outline.h index 215d957a..1d5effcb 100644 --- a/src/effects/Outline.h +++ b/src/effects/Outline.h @@ -27,10 +27,10 @@ namespace openshot { /** - * @brief This class add the outline around image with transparent background and can be animated + * @brief This class add an outline around image with transparent background and can be animated * with openshot::Keyframe curves over time. * - * Since outline effect is pretty useful in many cases, this effect is added to libopenshot. + * Outlines can be added around any image or text, and animated over time. */ class Outline : public EffectBase { @@ -47,9 +47,9 @@ namespace openshot public: Keyframe width; ///< Width of the outline - Keyframe blue; ///< Blue of the outline - Keyframe green; ///< Green of the outline Keyframe red; ///< Red channel of the outline + Keyframe green; ///< Green of the outline + Keyframe blue; ///< Blue of the outline Keyframe alpha; ///< Alpha of the outline /// Blank constructor, useful when using Json to load the effect properties @@ -58,11 +58,11 @@ namespace openshot /// Default constructor, which require width, red, green, blue, alpha /// /// @param width the width of the outline (between 0 and 1000, rounded to int) - /// @param blue the blue channel of the outline (between 0 and 255, rounded to int) - /// @param green the green channel of the outline (between 0 and 255, rounded to int) /// @param red the red channel of the outline (between 0 and 255, rounded to int) + /// @param green the green channel of the outline (between 0 and 255, rounded to int) + /// @param blue the blue channel of the outline (between 0 and 255, rounded to int) /// @param alpha the alpha channel of the outline (between 0 and 255, rounded to int) - Outline(Keyframe width, Keyframe blue, Keyframe green, Keyframe red, Keyframe alpha); + Outline(Keyframe width, Keyframe red, Keyframe green, Keyframe blue, Keyframe alpha); /// @brief This method is required for all derived classes of ClipBase, and returns a /// new openshot::Frame object. All Clip keyframes and effects are resolved into diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 216dadd6..84b63266 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -58,6 +58,7 @@ if($CACHE{HAVE_OPENCV}) list(APPEND OPENSHOT_TESTS CVTracker CVStabilizer + CVOutline # CVObjectDetection ) endif() diff --git a/tests/CVOutline.cpp b/tests/CVOutline.cpp new file mode 100644 index 00000000..dd8d9262 --- /dev/null +++ b/tests/CVOutline.cpp @@ -0,0 +1,62 @@ +/** + * @file + * @brief Unit tests for OpenCV Outline effect + * @author Jonathan Thomas + * + * @ref License + */ + +// Copyright (c) 2008-2025 OpenShot Studios, LLC +// +// SPDX-License-Identifier: LGPL-3.0-or-later + +#include +#include +#include + +#include "openshot_catch.h" + +#include "Clip.h" +#include "effects/Outline.h" + +using namespace openshot; + +TEST_CASE( "Outline_Tests", "[libopenshot][opencv][outline]" ) +{ + // Create a video clip + std::stringstream path; + path << TEST_MEDIA_PATH << "1F0CF.svg"; + + // Open clip + openshot::Clip c(path.str()); + c.Open(); + auto f = c.GetFrame(1); + + // Create effect constructor (default values) + openshot::Outline e1{}; + + // Get frame from effect + auto f1 = e1.GetFrame(f, 1); + std::shared_ptr i1 = f1->GetImage(); + + // Check effect colors + QColor pix1 = i1->pixelColor(3, 32); + QColor compare1{0, 0, 0, 0}; + CHECK(pix1 == compare1); + + // Test another effect constructor + openshot::Outline e2(Keyframe(3.0), Keyframe(255.0), + Keyframe(0.0), Keyframe(0.0), Keyframe(128.0)); + + // Get frame from effect + auto f2 = e2.GetFrame(f, 1); + std::shared_ptr i2 = f2->GetImage(); + + // Check effect colors + QColor pix2 = i2->pixelColor(3, 32); + QColor compare2{0, 0, 255, 128}; + CHECK(pix2 == compare2); + + // Close clip + c.Close(); +}