From ca2f6cb0dfc25b944230a503da471c61ff763f7b Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Thu, 30 Jul 2009 17:37:35 +0200 Subject: Clamp to edge when sampling outside the edge of an image. Solves many bugs with 1x1 sized images used as rectangles that got rendered like gradients. --- fitzdraw/imagedraw.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'fitzdraw') diff --git a/fitzdraw/imagedraw.c b/fitzdraw/imagedraw.c index ad488431..8a7cf2cc 100644 --- a/fitzdraw/imagedraw.c +++ b/fitzdraw/imagedraw.c @@ -8,8 +8,10 @@ typedef unsigned char byte; static inline byte getcomp(byte *s, int w, int h, int u, int v, int n, int k) { - if (u < 0 || u >= w) return 0; - if (v < 0 || v >= h) return 0; + if (u < 0) u = 0; + if (v < 0) v = 0; + if (u >= w) u = w - 1; + if (v >= h) v = h - 1; return s[(w * v + u) * n + k]; } @@ -30,8 +32,10 @@ static inline int samplecomp(byte *s, int w, int h, int u, int v, int n, int k) static inline byte getmask(byte *s, int w, int h, int u, int v) { - if (u < 0 || u >= w) return 0; - if (v < 0 || v >= h) return 0; + if (u < 0) u = 0; + if (v < 0) v = 0; + if (u >= w) u = w - 1; + if (v >= h) v = h - 1; return s[w * v + u]; } @@ -60,9 +64,10 @@ static inline void lerpargb(byte *dst, byte *a, byte *b, int t) static inline byte *getargb(byte *s, int w, int h, int u, int v) { - static byte zero[4] = { 0, 0, 0, 0 }; - if (u < 0 || u >= w) return zero; - if (v < 0 || v >= h) return zero; + if (u < 0) u = 0; + if (v < 0) v = 0; + if (u >= w) u = w - 1; + if (v >= h) v = h - 1; return s + ((w * v + u) << 2); } -- cgit v1.2.3