brightnessThreshold = 0.9 maxSignatureLen = 10 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 -- from Java class "SegmentCounter" function countSegments(y, flip) local lastPix = 1 local segments = 0 for x = 0, (flip and h or w)-1 do local pix = bright(rgb(flip and img.getInt(y, x) or img.getInt(x, y))) if pix >= brightnessThreshold and lastPix < brightnessThreshold then segments = segments+1 end lastPix = pix end if lastPix < brightnessThreshold then segments = segments+1 end return segments end function getSegmentSignature(flip) local s = "" local lastN = 0 for y = 0, (flip and w or h)-1 do local n = math.min(countSegments(y, flip), 9) if n ~= lastN then if #s >= maxSignatureLen then return nil end s = s..n end lastN = n end if lastN == 0 and #s > 0 then s = s:sub(1, #s-1) end return s end w, h = img.width, img.height sig = getSegmentSignature(false) if sig then return "Vertical signature: "..sig end