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

64
LINES

< > BotCompany Repo | #1006495 // Recursive Splitting With Reconstruction

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (4819L/34K/117K).

1  
!7
2  
3  
static int levels = 8;
4  
static Color bgColor = Color.red;
5  
sbool showBorders = true;
6  
7  
sclass Element {}
8  
Element > SingleColor { int color; }
9  
Element > Quad { Element[] subs; }
10  
Element > Abort {}
11  
12  
p {
13  
  RGBImage img = rgbShootScreen();
14  
  Element e = split(img, 0, 0, img.w(), img.h(), 1, levels);
15  
  //printStructure(e);
16  
  showImage(reconstruct(img.w(), img.h(), e));
17  
}
18  
19  
static Element split(RGBImage img, int x1, int y1, int w, int h, int level, int maxLevel) {
20  
  if (rgbAllOneColor(img, x1, y1, w, h)) {
21  
    int color = img.getInt(x1, y1);
22  
    //print("one color: " + color);
23  
    ret nu SingleColor(+color;
24  
  }
25  
  
26  
  if (level >= maxLevel) {
27  
    //print("abort");
28  
    ret nu Abort;
29  
  } else {
30  
    //print("split");
31  
    int x = x1+(w+1)/2, y = y1+(h+1)/2;
32  
    Element[] subs = {
33  
      split(img, x1, y1, x-x1, y-y1, level+1, maxLevel),
34  
      split(img, x, y1, x1+w-x, y-y1, level+1, maxLevel),
35  
      split(img, x1, y, x-x1, y1+h-y, level+1, maxLevel),
36  
      split(img, x, y, x1+w-x, y1+h-y, level+1, maxLevel) };
37  
    ret nu Quad(+subs;
38  
  }
39  
}
40  
41  
static RGBImage reconstruct(int w, int h, Element e) {
42  
  RGBImage img = new RGBImage(w, h, bgColor);
43  
  reconstruct(img, 0, 0, w, h, e);
44  
  ret img;
45  
}
46  
47  
svoid reconstruct(RGBImage img, int x1, int y1, int w, int h, Element e) {
48  
  if (w <= 0 || h <= 0) ret;
49  
  if (e << SingleColor) {
50  
    if (showBorders)
51  
      rgbFill(img, x1, y1, w-1, h-1, e/SingleColor.color);
52  
    else
53  
      rgbFill(img, x1, y1, w, h, e/SingleColor.color);
54  
    ret;
55  
  }
56  
  if (e << Quad) {
57  
    Element[] subs = e/Quad.subs;
58  
    int x = x1+(w+1)/2, y = y1+(h+1)/2;
59  
    reconstruct(img, x1, y1, x-x1, y-y1, subs[0]);
60  
    reconstruct(img, x, y1, x1+w-x, y-y1, subs[1]);
61  
    reconstruct(img, x1, y, x-x1, y1+h-y, subs[2]);
62  
    reconstruct(img, x, y, x1+w-x, y1+h-y, subs[3]);
63  
  }
64  
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1006495
Snippet name: Recursive Splitting With Reconstruction
Eternal ID of this version: #1006495/1
Text MD5: 228c0f750aecea5874992b7acdc78a3c
Transpilation MD5: b7df85c0981e9288c03c891e46b25452
Author: stefan
Category: javax / ocr
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-01-11 00:08:14
Source code size: 1849 bytes / 64 lines
Pitched / IR pitched: No / No
Views / Downloads: 568 / 703
Referenced in: [show references]