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

75
LINES

< > BotCompany Repo | #1004577 // Image Compressor ("go-basics.txt")

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

Uses 9620K of libraries. Click here for Pure Java version (4853L/32K/107K).

1  
!752
2  
3  
static BWImage bw;
4  
static Thread producer;
5  
static Reproducing rp;
6  
7  
!include #1004556 // class Reproducing
8  
9  
static int luaMaxSteps = 1000*1000;
10  
11  
p {
12  
  rp = new Reproducing;
13  
  rendererMaker = func(S s) {
14  
    if (startsWith(s, "lua "))
15  
      ret luaRenderer(dropPrefix("lua ", s));
16  
    null; // fallback to default (unstructure)
17  
  };
18  
  
19  
  bw = rp.bw = loadBWImage("#1004576");
20  
  rp.imageSurfaceToUpdateWithBest = showImage(bw.getBufferedImage());
21  
  
22  
  rp.startProduction = r { produce() };
23  
  
24  
  // Note: This doesn't terminate as it keeps looking for better
25  
  // solutions. It does write the shortest100.txt file in the mean
26  
  // time which is your real output.
27  
  rp.findShortest100_openEnd();
28  
}
29  
30  
static void produce() {
31  
  cancelThread(producer);
32  
  producer = new Thread(r {
33  
    // STRATEGY!
34  
    
35  
    rp.push(fullGrab(bw));
36  
    int w = 1;
37  
    while licensed {
38  
      rp.push(new RandomSolid().reproduce(bw));
39  
      S best = rp.bestDesc();
40  
      if (best != null)
41  
        rp.push(varyFloat(best));
42  
      if (w < bw.getWidth())
43  
        rp.push(hrepeat(bw, w++));
44  
    }
45  
  }, "Producer");
46  
  producer.start();
47  
}
48  
49  
static Renderer luaRenderer(final S code) {
50  
  ret new Renderer {
51  
    public BWImage render(int w, int h) {
52  
      BWImage img = new BWImage(w, h);
53  
      Sandbox sandbox = makeSandbox(img);
54  
      luaMaxSteps(luaMaxSteps);
55  
      quickLua(sandbox, code);
56  
      ret img;
57  
    }
58  
  };
59  
}
60  
61  
static Sandbox makeSandbox(final BWImage img) {
62  
  Sandbox s = luaSandbox();
63  
  
64  
  s.setOuter("w", Lua.value(img.getWidth()));
65  
  s.setOuter("h", Lua.value(img.getHeight()));
66  
  
67  
  s.setInner("set", new ThreeArgFunction() {
68  
    public LuaValue call(LuaValue x, LuaValue y, LuaValue theByte) {
69  
      img.setByte(x.toint(), y.toint(), (byte) theByte.toint());
70  
      ret Lua.NIL;
71  
    }
72  
  });
73  
74  
  ret s;
75  
}

Author comment

Began life as a copy of #1004557

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1004577
Snippet name: Image Compressor ("go-basics.txt")
Eternal ID of this version: #1004577/1
Text MD5: 52e96ccf0e6f668a991471c4f09a8f88
Transpilation MD5: 6af80c5a175a38e89d49da100eedd67f
Author: stefan
Category: javax / a.i.
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-08-22 16:30:01
Source code size: 1862 bytes / 75 lines
Pitched / IR pitched: No / No
Views / Downloads: 573 / 642
Referenced in: [show references]