2026-02-11 19:40:26 +08:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
2026-02-26 20:06:51 +08:00
|
|
|
|
#include <core/rect.h>
|
2026-02-25 06:23:53 +08:00
|
|
|
|
#include <core/types.h>
|
2026-02-26 20:06:51 +08:00
|
|
|
|
#include <core/vec2.h>
|
2026-02-11 19:40:26 +08:00
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
|
|
namespace extra2d {
|
|
|
|
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
|
|
// Alpha 遮罩 - 存储图片的非透明区域信息
|
|
|
|
|
|
// ============================================================================
|
|
|
|
|
|
class AlphaMask {
|
|
|
|
|
|
public:
|
|
|
|
|
|
AlphaMask() = default;
|
|
|
|
|
|
AlphaMask(int width, int height);
|
|
|
|
|
|
|
|
|
|
|
|
/// 从像素数据创建遮罩
|
|
|
|
|
|
static AlphaMask createFromPixels(const uint8_t *pixels, int width,
|
|
|
|
|
|
int height, int channels);
|
|
|
|
|
|
|
|
|
|
|
|
/// 获取指定位置的透明度(0-255)
|
|
|
|
|
|
uint8_t getAlpha(int x, int y) const;
|
|
|
|
|
|
|
|
|
|
|
|
/// 检查指定位置是否不透明
|
|
|
|
|
|
bool isOpaque(int x, int y, uint8_t threshold = 128) const;
|
|
|
|
|
|
|
|
|
|
|
|
/// 检查指定位置是否在遮罩范围内
|
|
|
|
|
|
bool isValid(int x, int y) const;
|
|
|
|
|
|
|
|
|
|
|
|
/// 获取遮罩尺寸
|
2026-02-26 00:59:16 +08:00
|
|
|
|
int width() const { return width_; }
|
|
|
|
|
|
int height() const { return height_; }
|
2026-02-26 00:55:13 +08:00
|
|
|
|
Size size() const {
|
2026-02-11 19:40:26 +08:00
|
|
|
|
return Size(static_cast<float>(width_), static_cast<float>(height_));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// 获取原始数据
|
|
|
|
|
|
const std::vector<uint8_t> &getData() const { return data_; }
|
|
|
|
|
|
|
|
|
|
|
|
/// 检查遮罩是否有效
|
|
|
|
|
|
bool isValid() const { return !data_.empty() && width_ > 0 && height_ > 0; }
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
int width_ = 0;
|
|
|
|
|
|
int height_ = 0;
|
|
|
|
|
|
std::vector<uint8_t> data_; // Alpha值数组
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace extra2d
|