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

53
LINES

< > BotCompany Repo | #1035844 // HashedImage2B - immutable Image2B with 64 bit contents hash. untested

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

Transpiled version (11240L) is out of date.

sclass HashedImage2B is IBinaryImage, MakesImage2B {
  Image2B image; // don't change this afterwards...
  long hash;
  bool hashComputed;
  
  *(BufferedImage image) { this(new Image2B(image)); }
  *(Image2B *image) { assertNotNull(image); }
  
  long hash() {
    while (!hashComputed) {
      synchronized {
        if (!hashComputed) {
          hash = hashBinaryImage(image);
          set hashComputed;
        }
      }
    }
    
    ret hash;
  }
  
  equals HashedImage2B o {
    if (hash() != o.hash()) false;
    if (!binaryImagesIdentical(image, o.image)) {
      reportHashCollision(this, o);
      false;
    }
    true;
  }
  
  public int hashCode() { ret (int) hash(); }
  
  public Image2B get aka getImage2B() { ret image; }
  
  public int getWidth() { ret image.getWidth(); }
  public int getHeight() { ret image.getHeight(); }
  public BufferedImage getBufferedImage() { ret image.getBufferedImage(); }
  
  // check that Image2B wasn't modified
  void verify {
    if (hash != 0)
      assertEquals(hash, hashBinaryImage(image));
  }
  
  S classNameForPrinting() { ret cnfp(this, selfType.class, "HashedImage2B"; }
  
  toString { ret formatFunctionCall(classNameForPrinting(),
    w() + "*" + h() + "px", "hash " + longToHex(hash())); }
  
  // implement IBinaryImage
  
  public bool getBoolPixel(int x, int y) { ret image.getBoolPixel(x, y); }
}

Author comment

Began life as a copy of #1034818

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): elmgxqgtpvxh, mqqgnosmbjvj, wnsclhtenguj

No comments. add comment

Snippet ID: #1035844
Snippet name: HashedImage2B - immutable Image2B with 64 bit contents hash. untested
Eternal ID of this version: #1035844/4
Text MD5: 3aa6fab80b8e0dfaceb89f778c4e5948
Author: stefan
Category: javax / imaging
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-08-02 20:58:08
Source code size: 1425 bytes / 53 lines
Pitched / IR pitched: No / No
Views / Downloads: 55 / 92
Version history: 3 change(s)
Referenced in: [show references]