-- needs rgb functions and rectangle functions function shrinkRectangle(r, img) local w, h = img.width, img.height local function g(x, y) return bright(rgb(img.getInt(x, y))) end local x1, y1, x2, y2 = r.x, r.y, r.x+r.width, r.y+r.height -- left side while x1 < x2 do for y=y1, y2-1 do if g(x1, y) <= 0.5 then goto end1 end end x1=x1+1 end ::end1:: -- top side while y1 < y2 do for x=x1, x2-1 do if g(x, y1) <= 0.5 then goto end2 end end y1=y1+1 end ::end2:: -- right side while x2 > x1 do for y=y1, y2-1 do if g(x2-1, y) <= 0.5 then goto end3 end end x2=x2-1 end ::end3:: -- bottom side while y2 > y1 do for x=x1, x2-1 do if g(x, y2-1) <= 0.5 then goto end4 end end y2=y2-1 end ::end4:: if x2 <= x1 or y2 <= y1 then return r else return newRectangle(x1, y1, x2-x1, y2-y1) end end