summaryrefslogtreecommitdiff
path: root/source/luametatex/source/luaoptional/lmtimagemagick.c
blob: 9af5e6cd7afa6cbde40960f1d89ebaa4e340fb4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
    See license.txt in the root of this project.
*/

/* This one is real simple. */

# include "luametatex.h"
# include "lmtoptional.h"

typedef struct imlib_state_info {

    int initialized;
    int padding;

    int (*im_MagickCommandGenesis) (
        void  *image_info,
     // int   *command,
        void  *command,
        int    argc,
        const  char **argv,
        char **metadata,
        void  *exception
    );

    void * (*im_AcquireImageInfo) (
        void
    );

    void * (*im_AcquireExceptionInfo) (
        void
    );

    int (*im_ConvertImageCommand) (
        void        *image_info,
        int          argc,
        const char **argv,
        char       **metadata,
        void        *exception
    );

} imlib_state_info;

static imlib_state_info imlib_state = {

    .initialized             = 0,
    .padding                 = 0,

    .im_MagickCommandGenesis = NULL,
    .im_AcquireImageInfo     = NULL,
    .im_AcquireExceptionInfo = NULL,
    .im_ConvertImageCommand  = NULL,

};

static int imlib_initialize(lua_State * L) // todo: table
{
    if (! imlib_state.initialized) {
        const char *filename1 = lua_tostring(L,1);
        const char *filename2 = lua_tostring(L,2);
        if (filename1) {

            lmt_library lib = lmt_library_load(filename1);

            imlib_state.initialized = lmt_library_okay(lib);

            imlib_state.im_AcquireImageInfo     = lmt_library_find(lib, "AcquireImageInfo");
            imlib_state.im_AcquireExceptionInfo = lmt_library_find(lib, "AcquireExceptionInfo");

        }
        if (imlib_state.initialized && filename2) {

            lmt_library lib = lmt_library_load(filename2);

            imlib_state.im_MagickCommandGenesis = lmt_library_find(lib, "MagickCommandGenesis");
            imlib_state.im_ConvertImageCommand  = lmt_library_find(lib, "ConvertImageCommand");

            imlib_state.initialized = lmt_library_okay(lib);
        }
    }
    lua_pushboolean(L, imlib_state.initialized);
    return 1;
}

static int imlib_execute(lua_State * L)
{
    if (imlib_state.initialized) {
        if (lua_type(L, 1) == LUA_TTABLE) {
            const char *inpname = NULL;
            const char *outname = NULL;
            lua_getfield(L, -1, "inputfile" ); inpname = lua_tostring(L, -1); lua_pop(L, 1);
            lua_getfield(L, -1, "outputfile"); outname = lua_tostring(L, -1); lua_pop(L, 1);
            if (inpname && outname) {
                lua_Integer   nofarguments = 0;
                lua_Integer   nofoptions   = 0;
                const char  **arguments    = NULL;
                void         *info         = imlib_state.im_AcquireImageInfo();
                void         *exep         = imlib_state.im_AcquireExceptionInfo();
                if (lua_getfield(L, -1, "options" ) == LUA_TTABLE) {
                    nofoptions = luaL_len(L, -1);
                }
                arguments = lmt_memory_malloc((nofoptions + 4) * sizeof(char *));
                arguments[nofarguments++] = "convert";
                arguments[nofarguments++] = inpname;
                for (lua_Integer i = 1; i <= nofoptions; i++) {
                    switch (lua_rawgeti(L, -1, i)) {
                        case LUA_TSTRING:
                        case LUA_TNUMBER:
                             arguments[nofarguments++] = lua_tostring(L, -1);
                             break;
                        case LUA_TBOOLEAN:
                             arguments[nofarguments++] = lua_toboolean(L, -1) ? "true" : "false";
                             break;
                    }
                    lua_pop(L, 1);
                }
                arguments[nofarguments++] = outname;
                imlib_state.im_MagickCommandGenesis(info, imlib_state.im_ConvertImageCommand, (int) nofarguments, arguments, NULL, exep);
                lmt_memory_free((char *) arguments);
                lua_pop(L, 1);
                lua_pushboolean(L, 1);
                return 2;
            }
        } else {
            lua_pushboolean(L, 0);
            lua_pushliteral(L, "invalid specification");
            return 2;
        }
    }
    lua_pushboolean(L, 0);
    lua_pushliteral(L, "not initialized");
    return 2;
}

static struct luaL_Reg imlib_function_list[] = {
    { "initialize", imlib_initialize },
    { "execute",    imlib_execute    },
    { NULL,         NULL             },
};

int luaopen_imagemagick(lua_State * L)
{
    lmt_library_register(L, "imagemagick", imlib_function_list);
    return 0;
}