summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian Rasmussen <sebras@gmail.com>2016-05-24 18:41:52 +0200
committerSebastian Rasmussen <sebras@gmail.com>2016-06-14 00:56:03 +0200
commit4d7bf58d137d884e4eb0e81fd576031f069105cb (patch)
tree01d01a9de9c7c9a2d51c8061cbd642ebb4728de1
parent163c30cf09bfc9e02abbc85fd02905ee9136abb4 (diff)
downloadmupdf-4d7bf58d137d884e4eb0e81fd576031f069105cb.tar.xz
Add optional support for Luratech JBIG2 decoder.
If thirdparty/luratech is populated then this decoder will be preferred over jbig2dec (even if both are present).
-rw-r--r--Makefile4
-rw-r--r--Makethird109
-rw-r--r--include/mupdf/fitz/filter.h2
-rw-r--r--source/fitz/filter-jbig2.c256
-rw-r--r--source/pdf/pdf-stream.c2
5 files changed, 365 insertions, 8 deletions
diff --git a/Makefile b/Makefile
index a1dee548..5ba326a8 100644
--- a/Makefile
+++ b/Makefile
@@ -26,6 +26,7 @@ LIBS += $(MUJS_LIBS)
LIBS += $(OPENJPEG_LIBS)
LIBS += $(OPENSSL_LIBS)
LIBS += $(ZLIB_LIBS)
+LIBS += $(LURATECH_LIBS)
CFLAGS += $(FREETYPE_CFLAGS)
CFLAGS += $(HARFBUZZ_CFLAGS)
@@ -35,6 +36,7 @@ CFLAGS += $(MUJS_CFLAGS)
CFLAGS += $(OPENJPEG_CFLAGS)
CFLAGS += $(OPENSSL_CFLAGS)
CFLAGS += $(ZLIB_CFLAGS)
+CFLAGS += $(LURATECH_CFLAGS)
# --- Commands ---
@@ -145,7 +147,7 @@ MUPDF_LIB = $(OUT)/libmupdf.a
THIRD_LIB = $(OUT)/libmupdfthird.a
MUPDF_OBJ := $(FITZ_OBJ) $(FONT_OBJ) $(PDF_OBJ) $(XPS_OBJ) $(SVG_OBJ) $(CBZ_OBJ) $(HTML_OBJ) $(GPRF_OBJ)
-THIRD_OBJ := $(FREETYPE_OBJ) $(HARFBUZZ_OBJ) $(JBIG2DEC_OBJ) $(JPEG_OBJ) $(MUJS_OBJ) $(OPENJPEG_OBJ) $(ZLIB_OBJ)
+THIRD_OBJ := $(FREETYPE_OBJ) $(HARFBUZZ_OBJ) $(JBIG2DEC_OBJ) $(JPEG_OBJ) $(LURATECH_OBJ) $(MUJS_OBJ) $(OPENJPEG_OBJ) $(ZLIB_OBJ)
$(MUPDF_LIB) : $(MUPDF_OBJ)
$(THIRD_LIB) : $(THIRD_OBJ)
diff --git a/Makethird b/Makethird
index dd6c54e6..6e0d4c45 100644
--- a/Makethird
+++ b/Makethird
@@ -13,6 +13,7 @@ JPEG_DIR := thirdparty/jpeg
MUJS_DIR := thirdparty/mujs
OPENJPEG_DIR := thirdparty/openjpeg/src/lib/openjp2
ZLIB_DIR := thirdparty/zlib
+LURATECH_DIR := thirdparty/luratech
CURL_DIR := thirdparty/curl
GLFW_DIR := thirdparty/glfw
@@ -172,6 +173,110 @@ HARFBUZZ_CFLAGS := $(SYS_HARFBUZZ_CFLAGS)
HARFBUZZ_LIBS := $(SYS_HARFBUZZ_LIBS)
endif
+# --- LURATECH ---
+
+ifneq "$(wildcard $(LURATECH_DIR)/ldf_jb2)" ""
+
+LURATECH_OUT := $(OUT)/luratech
+LURATECH_SRC := \
+ jb2_adt_cache.c \
+ jb2_adt_decoder_halftone_region.c \
+ jb2_adt_encoder_text_region.c \
+ jb2_adt_huffman_tree.c \
+ jb2_adt_symbol_instance.c \
+ jb2_adt_context_ref_encoder.c \
+ jb2_adt_context_ref_buffer.c \
+ jb2_adt_context_ref_encoder.c \
+ jb2_adt_mq_encoder.c \
+ jb2_adt_decoder_text_region.c \
+ jb2_adt_huffman_table_user_defined.c \
+ jb2_adt_mmr_tables.c \
+ jb2_adt_huffman_table_symbol.c \
+ jb2_adt_component.c \
+ jb2_adt_context_buffer.c \
+ jb2_adt_context_decoder.c \
+ jb2_adt_context_encoder.c \
+ jb2_adt_context_ref_decoder.c \
+ jb2_adt_decoder_collective_bitmap.c \
+ jb2_adt_decoder_generic_region.c \
+ jb2_adt_decoder_pattern_dict.c \
+ jb2_adt_decoder_symbol_dict.c \
+ jb2_adt_encoder_symbol_dict.c \
+ jb2_adt_external_cache.c \
+ jb2_adt_file.c \
+ jb2_adt_file_extras.c \
+ jb2_adt_handle_document.c \
+ jb2_adt_huffman_decoder.c \
+ jb2_adt_huffman_encoder.c \
+ jb2_adt_huffman_table.c \
+ jb2_adt_huffman_table_standard.c \
+ jb2_adt_location.c \
+ jb2_adt_memory.c \
+ jb2_adt_message.c \
+ jb2_adt_mmr_decoder.c \
+ jb2_adt_mq_decoder.c \
+ jb2_adt_mq_encoder.c \
+ jb2_adt_mq_state.c \
+ jb2_adt_pattern_dict.c \
+ jb2_adt_pdf_file.c \
+ jb2_adt_pdf_stream.c \
+ jb2_adt_props_decompress.c \
+ jb2_adt_read_bit_buffer.c \
+ jb2_adt_read_data.c \
+ jb2_adt_render_common.c \
+ jb2_adt_render_generic_region.c \
+ jb2_adt_render_halftone_region.c \
+ jb2_adt_render_text_region.c \
+ jb2_adt_run_array.c \
+ jb2_adt_segment_array.c \
+ jb2_adt_segment.c \
+ jb2_adt_segment_end_of_stripe.c \
+ jb2_adt_segment_generic_region.c \
+ jb2_adt_segment_halftone_region.c \
+ jb2_adt_segment_page_info.c \
+ jb2_adt_segment_pattern_dict.c \
+ jb2_adt_segment_region.c \
+ jb2_adt_segment_symbol_dict.c \
+ jb2_adt_segment_table.c \
+ jb2_adt_segment_text_region.c \
+ jb2_adt_segment_types.c \
+ jb2_adt_stack.c \
+ jb2_adt_symbol.c \
+ jb2_adt_symbol_dict.c \
+ jb2_adt_symbol_unify.c \
+ jb2_adt_write_bits.c \
+ jb2_adt_write_data.c \
+ jb2_adt_write_pdf.c \
+ jb2_common.c \
+ jb2_license_dummy.c
+
+LURATECH_OBJ := $(addprefix $(LURATECH_OUT)/, $(LURATECH_SRC:%.c=%.o))
+
+$(LURATECH_OUT):
+ $(MKDIR_CMD)
+$(LURATECH_OUT)/%.o: $(LURATECH_DIR)/ldf_jb2/source/common/%.c | $(LURATECH_OUT)
+ $(CC_CMD) \
+ -I$(LURATECH_DIR)/ldf_jb2/source/libraries \
+ -I$(LURATECH_DIR)/ldf_jb2/source/compress \
+ -I$(LURATECH_DIR)/ldf_jb2/source/common \
+ -DLINUX
+$(LURATECH_OUT)/%.o: $(LURATECH_DIR)/ldf_jb2/source/compress/%.c | $(LURATECH_OUT)
+ $(CC_CMD) \
+ -I$(LURATECH_DIR)/ldf_jb2/source/libraries \
+ -I$(LURATECH_DIR)/ldf_jb2/source/compress \
+ -I$(LURATECH_DIR)/ldf_jb2/source/common \
+ -DLINUX
+$(LURATECH_OUT)/%.o: $(LURATECH_DIR)/lwf_jp2/%.c | $(LURATECH_OUT)
+ $(CC_CMD) \
+ -I$(LURATECH_DIR)/ldf_jb2/source/libraries \
+ -I$(LURATECH_DIR)/ldf_jb2/source/compress \
+ -I$(LURATECH_DIR)/ldf_jb2/source/common \
+ -DLINUX
+
+LURATECH_CFLAGS := -I$(LURATECH_DIR)/ldf_jb2/source/libraries -DHAVE_LURATECH
+
+else # --- LURATECH ---
+
# --- JBIG2DEC ---
ifneq "$(wildcard $(JBIG2DEC_DIR)/README)" ""
@@ -192,7 +297,7 @@ JBIG2DEC_SRC := \
jbig2_refinement.c \
jbig2_segment.c \
jbig2_symbol_dict.c \
- jbig2_text.c \
+ jbig2_text.c
JBIG2DEC_OBJ := $(addprefix $(JBIG2DEC_OUT)/, $(JBIG2DEC_SRC:%.c=%.o))
@@ -207,6 +312,8 @@ JBIG2DEC_CFLAGS := $(SYS_JBIG2DEC_CFLAGS)
JBIG2DEC_LIBS := $(SYS_JBIG2DEC_LIBS)
endif
+endif # --- LURATECH ---
+
# --- JPEG library from IJG ---
ifneq "$(wildcard $(JPEG_DIR)/README)" ""
diff --git a/include/mupdf/fitz/filter.h b/include/mupdf/fitz/filter.h
index 8d5c6eb2..b5fcf525 100644
--- a/include/mupdf/fitz/filter.h
+++ b/include/mupdf/fitz/filter.h
@@ -27,7 +27,7 @@ fz_stream *fz_open_lzwd(fz_context *ctx, fz_stream *chain, int early_change, int
fz_stream *fz_open_predict(fz_context *ctx, fz_stream *chain, int predictor, int columns, int colors, int bpc);
fz_stream *fz_open_jbig2d(fz_context *ctx, fz_stream *chain, fz_jbig2_globals *globals);
-fz_jbig2_globals *fz_load_jbig2_globals(fz_context *ctx, unsigned char *data, int size);
+fz_jbig2_globals *fz_load_jbig2_globals(fz_context *ctx, fz_buffer *buf);
void fz_drop_jbig2_globals_imp(fz_context *ctx, fz_storable *globals);
#endif
diff --git a/source/fitz/filter-jbig2.c b/source/fitz/filter-jbig2.c
index 8aec1d67..b90f1d42 100644
--- a/source/fitz/filter-jbig2.c
+++ b/source/fitz/filter-jbig2.c
@@ -1,5 +1,252 @@
#include "mupdf/fitz.h"
+#ifdef HAVE_LURATECH
+
+#include <ldf_jb2.h>
+
+typedef struct fz_jbig2d_s fz_jbig2d;
+
+struct fz_jbig2_globals_s
+{
+ fz_storable storable;
+ fz_buffer *buf;
+};
+
+struct fz_jbig2d_s
+{
+ fz_stream *chain;
+ fz_context *ctx;
+ fz_jbig2_globals *gctx;
+ JB2_Handle_Document doc;
+ ulong width;
+ ulong height;
+ int stride;
+ fz_buffer *input;
+ unsigned char *output;
+ int idx;
+};
+
+static void
+fz_drop_jbig2_globals(fz_context *ctx, fz_jbig2_globals *globals)
+{
+ fz_drop_storable(ctx, &globals->storable);
+}
+
+static void
+close_jbig2d(fz_context *ctx, void *state_)
+{
+ fz_jbig2d *state = state_;
+ fz_free(ctx, state->output);
+ if (state->gctx)
+ fz_drop_jbig2_globals(ctx, state->gctx);
+ fz_drop_stream(ctx, state->chain);
+ fz_free(ctx, state);
+}
+
+static void * JB2_Callback
+jbig2_alloc(unsigned long size, void *userdata)
+{
+ fz_jbig2d *state = userdata;
+ return fz_malloc(state->ctx, size);
+}
+
+static JB2_Error JB2_Callback
+jbig2_free(void *ptr, void *userdata)
+{
+ fz_jbig2d *state = userdata;
+ fz_free(state->ctx, ptr);
+ return cJB2_Error_OK;
+}
+
+static void JB2_Callback
+jbig2_message(const char *msg, JB2_Message_Level level, void *userdata)
+{
+ fz_jbig2d *state = userdata;
+
+ if (msg != NULL && msg[0] != '\0')
+ switch (level)
+ {
+ case cJB2_Message_Information:
+#ifndef NDEBUG
+ fz_warn(state->ctx, "luratech jbig2 info: %s", msg);
+#endif
+ break;
+ case cJB2_Message_Warning:
+ fz_warn(state->ctx, "luratech jbig2 warning: %s", msg);
+ break;
+ case cJB2_Message_Error:
+ fz_warn(state->ctx, "luratech jbig2 error: %s", msg);
+ break;
+ default:
+ fz_warn(state->ctx, "luratech jbig2 message: %s", msg);
+ break;
+ }
+}
+
+
+static JB2_Size_T JB2_Callback
+jbig2_read(unsigned char *buf, JB2_Size_T offset, JB2_Size_T size, void *userdata)
+{
+ fz_jbig2d *state = userdata;
+ int available;
+
+ /* globals data */
+ if (state->gctx && offset < state->gctx->buf->len)
+ {
+ available = fz_mini(state->gctx->buf->len - offset, size);
+ memcpy(buf, state->gctx->buf->data + offset, available);
+ return available;
+ }
+
+ /* image data */
+ if (state->gctx)
+ offset -= state->gctx->buf->len;
+ if (state->input->len <= offset)
+ return 0;
+ available = fz_mini(state->input->len - offset, size);
+ memcpy(buf, state->input->data + offset, available);
+ return available;
+}
+
+static JB2_Error JB2_Callback
+jbig2_write(unsigned char *buf, unsigned long row, unsigned long width, unsigned long bpp, void *userdata)
+{
+ fz_jbig2d *state = userdata;
+ int stride = (width + 7) >> 3;
+ unsigned char *dp = state->output + row * stride;
+
+ if (row >= state->height)
+ {
+ fz_warn(state->ctx, "row %lu outside of image", row);
+ return cJB2_Error_OK;
+ }
+
+ while (stride--)
+ *(dp++) = *(buf++) ^ 0xff;
+
+ return cJB2_Error_OK;
+}
+
+static int
+next_jbig2d(fz_context *ctx, fz_stream *stm, int len)
+{
+ fz_jbig2d *state = stm->state;
+ JB2_Error err;
+ JB2_Scaling_Factor scale = {1, 1};
+ JB2_Rect rect = {0, 0, 0, 0};
+
+ if (!state->output)
+ {
+ fz_try(ctx)
+ {
+ state->input = fz_read_all(state->ctx, state->chain, 0);
+
+ err = JB2_Document_Start(&state->doc,
+ jbig2_alloc, state,
+ jbig2_free, state,
+ jbig2_read, state,
+ jbig2_message, state);
+ if (err != cJB2_Error_OK)
+ fz_throw(ctx, FZ_ERROR_GENERIC, "cannot open image: %d", (int) err);
+
+#if defined(JB2_LICENSE_NUM_1) && defined(JB2_LICENSE_NUM_2)
+ err = JB2_Document_Set_License(doc, JB2_LICENSE_NUM_1, JB2_LICENSE_NUM_2);
+ if (err != cJB2_Error_OK)
+ fz_throw(ctx, FZ_ERROR_GENERIC, "cannot set license: %d", (int) err);
+#endif
+
+ err = JB2_Document_Set_Page(state->doc, 0);
+ if (err != cJB2_Error_OK)
+ fz_throw(ctx, FZ_ERROR_GENERIC, "cannot select page: %d", (int) err);
+
+ err = JB2_Document_Get_Property(state->doc, cJB2_Prop_Page_Width, &state->width);
+ if (err != cJB2_Error_OK)
+ fz_throw(ctx, FZ_ERROR_GENERIC, "cannot get page width: %d", (int) err);
+ err = JB2_Document_Get_Property(state->doc, cJB2_Prop_Page_Height, &state->height);
+ if (err != cJB2_Error_OK)
+ fz_throw(ctx, FZ_ERROR_GENERIC, "cannot get page height: %d", (int) err);
+
+ state->stride = (state->width + 7) >> 3;
+ stm->pos = state->stride * state->height;
+ state->output = fz_malloc(state->ctx, stm->pos);
+ stm->rp = state->output;
+ stm->wp = state->output;
+
+ err = JB2_Document_Decompress_Page(state->doc, scale, rect, jbig2_write, state);
+ if (err != cJB2_Error_OK)
+ fz_throw(ctx, FZ_ERROR_GENERIC, "cannot decode image: %d", (int) err);
+
+ /* update wp last. rp == wp upon errors above, causing
+ subsequent EOFs in comparison below */
+ stm->wp += stm->pos;
+ }
+ fz_always(ctx)
+ {
+ fz_drop_buffer(ctx, state->input);
+ JB2_Document_End(&state->doc);
+ }
+ fz_catch(ctx)
+ {
+ fz_rethrow(ctx);
+ }
+ }
+
+ if (stm->rp == stm->wp)
+ return EOF;
+ return *stm->rp++;
+}
+
+fz_jbig2_globals *
+fz_load_jbig2_globals(fz_context *ctx, fz_buffer *buf)
+{
+ fz_jbig2_globals *globals = fz_malloc_struct(ctx, fz_jbig2_globals);
+
+ FZ_INIT_STORABLE(globals, 1, fz_drop_jbig2_globals_imp);
+ globals->buf = fz_keep_buffer(ctx, buf);
+
+ return globals;
+}
+
+void
+fz_drop_jbig2_globals_imp(fz_context *ctx, fz_storable *globals_)
+{
+ fz_jbig2_globals *globals = (fz_jbig2_globals *)globals_;
+ fz_drop_buffer(ctx, globals->buf);
+ fz_free(ctx, globals);
+}
+
+fz_stream *
+fz_open_jbig2d(fz_context *ctx, fz_stream *chain, fz_jbig2_globals *globals)
+{
+ fz_jbig2d *state = NULL;
+
+ fz_var(state);
+
+ fz_try(ctx)
+ {
+ state = fz_malloc_struct(ctx, fz_jbig2d);
+ state->ctx = ctx;
+ state->gctx = globals;
+ state->chain = chain;
+ state->idx = 0;
+ state->output = NULL;
+ state->doc = NULL;
+ fz_warn(ctx, "opening jbig2");
+ }
+ fz_catch(ctx)
+ {
+ if (state)
+ fz_drop_jbig2_globals(ctx, state->gctx);
+ fz_free(ctx, state);
+ fz_drop_stream(ctx, chain);
+ fz_rethrow(ctx);
+ }
+
+ return fz_new_stream(ctx, state, next_jbig2d, close_jbig2d);
+}
+
+#else /* HAVE_LURATECH */
+
#include <jbig2.h>
typedef struct fz_jbig2d_s fz_jbig2d;
@@ -29,7 +276,7 @@ fz_drop_jbig2_globals(fz_context *ctx, fz_jbig2_globals *globals)
static void
close_jbig2d(fz_context *ctx, void *state_)
{
- fz_jbig2d *state = (fz_jbig2d *)state_;
+ fz_jbig2d *state = state_;
if (state->page)
jbig2_release_page(state->ctx, state->page);
if (state->gctx)
@@ -98,12 +345,12 @@ error_callback(void *data, const char *msg, Jbig2Severity severity, int32_t seg_
}
fz_jbig2_globals *
-fz_load_jbig2_globals(fz_context *ctx, unsigned char *data, int size)
+fz_load_jbig2_globals(fz_context *ctx, fz_buffer *buf)
{
fz_jbig2_globals *globals = fz_malloc_struct(ctx, fz_jbig2_globals);
Jbig2Ctx *jctx = jbig2_ctx_new(NULL, JBIG2_OPTIONS_EMBEDDED, NULL, error_callback, ctx);
- jbig2_data_in(jctx, data, size);
+ jbig2_data_in(jctx, buf->data, buf->len);
FZ_INIT_STORABLE(globals, 1, fz_drop_jbig2_globals_imp);
globals->gctx = jbig2_make_global_ctx(jctx);
@@ -129,7 +376,6 @@ fz_open_jbig2d(fz_context *ctx, fz_stream *chain, fz_jbig2_globals *globals)
fz_try(ctx)
{
state = fz_malloc_struct(ctx, fz_jbig2d);
- state->ctx = NULL;
state->gctx = globals;
state->chain = chain;
state->ctx = jbig2_ctx_new(NULL, JBIG2_OPTIONS_EMBEDDED, globals ? globals->gctx : NULL, error_callback, ctx);
@@ -151,3 +397,5 @@ fz_open_jbig2d(fz_context *ctx, fz_stream *chain, fz_jbig2_globals *globals)
return fz_new_stream(ctx, state, next_jbig2d, close_jbig2d);
}
+
+#endif /* HAVE_LURATECH */
diff --git a/source/pdf/pdf-stream.c b/source/pdf/pdf-stream.c
index 167db0a7..6e64ae63 100644
--- a/source/pdf/pdf-stream.c
+++ b/source/pdf/pdf-stream.c
@@ -69,7 +69,7 @@ pdf_load_jbig2_globals(fz_context *ctx, pdf_document *doc, pdf_obj *dict)
fz_try(ctx)
{
buf = pdf_load_stream(ctx, doc, pdf_to_num(ctx, dict), pdf_to_gen(ctx, dict));
- globals = fz_load_jbig2_globals(ctx, buf->data, buf->len);
+ globals = fz_load_jbig2_globals(ctx, buf);
pdf_store_item(ctx, dict, globals, buf->len);
}
fz_always(ctx)