lmtimagemagick.c /size: 4539 b    last modification: 2024-01-16 10:22
1/*
2    See license.txt in the root of this project.
3*/
4
5/* This one is real simple. */
6
7# include "luametatex.h"
8# include "lmtoptional.h"
9
10typedef struct imlib_state_info {
11
12    int initialized;
13    int padding;
14
15    int (*im_MagickCommandGenesis) (
16        void  *image_info,
17     // int   *command,
18        void  *command,
19        int    argc,
20        const  char **argv,
21        char **metadata,
22        void  *exception
23    );
24
25    void * (*im_AcquireImageInfo) (
26        void
27    );
28
29    void * (*im_AcquireExceptionInfo) (
30        void
31    );
32
33    int (*im_ConvertImageCommand) (
34        void        *image_info,
35        int          argc,
36        const char **argv,
37        char       **metadata,
38        void        *exception
39    );
40
41} imlib_state_info;
42
43static imlib_state_info imlib_state = {
44
45    .initialized             = 0,
46    .padding                 = 0,
47
48    .im_MagickCommandGenesis = NULL,
49    .im_AcquireImageInfo     = NULL,
50    .im_AcquireExceptionInfo = NULL,
51    .im_ConvertImageCommand  = NULL,
52
53};
54
55static int imlib_initialize(lua_State * L) // todo: table
56{
57    if (! imlib_state.initialized) {
58        const char *filename1 = lua_tostring(L,1);
59        const char *filename2 = lua_tostring(L,2);
60        if (filename1) {
61
62            lmt_library lib = lmt_library_load(filename1);
63
64            imlib_state.initialized = lmt_library_okay(lib);
65
66            imlib_state.im_AcquireImageInfo     = lmt_library_find(lib, "AcquireImageInfo");
67            imlib_state.im_AcquireExceptionInfo = lmt_library_find(lib, "AcquireExceptionInfo");
68
69        }
70        if (imlib_state.initialized && filename2) {
71
72            lmt_library lib = lmt_library_load(filename2);
73
74            imlib_state.im_MagickCommandGenesis = lmt_library_find(lib, "MagickCommandGenesis");
75            imlib_state.im_ConvertImageCommand  = lmt_library_find(lib, "ConvertImageCommand");
76
77            imlib_state.initialized = lmt_library_okay(lib);
78        }
79    }
80    lua_pushboolean(L, imlib_state.initialized);
81    return 1;
82}
83
84static int imlib_execute(lua_State * L)
85{
86    if (imlib_state.initialized) {
87        if (lua_type(L, 1) == LUA_TTABLE) {
88            const char *inpname = NULL;
89            const char *outname = NULL;
90            lua_getfield(L, -1, "inputfile" ); inpname = lua_tostring(L, -1); lua_pop(L, 1);
91            lua_getfield(L, -1, "outputfile"); outname = lua_tostring(L, -1); lua_pop(L, 1);
92            if (inpname && outname) {
93                lua_Integer   nofarguments = 0;
94                lua_Integer   nofoptions   = 0;
95                const char  **arguments    = NULL;
96                void         *info         = imlib_state.im_AcquireImageInfo();
97                void         *exep         = imlib_state.im_AcquireExceptionInfo();
98                if (lua_getfield(L, -1, "options" ) == LUA_TTABLE) {
99                    nofoptions = luaL_len(L, -1);
100                }
101                arguments = lmt_memory_malloc((nofoptions + 4) * sizeof(char *));
102                arguments[nofarguments++] = "convert";
103                arguments[nofarguments++] = inpname;
104                for (lua_Integer i = 1; i <= nofoptions; i++) {
105                    switch (lua_rawgeti(L, -1, i)) {
106                        case LUA_TSTRING:
107                        case LUA_TNUMBER:
108                             arguments[nofarguments++] = lua_tostring(L, -1);
109                             break;
110                        case LUA_TBOOLEAN:
111                             arguments[nofarguments++] = lua_toboolean(L, -1) ? "true" : "false";
112                             break;
113                    }
114                    lua_pop(L, 1);
115                }
116                arguments[nofarguments++] = outname;
117                imlib_state.im_MagickCommandGenesis(info, imlib_state.im_ConvertImageCommand, (int) nofarguments, arguments, NULL, exep);
118                lmt_memory_free((char *) arguments);
119                lua_pop(L, 1);
120                lua_pushboolean(L, 1);
121                return 2;
122            }
123        } else {
124            lua_pushboolean(L, 0);
125            lua_pushliteral(L, "invalid specification");
126            return 2;
127        }
128    }
129    lua_pushboolean(L, 0);
130    lua_pushliteral(L, "not initialized");
131    return 2;
132}
133
134static struct luaL_Reg imlib_function_list[] = {
135    { "initialize", imlib_initialize },
136    { "execute",    imlib_execute    },
137    { NULL,         NULL             },
138};
139
140int luaopen_imagemagick(lua_State * L)
141{
142    lmt_library_register(L, "imagemagick", imlib_function_list);
143    return 0;
144}
145