summaryrefslogtreecommitdiff
path: root/src/image/image.c
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2022-08-23 13:38:27 -0500
committersanine <sanine.not@pm.me>2022-08-23 13:38:27 -0500
commit3afbf2a13b2dada445fb667bf25600407fea480a (patch)
tree551329e6f74fc9f177616de0d6739e8b5331ae96 /src/image/image.c
parent261e3f991221fbad6bbf262f5e65b773e4b6c73e (diff)
parent25ed7eb9f84e9a822f698ad803901fbb2a5354cf (diff)
:wMerge branch 'gl-window' into main
Diffstat (limited to 'src/image/image.c')
-rw-r--r--src/image/image.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/image/image.c b/src/image/image.c
new file mode 100644
index 0000000..cf08625
--- /dev/null
+++ b/src/image/image.c
@@ -0,0 +1,43 @@
+#include <lua.h>
+#include <honeysuckle.h>
+#define STB_IMAGE_IMPLEMENTATION
+#include "stb_image.h"
+#include "image.h"
+
+int load_image(lua_State *L)
+{
+ char *filename;
+ lua_Integer requested_channels;
+ hs_parse_args(L, hs_str(filename), hs_int(requested_channels));
+
+ int width, height, channels;
+ unsigned char *data = stbi_load(filename, &width, &height, &channels, requested_channels);
+
+ if (data == NULL) hs_throw_error(L, "failed to load image '%s'", filename);
+
+ lua_pushlightuserdata(L, data);
+ lua_pushinteger(L, width);
+ lua_pushinteger(L, height);
+ lua_pushinteger(L, channels);
+ return 4;
+}
+
+
+int free_image(lua_State *L)
+{
+ void *data;
+ hs_parse_args(L, hs_light(data));
+ stbi_image_free(data);
+ return 0;
+}
+
+
+void setup_image(lua_State *L, int honey_index)
+{
+ hs_create_table(L,
+ hs_str_cfunc("load", load_image),
+ hs_str_cfunc("destroy", free_image),
+ );
+
+ lua_setfield(L, honey_index, "image");
+}