Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

26
LINES

< > BotCompany Repo | #380 // hashImage (include)

Lua code

1  
get("#356") -- num2hex
2  
get("#348") -- rgb functions
3  
4  
function hashImage(img)
5  
  local hash = 0
6  
  local w, h = img.width, img.height
7  
  
8  
  for y = 0, h-1 do
9  
    for x = 0, w-1 do
10  
      local pix = img.getInt(x, y)
11  
      local r, g, b = rgb(pix)
12  
      local p = r+g*33+b*71
13  
      local scale = bit32.band(y*w+x, 0xFFFF)+1
14  
      --hash = (hash * a + pix) % 0x100000000
15  
      --a = (a * b) % 0x100000000
16  
      local arg = (p+1)*scale
17  
      hash = bit32.bxor(hash, arg)
18  
      --[[if x < 10 then
19  
        print("scale", scale, "pix", pix, "p", p, "arg", arg, "hash", hash)
20  
      end]]
21  
    end
22  
    --print("y", y, "hash", hash)
23  
  end
24  
  
25  
  return num2hex(hash, 8)
26  
end

Author comment

Probably fairly crappy (might cause hash collisions), but I guess it can be used for now

test run  test run with input  download  show line numbers   

Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #380
Snippet name: hashImage (include)
Eternal ID of this version: #380/1
Text MD5: 0137480d9879d478d1cbaea509066e51
Author: stefan
Category: hash functions
Type: Lua code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-02-17 20:35:12
Source code size: 681 bytes / 26 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 898 / 273
Referenced in: [show references]