static void image_to_lua(lua_State * L, image * a) { /* key user ... */ int i, j; image_dict *d = img_dict(a); assert(d != NULL); lua_pushstring(L, IMG_ENV); /* s k u ... */ lua_gettable(L, LUA_REGISTRYINDEX); /* t k u ... */ lua_pushvalue(L, -2); /* k t k u ... */ lua_gettable(L, -2); /* i? t k u ... */ if (!lua_isnumber(L, -1)) /* !i t k u ... */ luaL_error(L, "image_to_lua(): %s is not a valid image key", lua_tostring(L, -3)); i = (int) lua_tointeger(L, -1); /* i t k u ... */ lua_pop(L, 3); /* u ... */ switch (i) { case P_WIDTH: if (is_wd_running(a)) lua_pushnil(L); else lua_pushinteger(L, img_width(a)); break; case P_HEIGHT: if (is_ht_running(a)) lua_pushnil(L); else lua_pushinteger(L, img_height(a)); break; case P_DEPTH: if (is_dp_running(a)) lua_pushnil(L); else lua_pushinteger(L, img_depth(a)); break; case P_TRANSFORM: lua_pushinteger(L, img_transform(a)); break; /* now follow all image_dict entries */ case P_FILENAME: if (img_filename(d) == NULL || strlen(img_filename(d)) == 0) lua_pushnil(L); else lua_pushstring(L, img_filename(d)); break; case P_VISIBLEFILENAME: if (img_visiblefilename(d) == NULL || strlen(img_visiblefilename(d)) == 0) lua_pushnil(L); else lua_pushstring(L, img_visiblefilename(d)); break; case P_FILEPATH: if (img_filepath(d) == NULL || strlen(img_filepath(d)) == 0) lua_pushnil(L); else lua_pushstring(L, img_filepath(d)); break; case P_ATTR: if (img_attr(d) == NULL || strlen(img_attr(d)) == 0) lua_pushnil(L); else lua_pushstring(L, img_attr(d)); break; case P_PAGE: if (img_pagename(d) != NULL && strlen(img_pagename(d)) != 0) lua_pushstring(L, img_pagename(d)); else lua_pushinteger(L, img_pagenum(d)); break; case P_TOTALPAGES: lua_pushinteger(L, img_totalpages(d)); break; case P_XSIZE: /* Modify by /Rotate only for output */ if ((img_rotation(d) & 1) == 0) lua_pushinteger(L, img_xsize(d)); else lua_pushinteger(L, img_ysize(d)); break; case P_YSIZE: /* Modify by /Rotate only for output */ if ((img_rotation(d) & 1) == 0) lua_pushinteger(L, img_ysize(d)); else lua_pushinteger(L, img_xsize(d)); break; case P_XRES: lua_pushinteger(L, img_xres(d)); break; case P_YRES: lua_pushinteger(L, img_yres(d)); break; case P_ROTATION: lua_pushinteger(L, img_rotation(d)); break; case P_COLORSPACE: if (img_colorspace(d) == 0) lua_pushnil(L); else lua_pushinteger(L, img_colorspace(d)); break; case P_COLORDEPTH: if (img_colordepth(d) == 0) lua_pushnil(L); else lua_pushinteger(L, img_colordepth(d)); break; case P_IMAGETYPE: j = img_type(d); if (j >= 0 && j <= imgtype_max) { if (j == IMG_TYPE_NONE) lua_pushnil(L); else lua_pushstring(L, imgtype_s[j]); } else assert(0); break; case P_PAGEBOX: j = img_pagebox(d); if (j >= 0 && j <= pagebox_max) { if (j == PDF_BOX_SPEC_NONE) lua_pushnil(L); else lua_pushstring(L, pdfboxspec_s[j]); } else assert(0); break; case P_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); break; case P_OBJNUM: if (img_objnum(d) == 0) lua_pushnil(L); else lua_pushinteger(L, img_objnum(d)); break; case P_INDEX: if (img_index(d) == 0) lua_pushnil(L); else lua_pushinteger(L, img_index(d)); break; case P_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)); break; default: assert(0); } /* v u ... */ }
scaled_whd scale_img(image_dict * idict, scaled_whd alt_rule, int transform) { /*tex size and resolution of image */ int x, y, xr, yr, tmp; /*tex natural size corresponding to image resolution */ scaled_whd nat; int default_res; nat.dp = 0; nat.wd = 0; nat.ht = 0; if (img_nobbox(idict)) { if (img_is_bbox(idict)) { x = img_xsize(idict) = img_bbox(idict)[2] - img_bbox(idict)[0]; y = img_ysize(idict) = img_bbox(idict)[3] - img_bbox(idict)[1]; img_xorig(idict) = img_bbox(idict)[0]; img_yorig(idict) = img_bbox(idict)[1]; nat.wd = x; nat.ht = y; } else { normal_error("pdf backend","use boundingbox to pass dimensions"); } } else { if ((img_type(idict) == IMG_TYPE_PDF || img_type(idict) == IMG_TYPE_PDFMEMSTREAM || img_type(idict) == IMG_TYPE_PDFSTREAM) && img_is_bbox(idict)) { /*tex dimensions from image.bbox */ x = img_xsize(idict) = img_bbox(idict)[2] - img_bbox(idict)[0]; y = img_ysize(idict) = img_bbox(idict)[3] - img_bbox(idict)[1]; img_xorig(idict) = img_bbox(idict)[0]; img_yorig(idict) = img_bbox(idict)[1]; } else { /*tex dimensions, resolutions from image file */ x = img_xsize(idict); y = img_ysize(idict); } xr = img_xres(idict); yr = img_yres(idict); if (x <= 0 || y <= 0 || xr < 0 || yr < 0) normal_error("pdf backend","invalid image dimensions"); if (xr > 65535 || yr > 65535) { xr = 0; yr = 0; normal_warning("pdf backend","too large image resolution ignored"); } if (((transform - img_rotation(idict)) & 1) == 1) { tmp = x; x = y; y = tmp; tmp = xr; xr = yr; yr = tmp; } /*tex always for images */ if (img_type(idict) == IMG_TYPE_PDF || img_type(idict) == IMG_TYPE_PDFMEMSTREAM || img_type(idict) == IMG_TYPE_PDFSTREAM) { nat.wd = x; nat.ht = y; } else { default_res = fix_int(pdf_image_resolution, 0, 65535); if (default_res > 0 && (xr == 0 || yr == 0)) { xr = default_res; yr = default_res; } if (xr > 0 && yr > 0) { nat.wd = ext_xn_over_d(one_hundred_inch, x, 100 * xr); nat.ht = ext_xn_over_d(one_hundred_inch, y, 100 * yr); } else { nat.wd = ext_xn_over_d(one_hundred_inch, x, 7200); nat.ht = ext_xn_over_d(one_hundred_inch, y, 7200); } } } return tex_scale(nat, alt_rule); }
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; }
void read_jpg_info(integer img) { int i, units = 0; unsigned char jpg_id[] = "JFIF"; img_xres(img) = img_yres(img) = 0; jpg_ptr(img)->file = xfopen(img_name(img), FOPEN_RBIN_MODE); /* no LFS needed, as JPEG is limited to <4GiB */ xfseek(jpg_ptr(img)->file, 0, SEEK_END, cur_file_name); jpg_ptr(img)->length = xftell(jpg_ptr(img)->file, cur_file_name); xfseek(jpg_ptr(img)->file, 0, SEEK_SET, cur_file_name); if (read2bytes(jpg_ptr(img)->file) != 0xFFD8) pdftex_fail("reading JPEG image failed (no JPEG header found)"); /* currently only true JFIF files allow extracting img_xres and img_yres */ if (read2bytes(jpg_ptr(img)->file) == 0xFFE0) { /* check for JFIF */ (void) read2bytes(jpg_ptr(img)->file); for (i = 0; i < 5; i++) { if (xgetc(jpg_ptr(img)->file) != jpg_id[i]) break; } if (i == 5) { /* it's JFIF */ read2bytes(jpg_ptr(img)->file); units = xgetc(jpg_ptr(img)->file); img_xres(img) = read2bytes(jpg_ptr(img)->file); img_yres(img) = read2bytes(jpg_ptr(img)->file); switch (units) { case 1: break; /* pixels per inch */ case 2: img_xres(img) *= 2.54; img_yres(img) *= 2.54; break; /* pixels per cm */ default: img_xres(img) = img_yres(img) = 0; break; } } } xfseek(jpg_ptr(img)->file, 0, SEEK_SET, cur_file_name); while (1) { if (feof(jpg_ptr(img)->file)) pdftex_fail("reading JPEG image failed (premature file end)"); if (fgetc(jpg_ptr(img)->file) != 0xFF) pdftex_fail("reading JPEG image failed (no marker found)"); switch (xgetc(jpg_ptr(img)->file)) { case M_SOF5: case M_SOF6: case M_SOF7: case M_SOF9: case M_SOF10: case M_SOF11: case M_SOF13: case M_SOF14: case M_SOF15: pdftex_fail("unsupported type of compression"); case M_SOF2: if (fixedpdfminorversion <= 2) pdftex_fail("cannot use progressive DCT with PDF-1.2"); case M_SOF0: case M_SOF1: case M_SOF3: (void) read2bytes(jpg_ptr(img)->file); /* read segment length */ jpg_ptr(img)->bits_per_component = xgetc(jpg_ptr(img)->file); img_height(img) = read2bytes(jpg_ptr(img)->file); img_width(img) = read2bytes(jpg_ptr(img)->file); jpg_ptr(img)->color_space = xgetc(jpg_ptr(img)->file); xfseek(jpg_ptr(img)->file, 0, SEEK_SET, cur_file_name); switch (jpg_ptr(img)->color_space) { case JPG_GRAY: img_color(img) = IMAGE_COLOR_B; break; case JPG_RGB: img_color(img) = IMAGE_COLOR_C; break; case JPG_CMYK: img_color(img) = IMAGE_COLOR_C; break; default: pdftex_fail("Unsupported color space %i", (int) jpg_ptr(img)->color_space); } return; case M_SOI: /* ignore markers without parameters */ case M_EOI: case M_TEM: case M_RST0: case M_RST1: case M_RST2: case M_RST3: case M_RST4: case M_RST5: case M_RST6: case M_RST7: break; default: /* skip variable length markers */ xfseek(jpg_ptr(img)->file, read2bytes(jpg_ptr(img)->file) - 2, SEEK_CUR, cur_file_name); break; } } }