Skip to content

Commit 017e830

Browse files
committed
feat: ImageBuffer resize
1 parent 28481dc commit 017e830

2 files changed

Lines changed: 27 additions & 5 deletions

File tree

include/MaaUtils/Buffer/BufferTypes.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ struct MaaImageBuffer
3737
virtual int32_t height() const = 0;
3838
virtual int32_t channels() const = 0;
3939
virtual int32_t type() const = 0;
40+
virtual bool resize(int32_t width, int32_t height) = 0;
4041

4142
virtual uint8_t* encoded() const = 0;
4243
virtual size_t encoded_size() const = 0;

include/MaaUtils/Buffer/ImageBuffer.hpp

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@
33
#include "BufferTypes.hpp"
44
#include "ListBuffer.hpp"
55
#include "MaaUtils/Conf.h"
6-
7-
MAA_SUPPRESS_CV_WARNINGS_BEGIN
8-
#include <opencv2/core/mat.hpp>
9-
#include <opencv2/imgcodecs.hpp>
10-
MAA_SUPPRESS_CV_WARNINGS_END
6+
#include "MaaUtils/NoWarningCV.hpp"
117

128
MAA_NS_BEGIN
139

@@ -65,6 +61,31 @@ class ImageBuffer : public MaaImageBuffer
6561

6662
virtual void set(ImageEncodedBuffer buffer) override { from_encoded(std::move(buffer)); }
6763

64+
virtual bool resize(int32_t width, int32_t height) override
65+
{
66+
if (empty()) {
67+
return false;
68+
}
69+
70+
if (!width && !height) {
71+
return false;
72+
}
73+
74+
if (!width) {
75+
double scale = static_cast<double>(height) / this->height();
76+
width = static_cast<int32_t>(std::round(this->width() * scale));
77+
}
78+
if (!height) {
79+
double scale = static_cast<double>(width) / this->width();
80+
height = static_cast<int32_t>(std::round(this->height() * scale));
81+
}
82+
83+
dirty_ = true;
84+
cv::resize(image_, image_, { width, height }, 0, 0, cv::INTER_AREA);
85+
86+
return true;
87+
}
88+
6889
private:
6990
void from_encoded(ImageEncodedBuffer buffer)
7091
{

0 commit comments

Comments
 (0)