function rgb(i) local r = bit32.band(bit32.rshift(i, 16), 255) local g = bit32.band(bit32.rshift(i, 8), 255) local b = bit32.band(i, 255) return r, g, b end function bright(r, g, b) return (r+g+b)/(255*3) end function map(list, f) local tbl2 = {} for _, x in ipairs(list) do table.insert(tbl2, f(x)) end return tbl2 end function segment2string(s) local x1, x2 = unpack(s) return x1.."-"..x2 end w, h = img.width, img.height brightnessThreshold = 0.9 function isEmpty(x) for y=0, h-1 do if bright(rgb(img.getInt(x, y))) <= brightnessThreshold then return false end end return true end lastEmpty = true segments = {} for x=0, w-1 do empty = isEmpty(x) if empty and not lastEmpty then table.insert(segments, {start, x}) elseif not empty and lastEmpty then start = x end lastEmpty = empty end if not lastEmpty then table.insert(segments, {start, w}) end if #segments >= 2 then -- don't bother with only 1 segment return "Horizontal segments: "..table.concat(map(segments, segment2string), ", ") end