Skip to content

Instantly share code, notes, and snippets.

@clofresh
Created August 29, 2018 22:59
Show Gist options
  • Save clofresh/7f51970ed8ead429ae0dd5a4b60cfba3 to your computer and use it in GitHub Desktop.
Save clofresh/7f51970ed8ead429ae0dd5a4b60cfba3 to your computer and use it in GitHub Desktop.
package main
// #include "lua.h"
// #include "lualib.h"
// #include "lauxlib.h"
// static int imgsize(lua_State *L)
// {
// char *path = luaL_checkstring(L, 1);
// char *err;
// GoUint w, h;
// err = ImgutilGetImageSize(path, &w, &h);
// if (err != NULL)
// {
// lua_pushstring(L, err); /* error message */
// free(err);
// return 1;
// }
// //push the results
// lua_pushnumber(L, w);
// lua_pushnumber(L, h);
// //return number of results
// return 2;
// }
// //library to be registered
// static const struct luaL_Reg mylib[] = {
// {"imgsize", imgsize},
// {NULL, NULL} /* sentinel */
// };
// //name of this function is not flexible
// int luaopen_libimgutil(lua_State *L)
// {
// luaL_newlib(L, mylib);
// return 1;
// }
import "C"
import (
"image"
_ "image/jpeg"
_ "image/png"
"os"
)
//export ImgutilGetImageSize
func ImgutilGetImageSize(path *C.char, w *uint, h *uint) *C.char {
file, err := os.Open(C.GoString(path))
if err != nil {
return C.CString(err.Error())
}
defer file.Close()
img, _, err := image.Decode(file)
if err != nil {
return C.CString(err.Error())
}
rect := img.Bounds()
*w = uint(rect.Dx())
*h = uint(rect.Dy())
return nil
}
func main() {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment