summaryrefslogtreecommitdiff
path: root/source/fitz/filter-dct.c
diff options
context:
space:
mode:
authorRobin Watts <robin.watts@artifex.com>2014-03-11 19:04:17 +0000
committerRobin Watts <robin.watts@artifex.com>2014-03-17 19:38:40 +0000
commit551de42088c58dc69fba06fb53e36c2ddb12367f (patch)
treef073b5ac7d3dee2de2f5609a13fd9c47e605d3d8 /source/fitz/filter-dct.c
parent8fdcb140930c3027841af28a4632ce6d23aa44b6 (diff)
downloadmupdf-551de42088c58dc69fba06fb53e36c2ddb12367f.tar.xz
Rework fz_streams.
Currently fz_streams have a 4K buffer within their header. The call to read from a stream fills this buffer, resulting in more data being pulled from any underlying stream than we might like. This causes problems with the forthcoming 'leech' filter. Here we simplify the fields available in the public stream header. No specific buffer is given; simply the read and write pointers. The underlying 'read' function is replaced by a 'next' function that makes the next block of data available and returns the first character of it (or EOF). A caller to the 'next' function should supply the maximum number of bytes that it knows it will need (possibly not now, but eventually). This enables the underlying stream to efficiently decode just enough. The underlying stream is free to return fewer, or a greater number if it wants to. The exact size of the 'block' of data returned will depend on the filter in use and (possibly) the data therein. Callers can get the currently available amount of data by calling fz_available (but again should pass the maximum amount of data they know they will need). The only time this will ever return 0 is if we have hit EOF.
Diffstat (limited to 'source/fitz/filter-dct.c')
-rw-r--r--source/fitz/filter-dct.c25
1 files changed, 18 insertions, 7 deletions
diff --git a/source/fitz/filter-dct.c b/source/fitz/filter-dct.c
index 23665909..86268032 100644
--- a/source/fitz/filter-dct.c
+++ b/source/fitz/filter-dct.c
@@ -22,6 +22,8 @@ struct fz_dctd_s
struct jpeg_error_mgr errmgr;
jmp_buf jb;
char msg[JMSG_LENGTH_MAX];
+
+ unsigned char buffer[4096];
};
static void error_exit(j_common_ptr cinfo)
@@ -48,10 +50,11 @@ static boolean fill_input_buffer(j_decompress_ptr cinfo)
fz_stream *curr_stm = state->curr_stm;
fz_context *ctx = curr_stm->ctx;
+
curr_stm->rp = curr_stm->wp;
fz_try(ctx)
{
- fz_fill_buffer(curr_stm);
+ src->bytes_in_buffer = fz_available(curr_stm, 1);
}
fz_catch(ctx)
{
@@ -59,7 +62,6 @@ static boolean fill_input_buffer(j_decompress_ptr cinfo)
return 0;
}
src->next_input_byte = curr_stm->rp;
- src->bytes_in_buffer = curr_stm->wp - curr_stm->rp;
if (src->bytes_in_buffer == 0)
{
@@ -88,12 +90,16 @@ static void skip_input_data(j_decompress_ptr cinfo, long num_bytes)
}
static int
-read_dctd(fz_stream *stm, unsigned char *buf, int len)
+next_dctd(fz_stream *stm, int max)
{
fz_dctd *state = stm->state;
j_decompress_ptr cinfo = &state->cinfo;
- unsigned char *p = buf;
- unsigned char *ep = buf + len;
+ unsigned char *p = state->buffer;
+ unsigned char *ep;
+
+ if (max > sizeof(state->buffer))
+ max = sizeof(state->buffer);
+ ep = state->buffer + max;
if (setjmp(state->jb))
{
@@ -202,8 +208,13 @@ read_dctd(fz_stream *stm, unsigned char *buf, int len)
while (state->rp < state->wp && p < ep)
*p++ = *state->rp++;
}
+ stm->rp = state->buffer;
+ stm->wp = p;
+ stm->pos += (p - state->buffer);
+ if (p == stm->rp)
+ return EOF;
- return p - buf;
+ return *stm->rp++;
}
static void
@@ -267,5 +278,5 @@ fz_open_dctd(fz_stream *chain, int color_transform, int l2factor, fz_stream *jpe
fz_rethrow(ctx);
}
- return fz_new_stream(ctx, state, read_dctd, close_dctd, rebind_dctd);
+ return fz_new_stream(ctx, state, next_dctd, close_dctd, rebind_dctd);
}