67 lines
1.5 KiB
C++
67 lines
1.5 KiB
C++
#include <fostbite2D/render/texture.h>
|
||
|
||
namespace frostbite2D {
|
||
|
||
/**
|
||
* @brief 从像素数据创建Alpha遮罩
|
||
* @param pixels 像素数据指针
|
||
* @param width 宽度
|
||
* @param height 高度
|
||
* @param channels 通道数
|
||
* @return 创建的AlphaMask
|
||
*/
|
||
AlphaMask AlphaMask::createFromPixels(const uint8_t *pixels, int width,
|
||
int height, int channels) {
|
||
AlphaMask mask;
|
||
mask.width_ = width;
|
||
mask.height_ = height;
|
||
mask.data_.resize(width * height);
|
||
|
||
for (int y = 0; y < height; ++y) {
|
||
for (int x = 0; x < width; ++x) {
|
||
int pixelIndex = (y * width + x) * channels;
|
||
uint8_t alpha;
|
||
|
||
if (channels == 4) {
|
||
// RGBA 格式
|
||
alpha = pixels[pixelIndex + 3];
|
||
} else if (channels == 1) {
|
||
// 灰度格式
|
||
alpha = pixels[pixelIndex];
|
||
} else {
|
||
// RGB 或其他格式,假设不透明
|
||
alpha = 255;
|
||
}
|
||
|
||
mask.data_[y * width + x] = alpha;
|
||
}
|
||
}
|
||
|
||
return mask;
|
||
}
|
||
|
||
/**
|
||
* @brief 获取指定位置的透明度
|
||
* @param x X坐标
|
||
* @param y Y坐标
|
||
* @return 透明度值(0-255)
|
||
*/
|
||
uint8_t AlphaMask::getAlpha(int x, int y) const {
|
||
if (x < 0 || x >= width_ || y < 0 || y >= height_) {
|
||
return 0;
|
||
}
|
||
return data_[y * width_ + x];
|
||
}
|
||
|
||
/**
|
||
* @brief 检查指定位置是否不透明
|
||
* @param x X坐标
|
||
* @param y Y坐标
|
||
* @return 不透明返回true
|
||
*/
|
||
bool AlphaMask::isOpaque(int x, int y) const {
|
||
return getAlpha(x, y) > 128; // 使用128作为阈值
|
||
}
|
||
|
||
} // namespace frostbite2D
|