static void check_type_by_header(image_dict * idict) { int i; FILE *file = NULL; char header[MAX_HEADER]; char prefix[LEN_PDF_MEMSTREAM+1]; if (idict == NULL) return; if (img_type(idict) != IMG_TYPE_NONE) return; /*tex Here we read the and also check for a memstream object. */ if (!img_filepath(idict) || !FOPEN_RBIN_MODE) { normal_error("pdf backend","reading image file failed"); } file = fopen(img_filepath(idict), FOPEN_RBIN_MODE); if (file == NULL) { /*tex We check the prefix of img_filepath(idict). */ for (i = 0; (unsigned) i < LEN_PDF_MEMSTREAM; i++) { prefix[i] = (char) (img_filepath(idict)[i]); } prefix[LEN_PDF_MEMSTREAM]='\0'; if (strncmp(prefix, HEADER_PDF_MEMSTREAM, LEN_PDF_MEMSTREAM) == 0) { img_type(idict) = IMG_TYPE_PDFMEMSTREAM; return; } else { formatted_error("pdf backend","reading image file '%s' failed",img_filepath(idict)); } } /*tex Do we have a valid file but perhaps unsupported? */ for (i = 0; (unsigned) i < MAX_HEADER; i++) { header[i] = (char) xgetc(file); if (feof(file)) { normal_error("pdf backend","reading image file failed"); } } xfclose(file, img_filepath(idict)); /*tex Further tests: */ if (strncmp(header, HEADER_JPG, sizeof(HEADER_JPG) - 1) == 0) img_type(idict) = IMG_TYPE_JPG; else if (strncmp(header + 4, HEADER_JP2, sizeof(HEADER_JP2) - 1) == 0) img_type(idict) = IMG_TYPE_JP2; else if (strncmp(header, HEADER_PNG, sizeof(HEADER_PNG) - 1) == 0) img_type(idict) = IMG_TYPE_PNG; else if (strncmp(header, HEADER_JBIG2, sizeof(HEADER_JBIG2) - 1) == 0) img_type(idict) = IMG_TYPE_JBIG2; else if (strncmp(header, HEADER_PDF, sizeof(HEADER_PDF) - 1) == 0) img_type(idict) = IMG_TYPE_PDF; }
void pdf_dict_add_img_filename(PDF pdf, image_dict * idict) { char *p; if ((pdf_image_addfilename > 0) && ((pdf_suppress_optional_info & 2) == 0)) { /*tex For now |PTEX.FileName| is only used for \PDF, but we're prepared for \JPG, \PNG, ... */ if (! ( (img_type(idict) == IMG_TYPE_PDF) || (img_type(idict) == IMG_TYPE_PDFMEMSTREAM) )) return; if (img_visiblefilename(idict) != NULL) { if (strlen(img_visiblefilename(idict)) == 0) { /*tex empty string blocks PTEX.FileName output */ return; } else { p = img_visiblefilename(idict); } } else { /*tex unset so let's use the default */ p = img_filepath(idict); } /*tex write additional information */ pdf_add_name(pdf, "PTEX.FileName"); pdf_printf(pdf, " (%s)", convertStringToPDFString(p, strlen(p))); } }
void write_img(PDF pdf, image_dict * idict) { if (img_state(idict) < DICT_WRITTEN) { report_start_file(filetype_image, img_filepath(idict)); switch (img_type(idict)) { case IMG_TYPE_PNG: write_png(pdf, idict); break; case IMG_TYPE_JPG: write_jpg(pdf, idict); break; case IMG_TYPE_JP2: write_jp2(pdf, idict); break; case IMG_TYPE_JBIG2: write_jbig2(pdf, idict); break; case IMG_TYPE_PDFMEMSTREAM: case IMG_TYPE_PDF: write_epdf(pdf, idict,(int) pdf_suppress_optional_info); break; case IMG_TYPE_PDFSTREAM: write_pdfstream(pdf, idict); break; default: normal_error("pdf backend","internal error: writing unknown image type"); } report_stop_file(filetype_image); if (img_type(idict) == IMG_TYPE_PNG) { write_additional_png_objects(pdf); } } if (img_state(idict) < DICT_WRITTEN) img_state(idict) = DICT_WRITTEN; }
static void free_dict_strings(image_dict * p) { xfree(img_filename(p)); xfree(img_filepath(p)); xfree(img_attr(p)); xfree(img_pagename(p)); }
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 ... */ }
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_img(image_dict * idict) { char *filepath = NULL; int callback_id; if (img_filename(idict) == NULL) { normal_error("pdf backend","image file name missing"); } callback_id = callback_defined(find_image_file_callback); if (img_filepath(idict) == NULL) { if (callback_id > 0) { /*tex We always callback, also for a mem stream. */ if (run_callback(callback_id, "S->S", img_filename(idict),&filepath)) { if (filepath && (strlen(filepath) > 0)) { img_filepath(idict) = strdup(filepath); } } } if (img_filepath(idict) == NULL && (strstr(img_filename(idict),"data:application/pdf,") != NULL)) { /*tex We need to check here for a pdf memstream. */ img_filepath(idict) = strdup(img_filename(idict)); } else if (callback_id == 0) { /*tex Otherwise we use kpse but only when we don't callback. */ img_filepath(idict) = kpse_find_file(img_filename(idict), kpse_tex_format, true); } if (img_filepath(idict) == NULL) { /*tex In any case we need a name. */ formatted_error("pdf backend","cannot find image file '%s'", img_filename(idict)); } } recorder_record_input(img_filepath(idict)); /*tex A few type checks. */ check_type_by_header(idict); check_type_by_extension(idict); /*tex Now we're ready to read the image. */ switch (img_type(idict)) { case IMG_TYPE_PDFMEMSTREAM: case IMG_TYPE_PDF: read_pdf_info(idict); break; case IMG_TYPE_PNG: read_png_info(idict); break; case IMG_TYPE_JPG: read_jpg_info(idict); break; case IMG_TYPE_JP2: read_jp2_info(idict); break; case IMG_TYPE_JBIG2: read_jbig2_info(idict); break; default: img_type(idict) = IMG_TYPE_NONE; if (pdf_ignore_unknown_images) { normal_warning("pdf backend","internal error: ignoring unknown image type"); } else { normal_error("pdf backend","internal error: unknown image type"); } break; } cur_file_name = NULL; if (img_type(idict) == IMG_TYPE_NONE) { img_state(idict) = DICT_NEW; } else if (img_state(idict) < DICT_FILESCANNED) { img_state(idict) = DICT_FILESCANNED; } }