-- maximum luminance difference
-- NOT OPTIMIZED!
-- SLOW!
-- search for maximum luminance difference and replace current pixel
-- with pixel having max lum difference
-- author: Siniša Petrić http://www.sigmapi-design.com, 2008-2014

-- create images/process objects
imgA = TLuaImageWrap(inputImage)
imgB = TLuaImageWrap(outputImage) 
proC = TLuaProcessWrap(procObj)

size = 2
function maxlumdiff (x,y, size)
   local i, j, x_pos, y_pos, lum, lum_max, lum_center
   x_pos, y_pos = x, y   
   lum_max = 0  
   lum_center =   imgA:GetImageLuminance(x, y)
   for i=-size,size do
       for j=-size,size do
       lum = math.abs(imgA:GetImageLuminance(x+j, y+i) - lum_center)
       if lum > lum_max then
            lum_max = lum
            x_pos, y_pos = x + j, y + i                      
         end   
      end
   end
   return x_pos, y_pos
end
--
eos = false
x = 0
y = 0      
iWidth, iHeight = imgA:GetImageSize()
total = iHeight   
done = 0
while (not eos) do
   new_x, new_y = maxlumdiff(x, y, size)
   b, g, r = imgA:GetImageBGR(new_x, new_y)
   imgB:SetScanBGR(b, g, r)   
   eos, x, y = imgB:ScanNext()      
   if (y > done) then
      proC:ShowProgress(y, total)
      done = y
   end
end   
                    