Esempio n. 1
0
static void copy_image(lua_State * L, lua_Number scale)
{
    image *a, **aa, *b, **bb;
    if (lua_gettop(L) != 1)
        luaL_error(L, "img.copy() needs exactly 1 argument");
    aa = (image **) luaL_checkudata(L, 1, TYPE_IMG);    /* a */
    lua_pop(L, 1);              /* - */
    a = *aa;
    bb = (image **) lua_newuserdata(L, sizeof(image *));        /* b */
    luaL_getmetatable(L, TYPE_IMG);     /* m b */
    lua_setmetatable(L, -2);    /* b */
    b = *bb = new_image();
    if (!is_wd_running(a))
        img_width(b) = do_zround(img_width(a) * scale);
    if (!is_ht_running(a))
        img_height(b) = do_zround(img_height(a) * scale);
    if (!is_dp_running(a))
        img_depth(b) = do_zround(img_depth(a) * scale);
    img_transform(b) = img_transform(a);
    img_dict(b) = img_dict(a);
    if (img_dictref(a) != LUA_NOREF) {
        lua_rawgeti(L, LUA_GLOBALSINDEX, img_dictref(a));       /* ad b */
        img_dictref(b) = luaL_ref(L, LUA_GLOBALSINDEX); /* b */
    } else
        assert(img_state(img_dict(a)) >= DICT_REFERED);
}
Esempio n. 2
0
static void copy_image(lua_State * L, lua_Number scale)
{
    image *a, **aa, *b, **bb;
    image_dict *d;
    if (lua_gettop(L) == 0)
        luaL_error(L, "img.copy needs an image as argument");
    aa = (image **) luaL_checkudata(L, 1, TYPE_IMG);       /* a */
    lua_pop(L, 1);                                         /* - */
    a = *aa;
    bb = (image **) lua_newuserdata(L, sizeof(image *));   /* b */
    luaL_getmetatable(L, TYPE_IMG);                        /* m b */
    lua_setmetatable(L, -2);                               /* b */
    b = *bb = new_image();
    if (!is_wd_running(a))
        img_width(b) = do_zround(img_width(a) * scale);
    if (!is_ht_running(a))
        img_height(b) = do_zround(img_height(a) * scale);
    if (!is_dp_running(a))
        img_depth(b) = do_zround(img_depth(a) * scale);
    img_transform(b) = img_transform(a);
    img_dict(b) = img_dict(a);
    if (img_dictref(a) != LUA_NOREF) {
        lua_rawgeti(L, LUA_REGISTRYINDEX, img_dictref(a)); /* ad b */
        img_dictref(b) = luaL_ref(L, LUA_REGISTRYINDEX);   /* b */
        d = img_dict(*aa);
        img_luaref(d) += 1;
    } else if (img_state(img_dict(a)) < DICT_REFERED) {
        luaL_error(L, "img.copy needs an proper image as argument");
    }
}
Esempio n. 3
0
int l_new_image(lua_State * L)
{
    image *a, **aa;
    image_dict **add;
    if (lua_gettop(L) > 1)
        luaL_error(L, "img.new() needs maximum 1 argument");
    if (lua_gettop(L) == 1 && !lua_istable(L, -1))
        luaL_error(L, "img.new() needs table as optional argument");    /* (t) */
    aa = (image **) lua_newuserdata(L, sizeof(image *));        /* i (t) */
    luaL_getmetatable(L, TYPE_IMG);     /* m i (t) */
    lua_setmetatable(L, -2);    /* i (t) */
    a = *aa = new_image();
    add = (image_dict **) lua_newuserdata(L, sizeof(image_dict *));     /* ad i (t) */
    luaL_getmetatable(L, TYPE_IMG_DICT);        /* m ad i (t) */
    lua_setmetatable(L, -2);    /* ad i (t) */
    img_dict(a) = *add = new_image_dict();
    img_dictref(a) = luaL_ref(L, LUA_GLOBALSINDEX);     /* i (t) */
    if (lua_gettop(L) == 2) {   /* i t, else just i */
        lua_insert(L, -2);      /* t i */
        lua_pushnil(L);         /* n t i (1st key for iterator) */
        while (lua_next(L, -2) != 0) {  /* v k t i */
            lua_to_image(L, a); /* v k t i */
            lua_pop(L, 1);      /* k t i */
        }                       /* t i */
        lua_pop(L, 1);          /* i */
    }                           /* i */
    return 1;                   /* i */
}
Esempio n. 4
0
image *new_image(void)
{
    image *p = xtalloc(1, image);
    set_wd_running(p);
    set_ht_running(p);
    set_dp_running(p);
    img_transform(p) = 0;
    img_dict(p) = NULL;
    img_dictref(p) = LUA_NOREF;
    return p;
}
Esempio n. 5
0
static int m_img_print(lua_State * L)
{
    image **aa;
    image_dict *d;
    aa = (image **) luaL_checkudata(L, 1, TYPE_IMG);
    d = img_dict(*aa);
    /* formatted a bit like a node */
    if (img_filename(d) == NULL) {
        if (img_pagename(d) != NULL && strlen(img_pagename(d)) != 0) {
            lua_pushfstring(L, "<img unset : %d >", img_dictref(*aa));
        } else {
            lua_pushfstring(L, "<img unset : %d >", img_dictref(*aa));
        }
    } else {
        if (img_pagename(d) != NULL && strlen(img_pagename(d)) != 0) {
            lua_pushfstring(L, "<img %s : %s : %d >", img_filename(d), img_pagename(d), img_dictref(*aa));
        } else {
            lua_pushfstring(L, "<img %s : %d : %d >", img_filename(d), img_pagenum(d), img_dictref(*aa));
        }
    }
    return 1;
}
Esempio n. 6
0
static int m_img_gc(lua_State * L)
{
    image *a, **aa;
    image_dict *d;
    aa = (image **) luaL_checkudata(L, 1, TYPE_IMG);
    a = *aa;
    d = img_dict(*aa);
#ifdef DEBUG
    printf("\n===== IMG GC ===== a=%d ad=%d\n", a, img_dict(a));
#endif
    luaL_unref(L, LUA_GLOBALSINDEX, img_dictref(a));
    if (!img_is_refered(d))
        xfree(a);
    return 0;
}
Esempio n. 7
0
static int m_img_gc(lua_State * L)
{
    image *a, **aa;
    image_dict *d;
    aa = (image **) luaL_checkudata(L, 1, TYPE_IMG);
    a = *aa;
    d = img_dict(*aa);
    luaL_unref(L, LUA_REGISTRYINDEX, img_dictref(a));
    img_luaref(d) -= 1;
    /* we need to check this */
    if (!img_is_refered(d)) {
        xfree(a);
    }
    /* till here */
    return 0;
}