Skip to content

Commit 143e994

Browse files
neko-paraMistEO
andauthored
feat: resize image utils (#1078)
Co-authored-by: MistEO <mistereo@hotmail.com>
1 parent 1b08cef commit 143e994

9 files changed

Lines changed: 94 additions & 0 deletions

File tree

docs/en_us/2.2-IntegratedInterfaceOverview.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Different language bindings may have different naming conventions and are wrappe
1313
| MaaControllerPostClick(ctrl_handle, x, y) | controller.post_click(x, y) | ctrl.post_click(x, y) |
1414
| MaaTaskerInited(tasker_handle) | tasker.inited | tasker.inited |
1515
| MaaTaskerPostTask(tasker_handle, entry, override) | tasker.post_task(entry, override) | tasker.post_task(entry, override) |
16+
| MaaImageBufferResize(image_handle, width, height) | ImageBuffer.resize(width, height) | Global.resize_image(image, width, height) |
1617

1718
## MaaUtility.h
1819

@@ -51,6 +52,16 @@ Set global options. Will be split into specific options in bindings.
5152

5253
Load plugin. Can use full path or name only. When using name only, will search in system directory and current directory. Can also recursively search for plugins in a directory.
5354

55+
## MaaBuffer.h
56+
57+
### MaaImageBufferResize
58+
59+
- `handle`: Image buffer
60+
- `width`: Target width, pass 0 to scale by height
61+
- `height`: Target height, pass 0 to scale by width
62+
63+
Resize the image data in `handle` with aspect ratio preserved. `width` and `height` cannot both be 0.
64+
5465
## MaaResource.h
5566

5667
### MaaResourceCreate

docs/zh_cn/2.2-集成接口一览.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
| MaaControllerPostClick(ctrl_handle, x, y) | controller.post_click(x, y) | ctrl.post_click(x, y) |
1414
| MaaTaskerInited(tasker_handle) | tasker.inited | tasker.inited |
1515
| MaaTaskerPostTask(tasker_handle, entry, override) | tasker.post_task(entry, override) | tasker.post_task(entry, override) |
16+
| MaaImageBufferResize(image_handle, width, height) | ImageBuffer.resize(width, height) | Global.resize_image(image, width, height) |
1617

1718
## MaaUtility.h
1819

@@ -51,6 +52,16 @@
5152

5253
加载插件。可以使用完整路径或仅使用名称,仅使用名称时会在系统目录和当前目录中搜索。也可以递归搜索目录中的插件
5354

55+
## MaaBuffer.h
56+
57+
### MaaImageBufferResize
58+
59+
- `handle`: 图像缓冲区
60+
- `width`: 目标宽度,传 0 表示按高度等比缩放
61+
- `height`: 目标高度,传 0 表示按宽度等比缩放
62+
63+
按比例缩放 `handle` 中的图像数据,修改原缓冲区。`width``height` 不能同时为 0。
64+
5465
## MaaResource.h
5566

5667
### MaaResourceCreate

include/MaaFramework/Utility/MaaBuffer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ extern "C"
5454
MAA_FRAMEWORK_API int32_t MaaImageBufferType(const MaaImageBuffer* handle);
5555
MAA_FRAMEWORK_API MaaBool
5656
MaaImageBufferSetRawData(MaaImageBuffer* handle, MaaImageRawData data, int32_t width, int32_t height, int32_t type);
57+
MAA_FRAMEWORK_API MaaBool MaaImageBufferResize(MaaImageBuffer* handle, int32_t width, int32_t height);
5758

5859
typedef uint8_t* MaaImageEncodedData;
5960
MAA_FRAMEWORK_API MaaImageEncodedData MaaImageBufferGetEncoded(const MaaImageBuffer* handle);

source/Common/MaaBuffer.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "MaaUtils/Buffer/ListBuffer.hpp"
55
#include "MaaUtils/Buffer/StringBuffer.hpp"
66
#include "MaaUtils/Logger.h"
7+
#include "MaaUtils/NoWarningCV.hpp"
78

89
MaaStringBuffer* MaaStringBufferCreate()
910
{
@@ -272,6 +273,16 @@ MaaBool MaaImageBufferSetRawData(MaaImageBuffer* handle, MaaImageRawData data, i
272273
return true;
273274
}
274275

276+
MaaBool MaaImageBufferResize(MaaImageBuffer* handle, int32_t width, int32_t height)
277+
{
278+
if (!handle) {
279+
LogError << "handle is null";
280+
return 0;
281+
}
282+
283+
return handle->resize(width, height);
284+
}
285+
275286
MaaImageEncodedData MaaImageBufferGetEncoded(const MaaImageBuffer* handle)
276287
{
277288
if (!handle) {

source/binding/NodeJS/src/apis/global.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <MaaToolkit/MaaToolkitAPI.h>
55

66
#include "../foundation/spec.h"
7+
#include "buffer.h"
78

89
std::string version_from_macro()
910
{
@@ -100,6 +101,16 @@ void config_init_option(std::string user_path, maajs::OptionalParam<std::string>
100101
}
101102
}
102103

104+
maajs::ArrayBufferType resize_image(maajs::ArrayBufferType src, int32_t width, int32_t height)
105+
{
106+
ImageBuffer buf;
107+
buf.set(src);
108+
if (!MaaImageBufferResize(buf, width, height)) {
109+
throw maajs::MaaError { "Global resize_image failed" };
110+
}
111+
return buf.data(src.Env());
112+
}
113+
103114
maajs::ObjectType load_global(maajs::EnvType env)
104115
{
105116
auto globalObject = maajs::ObjectType::New(env);
@@ -114,6 +125,7 @@ maajs::ObjectType load_global(maajs::EnvType env)
114125
MAA_BIND_SETTER(globalObject, "draw_quality", set_draw_quality);
115126
MAA_BIND_SETTER(globalObject, "reco_image_cache_limit", set_reco_image_cache_limit);
116127
MAA_BIND_FUNC(globalObject, "config_init_option", config_init_option);
128+
MAA_BIND_FUNC(globalObject, "resize_image", resize_image);
117129

118130
return globalObject;
119131
}

source/binding/NodeJS/src/apis/global.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ declare global {
1313
set draw_quality(value: number)
1414
set reco_image_cache_limit(value: number)
1515
config_init_option(user_path: string, default_json?: string): void
16+
17+
resize_image(image: ArrayBuffer, width: number, height: number): ArrayBuffer
1618
}
1719
}
1820
}

source/binding/Python/maa/buffer.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,20 @@ def set(self, value: numpy.ndarray) -> bool:
334334
)
335335
)
336336

