Files
Frostbite2D/Fostbite2D/src/fostbite2D/render/texture.cpp
2026-02-17 13:28:38 +08:00

67 lines
1.5 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#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