diff options
-rw-r--r-- | fitz/fitz-internal.h | 66 | ||||
-rw-r--r-- | fitz/stm_comp_buf.c | 68 | ||||
-rw-r--r-- | pdf/mupdf-internal.h | 67 | ||||
-rw-r--r-- | pdf/pdf_image.c | 13 | ||||
-rw-r--r-- | pdf/pdf_stream.c | 141 | ||||
-rw-r--r-- | win32/libmupdf.vcproj | 4 |
6 files changed, 204 insertions, 155 deletions
diff --git a/fitz/fitz-internal.h b/fitz/fitz-internal.h index b923429f..5c9270a1 100644 --- a/fitz/fitz-internal.h +++ b/fitz/fitz-internal.h @@ -694,6 +694,72 @@ fz_pixmap *fz_scale_pixmap(fz_context *ctx, fz_pixmap *src, float x, float y, fl fz_bbox fz_pixmap_bbox_no_ctx(fz_pixmap *src); +typedef struct fz_compression_params_s fz_compression_params; + +typedef struct fz_compressed_buffer_s fz_compressed_buffer; + +fz_stream *fz_open_image_decomp_stream(fz_context *ctx, fz_compressed_buffer *, int *factor); + +enum +{ + FZ_IMAGE_UNKNOWN = 0, + FZ_IMAGE_JPEG = 1, + FZ_IMAGE_JPX = 2, /* Placeholder until supported */ + FZ_IMAGE_FAX = 3, + FZ_IMAGE_JBIG2 = 4, /* Placeholder until supported */ + FZ_IMAGE_RAW = 5, + FZ_IMAGE_RLD = 6, + FZ_IMAGE_FLATE = 7, + FZ_IMAGE_LZW = 8 +}; + +struct fz_compression_params_s +{ + int type; + union { + struct { + int color_transform; + } jpeg; + struct { + int smask_in_data; + } jpx; + struct { + int columns; + int rows; + int k; + int end_of_line; + int encoded_byte_align; + int end_of_block; + int black_is_1; + int damaged_rows_before_error; + } fax; + struct + { + int columns; + int colors; + int predictor; + int bpc; + } + flate; + struct + { + int columns; + int colors; + int predictor; + int bpc; + int early_change; + } lzw; + } u; +}; + +struct fz_compressed_buffer_s +{ + fz_compression_params params; + fz_buffer *buffer; +}; + +void fz_free_compressed_buffer(fz_context *ctx, fz_compressed_buffer *buf); + struct fz_image_s { fz_storable storable; diff --git a/fitz/stm_comp_buf.c b/fitz/stm_comp_buf.c new file mode 100644 index 00000000..93df0ef0 --- /dev/null +++ b/fitz/stm_comp_buf.c @@ -0,0 +1,68 @@ +#include "fitz-internal.h" + +/* This code needs to be kept out of stm_buffer.c to avoid it being + * pulled into cmapdump.c */ + +void +fz_free_compressed_buffer(fz_context *ctx, fz_compressed_buffer *buf) +{ + if (!buf) + return; + + fz_drop_buffer(ctx, buf->buffer); + fz_free(ctx, buf); +} + + +fz_stream * +fz_open_image_decomp_stream(fz_context *ctx, fz_compressed_buffer *buffer, int *factor) +{ + fz_stream *chain = fz_open_buffer(ctx, buffer->buffer); + fz_compression_params *params = &buffer->params; + + switch (params->type) + { + case FZ_IMAGE_FAX: + *factor = 1; + return fz_open_faxd(chain, + params->u.fax.k, + params->u.fax.end_of_line, + params->u.fax.encoded_byte_align, + params->u.fax.columns, + params->u.fax.rows, + params->u.fax.end_of_block, + params->u.fax.black_is_1); + case FZ_IMAGE_JPEG: + if (*factor > 8) + *factor = 8; + return fz_open_resized_dctd(chain, params->u.jpeg.color_transform, *factor); + case FZ_IMAGE_RLD: + *factor = 1; + return fz_open_rld(chain); + case FZ_IMAGE_FLATE: + *factor = 1; + chain = fz_open_flated(chain); + if (params->u.flate.predictor > 1) + chain = fz_open_predict(chain, params->u.flate.predictor, params->u.flate.columns, params->u.flate.colors, params->u.flate.bpc); + return chain; + case FZ_IMAGE_LZW: + *factor = 1; + chain = fz_open_lzwd(chain, params->u.lzw.early_change); + if (params->u.lzw.predictor > 1) + chain = fz_open_predict(chain, params->u.lzw.predictor, params->u.lzw.columns, params->u.lzw.colors, params->u.lzw.bpc); + return chain; + default: + *factor = 1; + break; + } + + return chain; +} + +fz_stream * +fz_open_compressed_stream(fz_context *ctx, fz_compressed_buffer *buffer) +{ + int factor = 1; + + return fz_open_image_decomp_stream(ctx, buffer, &factor); +} diff --git a/pdf/mupdf-internal.h b/pdf/mupdf-internal.h index 20503604..42af9aa9 100644 --- a/pdf/mupdf-internal.h +++ b/pdf/mupdf-internal.h @@ -12,52 +12,6 @@ void pdf_set_int(pdf_obj *obj, int i); * PDF Images */ -typedef struct pdf_image_params_s pdf_image_params; - -struct pdf_image_params_s -{ - int type; - fz_colorspace *colorspace; - union - { - struct - { - int columns; - int rows; - int k; - int eol; - int eba; - int eob; - int bi1; - } - fax; - struct - { - int ct; - } - jpeg; - struct - { - int columns; - int colors; - int predictor; - int bpc; - } - flate; - struct - { - int columns; - int colors; - int predictor; - int bpc; - int ec; - } - lzw; - } - u; -}; - - typedef struct pdf_image_s pdf_image; struct pdf_image_s @@ -65,8 +19,7 @@ struct pdf_image_s fz_image base; fz_pixmap *tile; int n, bpc; - pdf_image_params params; - fz_buffer *buffer; + fz_compressed_buffer *buffer; int colorkey[FZ_MAX_COLORS * 2]; float decode[FZ_MAX_COLORS * 2]; int imagemask; @@ -74,17 +27,6 @@ struct pdf_image_s int usecolorkey; }; -enum -{ - PDF_IMAGE_RAW, - PDF_IMAGE_FAX, - PDF_IMAGE_JPEG, - PDF_IMAGE_RLD, - PDF_IMAGE_FLATE, - PDF_IMAGE_LZW, - PDF_IMAGE_JPX -}; - /* * tokenizer and low-level object parser */ @@ -240,11 +182,10 @@ void pdf_localise_page_resources(pdf_document *xref); void pdf_cache_object(pdf_document *doc, int num, int gen); -fz_stream *pdf_open_inline_stream(pdf_document *doc, pdf_obj *stmobj, int length, fz_stream *chain, pdf_image_params *params); -fz_buffer *pdf_load_image_stream(pdf_document *doc, int num, int gen, int orig_num, int orig_gen, pdf_image_params *params); -fz_stream *pdf_open_image_stream(pdf_document *doc, int num, int gen, int orig_num, int orig_gen, pdf_image_params *params); +fz_stream *pdf_open_inline_stream(pdf_document *doc, pdf_obj *stmobj, int length, fz_stream *chain, fz_compression_params *params); +fz_compressed_buffer *pdf_load_compressed_stream(pdf_document *doc, int num, int gen); fz_stream *pdf_open_stream_with_offset(pdf_document *doc, int num, int gen, pdf_obj *dict, int stm_ofs); -fz_stream *pdf_open_image_decomp_stream(fz_context *ctx, fz_buffer *, pdf_image_params *params, int *factor); +fz_stream *pdf_open_compressed_stream(fz_context *ctx, fz_compressed_buffer *); fz_stream *pdf_open_contents_stream(pdf_document *xref, pdf_obj *obj); fz_buffer *pdf_load_raw_renumbered_stream(pdf_document *doc, int num, int gen, int orig_num, int orig_gen); fz_buffer *pdf_load_renumbered_stream(pdf_document *doc, int num, int gen, int orig_num, int orig_gen); diff --git a/pdf/pdf_image.c b/pdf/pdf_image.c index 03872726..262be821 100644 --- a/pdf/pdf_image.c +++ b/pdf/pdf_image.c @@ -234,7 +234,7 @@ pdf_free_image(fz_context *ctx, fz_storable *image_) if (image == NULL) return; fz_drop_pixmap(ctx, image->tile); - fz_drop_buffer(ctx, image->buffer); + fz_free_compressed_buffer(ctx, image->buffer); fz_drop_colorspace(ctx, image->base.colorspace); fz_drop_image(ctx, image->base.mask); fz_free(ctx, image); @@ -284,7 +284,7 @@ pdf_image_get_pixmap(fz_context *ctx, fz_image *image_, int w, int h) while (key.factor > 0); /* We need to make a new one. */ - stm = pdf_open_image_decomp_stream(ctx, image->buffer, &image->params, &factor); + stm = fz_open_image_decomp_stream(ctx, image->buffer, &factor); return decomp_image_from_stream(ctx, stm, image, 0, 0, factor, 1); } @@ -418,7 +418,6 @@ pdf_load_image_imp(pdf_document *xref, pdf_obj *rdb, pdf_obj *dict, fz_stream *c } /* Now, do we load a ref, or do we load the actual thing? */ - image->params.type = PDF_IMAGE_RAW; FZ_INIT_STORABLE(&image->base, 1, pdf_free_image); image->base.get_pixmap = pdf_image_get_pixmap; image->base.w = w; @@ -429,14 +428,13 @@ pdf_load_image_imp(pdf_document *xref, pdf_obj *rdb, pdf_obj *dict, fz_stream *c image->imagemask = imagemask; image->usecolorkey = usecolorkey; image->base.mask = mask; - image->params.colorspace = image->base.colorspace; /* Uses the same ref as for the base one */ if (!indexed && !cstm) { /* Just load the compressed image data now and we can * decode it on demand. */ int num = pdf_to_num(dict); int gen = pdf_to_gen(dict); - image->buffer = pdf_load_image_stream(xref, num, gen, num, gen, &image->params); + image->buffer = pdf_load_compressed_stream(xref, num, gen); break; /* Out of fz_try */ } @@ -543,19 +541,18 @@ pdf_load_jpx(pdf_document *xref, pdf_obj *dict, pdf_image *image) fz_drop_pixmap(ctx, img); fz_rethrow(ctx); } - image->params.type = PDF_IMAGE_RAW; FZ_INIT_STORABLE(&image->base, 1, pdf_free_image); image->base.get_pixmap = pdf_image_get_pixmap; image->base.w = img->w; image->base.h = img->h; image->base.colorspace = colorspace; + image->buffer = NULL; image->tile = img; image->n = img->n; image->bpc = 8; image->interpolate = 0; image->imagemask = 0; image->usecolorkey = 0; - image->params.colorspace = colorspace; /* Uses the same ref as for the base one */ } static int @@ -563,7 +560,7 @@ pdf_image_size(fz_context *ctx, pdf_image *im) { if (im == NULL) return 0; - return sizeof(*im) + fz_pixmap_size(ctx, im->tile) + (im->buffer ? im->buffer->cap : 0); + return sizeof(*im) + fz_pixmap_size(ctx, im->tile) + (im->buffer && im->buffer->buffer ? im->buffer->buffer->cap : 0); } fz_image * diff --git a/pdf/pdf_stream.c b/pdf/pdf_stream.c index b8a8f5c6..41e438ac 100644 --- a/pdf/pdf_stream.c +++ b/pdf/pdf_stream.c @@ -48,7 +48,7 @@ pdf_stream_has_crypt(fz_context *ctx, pdf_obj *stm) * Create a filter given a name and param dictionary. */ static fz_stream * -build_filter(fz_stream *chain, pdf_document * xref, pdf_obj * f, pdf_obj * p, int num, int gen, pdf_image_params *params) +build_filter(fz_stream *chain, pdf_document * xref, pdf_obj * f, pdf_obj * p, int num, int gen, fz_compression_params *params) { fz_context *ctx = chain->ctx; char *s = pdf_to_name(f); @@ -76,14 +76,14 @@ build_filter(fz_stream *chain, pdf_document * xref, pdf_obj * f, pdf_obj * p, in if (params) { /* We will shortstop here */ - params->type = PDF_IMAGE_FAX; + params->type = FZ_IMAGE_FAX; params->u.fax.k = (k ? pdf_to_int(k) : 0); - params->u.fax.eol = (eol ? pdf_to_bool(eol) : 0); - params->u.fax.eba = (eba ? pdf_to_bool(eba) : 0); + params->u.fax.end_of_line = (eol ? pdf_to_bool(eol) : 0); + params->u.fax.encoded_byte_align = (eba ? pdf_to_bool(eba) : 0); params->u.fax.columns = (columns ? pdf_to_int(columns) : 1728); params->u.fax.rows = (rows ? pdf_to_int(rows) : 0); - params->u.fax.eob = (eob ? pdf_to_bool(eob) : 1); - params->u.fax.bi1 = (bi1 ? pdf_to_bool(bi1) : 0); + params->u.fax.end_of_block = (eob ? pdf_to_bool(eob) : 1); + params->u.fax.black_is_1 = (bi1 ? pdf_to_bool(bi1) : 0); return chain; } return fz_open_faxd(chain, @@ -102,8 +102,8 @@ build_filter(fz_stream *chain, pdf_document * xref, pdf_obj * f, pdf_obj * p, in if (params) { /* We will shortstop here */ - params->type = PDF_IMAGE_JPEG; - params->u.jpeg.ct = (ct ? pdf_to_int(ct) : -1); + params->type = FZ_IMAGE_JPEG; + params->u.jpeg.color_transform = (ct ? pdf_to_int(ct) : -1); return chain; } return fz_open_dctd(chain, ct ? pdf_to_int(ct) : -1); @@ -114,7 +114,7 @@ build_filter(fz_stream *chain, pdf_document * xref, pdf_obj * f, pdf_obj * p, in if (params) { /* We will shortstop here */ - params->type = PDF_IMAGE_RLD; + params->type = FZ_IMAGE_RLD; return chain; } return fz_open_rld(chain); @@ -124,7 +124,7 @@ build_filter(fz_stream *chain, pdf_document * xref, pdf_obj * f, pdf_obj * p, in if (params) { /* We will shortstop here */ - params->type = PDF_IMAGE_FLATE; + params->type = FZ_IMAGE_FLATE; params->u.flate.predictor = predictor; params->u.flate.columns = columns; params->u.flate.colors = colors; @@ -143,12 +143,12 @@ build_filter(fz_stream *chain, pdf_document * xref, pdf_obj * f, pdf_obj * p, in if (params) { /* We will shortstop here */ - params->type = PDF_IMAGE_LZW; + params->type = FZ_IMAGE_LZW; params->u.lzw.predictor = predictor; params->u.lzw.columns = columns; params->u.lzw.colors = colors; params->u.lzw.bpc = bpc; - params->u.lzw.ec = (ec ? pdf_to_int(ec) : 1); + params->u.lzw.early_change = (ec ? pdf_to_int(ec) : 1); return chain; } chain = fz_open_lzwd(chain, ec ? pdf_to_int(ec) : 1); @@ -197,7 +197,7 @@ build_filter(fz_stream *chain, pdf_document * xref, pdf_obj * f, pdf_obj * p, in * Assume ownership of head. */ static fz_stream * -build_filter_chain(fz_stream *chain, pdf_document *xref, pdf_obj *fs, pdf_obj *ps, int num, int gen, pdf_image_params *params) +build_filter_chain(fz_stream *chain, pdf_document *xref, pdf_obj *fs, pdf_obj *ps, int num, int gen, fz_compression_params *params) { pdf_obj *f; pdf_obj *p; @@ -258,7 +258,7 @@ pdf_open_raw_filter(fz_stream *chain, pdf_document *xref, pdf_obj *stmobj, int n * to stream length and decrypting. */ static fz_stream * -pdf_open_filter(fz_stream *chain, pdf_document *xref, pdf_obj *stmobj, int num, int gen, int offset, pdf_image_params *imparams) +pdf_open_filter(fz_stream *chain, pdf_document *xref, pdf_obj *stmobj, int num, int gen, int offset, fz_compression_params *imparams) { pdf_obj *filters; pdf_obj *params; @@ -281,7 +281,7 @@ pdf_open_filter(fz_stream *chain, pdf_document *xref, pdf_obj *stmobj, int num, * constraining to stream length, and without decryption. */ fz_stream * -pdf_open_inline_stream(pdf_document *xref, pdf_obj *stmobj, int length, fz_stream *chain, pdf_image_params *imparams) +pdf_open_inline_stream(pdf_document *xref, pdf_obj *stmobj, int length, fz_stream *chain, fz_compression_params *imparams) { pdf_obj *filters; pdf_obj *params; @@ -327,19 +327,8 @@ pdf_open_raw_renumbered_stream(pdf_document *xref, int num, int gen, int orig_nu return pdf_open_raw_filter(xref->file, xref, x->obj, num, orig_num, orig_gen, x->stm_ofs); } -/* - * Open a stream for reading uncompressed data. - * Put the opened file in xref->stream. - * Using xref->file while a stream is open is a Bad idea. - */ -fz_stream * -pdf_open_stream(pdf_document *xref, int num, int gen) -{ - return pdf_open_image_stream(xref, num, gen, num, gen, NULL); -} - -fz_stream * -pdf_open_image_stream(pdf_document *xref, int num, int gen, int orig_num, int orig_gen, pdf_image_params *params) +static fz_stream * +pdf_open_image_stream(pdf_document *xref, int num, int gen, int orig_num, int orig_gen, fz_compression_params *params) { pdf_xref_entry *x; @@ -356,48 +345,15 @@ pdf_open_image_stream(pdf_document *xref, int num, int gen, int orig_num, int or return pdf_open_filter(xref->file, xref, x->obj, orig_num, orig_gen, x->stm_ofs, params); } +/* + * Open a stream for reading uncompressed data. + * Put the opened file in xref->stream. + * Using xref->file while a stream is open is a Bad idea. + */ fz_stream * -pdf_open_image_decomp_stream(fz_context *ctx, fz_buffer *buffer, pdf_image_params *params, int *factor) +pdf_open_stream(pdf_document *xref, int num, int gen) { - fz_stream *chain = fz_open_buffer(ctx, buffer); - - switch (params->type) - { - case PDF_IMAGE_FAX: - *factor = 1; - return fz_open_faxd(chain, - params->u.fax.k, - params->u.fax.eol, - params->u.fax.eba, - params->u.fax.columns, - params->u.fax.rows, - params->u.fax.eob, - params->u.fax.bi1); - case PDF_IMAGE_JPEG: - if (*factor > 8) - *factor = 8; - return fz_open_resized_dctd(chain, params->u.jpeg.ct, *factor); - case PDF_IMAGE_RLD: - *factor = 1; - return fz_open_rld(chain); - case PDF_IMAGE_FLATE: - *factor = 1; - chain = fz_open_flated(chain); - if (params->u.flate.predictor > 1) - chain = fz_open_predict(chain, params->u.flate.predictor, params->u.flate.columns, params->u.flate.colors, params->u.flate.bpc); - return chain; - case PDF_IMAGE_LZW: - *factor = 1; - chain = fz_open_lzwd(chain, params->u.lzw.ec); - if (params->u.lzw.predictor > 1) - chain = fz_open_predict(chain, params->u.lzw.predictor, params->u.lzw.columns, params->u.lzw.colors, params->u.lzw.bpc); - return chain; - default: - *factor = 1; - break; - } - - return chain; + return pdf_open_image_stream(xref, num, gen, num, gen, NULL); } fz_stream * @@ -459,23 +415,8 @@ pdf_guess_filter_length(int len, char *filter) return len; } -/* - * Load uncompressed contents of a stream into buf. - */ fz_buffer * -pdf_load_stream(pdf_document *xref, int num, int gen) -{ - return pdf_load_image_stream(xref, num, gen, num, gen, NULL); -} - -fz_buffer * -pdf_load_renumbered_stream(pdf_document *xref, int num, int gen, int orig_num, int orig_gen) -{ - return pdf_load_image_stream(xref, num, gen, orig_num, orig_gen, NULL); -} - -fz_buffer * -pdf_load_image_stream(pdf_document *xref, int num, int gen, int orig_num, int orig_gen, pdf_image_params *params) +pdf_load_image_stream(pdf_document *xref, int num, int gen, int orig_num, int orig_gen, fz_compression_params *params) { fz_context *ctx = xref->ctx; fz_stream *stm = NULL; @@ -517,6 +458,38 @@ pdf_load_image_stream(pdf_document *xref, int num, int gen, int orig_num, int or return buf; } +/* + * Load uncompressed contents of a stream into buf. + */ +fz_buffer * +pdf_load_stream(pdf_document *xref, int num, int gen) +{ + return pdf_load_image_stream(xref, num, gen, num, gen, NULL); +} + +fz_buffer * +pdf_load_renumbered_stream(pdf_document *xref, int num, int gen, int orig_num, int orig_gen) +{ + return pdf_load_image_stream(xref, num, gen, orig_num, orig_gen, NULL); +} + +fz_compressed_buffer * +pdf_load_compressed_stream(pdf_document *xref, int num, int gen) +{ + fz_context *ctx = xref->ctx; + fz_compressed_buffer *bc = fz_malloc_struct(ctx, fz_compressed_buffer); + + fz_try(ctx) + { + bc->buffer = pdf_load_image_stream(xref, num, gen, num, gen, &bc->params); + } + fz_catch(ctx) + { + fz_free(ctx, bc); + } + return bc; +} + static fz_stream * pdf_open_object_array(pdf_document *xref, pdf_obj *list) { diff --git a/win32/libmupdf.vcproj b/win32/libmupdf.vcproj index e7d189d3..726b23a0 100644 --- a/win32/libmupdf.vcproj +++ b/win32/libmupdf.vcproj @@ -558,6 +558,10 @@ > </File> <File + RelativePath="..\fitz\stm_comp_buf.c" + > + </File> + <File RelativePath="..\fitz\stm_open.c" > </File> |