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

39
LINES

< > BotCompany Repo | #1034798 // HashBufferedImage - makes a 64 bit hash of an image (fast but not as good as MD5)

JavaX fragment (include) [tags: use-pretranspiled]

Transpiled version (8035L) is out of date.

// The hash for an image without alpha channel is guaranteed to be
// identical for a completely opaque image with alpha.
srecord noeq HashBufferedImage(BufferedImage img) {
  long hash;
  
  long get() {
    if (img == null) ret hash;
    
    var gp = grabbableIntPixels_fastOrSlow(img);
    bool hasAlpha = hasAlpha(img);
    
    var ping = pingSource();
    int[] pixelsIn = gp.data;
    int w = gp.w, h = gp.h;
    int scanlineExtra = gp.scanlineStride-w;
    int iIn = gp.offset;

    addToHash(w);
    addToHash(h);
    
    for y to h: {
      for x to w: {
        int packed = pixelsIn[iIn++];
        if (!hasAlpha) packed |= 0xFF000000;
        addToHash(packed);
      }

      iIn += scanlineExtra;
      ping?!;
    }
    
    ret hash;
  }
  
  void addToHash(long x) {
    hash = boostHashCombine64(hash, x);
    printVars ifdef HashBufferedImage_debug(x := longToHex(x), +hash);
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1034798
Snippet name: HashBufferedImage - makes a 64 bit hash of an image (fast but not as good as MD5)
Eternal ID of this version: #1034798/8
Text MD5: 088589a5c62c2301a77bd2dd5dd1178e
Author: stefan
Category: javax / imaging
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-03-10 22:57:41
Source code size: 941 bytes / 39 lines
Pitched / IR pitched: No / No
Views / Downloads: 71 / 148
Version history: 7 change(s)
Referenced in: [show references]