replace get_texture_average_color with texture_to_lua_table

This commit is contained in:
Isaac0-dev 2025-06-09 14:39:07 +10:00
parent 17c311ae7d
commit b7edf71499
6 changed files with 51 additions and 42 deletions

View file

@ -11099,10 +11099,8 @@ function geo_get_current_held_object()
end
--- @param tex Pointer_integer
--- @param out Color
--- @return boolean
--- Calculates the average color of a given texture. Returns true if success
function get_texture_average_color(tex, out)
--- Converts a texture's pixels to a Lua table. Returns nil if failed. Otherwise, returns a table as a pure memory buffer. Supports rgba16 and rgba32 textures.
function texture_to_lua_table(tex)
-- ...
end

View file

@ -5105,25 +5105,24 @@ Gets the current GraphNodeHeldObject
<br />
## [get_texture_average_color](#get_texture_average_color)
## [texture_to_lua_table](#texture_to_lua_table)
### Description
Calculates the average color of a given texture. Returns true if success
Converts a texture's pixels to a Lua table. Returns nil if failed. Otherwise, returns a table as a pure memory buffer. Supports rgba16 and rgba32 textures.
### Lua Example
`local booleanValue = get_texture_average_color(tex, out)`
`texture_to_lua_table(tex)`
### Parameters
| Field | Type |
| ----- | ---- |
| tex | `Pointer` <`integer`> |
| out | [Color](structs.md#Color) |
### Returns
- `boolean`
- None
### C Prototype
`bool get_texture_average_color(const u8 *tex, OUT Color out);`
`void texture_to_lua_table(const u8 *tex);`
[:arrow_up_small:](#)

View file

@ -1990,7 +1990,7 @@
- [geo_get_current_perspective](functions-6.md#geo_get_current_perspective)
- [geo_get_current_camera](functions-6.md#geo_get_current_camera)
- [geo_get_current_held_object](functions-6.md#geo_get_current_held_object)
- [get_texture_average_color](functions-6.md#get_texture_average_color)
- [texture_to_lua_table](functions-6.md#texture_to_lua_table)
<br />

View file

@ -33279,25 +33279,19 @@ int smlua_func_geo_get_current_held_object(UNUSED lua_State* L) {
return 1;
}
int smlua_func_get_texture_average_color(lua_State* L) {
int smlua_func_texture_to_lua_table(lua_State* L) {
if (L == NULL) { return 0; }
int top = lua_gettop(L);
if (top != 2) {
LOG_LUA_LINE("Improper param count for '%s': Expected %u, Received %u", "get_texture_average_color", 2, top);
if (top != 1) {
LOG_LUA_LINE("Improper param count for '%s': Expected %u, Received %u", "texture_to_lua_table", 1, top);
return 0;
}
u8 * tex = (u8 *)smlua_to_cpointer(L, 1, LVT_U8_P);
if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter %u for function '%s'", 1, "get_texture_average_color"); return 0; }
if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter %u for function '%s'", 1, "texture_to_lua_table"); return 0; }
Color out;
smlua_get_color(out, 2);
if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter %u for function '%s'", 2, "get_texture_average_color"); return 0; }
lua_pushboolean(L, get_texture_average_color(tex, out));
smlua_push_color(out, 2);
texture_to_lua_table(tex);
return 1;
}
@ -37184,7 +37178,7 @@ void smlua_bind_functions_autogen(void) {
smlua_bind_function(L, "geo_get_current_perspective", smlua_func_geo_get_current_perspective);
smlua_bind_function(L, "geo_get_current_camera", smlua_func_geo_get_current_camera);
smlua_bind_function(L, "geo_get_current_held_object", smlua_func_geo_get_current_held_object);
smlua_bind_function(L, "get_texture_average_color", smlua_func_get_texture_average_color);
smlua_bind_function(L, "texture_to_lua_table", smlua_func_texture_to_lua_table);
// smlua_model_utils.h
smlua_bind_function(L, "smlua_model_util_get_id", smlua_func_smlua_model_util_get_id);

View file

@ -586,24 +586,42 @@ struct GraphNodeHeldObject* geo_get_current_held_object(void) {
return gCurGraphNodeHeldObject;
}
bool get_texture_average_color(const u8 *tex, OUT Color out) {
void texture_to_lua_table(const u8 *tex) {
lua_State *L = gLuaState;
if (!L || !tex) { return; }
struct TextureInfo texInfo;
if (!tex) { return false; }
if (!dynos_texture_get_from_data(tex, &texInfo)) { return false; }
u32 w = texInfo.width;
u32 h = texInfo.height;
u32 texSize = w * h;
if (!dynos_texture_get_from_data(tex, &texInfo)) { return; }
u32 bpp = texInfo.bitSize;
if (bpp != 16 && bpp != 32) { return; }
u32 bytesPerPixel = bpp / 8;
const u8 *data = texInfo.texture;
u32 r = 0;
u32 g = 0;
u32 b = 0;
for (u32 i = 0; i < texSize; i++) {
r += data[4 * i + 0];
g += data[4 * i + 1];
b += data[4 * i + 2];
u32 texSize = texInfo.width * texInfo.height * bytesPerPixel;
lua_newtable(L);
for (u32 i = 0; i < texSize; i += bytesPerPixel) {
lua_newtable(L);
if (bpp == 16) {
u16 col = (data[i] << 8) | data[i + 1];
u8 r = SCALE_5_8((col >> 11) & 0x1F);
u8 g = SCALE_5_8((col >> 6) & 0x1F);
u8 b = SCALE_5_8((col >> 1) & 0x1F);
u8 a = 0xFF * (col & 0x1);
smlua_push_integer_field(-2, "r", r);
smlua_push_integer_field(-2, "g", g);
smlua_push_integer_field(-2, "b", b);
smlua_push_integer_field(-2, "a", a);
} else if (bpp == 32) {
smlua_push_integer_field(-2, "r", data[i]);
smlua_push_integer_field(-2, "g", data[i + 1]);
smlua_push_integer_field(-2, "b", data[i + 2]);
smlua_push_integer_field(-2, "a", data[i + 3]);
}
lua_rawseti(L, -2, i / bytesPerPixel + 1);
}
out[0] = r / texSize;
out[1] = g / texSize;
out[2] = b / texSize;
return true;
}

View file

@ -231,7 +231,7 @@ struct GraphNodeCamera* geo_get_current_camera(void);
/* |description|Gets the current GraphNodeHeldObject|descriptionEnd|*/
struct GraphNodeHeldObject* geo_get_current_held_object(void);
/* |description|Calculates the average color of a given texture. Returns true if success|descriptionEnd|*/
bool get_texture_average_color(const u8 *tex, OUT Color out);
/* |description|Converts a texture's pixels to a Lua table. Returns nil if failed. Otherwise, returns a table as a pure memory buffer. Supports rgba16 and rgba32 textures.|descriptionEnd|*/
void texture_to_lua_table(const u8 *tex);
#endif