static void generate_image(fz_context *ctx, fz_archive *zip, const char *base_uri, fz_html *box, const char *src) { fz_image *img; fz_buffer *buf; char path[2048]; fz_html *flow = box; while (flow->type != BOX_FLOW) flow = flow->up; fz_strlcpy(path, base_uri, sizeof path); fz_strlcat(path, "/", sizeof path); fz_strlcat(path, src, sizeof path); fz_cleanname(path); fz_try(ctx) { buf = fz_read_archive_entry(ctx, zip, path); img = fz_new_image_from_buffer(ctx, buf); fz_drop_buffer(ctx, buf); add_flow_image(ctx, flow, &box->style, img); } fz_catch(ctx) { const char *alt = "[image]"; fz_warn(ctx, "html: cannot add image src='%s'", src); add_flow_word(ctx, flow, &box->style, alt, alt + 7); } }
cbz_page * cbz_load_page(cbz_document *doc, int number) { fz_context *ctx = doc->ctx; unsigned char *data = NULL; cbz_page *page = NULL; if (number < 0 || number >= doc->page_count) return NULL; fz_var(data); fz_var(page); fz_buffer *buf = fz_read_archive_entry(doc->ctx, doc->zip, doc->page[number]); fz_try(ctx) { page = fz_malloc_struct(ctx, cbz_page); page->image = fz_new_image_from_buffer(ctx, buf); } fz_always(ctx) { fz_drop_buffer(doc->ctx, buf); } fz_catch(ctx) { fz_free(ctx, data); cbz_free_page(doc, page); fz_rethrow(ctx); } return page; }
fz_image * fz_new_image_from_data(fz_context *ctx, unsigned char *data, int len) { fz_buffer *buffer = NULL; fz_image *image; fz_var(buffer); fz_var(data); fz_try(ctx) { buffer = fz_new_buffer_from_data(ctx, data, len); data = NULL; image = fz_new_image_from_buffer(ctx, buffer); } fz_always(ctx) { fz_drop_buffer(ctx, buffer); } fz_catch(ctx) { fz_free(ctx, data); fz_rethrow(ctx); } return image; }
static cbz_page * cbz_load_page(fz_context *ctx, cbz_document *doc, int number) { unsigned char *data = NULL; cbz_page *page = NULL; fz_buffer *buf; if (number < 0 || number >= doc->page_count) return NULL; fz_var(data); fz_var(page); buf = fz_read_archive_entry(ctx, doc->zip, doc->page[number]); fz_try(ctx) { page = fz_new_page(ctx, sizeof *page); page->super.bound_page = (fz_page_bound_page_fn *)cbz_bound_page; page->super.run_page_contents = (fz_page_run_page_contents_fn *)cbz_run_page; page->super.drop_page_imp = (fz_page_drop_page_imp_fn *)cbz_drop_page_imp; page->image = fz_new_image_from_buffer(ctx, buf); } fz_always(ctx) { fz_drop_buffer(ctx, buf); } fz_catch(ctx) { fz_free(ctx, data); cbz_drop_page_imp(ctx, page); fz_rethrow(ctx); } return page; }
static img_document * img_open_document_with_stream(fz_context *ctx, fz_stream *stm) { fz_buffer *buffer = NULL; fz_image *image = NULL; img_document *doc; fz_var(buffer); fz_var(image); fz_try(ctx) { buffer = fz_read_all(ctx, stm, 1024); image = fz_new_image_from_buffer(ctx, buffer); doc = img_new_document(ctx, image); } fz_always(ctx) { fz_drop_image(ctx, image); fz_drop_buffer(ctx, buffer); } fz_catch(ctx) fz_rethrow(ctx); return doc; }
static fz_page * cbz_load_page(fz_context *ctx, fz_document *doc_, int number) { cbz_document *doc = (cbz_document*)doc_; cbz_page *page = NULL; fz_buffer *buf = NULL; if (number < 0 || number >= doc->page_count) fz_throw(ctx, FZ_ERROR_GENERIC, "cannot load page %d", number); fz_var(page); if (doc->arch) buf = fz_read_archive_entry(ctx, doc->arch, doc->page[number]); if (!buf) fz_throw(ctx, FZ_ERROR_GENERIC, "cannot load cbz page"); fz_try(ctx) { page = fz_new_derived_page(ctx, cbz_page); page->super.bound_page = cbz_bound_page; page->super.run_page_contents = cbz_run_page; page->super.drop_page = cbz_drop_page; page->image = fz_new_image_from_buffer(ctx, buf); } fz_always(ctx) { fz_drop_buffer(ctx, buf); } fz_catch(ctx) { fz_drop_page(ctx, (fz_page*)page); fz_rethrow(ctx); } return (fz_page*)page; }