From a34c833606d15a9a77f390cf99c7598ae81374ba Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Mon, 18 Jun 2012 21:06:51 +0200 Subject: Fix indentation and white space errors. --- fitz/image_png.c | 2 +- fitz/res_pixmap.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'fitz') diff --git a/fitz/image_png.c b/fitz/image_png.c index 9e3f39c5..f28203ee 100644 --- a/fitz/image_png.c +++ b/fitz/image_png.c @@ -192,7 +192,7 @@ png_deinterlace(struct info *info, int *passw, int *passh, int *passofs) for (p = 0; p < 7; p++) { - unsigned char *sp = info->samples + (unsigned int)(passofs[p]); + unsigned char *sp = info->samples + (unsigned int)(passofs[p]); int w = passw[p]; int h = passh[p]; diff --git a/fitz/res_pixmap.c b/fitz/res_pixmap.c index b7cf5f58..0fb3cdad 100644 --- a/fitz/res_pixmap.c +++ b/fitz/res_pixmap.c @@ -136,7 +136,7 @@ fz_pixmap_height(fz_context *ctx, fz_pixmap *pix) void fz_clear_pixmap(fz_context *ctx, fz_pixmap *pix) { - memset(pix->samples, 0, (unsigned int)(pix->w * pix->h * pix->n)); + memset(pix->samples, 0, (unsigned int)(pix->w * pix->h * pix->n)); } void @@ -223,7 +223,7 @@ fz_copy_pixmap_rect(fz_context *ctx, fz_pixmap *dest, fz_pixmap *src, fz_bbox r) for (x = w; x > 0; x--) { int v; - v = *srcp++; + v = *srcp++; v += *srcp++; v += *srcp++; *destp++ = (unsigned char)((v+1)/3); -- cgit v1.2.3 From f4eb518ea74189d75f6603f49e29bc49068b73b2 Mon Sep 17 00:00:00 2001 From: Robin Watts Date: Fri, 22 Jun 2012 16:24:32 +0100 Subject: Fix a char vs unsigned char warning. --- fitz/stm_buffer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'fitz') diff --git a/fitz/stm_buffer.c b/fitz/stm_buffer.c index d14b738f..7a676a67 100644 --- a/fitz/stm_buffer.c +++ b/fitz/stm_buffer.c @@ -170,7 +170,7 @@ fz_buffer_printf(fz_context *ctx, fz_buffer *buffer, char *fmt, ...) while(buffer->cap - buffer->len < 256) fz_grow_buffer(ctx, buffer); - buffer->len += vsprintf(buffer->data + buffer->len, fmt, args); + buffer->len += vsprintf((char *)buffer->data + buffer->len, fmt, args); va_end(args); } -- cgit v1.2.3 From 767b08e66fef57327b9a2af023207b97300d42e2 Mon Sep 17 00:00:00 2001 From: Sebastian Rasmussen Date: Fri, 22 Jun 2012 01:29:17 +0200 Subject: Warning fixes and various clean ups: Remove unused variable, silencing compiler warning. No need to initialize variables twice. Remove initialization of unread variable. Remove unnecessary check for NULL. Close output file upon error in cmapdump. --- fitz/base_hash.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'fitz') diff --git a/fitz/base_hash.c b/fitz/base_hash.c index 4ba02f4d..ccdffe63 100644 --- a/fitz/base_hash.c +++ b/fitz/base_hash.c @@ -138,7 +138,7 @@ static void fz_resize_hash(fz_context *ctx, fz_hash_table *table, int newsize) { fz_hash_entry *oldents = table->ents; - fz_hash_entry *newents = table->ents; + fz_hash_entry *newents; int oldsize = table->size; int oldload = table->load; int i; -- cgit v1.2.3 From 69b926f318b8be17d72345eb32870946e9bd40a0 Mon Sep 17 00:00:00 2001 From: Sebastian Rasmussen Date: Sun, 24 Jun 2012 01:18:21 +0200 Subject: Fix incorrect assignment in unused bitmap accessor. --- fitz/res_bitmap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'fitz') diff --git a/fitz/res_bitmap.c b/fitz/res_bitmap.c index 1bd2827d..4b871c23 100644 --- a/fitz/res_bitmap.c +++ b/fitz/res_bitmap.c @@ -117,5 +117,5 @@ void fz_bitmap_details(fz_bitmap *bit, int *w, int *h, int *n, int *stride) if (n) *n = bit->n; if (stride) - *w = bit->stride; + *stride = bit->stride; } -- cgit v1.2.3 From 50cc85d465a631c3bdbf204e6ab8bc7c97431eb6 Mon Sep 17 00:00:00 2001 From: Robin Watts Date: Wed, 27 Jun 2012 18:04:57 +0100 Subject: Make ASCII85 decoding more tolerant of end of stream errors. This solves the normal_87.pdf rendering issues. --- fitz/filt_basic.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'fitz') diff --git a/fitz/filt_basic.c b/fitz/filt_basic.c index 7d504f29..ef6dc920 100644 --- a/fitz/filt_basic.c +++ b/fitz/filt_basic.c @@ -350,7 +350,11 @@ read_a85d(fz_stream *stm, unsigned char *buf, int len) case 0: break; case 1: - fz_throw(stm->ctx, "partial final byte in a85d"); + /* Specifically illegal in the spec, but adobe + * and gs both cope. See normal_87.pdf for a + * case where this matters. */ + fz_warn(stm->ctx, "partial final byte in a85d"); + break; case 2: word = word * (85 * 85 * 85) + 0xffffff; state->bp[0] = word >> 24; -- cgit v1.2.3 From bf9d830adbca9978f82439af7f9b742d00d97214 Mon Sep 17 00:00:00 2001 From: Robin Watts Date: Wed, 27 Jun 2012 19:43:37 +0100 Subject: Fix clipping of stroked text seen in displaylist cases. When calculating the displaylist node rectangles, we were failing to adjust for linewidth/mitrewidth etc. This could result in glyphs being clipped; see normal_130.pdf for example. --- fitz/dev_list.c | 2 ++ fitz/fitz-internal.h | 1 + fitz/res_path.c | 30 ++++++++++++++++++++---------- 3 files changed, 23 insertions(+), 10 deletions(-) (limited to 'fitz') diff --git a/fitz/dev_list.c b/fitz/dev_list.c index d84fb15b..560fb5c6 100644 --- a/fitz/dev_list.c +++ b/fitz/dev_list.c @@ -334,6 +334,7 @@ fz_list_stroke_text(fz_device *dev, fz_text *text, fz_stroke_state *stroke, fz_m fz_try(ctx) { node->rect = fz_bound_text(dev->ctx, text, ctm); + fz_adjust_rect_for_stroke(&node->rect, stroke, &ctm); node->item.text = fz_clone_text(dev->ctx, text); node->stroke = fz_keep_stroke_state(dev->ctx, stroke); } @@ -377,6 +378,7 @@ fz_list_clip_stroke_text(fz_device *dev, fz_text *text, fz_stroke_state *stroke, fz_try(ctx) { node->rect = fz_bound_text(dev->ctx, text, ctm); + fz_adjust_rect_for_stroke(&node->rect, stroke, &ctm); node->item.text = fz_clone_text(dev->ctx, text); node->stroke = fz_keep_stroke_state(dev->ctx, stroke); } diff --git a/fitz/fitz-internal.h b/fitz/fitz-internal.h index 2f7f8041..6fc40f49 100644 --- a/fitz/fitz-internal.h +++ b/fitz/fitz-internal.h @@ -814,6 +814,7 @@ void fz_transform_path(fz_context *ctx, fz_path *path, fz_matrix transform); fz_path *fz_clone_path(fz_context *ctx, fz_path *old); fz_rect fz_bound_path(fz_context *ctx, fz_path *path, fz_stroke_state *stroke, fz_matrix ctm); +void fz_adjust_rect_for_stroke(fz_rect *r, fz_stroke_state *stroke, fz_matrix *ctm); void fz_print_path(fz_context *ctx, FILE *out, fz_path *, int indent); fz_stroke_state *fz_new_stroke_state(fz_context *ctx); diff --git a/fitz/res_path.c b/fitz/res_path.c index e3f07192..b8a6a1a2 100644 --- a/fitz/res_path.c +++ b/fitz/res_path.c @@ -275,21 +275,31 @@ fz_bound_path(fz_context *ctx, fz_path *path, fz_stroke_state *stroke, fz_matrix if (stroke) { - float expand = stroke->linewidth; - if (expand == 0) - expand = 1.0f; - expand *= fz_matrix_max_expansion(ctm); - if ((stroke->linejoin == FZ_LINEJOIN_MITER || stroke->linejoin == FZ_LINEJOIN_MITER_XPS) && stroke->miterlimit > 1) - expand *= stroke->miterlimit; - r.x0 -= expand; - r.y0 -= expand; - r.x1 += expand; - r.y1 += expand; + fz_adjust_rect_for_stroke(&r, stroke, &ctm); } return r; } +void +fz_adjust_rect_for_stroke(fz_rect *r, fz_stroke_state *stroke, fz_matrix *ctm) +{ + float expand; + + if (!stroke) + return; + expand = stroke->linewidth; + if (expand == 0) + expand = 1.0f; + expand *= fz_matrix_max_expansion(*ctm); + if ((stroke->linejoin == FZ_LINEJOIN_MITER || stroke->linejoin == FZ_LINEJOIN_MITER_XPS) && stroke->miterlimit > 1) + expand *= stroke->miterlimit; + r->x0 -= expand; + r->y0 -= expand; + r->x1 += expand; + r->y1 += expand; +} + void fz_transform_path(fz_context *ctx, fz_path *path, fz_matrix ctm) { -- cgit v1.2.3 From 8c7278a14fb76140ba0a79f00f1f40aea6f2d568 Mon Sep 17 00:00:00 2001 From: Robin Watts Date: Thu, 28 Jun 2012 12:05:38 +0100 Subject: Update tiff iamge predictor to cope with 16 bits. normal_178.pdf contains a monochrome black and white image, encoded as 16bpc rgb. --- fitz/filt_predict.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'fitz') diff --git a/fitz/filt_predict.c b/fitz/filt_predict.c index e68743d3..5fbd7d1d 100644 --- a/fitz/filt_predict.c +++ b/fitz/filt_predict.c @@ -31,6 +31,7 @@ static inline int getcomponent(unsigned char *line, int x, int bpc) case 2: return (line[x >> 2] >> ( ( 3 - (x & 3) ) << 1 ) ) & 3; case 4: return (line[x >> 1] >> ( ( 1 - (x & 1) ) << 2 ) ) & 15; case 8: return line[x]; + case 16: return (line[x<<1]<<8)+line[(x<<1)+1]; } return 0; } @@ -43,6 +44,7 @@ static inline void putcomponent(unsigned char *buf, int x, int bpc, int value) case 2: buf[x >> 2] |= value << ((3 - (x & 3)) << 1); break; case 4: buf[x >> 1] |= value << ((1 - (x & 1)) << 2); break; case 8: buf[x] = value; break; + case 16: buf[x<<1] = value>>8; buf[(x<<1)+1] = value; break; } } -- cgit v1.2.3 From 12799e9be77c85eba11f70523d6ca9112266adae Mon Sep 17 00:00:00 2001 From: Robin Watts Date: Mon, 2 Jul 2012 17:58:08 +0100 Subject: Prediction filter assumes it's writing to zeroed memory. The putcomponent function assumes the function has been cleared. Simple fix to clear bytes at the start of each scanline. Problem seen with normal_217.pdf --- fitz/filt_predict.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'fitz') diff --git a/fitz/filt_predict.c b/fitz/filt_predict.c index 5fbd7d1d..ebd712fd 100644 --- a/fitz/filt_predict.c +++ b/fitz/filt_predict.c @@ -63,9 +63,11 @@ fz_predict_tiff(fz_predict *state, unsigned char *out, unsigned char *in, int le { int left[MAXC]; int i, k; + const int mask = (1 << state->bpc)-1; for (k = 0; k < state->colors; k++) left[k] = 0; + memset(out, 0, state->stride); for (i = 0; i < state->columns; i++) { @@ -73,7 +75,7 @@ fz_predict_tiff(fz_predict *state, unsigned char *out, unsigned char *in, int le { int a = getcomponent(in, i * state->colors + k, state->bpc); int b = a + left[k]; - int c = b % (1 << state->bpc); + int c = b & mask; putcomponent(out, i * state->colors + k, state->bpc, c); left[k] = c; } -- cgit v1.2.3 From 05908e59a3db355cd8cbaab99406bb401d3afa2d Mon Sep 17 00:00:00 2001 From: Robin Watts Date: Fri, 29 Jun 2012 17:32:59 +0100 Subject: Bug 693160: Fix bug in fz_write_buffer_bits. When writing few enough bits that they would fit into the 'spare' bits in the last byte, I was failing to update the buffer. Also, I was failing to grow the buffer enough, and calculating the wrong number of bits left over in various places. Both fixed here. Thanks to Robert Jedrzejczyk and Sebras! --- fitz/stm_buffer.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 66 insertions(+), 6 deletions(-) (limited to 'fitz') diff --git a/fitz/stm_buffer.c b/fitz/stm_buffer.c index 7a676a67..706be39a 100644 --- a/fitz/stm_buffer.c +++ b/fitz/stm_buffer.c @@ -110,9 +110,13 @@ void fz_write_buffer_byte(fz_context *ctx, fz_buffer *buf, int val) void fz_write_buffer_bits(fz_context *ctx, fz_buffer *buf, int val, int bits) { - int extra; int shift; + /* Throughout this code, the invariant is that we need to write the + * bottom 'bits' bits of 'val' into the stream. On entry we assume + * that val & ((1<unused_bits = the number of unused bits in the last byte. */ - /* Extend the buffer as required before we start; that way we never - * fail part way during writing. */ + /* Find the amount we need to shift val up by so that it will be in + * the correct position to be inserted into any existing data byte. */ shift = (buf->unused_bits - bits); + + /* Extend the buffer as required before we start; that way we never + * fail part way during writing. If shift < 0, then we'll need -shift + * more bits. */ if (shift < 0) { - extra = (7-buf->unused_bits)>>3; + int extra = (7-shift)>>3; /* Round up to bytes */ fz_ensure_buffer(ctx, buf, buf->len + extra); } @@ -135,8 +143,14 @@ void fz_write_buffer_bits(fz_context *ctx, fz_buffer *buf, int val, int bits) { buf->data[buf->len-1] |= (shift >= 0 ? (((unsigned int)val)<>-shift)); if (shift >= 0) + { + /* If we were shifting up, we're done. */ + buf->unused_bits -= bits; return; - bits += shift; + } + /* The number of bits left to write is the number that didn't + * fit in this first byte. */ + bits = -shift; } /* Write any whole bytes */ @@ -149,7 +163,7 @@ void fz_write_buffer_bits(fz_context *ctx, fz_buffer *buf, int val, int bits) /* Write trailing bits (with 0's in unused bits) */ if (bits > 0) { - bits += 8; + bits = 8-bits; buf->data[buf->len++] = val<unused_bits = bits; @@ -174,3 +188,49 @@ fz_buffer_printf(fz_context *ctx, fz_buffer *buffer, char *fmt, ...) va_end(args); } + +#ifdef TEST_BUFFER_WRITE + +#define TEST_LEN 1024 + +void +fz_test_buffer_write(fz_context *ctx) +{ + fz_buffer *master = fz_new_buffer(ctx, TEST_LEN); + fz_buffer *copy = fz_new_buffer(ctx, TEST_LEN); + fz_stream *stm; + int i, j, k; + + /* Make us a dummy buffer */ + for (i = 0; i < TEST_LEN; i++) + { + master->data[i] = rand(); + } + master->len = TEST_LEN; + + /* Now copy that buffer several times, checking it for validity */ + stm = fz_open_buffer(ctx, master); + for (i = 0; i < 256; i++) + { + memset(copy->data, i, TEST_LEN); + copy->len = 0; + j = TEST_LEN * 8; + do + { + k = (rand() & 31)+1; + if (k > j) + k = j; + fz_write_buffer_bits(ctx, copy, fz_read_bits(stm, k), k); + j -= k; + } + while (j); + + if (memcmp(copy->data, master->data, TEST_LEN) != 0) + fprintf(stderr, "Copied buffer is different!\n"); + fz_seek(stm, 0, 0); + } + fz_close(stm); + fz_drop_buffer(ctx, master); + fz_drop_buffer(ctx, copy); +} +#endif -- cgit v1.2.3 From 8ff2db02dba00a0fbc53ee4c89dcab60aab181ec Mon Sep 17 00:00:00 2001 From: Robin Watts Date: Wed, 4 Jul 2012 20:10:28 +0100 Subject: Cope with negative lengths being passed to fz_open_null normal_994.pdf SEGVs due to a negative length. Simple fix to treat negative length streams as 0 length. --- fitz/filt_basic.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'fitz') diff --git a/fitz/filt_basic.c b/fitz/filt_basic.c index ef6dc920..378abe21 100644 --- a/fitz/filt_basic.c +++ b/fitz/filt_basic.c @@ -46,6 +46,8 @@ fz_open_null(fz_stream *chain, int len, int offset) struct null_filter *state; fz_context *ctx = chain->ctx; + if (len < 0) + len = 0; fz_try(ctx) { state = fz_malloc_struct(ctx, struct null_filter); -- cgit v1.2.3 From ee382eb9e03bd609bc0da95a77e7b7232d7e56d5 Mon Sep 17 00:00:00 2001 From: Robin Watts Date: Thu, 5 Jul 2012 16:56:32 +0100 Subject: Move to static inline functions from macros. Instead of using macros for min/max/abs/clamp, we move to using inline functions. These are more typesafe, and should produce equivalent code on compilers that support inline (i.e. pretty much everything we care about these days). People can always do their own macro versions if they prefer. --- fitz/base_geometry.c | 36 ++++++++++++++--------------- fitz/base_string.c | 2 +- fitz/dev_text.c | 10 ++++---- fitz/filt_basic.c | 2 +- fitz/filt_predict.c | 6 ++--- fitz/fitz.h | 63 ++++++++++++++++++++++++++++++++++++++++++++++----- fitz/res_colorspace.c | 52 +++++++++++++++++++++--------------------- fitz/res_pixmap.c | 8 +++---- fitz/res_text.c | 8 +++---- fitz/stm_open.c | 2 +- fitz/stm_read.c | 4 ++-- 11 files changed, 122 insertions(+), 71 deletions(-) (limited to 'fitz') diff --git a/fitz/base_geometry.c b/fitz/base_geometry.c index 4d83e8c2..27cc3cf3 100644 --- a/fitz/base_geometry.c +++ b/fitz/base_geometry.c @@ -1,7 +1,7 @@ #include "fitz-internal.h" -#define MAX4(a,b,c,d) MAX(MAX(a,b), MAX(c,d)) -#define MIN4(a,b,c,d) MIN(MIN(a,b), MIN(c,d)) +#define MAX4(a,b,c,d) fz_max(fz_max(a,b), fz_max(c,d)) +#define MIN4(a,b,c,d) fz_min(fz_min(a,b), fz_min(c,d)) /* Matrices, points and affine transformations */ @@ -209,10 +209,10 @@ fz_intersect_rect(fz_rect a, fz_rect b) if (fz_is_infinite_rect(b)) return a; if (fz_is_empty_rect(a)) return fz_empty_rect; if (fz_is_empty_rect(b)) return fz_empty_rect; - r.x0 = MAX(a.x0, b.x0); - r.y0 = MAX(a.y0, b.y0); - r.x1 = MIN(a.x1, b.x1); - r.y1 = MIN(a.y1, b.y1); + r.x0 = fz_max(a.x0, b.x0); + r.y0 = fz_max(a.y0, b.y0); + r.x1 = fz_min(a.x1, b.x1); + r.y1 = fz_min(a.y1, b.y1); return (r.x1 < r.x0 || r.y1 < r.y0) ? fz_empty_rect : r; } @@ -224,10 +224,10 @@ fz_union_rect(fz_rect a, fz_rect b) if (fz_is_infinite_rect(b)) return b; if (fz_is_empty_rect(a)) return b; if (fz_is_empty_rect(b)) return a; - r.x0 = MIN(a.x0, b.x0); - r.y0 = MIN(a.y0, b.y0); - r.x1 = MAX(a.x1, b.x1); - r.y1 = MAX(a.y1, b.y1); + r.x0 = fz_min(a.x0, b.x0); + r.y0 = fz_min(a.y0, b.y0); + r.x1 = fz_max(a.x1, b.x1); + r.y1 = fz_max(a.y1, b.y1); return r; } @@ -239,10 +239,10 @@ fz_intersect_bbox(fz_bbox a, fz_bbox b) if (fz_is_infinite_rect(b)) return a; if (fz_is_empty_rect(a)) return fz_empty_bbox; if (fz_is_empty_rect(b)) return fz_empty_bbox; - r.x0 = MAX(a.x0, b.x0); - r.y0 = MAX(a.y0, b.y0); - r.x1 = MIN(a.x1, b.x1); - r.y1 = MIN(a.y1, b.y1); + r.x0 = fz_maxi(a.x0, b.x0); + r.y0 = fz_maxi(a.y0, b.y0); + r.x1 = fz_mini(a.x1, b.x1); + r.y1 = fz_mini(a.y1, b.y1); return (r.x1 < r.x0 || r.y1 < r.y0) ? fz_empty_bbox : r; } @@ -254,10 +254,10 @@ fz_union_bbox(fz_bbox a, fz_bbox b) if (fz_is_infinite_rect(b)) return b; if (fz_is_empty_rect(a)) return b; if (fz_is_empty_rect(b)) return a; - r.x0 = MIN(a.x0, b.x0); - r.y0 = MIN(a.y0, b.y0); - r.x1 = MAX(a.x1, b.x1); - r.y1 = MAX(a.y1, b.y1); + r.x0 = fz_mini(a.x0, b.x0); + r.y0 = fz_mini(a.y0, b.y0); + r.x1 = fz_maxi(a.x1, b.x1); + r.y1 = fz_maxi(a.y1, b.y1); return r; } diff --git a/fitz/base_string.c b/fitz/base_string.c index 60e20ac4..fbb1cf4d 100644 --- a/fitz/base_string.c +++ b/fitz/base_string.c @@ -252,6 +252,6 @@ float fz_atof(const char *s) /* Return 1.0, as it's a small known value that won't cause a divide by 0. */ return 1.0; } - d = CLAMP(d, -FLT_MAX, FLT_MAX); + d = fz_clampd(d, -FLT_MAX, FLT_MAX); return (float)d; } diff --git a/fitz/dev_text.c b/fitz/dev_text.c index cd76830d..280d1b55 100644 --- a/fitz/dev_text.c +++ b/fitz/dev_text.c @@ -129,7 +129,7 @@ append_char(fz_context *ctx, fz_text_span *span, int c, fz_rect bbox) { if (span->len == span->cap) { - int new_cap = MAX(64, span->cap * 2); + int new_cap = fz_maxi(64, span->cap * 2); span->text = fz_resize_array(ctx, span->text, new_cap, sizeof(*span->text)); span->cap = new_cap; } @@ -155,7 +155,7 @@ append_span(fz_context *ctx, fz_text_line *line, fz_text_span *span) return; if (line->len == line->cap) { - int new_cap = MAX(8, line->cap * 2); + int new_cap = fz_maxi(8, line->cap * 2); line->spans = fz_resize_array(ctx, line->spans, new_cap, sizeof(*line->spans)); line->cap = new_cap; } @@ -176,7 +176,7 @@ append_line(fz_context *ctx, fz_text_block *block, fz_text_line *line) { if (block->len == block->cap) { - int new_cap = MAX(16, block->cap * 2); + int new_cap = fz_maxi(16, block->cap * 2); block->lines = fz_resize_array(ctx, block->lines, new_cap, sizeof *block->lines); block->cap = new_cap; } @@ -198,13 +198,13 @@ lookup_block_for_line(fz_context *ctx, fz_text_page *page, fz_text_line *line) float dy = line->bbox.y0 - block->bbox.y1; if (dy > -size * 1.5f && dy < size * PARAGRAPH_DIST) if (line->bbox.x0 <= block->bbox.x1 && line->bbox.x1 >= block->bbox.x0) - if (ABS(dx) < w / 2) + if (fz_abs(dx) < w / 2) return block; } if (page->len == page->cap) { - int new_cap = MAX(16, page->cap * 2); + int new_cap = fz_maxi(16, page->cap * 2); page->blocks = fz_resize_array(ctx, page->blocks, new_cap, sizeof(*page->blocks)); page->cap = new_cap; } diff --git a/fitz/filt_basic.c b/fitz/filt_basic.c index 378abe21..db95533d 100644 --- a/fitz/filt_basic.c +++ b/fitz/filt_basic.c @@ -21,7 +21,7 @@ static int read_null(fz_stream *stm, unsigned char *buf, int len) { struct null_filter *state = stm->state; - int amount = MIN(len, state->remain); + int amount = fz_mini(len, state->remain); int n; fz_seek(state->chain, state->pos, 0); diff --git a/fitz/filt_predict.c b/fitz/filt_predict.c index ebd712fd..b82c1281 100644 --- a/fitz/filt_predict.c +++ b/fitz/filt_predict.c @@ -52,9 +52,9 @@ static inline int paeth(int a, int b, int c) { /* The definitions of ac and bc are correct, not a typo. */ int ac = b - c, bc = a - c, abcc = ac + bc; - int pa = ABS(ac); - int pb = ABS(bc); - int pc = ABS(abcc); + int pa = fz_absi(ac); + int pb = fz_absi(bc); + int pc = fz_absi(abcc); return pa <= pb && pa <= pc ? a : pb <= pc ? b : c; } diff --git a/fitz/fitz.h b/fitz/fitz.h index a66976af..ba7088ae 100644 --- a/fitz/fitz.h +++ b/fitz/fitz.h @@ -40,12 +40,6 @@ #define nelem(x) (sizeof(x)/sizeof((x)[0])) -#define ABS(x) ( (x) < 0 ? -(x) : (x) ) -#define MIN(a,b) ( (a) < (b) ? (a) : (b) ) -#define MAX(a,b) ( (a) > (b) ? (a) : (b) ) -#define CLAMP(x,a,b) ( (x) > (b) ? (b) : ( (x) < (a) ? (a) : (x) ) ) -#define DIV_BY_ZERO(a, b, min, max) (((a) < 0) ^ ((b) < 0) ? (min) : (max)) - /* Some differences in libc can be smoothed over */ @@ -126,6 +120,63 @@ int gettimeofday(struct timeval *tv, struct timezone *tz); #endif #endif +/* + Some standard math functions, done as static inlines for speed. + People with compilers that do not adequately implement inlines may + like to reimplement these using macros. +*/ +static inline float fz_abs(float f) +{ + return (f < 0 ? -f : f); +} + +static inline int fz_absi(int i) +{ + return (i < 0 ? -i : i); +} + +static inline float fz_min(float a, float b) +{ + return (a < b ? a : b); +} + +static inline int fz_mini(int a, int b) +{ + return (a < b ? a : b); +} + +static inline float fz_max(float a, float b) +{ + return (a > b ? a : b); +} + +static inline int fz_maxi(int a, int b) +{ + return (a > b ? a : b); +} + +static inline float fz_clamp(float f, float min, float max) +{ + return (f > min ? (f < max ? f : max) : min); +} + +static inline int fz_clampi(int i, float min, float max) +{ + return (i > min ? (i < max ? i : max) : min); +} + +static inline double fz_clampd(double d, double min, double max) +{ + return (d > min ? (d < max ? d : max) : min); +} + +static inline void *fz_clampp(void *p, void *min, void *max) +{ + return (p > min ? (p < max ? p : max) : min); +} + +#define DIV_BY_ZERO(a, b, min, max) (((a) < 0) ^ ((b) < 0) ? (min) : (max)) + /* Contexts */ diff --git a/fitz/res_colorspace.c b/fitz/res_colorspace.c index 587db381..c0898883 100644 --- a/fitz/res_colorspace.c +++ b/fitz/res_colorspace.c @@ -130,13 +130,13 @@ static void cmyk_to_rgb(fz_context *ctx, fz_colorspace *cs, float *cmyk, float * g += 0.2119 * x; b += 0.2235 * x; - rgb[0] = CLAMP(r, 0, 1); - rgb[1] = CLAMP(g, 0, 1); - rgb[2] = CLAMP(b, 0, 1); + rgb[0] = fz_clamp(r, 0, 1); + rgb[1] = fz_clamp(g, 0, 1); + rgb[2] = fz_clamp(b, 0, 1); #else - rgb[0] = 1 - MIN(1, cmyk[0] + cmyk[3]); - rgb[1] = 1 - MIN(1, cmyk[1] + cmyk[3]); - rgb[2] = 1 - MIN(1, cmyk[2] + cmyk[3]); + rgb[0] = 1 - fz_min(1, cmyk[0] + cmyk[3]); + rgb[1] = 1 - fz_min(1, cmyk[1] + cmyk[3]); + rgb[2] = 1 - fz_min(1, cmyk[2] + cmyk[3]); #endif } @@ -146,7 +146,7 @@ static void rgb_to_cmyk(fz_context *ctx, fz_colorspace *cs, float *rgb, float *c c = 1 - rgb[0]; m = 1 - rgb[1]; y = 1 - rgb[2]; - k = MIN(c, MIN(m, y)); + k = fz_min(c, fz_min(m, y)); cmyk[0] = c - k; cmyk[1] = m - k; cmyk[2] = y - k; @@ -251,7 +251,7 @@ static void fast_rgb_to_cmyk(fz_pixmap *dst, fz_pixmap *src) unsigned char c = 255 - s[0]; unsigned char m = 255 - s[1]; unsigned char y = 255 - s[2]; - unsigned char k = MIN(c, MIN(m, y)); + unsigned char k = (unsigned char)fz_mini(c, fz_mini(m, y)); d[0] = c - k; d[1] = m - k; d[2] = y - k; @@ -272,7 +272,7 @@ static void fast_bgr_to_cmyk(fz_pixmap *dst, fz_pixmap *src) unsigned char c = 255 - s[2]; unsigned char m = 255 - s[1]; unsigned char y = 255 - s[0]; - unsigned char k = MIN(c, MIN(m, y)); + unsigned char k = (unsigned char)fz_mini(c, fz_mini(m, y)); d[0] = c - k; d[1] = m - k; d[2] = y - k; @@ -293,7 +293,7 @@ static void fast_cmyk_to_gray(fz_pixmap *dst, fz_pixmap *src) unsigned char c = fz_mul255(s[0], 77); unsigned char m = fz_mul255(s[1], 150); unsigned char y = fz_mul255(s[2], 28); - d[0] = 255 - MIN(c + m + y + s[3], 255); + d[0] = 255 - (unsigned char)fz_mini(c + m + y + s[3], 255); d[1] = s[4]; s += 5; d += 2; @@ -318,9 +318,9 @@ static void fast_cmyk_to_rgb(fz_context *ctx, fz_pixmap *dst, fz_pixmap *src) d[1] = rgb[1] * 255; d[2] = rgb[2] * 255; #else - d[0] = 255 - MIN(s[0] + s[3], 255); - d[1] = 255 - MIN(s[1] + s[3], 255); - d[2] = 255 - MIN(s[2] + s[3], 255); + d[0] = 255 - (unsigned char)fz_mini(s[0] + s[3], 255); + d[1] = 255 - (unsigned char)fz_mini(s[1] + s[3], 255); + d[2] = 255 - (unsigned char)fz_mini(s[2] + s[3], 255); #endif d[3] = s[4]; s += 5; @@ -346,9 +346,9 @@ static void fast_cmyk_to_bgr(fz_context *ctx, fz_pixmap *dst, fz_pixmap *src) d[1] = rgb[1] * 255; d[2] = rgb[0] * 255; #else - d[0] = 255 - MIN(s[2] + s[3], 255); - d[1] = 255 - MIN(s[1] + s[3], 255); - d[2] = 255 - MIN(s[0] + s[3], 255); + d[0] = 255 - (unsigned char)fz_mini(s[2] + s[3], 255); + d[1] = 255 - (unsigned char)fz_mini(s[1] + s[3], 255); + d[2] = 255 - (unsigned char)fz_mini(s[0] + s[3], 255); #endif d[3] = s[4]; s += 5; @@ -557,7 +557,7 @@ fz_std_conv_color(fz_context *ctx, fz_colorspace *srcs, float *srcv, fz_colorspa srcs->to_rgb(ctx, srcs, srcv, rgb); dsts->from_rgb(ctx, dsts, rgb, dstv); for (i = 0; i < dsts->n; i++) - dstv[i] = CLAMP(dstv[i], 0, 1); + dstv[i] = fz_clamp(dstv[i], 0, 1); } else { @@ -605,7 +605,7 @@ fz_convert_color(fz_context *ctx, fz_colorspace *ds, float *dv, fz_colorspace *s float c = 1 - sv[0]; float m = 1 - sv[1]; float y = 1 - sv[2]; - float k = MIN(c, MIN(m, y)); + float k = fz_min(c, fz_min(m, y)); dv[0] = c - k; dv[1] = m - k; dv[2] = y - k; @@ -632,7 +632,7 @@ fz_convert_color(fz_context *ctx, fz_colorspace *ds, float *dv, fz_colorspace *s float c = 1 - sv[2]; float m = 1 - sv[1]; float y = 1 - sv[0]; - float k = MIN(c, MIN(m, y)); + float k = fz_min(c, fz_min(m, y)); dv[0] = c - k; dv[1] = m - k; dv[2] = y - k; @@ -649,16 +649,16 @@ fz_convert_color(fz_context *ctx, fz_colorspace *ds, float *dv, fz_colorspace *s float c = sv[0] * 0.3f; float m = sv[1] * 0.59f; float y = sv[2] * 0.11f; - dv[0] = 1 - MIN(c + m + y + sv[3], 1); + dv[0] = 1 - fz_min(c + m + y + sv[3], 1); } else if (ds == fz_device_rgb) { #ifdef SLOWCMYK cmyk_to_rgb(ctx, NULL, sv, dv); #else - dv[0] = 1 - MIN(sv[0] + sv[3], 1); - dv[1] = 1 - MIN(sv[1] + sv[3], 1); - dv[2] = 1 - MIN(sv[2] + sv[3], 1); + dv[0] = 1 - fz_min(sv[0] + sv[3], 1); + dv[1] = 1 - fz_min(sv[1] + sv[3], 1); + dv[2] = 1 - fz_min(sv[2] + sv[3], 1); #endif } else if (ds == fz_device_bgr) @@ -670,9 +670,9 @@ fz_convert_color(fz_context *ctx, fz_colorspace *ds, float *dv, fz_colorspace *s dv[1] = rgb[1]; dv[2] = rgb[0]; #else - dv[0] = 1 - MIN(sv[2] + sv[3], 1); - dv[1] = 1 - MIN(sv[1] + sv[3], 1); - dv[2] = 1 - MIN(sv[0] + sv[3], 1); + dv[0] = 1 - fz_min(sv[2] + sv[3], 1); + dv[1] = 1 - fz_min(sv[1] + sv[3], 1); + dv[2] = 1 - fz_min(sv[0] + sv[3], 1); #endif } else diff --git a/fitz/res_pixmap.c b/fitz/res_pixmap.c index 0fb3cdad..e9dea86a 100644 --- a/fitz/res_pixmap.c +++ b/fitz/res_pixmap.c @@ -384,10 +384,10 @@ void fz_invert_pixmap_rect(fz_pixmap *image, fz_bbox rect) unsigned char *p; int x, y, n; - int x0 = CLAMP(rect.x0 - image->x, 0, image->w - 1); - int x1 = CLAMP(rect.x1 - image->x, 0, image->w - 1); - int y0 = CLAMP(rect.y0 - image->y, 0, image->h - 1); - int y1 = CLAMP(rect.y1 - image->y, 0, image->h - 1); + int x0 = fz_clampi(rect.x0 - image->x, 0, image->w - 1); + int x1 = fz_clampi(rect.x1 - image->x, 0, image->w - 1); + int y0 = fz_clampi(rect.y0 - image->y, 0, image->h - 1); + int y1 = fz_clampi(rect.y1 - image->y, 0, image->h - 1); for (y = y0; y < y1; y++) { diff --git a/fitz/res_text.c b/fitz/res_text.c index 6b5e3e3a..2bef6d4a 100644 --- a/fitz/res_text.c +++ b/fitz/res_text.c @@ -81,10 +81,10 @@ fz_bound_text(fz_context *ctx, fz_text *text, fz_matrix ctm) trm = fz_concat(tm, ctm); gbox = fz_bound_glyph(ctx, text->font, text->items[i].gid, trm); - bbox.x0 = MIN(bbox.x0, gbox.x0); - bbox.y0 = MIN(bbox.y0, gbox.y0); - bbox.x1 = MAX(bbox.x1, gbox.x1); - bbox.y1 = MAX(bbox.y1, gbox.y1); + bbox.x0 = fz_min(bbox.x0, gbox.x0); + bbox.y0 = fz_min(bbox.y0, gbox.y0); + bbox.x1 = fz_max(bbox.x1, gbox.x1); + bbox.y1 = fz_max(bbox.y1, gbox.y1); } } diff --git a/fitz/stm_open.c b/fitz/stm_open.c index be069fb9..1709340b 100644 --- a/fitz/stm_open.c +++ b/fitz/stm_open.c @@ -160,7 +160,7 @@ static void seek_buffer(fz_stream *stm, int offset, int whence) stm->rp += offset; if (whence == 2) stm->rp = stm->ep - offset; - stm->rp = CLAMP(stm->rp, stm->bp, stm->ep); + stm->rp = fz_clampp(stm->rp, stm->bp, stm->ep); stm->wp = stm->ep; } diff --git a/fitz/stm_read.c b/fitz/stm_read.c index 216d2207..848ceef1 100644 --- a/fitz/stm_read.c +++ b/fitz/stm_read.c @@ -7,7 +7,7 @@ fz_read(fz_stream *stm, unsigned char *buf, int len) { int count, n; - count = MIN(len, stm->wp - stm->rp); + count = fz_mini(len, stm->wp - stm->rp); if (count) { memcpy(buf, stm->rp, count); @@ -33,7 +33,7 @@ fz_read(fz_stream *stm, unsigned char *buf, int len) stm->pos += n; } - n = MIN(len - count, stm->wp - stm->rp); + n = fz_mini(len - count, stm->wp - stm->rp); if (n) { memcpy(buf + count, stm->rp, n); -- cgit v1.2.3 From 15fc25b0055dbdbfaf4257ac908d43fd5a2da19d Mon Sep 17 00:00:00 2001 From: Robin Watts Date: Wed, 4 Jul 2012 16:12:02 +0100 Subject: Cope with stray returns at the start of a JPEG stream. Acrobat seems to cope, we should too. See normal_439.pdf for an example. --- fitz/filt_dctd.c | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'fitz') diff --git a/fitz/filt_dctd.c b/fitz/filt_dctd.c index 23744f01..1b588d2a 100644 --- a/fitz/filt_dctd.c +++ b/fitz/filt_dctd.c @@ -101,12 +101,17 @@ read_dctd(fz_stream *stm, unsigned char *buf, int len) if (!state->init) { + int c; cinfo->client_data = state; cinfo->err = &state->errmgr; jpeg_std_error(cinfo->err); cinfo->err->error_exit = error_exit; jpeg_create_decompress(cinfo); + /* Skip over any stray returns at the start of the stream */ + while ((c = fz_peek_byte(state->chain)) == '\n' || c == '\r') + (void)fz_read_byte(state->chain); + cinfo->src = &state->srcmgr; cinfo->src->init_source = init_source; cinfo->src->fill_input_buffer = fill_input_buffer; -- cgit v1.2.3