diff --git a/blitbuffer.c b/blitbuffer.c index 1a8253967..c8a51de7f 100644 --- a/blitbuffer.c +++ b/blitbuffer.c @@ -93,26 +93,53 @@ static int blitToBuffer(lua_State *L) { int x, y; // check bounds + if(ydest < 0) { + // negative ydest, try to compensate + if(ydest + h > 0) { + // shrink h by negative dest offset + h += ydest; + // extend source offset + yoffs += -ydest; + ydest = 0; + } else { + // effectively no height + return 0; + } + } else if(ydest >= dst->h) { + // we're told to paint to off-bound target coords + return 0; + } + if(ydest + h > dst->h) { + // clamp height if too large for target size + h = dst->h - ydest; + } if(yoffs >= src->h) { + // recalculated source offset is out of bounds return 0; } else if(yoffs + h > src->h) { + // clamp height if too large for source size h = src->h - yoffs; } - if(ydest >= dst->h) { + // same stuff for x coords: + if(xdest < 0) { + if(xdest + w > 0) { + w += xdest; + xoffs += -xdest; + xdest = 0; + } else { + return 0; + } + } else if(xdest >= dst->w) { return 0; - } else if(ydest + h > dst->h) { - h = dst->h - ydest; + } + if(xdest + w > dst->w) { + w = dst->w - xdest; } if(xoffs >= src->w) { return 0; } else if(xoffs + w > src->w) { w = src->w - xoffs; } - if(xdest >= dst->w) { - return 0; - } else if(xdest + w > dst->w) { - w = dst->w - xdest; - } uint8_t *dstptr; uint8_t *srcptr;