int l_new_image(lua_State * L) { image *a, **aa; image_dict **add; if (lua_gettop(L) > 0 && ! 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_REGISTRYINDEX); /* i (t) */ img_luaref(*add) += 1; 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, *add); /* v k t i */ lua_pop(L, 1); /* k t i */ } /* t i */ lua_pop(L, 1); /* i */ } /* i */ return 1; /* i */ }
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"); } }
static int m_img_dict_gc(lua_State * L) { image_dict *ad, **add; add = (image_dict **) luaL_checkudata(L, 1, TYPE_IMG_DICT); ad = *add; if (img_luaref(ad) > 0) { luaL_error(L, "disposing image dict that has references"); } else { /* we need to check this */ if (img_state(ad) < DICT_REFERED) { free_image_dict(ad); } /* till here */ } return 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; }
image_dict *new_image_dict(void) { image_dict *p = xtalloc(1, image_dict); memset(p, 0, sizeof(image_dict)); set_wd_running(p); set_ht_running(p); set_dp_running(p); img_transform(p) = 0; img_pagenum(p) = 1; img_type(p) = IMG_TYPE_NONE; img_pagebox(p) = PDF_BOX_SPEC_MEDIA; img_unset_bbox(p); img_unset_group(p); img_state(p) = DICT_NEW; /*tex A value of -1 means unused while the used counts from 0 */ img_index(p) = -1; img_luaref(p) = 0; img_errorlevel(p) = pdf_inclusion_errorlevel; fix_pdf_version(static_pdf); img_pdfmajorversion(p) = pdf_major_version; img_pdfminorversion(p) = pdf_minor_version; return p; }
static int m_img_get(lua_State * L) { int j; const char *s; image **a = (image **) luaL_checkudata(L, 1, TYPE_IMG); /* k u */ image_dict *d = img_dict(*a); if (d == NULL) { luaL_error(L, "invalid image dictionary"); } s = lua_tostring(L, 2); if (lua_key_eq(s,width)) { if (is_wd_running(*a)) { lua_pushnil(L); } else { lua_pushinteger(L, img_width(*a)); } } else if (lua_key_eq(s,height)) { if (is_ht_running(*a)) { lua_pushnil(L); } else { lua_pushinteger(L, img_height(*a)); } } else if (lua_key_eq(s,depth)) { if (is_dp_running(*a)) { lua_pushnil(L); } else { lua_pushinteger(L, img_depth(*a)); } } else if (lua_key_eq(s,transform)) { lua_pushinteger(L, img_transform(*a)); } else if (lua_key_eq(s,filename)) { if (img_filename(d) == NULL || strlen(img_filename(d)) == 0) { lua_pushnil(L); } else { lua_pushstring(L, img_filename(d)); } } else if (lua_key_eq(s,visiblefilename)) { if (img_visiblefilename(d) == NULL || strlen(img_visiblefilename(d)) == 0) { lua_pushnil(L); } else { lua_pushstring(L, img_visiblefilename(d)); } } else if (lua_key_eq(s,keepopen)) { lua_pushboolean(L, img_keepopen(d)); } else if (lua_key_eq(s,filepath)) { if (img_filepath(d) == NULL || strlen(img_filepath(d)) == 0) { lua_pushnil(L); } else { lua_pushstring(L, img_filepath(d)); } } else if (lua_key_eq(s,attr)) { if (img_attr(d) == NULL || strlen(img_attr(d)) == 0) { lua_pushnil(L); } else { lua_pushstring(L, img_attr(d)); } } else if (lua_key_eq(s,page)) { if (img_pagename(d) != NULL && strlen(img_pagename(d)) != 0) { lua_pushstring(L, img_pagename(d)); } else { lua_pushinteger(L, img_pagenum(d)); } } else if (lua_key_eq(s,pages)) { lua_pushinteger(L, img_totalpages(d)); } else if (lua_key_eq(s,xsize)) { if ((img_rotation(d) & 1) == 0) { lua_pushinteger(L, img_xsize(d)); } else { lua_pushinteger(L, img_ysize(d)); } } else if (lua_key_eq(s,ysize)) { if ((img_rotation(d) & 1) == 0) { lua_pushinteger(L, img_ysize(d)); } else { lua_pushinteger(L, img_xsize(d)); } } else if (lua_key_eq(s,xres)) { lua_pushinteger(L, img_xres(d)); } else if (lua_key_eq(s,yres)) { lua_pushinteger(L, img_yres(d)); } else if (lua_key_eq(s,rotation)) { lua_pushinteger(L, img_rotation(d)); } else if (lua_key_eq(s,colorspace)) { if (img_colorspace(d) == 0) { lua_pushnil(L); } else { lua_pushinteger(L, img_colorspace(d)); } } else if (lua_key_eq(s,colordepth)) { if (img_colordepth(d) == 0) { lua_pushnil(L); } else { lua_pushinteger(L, img_colordepth(d)); } } else if (lua_key_eq(s,imagetype)) { j = img_type(d); if (j >= 0 && j <= img_types_max) { if (j == IMG_TYPE_NONE) { lua_pushnil(L); } else { lua_pushstring(L, img_types[j]); } } else { lua_pushnil(L); } } else if (lua_key_eq(s,pagebox)) { j = img_pagebox(d); if (j < 0 || j >= img_pageboxes_max) { j = 0; } lua_push_img_pagebox(L, j); } else if (lua_key_eq(s,bbox)) { if (!img_is_bbox(d)) { img_bbox(d)[0] = img_xorig(d); img_bbox(d)[1] = img_yorig(d); img_bbox(d)[2] = img_xorig(d) + img_xsize(d); img_bbox(d)[3] = img_yorig(d) + img_ysize(d); } lua_newtable(L); lua_pushinteger(L, 1); lua_pushinteger(L, img_bbox(d)[0]); lua_settable(L, -3); lua_pushinteger(L, 2); lua_pushinteger(L, img_bbox(d)[1]); lua_settable(L, -3); lua_pushinteger(L, 3); lua_pushinteger(L, img_bbox(d)[2]); lua_settable(L, -3); lua_pushinteger(L, 4); lua_pushinteger(L, img_bbox(d)[3]); lua_settable(L, -3); } else if (lua_key_eq(s,objnum)) { if (img_objnum(d) == 0) { lua_pushnil(L); } else { lua_pushinteger(L, img_objnum(d)); } } else if (lua_key_eq(s,index)) { if (img_index(d) == 0) { lua_pushnil(L); } else { lua_pushinteger(L, img_index(d)); } } else if (lua_key_eq(s,stream)) { if (img_type(d) != IMG_TYPE_PDFSTREAM || img_pdfstream_ptr(d) == NULL || img_pdfstream_stream(d) == NULL || strlen(img_pdfstream_stream(d)) == 0) { lua_pushnil(L); } else { lua_pushstring(L, img_pdfstream_stream(d)); } } else if (lua_key_eq(s,ref_count)) { lua_pushinteger(L, img_luaref(d)); } else { lua_pushnil(L); } return 1; }