add canvas:mul and canvas:add methods

This commit is contained in:
MihailRis 2025-11-11 19:30:27 +03:00
parent b9777cc682
commit 83f7bf80c4
5 changed files with 131 additions and 0 deletions

View File

@ -197,6 +197,8 @@ Here, *color* can be specified in the following ways:
| data:set_data(data: table<int>) | replaces pixel data (width * height * 4 numbers) |
| data:create_texture(name: str) | creates and shares texture to renderer |
| data:unbind_texture() | unbinds the texture from the canvas |
| data:mul(*color* or Canvas) | multiplies a color by the specified color or canvas |
| data:add(*color* or Canvas) | adds a color or another canvas to a color |
## Inline frame (iframe)

View File

@ -197,6 +197,8 @@ document["worlds-panel"]:clear()
| data:set_data(data: table<int>) | заменяет данные пикселей (ширина * высота * 4 чисел) |
| data:create_texture(name: str) | создаёт и делится текстурой с рендерером |
| data:unbind_texture() | отвязывает текстуру от холста |
| data:mul(*цвет* или Canvas) | умножает увет на указанный цвет или холст |
| data:add(*цвет* или Canvas) | прибавляет цвет или другой холст к цвету |
## Рамка встраивания (iframe)

View File

@ -420,6 +420,99 @@ void ImageData::fixAlphaColor() {
}
}
static void check_matching(const ImageData& a, const ImageData& b) {
if (b.getWidth() != a.getWidth() ||
b.getHeight() != a.getHeight() ||
b.getFormat() != a.getFormat()) {
throw std::runtime_error("image sizes or formats do not match");
}
}
void ImageData::mulColor(const glm::ivec4& color) {
uint comps;
switch (format) {
case ImageFormat::rgb888: comps = 3; break;
case ImageFormat::rgba8888: comps = 4; break;
default:
throw std::runtime_error("only unsigned byte formats supported");
}
for (uint y = 0; y < height; y++) {
for (uint x = 0; x < width; x++) {
uint idx = (y * width + x) * comps;
for (uint c = 0; c < comps; c++) {
float val = static_cast<float>(data[idx + c]) * color[c] / 255.0f;
data[idx + c] =
static_cast<ubyte>(std::min(std::max(val, 0.0f), 255.0f));
}
}
}
}
void ImageData::addColor(const ImageData& other) {
check_matching(*this, other);
uint comps;
switch (format) {
case ImageFormat::rgb888: comps = 3; break;
case ImageFormat::rgba8888: comps = 4; break;
default:
throw std::runtime_error("only unsigned byte formats supported");
}
for (uint y = 0; y < height; y++) {
for (uint x = 0; x < width; x++) {
uint idx = (y * width + x) * comps;
for (uint c = 0; c < comps; c++) {
int val = data[idx + c] + other.data[idx + c];
data[idx + c] =
static_cast<ubyte>(std::min(std::max(val, 0), 255));
}
}
}
}
void ImageData::addColor(const glm::ivec4& color) {
uint comps;
switch (format) {
case ImageFormat::rgb888: comps = 3; break;
case ImageFormat::rgba8888: comps = 4; break;
default:
throw std::runtime_error("only unsigned byte formats supported");
}
for (uint y = 0; y < height; y++) {
for (uint x = 0; x < width; x++) {
uint idx = (y * width + x) * comps;
for (uint c = 0; c < comps; c++) {
int val = data[idx + c] + color[c];
data[idx + c] =
static_cast<ubyte>(std::min(std::max(val, 0), 255));
}
}
}
}
void ImageData::mulColor(const ImageData& other) {
check_matching(*this, other);
uint comps;
switch (format) {
case ImageFormat::rgb888: comps = 3; break;
case ImageFormat::rgba8888: comps = 4; break;
default:
throw std::runtime_error("only unsigned byte formats supported");
}
for (uint y = 0; y < height; y++) {
for (uint x = 0; x < width; x++) {
uint idx = (y * width + x) * comps;
for (uint c = 0; c < comps; c++) {
float val = static_cast<float>(data[idx + c]) *
static_cast<float>(other.data[idx + c]) / 255.0f;
data[idx + c] =
static_cast<ubyte>(std::min(std::max(val, 0.0f), 255.0f));
}
}
}
}
std::unique_ptr<ImageData> add_atlas_margins(ImageData* image, int grid_size) {
// RGBA is only supported
assert(image->getFormat() == ImageFormat::rgba8888);

View File

@ -32,6 +32,10 @@ public:
void blit(const ImageData& image, int x, int y);
void extrude(int x, int y, int w, int h);
void fixAlphaColor();
void mulColor(const glm::ivec4& color);
void mulColor(const ImageData& other);
void addColor(const glm::ivec4& color);
void addColor(const ImageData& other);
std::unique_ptr<ImageData> cropped(int x, int y, int width, int height) const;

View File

@ -240,6 +240,34 @@ static int l_create_texture(State* L) {
return 0;
}
static int l_mul(State* L) {
auto canvas = touserdata<LuaCanvas>(L, 1);
if (canvas == nullptr) {
return 0;
}
if (lua::isnumber(L, 2)) {
RGBA rgba = get_rgba(L, 2);
canvas->getData().mulColor(glm::ivec4 {rgba.r, rgba.g, rgba.b, rgba.a});
} else if (auto other = touserdata<LuaCanvas>(L, 2)) {
canvas->getData().mulColor(other->getData());
}
return 0;
}
static int l_add(State* L) {
auto canvas = touserdata<LuaCanvas>(L, 1);
if (canvas == nullptr) {
return 0;
}
if (lua::istable(L, 2)) {
RGBA rgba = get_rgba(L, 2);
canvas->getData().addColor(glm::ivec4 {rgba.r, rgba.g, rgba.b, rgba.a});
} else if (auto other = touserdata<LuaCanvas>(L, 2)) {
canvas->getData().addColor(other->getData());
}
return 0;
}
static std::unordered_map<std::string, lua_CFunction> methods {
{"at", lua::wrap<l_at>},
{"set", lua::wrap<l_set>},
@ -249,6 +277,8 @@ static std::unordered_map<std::string, lua_CFunction> methods {
{"update", lua::wrap<l_update>},
{"create_texture", lua::wrap<l_create_texture>},
{"unbind_texture", lua::wrap<l_unbind_texture>},
{"mul", lua::wrap<l_mul>},
{"add", lua::wrap<l_add>},
{"_set_data", lua::wrap<l_set_data>},
};