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

49
LINES

< > BotCompany Repo | #1001877 // Image rendering from script, with comparison to target image

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

Libraryless. Click here for Pure Java version (1404L/10K/29K).

!752

!include #1000522 // image helper functions

p {
  RGBImage targetImg = new RGBImage(200, 200, Color.blue);
  
  RGBImage img = new RGBImage(200, 200, Color.white);
  render(img, "fill(\"808000\");");
  print("Diff: " + diff(img, targetImg));
  showImage(img);
}

static void render(RGBImage img, S script) {
  L<S> tok = javaTok(script);
  new Matches m;
  for (L<S> s : statements(tok)) {
    if (jmatch("fill(*);", s, m)) {
      fill(img, m.unq(0));
    } else
      fail("unknown statement: " + join(s));
  }
}

static void fill(RGBImage img, S col) {
  RGB rgb = new RGB(col);
  int w = img.getWidth(), h = img.getHeight();
  for (int y = 0; y < h; y++)
    for (int x = 0; x < w; x++)
      img.setPixel(x, y, rgb);
}

// assumes that it's a list of statements; return them one by one
static L<L<S>> statements(L<S> tok) {
  new L<L<S>> l;
  int i = 0;
  while (i < l(tok)) {
    int j = indexOf(tok, ";", i);
    if (j < 0) {
      if (l(tok)-i >= 3)
        l.add(tok.subList(i, l(tok)));
      break;
    } else {
      l.add(tok.subList(i, j+2)); // actual CNC - we may return some comments twice, but we don't look at them anyways.
      i = j+1; // go to non-code token
    }
  }
  ret l;
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1001877
Snippet name: Image rendering from script, with comparison to target image
Eternal ID of this version: #1001877/1
Text MD5: 89b08b6f26bb98bd943ebf0a5a5cae0d
Transpilation MD5: be968a40173092f2046728f7fcdbe469
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-12-01 17:36:08
Source code size: 1257 bytes / 49 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 629 / 662
Referenced in: [show references]