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

86
LINES

< > BotCompany Repo | #1033829 // ScanlineBitMatrix - Scanline (runs) based implementation of BitMatrix. immutable

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

Libraryless. Click here for Pure Java version (6001L/33K).

1  
persistable sclass ScanlineBitMatrix extends AbstractMatrix<Bool> is BitMatrix {
2  
  int bx1 = Int.MAX_VALUE, by1, bx2, by2; // bounding box of positive bits
3  
  int pixels; // number of set bits
4  
  
5  
  // format for each line: start x, end x (excl.), start x, end x, ...
6  
  int[] scanlineData;
7  
  
8  
  // pointer into scanlineData for each line
9  
  int[] scanlineStarts;
10  
  
11  
  *(BitMatrix m) {
12  
    super(m.getWidth(), m.getHeight());
13  
    
14  
    // find upper and lower bounding line
15  
    while (by1 < h && bitMatrixRowEmpty(m, by1)) by1++;
16  
    
17  
    by2 = h;
18  
    while (by2 > by1 && bitMatrixRowEmpty(m, by2-1)) by2--;
19  
    
20  
    int ptr = 0, pixels = 0;
21  
    scanlineStarts = new int[by2-by1];
22  
    new IntBuffer scanlineData;
23  
    lineLoop: for (int y = by1; y < by2; y++) {
24  
      scanlineStarts[y-by1] = l(scanlineData);
25  
      
26  
      for (int x = 0; x < w; ) {
27  
        while (!m.get(x, y))
28  
          if (++x >= w)
29  
            continue lineLoop;
30  
        
31  
        int x2 = x+1;
32  
        while (x2 < w && m.get(x2, y)) ++x2;
33  
        
34  
        scanlineData.add(x);
35  
        scanlineData.add(x2);
36  
        pixels += x2-x;
37  
        if (x < bx1) bx1 = x;
38  
        if (x2 > bx2) bx2 = x2;
39  
        x = x2;
40  
      }
41  
    }
42  
    this.scanlineData = scanlineData.toArray();
43  
    this.pixels = pixels;
44  
  }
45  
  
46  
  public Bool get(int x, int y) {
47  
    if (x < bx1 || y < by1 || x >= bx2 || y >= by2) false;
48  
    int ptr = scanlineStarts[y-by1];
49  
    
50  
    int end = y == by2-1 ? scanlineData.length : scanlineStarts[y-by1+1];
51  
52  
    // linear search for now
53  
    while (ptr < end) {
54  
      int x1 = scanlineData[ptr++];
55  
      if (x < x1) false;
56  
      int x2 = scanlineData[ptr++];
57  
      if (x < x2) true;
58  
    }
59  
    
60  
    false;
61  
  }
62  
  
63  
  public void set(int x, int y, Bool a) { unimplemented(); }
64  
  
65  
  Rect boundingBoxOfTrueBits() { ret bx1 >= bx2 ? null : rect(bx1, by1, bx2, by2); }
66  
  
67  
  int nRuns() { ret scanlineData.length/2; }
68  
  
69  
  ItIt<Rect> runs() {
70  
    ret new ItIt<Rect> {
71  
      int y = 0, ptr = 0;
72  
      
73  
      public bool hasNext() { ret ptr < l(scanlineData); }
74  
      public Rect next() {
75  
        while (y < scanlineStarts.length-1
76  
          && ptr >= scanlineStarts[y+1])
77  
            ++y;
78  
        int x1 = scanlineData[ptr++];
79  
        int x2 = scanlineData[ptr++];
80  
        ret rect(x1, by1+y, x2-x1, 1);
81  
      }
82  
    };
83  
  }
84  
  
85  
  int pixelCount() { ret pixels; }
86  
}

Author comment

Began life as a copy of #1033585

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1033829
Snippet name: ScanlineBitMatrix - Scanline (runs) based implementation of BitMatrix. immutable
Eternal ID of this version: #1033829/21
Text MD5: 9e7d5259d75e896eaae6d2cae473a473
Transpilation MD5: 4ff677ac0fbf96dea9f87b826921af27
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-01-13 16:11:11
Source code size: 2406 bytes / 86 lines
Pitched / IR pitched: No / No
Views / Downloads: 112 / 253
Version history: 20 change(s)
Referenced in: [show references]