summaryrefslogtreecommitdiff
path: root/fitz
diff options
context:
space:
mode:
Diffstat (limited to 'fitz')
-rw-r--r--fitz/base_geometry.c36
-rw-r--r--fitz/base_string.c2
-rw-r--r--fitz/dev_text.c10
-rw-r--r--fitz/filt_basic.c2
-rw-r--r--fitz/filt_predict.c6
-rw-r--r--fitz/fitz.h63
-rw-r--r--fitz/res_colorspace.c52
-rw-r--r--fitz/res_pixmap.c8
-rw-r--r--fitz/res_text.c8
-rw-r--r--fitz/stm_open.c2
-rw-r--r--fitz/stm_read.c4
11 files changed, 122 insertions, 71 deletions
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
*/
@@ -127,6 +121,63 @@ int gettimeofday(struct timeval *tv, struct timezone *tz);
#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);