From b7edf714991561ac8f7182b8c737e62f9bc0bace Mon Sep 17 00:00:00 2001
From: Isaac0-dev <62234577+Isaac0-dev@users.noreply.github.com>
Date: Mon, 9 Jun 2025 14:39:07 +1000
Subject: [PATCH] replace get_texture_average_color with texture_to_lua_table
---
autogen/lua_definitions/functions.lua | 6 ++--
docs/lua/functions-6.md | 11 +++---
docs/lua/functions.md | 2 +-
src/pc/lua/smlua_functions_autogen.c | 18 ++++------
src/pc/lua/utils/smlua_misc_utils.c | 52 ++++++++++++++++++---------
src/pc/lua/utils/smlua_misc_utils.h | 4 +--
6 files changed, 51 insertions(+), 42 deletions(-)
diff --git a/autogen/lua_definitions/functions.lua b/autogen/lua_definitions/functions.lua
index 4023bf88b..f3a0bd860 100644
--- a/autogen/lua_definitions/functions.lua
+++ b/autogen/lua_definitions/functions.lua
@@ -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
diff --git a/docs/lua/functions-6.md b/docs/lua/functions-6.md
index 3eb2397b1..8c70d954b 100644
--- a/docs/lua/functions-6.md
+++ b/docs/lua/functions-6.md
@@ -5105,25 +5105,24 @@ Gets the current GraphNodeHeldObject
-## [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:](#)
diff --git a/docs/lua/functions.md b/docs/lua/functions.md
index 8a6b829dd..10d832f15 100644
--- a/docs/lua/functions.md
+++ b/docs/lua/functions.md
@@ -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)
diff --git a/src/pc/lua/smlua_functions_autogen.c b/src/pc/lua/smlua_functions_autogen.c
index 1f7fce0ed..2bf3ba87c 100644
--- a/src/pc/lua/smlua_functions_autogen.c
+++ b/src/pc/lua/smlua_functions_autogen.c
@@ -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);
diff --git a/src/pc/lua/utils/smlua_misc_utils.c b/src/pc/lua/utils/smlua_misc_utils.c
index d9b11800e..5fe69642c 100644
--- a/src/pc/lua/utils/smlua_misc_utils.c
+++ b/src/pc/lua/utils/smlua_misc_utils.c
@@ -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;
}
diff --git a/src/pc/lua/utils/smlua_misc_utils.h b/src/pc/lua/utils/smlua_misc_utils.h
index 4224ba73d..401467a6e 100644
--- a/src/pc/lua/utils/smlua_misc_utils.h
+++ b/src/pc/lua/utils/smlua_misc_utils.h
@@ -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