337+
def resize(self, width: int = 0, height: int = 0) -> bool:
338+
"""调整图像尺寸 / Resize image
339+
340+
Args:
341+
width: 目标宽度,0 表示按高度等比缩放 / Target width, 0 to scale by height
342+
height: 目标高度,0 表示按宽度等比缩放 / Target height, 0 to scale by width
343+
344+
Returns:
345+
bool: 是否成功 / Whether successful
346+
"""
347+
return bool(
348+
Library.framework().MaaImageBufferResize(self._handle, width, height)
349+
)
350+
337351
@property
338352
def empty(self) -> bool:
339353
"""判断缓冲区是否为空 / Check if buffer is empty
@@ -389,6 +403,13 @@ def _set_api_properties():
389403
ctypes.c_int32,
390404
]
391405

406+
Library.framework().MaaImageBufferResize.restype = MaaBool
407+
Library.framework().MaaImageBufferResize.argtypes = [
408+
MaaImageBufferHandle,
409+
ctypes.c_int32,
410+
ctypes.c_int32,
411+
]
412+
392413
Library.framework().MaaImageBufferIsEmpty.restype = MaaBool
393414
Library.framework().MaaImageBufferIsEmpty.argtypes = [MaaImageBufferHandle]
394415

source/modules/MaaFramework.cppm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ export using ::MaaImageBufferHeight;
268268
export using ::MaaImageBufferChannels;
269269
export using ::MaaImageBufferType;
270270
export using ::MaaImageBufferSetRawData;
271+
export using ::MaaImageBufferResize;
271272
export using ::MaaImageBufferGetEncoded;
272273
export using ::MaaImageBufferGetEncodedSize;
273274
export using ::MaaImageBufferSetEncoded;

test/python/binding_test.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
from maa.toolkit import Toolkit
4646
from maa.custom_action import CustomAction
4747
from maa.custom_recognition import CustomRecognition
48+
from maa.buffer import ImageBuffer
4849
from maa.define import MaaDbgControllerTypeEnum, LoggingLevelEnum
4950
from maa.context import Context, ContextEventSink
5051
from maa.event_sink import EventSink
@@ -408,6 +409,28 @@ def test_controller_api():
408409
return dbg_controller
409410

410411

412+
# ============================================================================
413+
# Buffer API 测试
414+
# ============================================================================
415+
416+
417+
def test_buffer_api():
418+
print("\n=== test_buffer_api ===")
419+
420+
buf = ImageBuffer()
421+
src = numpy.zeros((100, 200, 3), dtype=numpy.uint8)
422+
assert buf.set(src), "set should succeed"
423+
424+
# 仅指定宽度,按比例缩放高度
425+
assert buf.resize(50, 0), "resize should succeed"
426+
resized = buf.get()
427+
print(f" resized shape: {resized.shape}")
428+
assert resized.shape[1] == 50, "width should be 50"
429+
assert resized.shape[0] == 25, "height should keep aspect ratio"
430+
431+
print(" PASS: buffer API")
432+
433+
411434
# ============================================================================
412435
# Tasker API 测试
413436
# ============================================================================
@@ -688,6 +711,7 @@ def test_toolkit():
688711
# 测试各模块 API
689712
resource = test_resource_api()
690713
controller = test_controller_api()
714+
test_buffer_api()
691715
tasker = test_tasker_api(resource, controller)
692716

693717
# 验证自定义识别和动作被调用

0 commit comments

Comments
 (0)