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).

!7

static int levels = 8;
static Color bgColor = Color.red;
sbool showBorders = true;

sclass Element {}
Element > SingleColor { int color; }
Element > Quad { Element[] subs; }
Element > Abort {}

p {
  RGBImage img = rgbShootScreen();
  Element e = split(img, 0, 0, img.w(), img.h(), 1, levels);
  //printStructure(e);
  showImage(reconstruct(img.w(), img.h(), e));
}

static Element split(RGBImage img, int x1, int y1, int w, int h, int level, int maxLevel) {
  if (rgbAllOneColor(img, x1, y1, w, h)) {
    int color = img.getInt(x1, y1);
    //print("one color: " + color);
    ret nu SingleColor(+color;
  }
  
  if (level >= maxLevel) {
    //print("abort");
    ret nu Abort;
  } else {
    //print("split");
    int x = x1+(w+1)/2, y = y1+(h+1)/2;
    Element[] subs = {
      split(img, x1, y1, x-x1, y-y1, level+1, maxLevel),
      split(img, x, y1, x1+w-x, y-y1, level+1, maxLevel),
      split(img, x1, y, x-x1, y1+h-y, level+1, maxLevel),
      split(img, x, y, x1+w-x, y1+h-y, level+1, maxLevel) };
    ret nu Quad(+subs;
  }
}

static RGBImage reconstruct(int w, int h, Element e) {
  RGBImage img = new RGBImage(w, h, bgColor);
  reconstruct(img, 0, 0, w, h, e);
  ret img;
}

svoid reconstruct(RGBImage img, int x1, int y1, int w, int h, Element e) {
  if (w <= 0 || h <= 0) ret;
  if (e << SingleColor) {
    if (showBorders)
      rgbFill(img, x1, y1, w-1, h-1, e/SingleColor.color);
    else
      rgbFill(img, x1, y1, w, h, e/SingleColor.color);
    ret;
  }
  if (e << Quad) {
    Element[] subs = e/Quad.subs;
    int x = x1+(w+1)/2, y = y1+(h+1)/2;
    reconstruct(img, x1, y1, x-x1, y-y1, subs[0]);
    reconstruct(img, x, y1, x1+w-x, y-y1, subs[1]);
    reconstruct(img, x1, y, x-x1, y1+h-y, subs[2]);
    reconstruct(img, x, y, x1+w-x, y1+h-y, subs[3]);
  }
}

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: 569 / 703
Referenced in: