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.

1  
sclass HashedImage2B is IBinaryImage, MakesImage2B {
2  
  Image2B image; // don't change this afterwards...
3  
  long hash;
4  
  bool hashComputed;
5  
  
6  
  *(BufferedImage image) { this(new Image2B(image)); }
7  
  *(Image2B *image) { assertNotNull(image); }
8  
  
9  
  long hash() {
10  
    while (!hashComputed) {
11  
      synchronized {
12  
        if (!hashComputed) {
13  
          hash = hashBinaryImage(image);
14  
          set hashComputed;
15  
        }
16  
      }
17  
    }
18  
    
19  
    ret hash;
20  
  }
21  
  
22  
  equals HashedImage2B o {
23  
    if (hash() != o.hash()) false;
24  
    if (!binaryImagesIdentical(image, o.image)) {
25  
      reportHashCollision(this, o);
26  
      false;
27  
    }
28  
    true;
29  
  }
30  
  
31  
  public int hashCode() { ret (int) hash(); }
32  
  
33  
  public Image2B get aka getImage2B() { ret image; }
34  
  
35  
  public int getWidth() { ret image.getWidth(); }
36  
  public int getHeight() { ret image.getHeight(); }
37  
  public BufferedImage getBufferedImage() { ret image.getBufferedImage(); }
38  
  
39  
  // check that Image2B wasn't modified
40  
  void verify {
41  
    if (hash != 0)
42  
      assertEquals(hash, hashBinaryImage(image));
43  
  }
44  
  
45  
  S classNameForPrinting() { ret cnfp(this, selfType.class, "HashedImage2B"; }
46  
  
47  
  toString { ret formatFunctionCall(classNameForPrinting(),
48  
    w() + "*" + h() + "px", "hash " + longToHex(hash())); }
49  
  
50  
  // implement IBinaryImage
51  
  
52  
  public bool getBoolPixel(int x, int y) { ret image.getBoolPixel(x, y); }
53  
}

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: 123 / 196
Version history: 3 change(s)
Referenced in: [show references]