get("#388") -- rect functions get("#137") -- split get("#390") -- msinsert get("#348") -- rgb functions _debug = false function findSymbol(img, icon, minSimilarity) -- split icon into lines if type(icon) == 'string' then icon = split(icon, "\n") end local w, h = #icon[1], #icon if _debug then print("w/h, img="..w.."/"..h..", "..img.width.."/"..img.height) end -- count non-question marks local count = 0 for yy=1, h do line = icon[yy] for xx=1, w do c = line:sub(xx, xx) if c ~= '?' then count=count+1 end end end local limit = count*25/100 if _debug then print("count/limit: "..count.."/"..limit) end for y=0, img.height-h do for x=0, img.width-w do if _debug then print("Testing "..x.."/"..y) end local total, innerPixels = 0, {} local sim for yy=y, y+h-1 do local line = icon[yy-y+1] for xx=x, x+w-1 do local pix = img.getInt(xx, yy) local c = line:sub(xx-x+1, xx-x+1) if c == '?' then msinsert(innerPixels, pix) else local b = math.floor(bright(rgb(pix))*25+0.5) local lb = string.byte(c)-string.byte("A") local diff = math.abs(b-lb) total = total+diff if total > limit then if _debug then print("limit reached") end goto out end end end end sim = math.floor(100*(1-total/(count*25))+0.5) if _debug then print("Similarity at "..x.."/"..y..": "..sim.."%") end if sim >= minSimilarity then return newRectangle(x, y, w, h) end ::out:: if _debug then print("Skipping "..x.."/"..y) end end end end