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

143
LINES

< > BotCompany Repo | #1033717 // GazelleVScript - "Linear" script [v1]

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

Libraryless. Click here for Pure Java version (31578L/199K).

// without types and proper AOT parsing
srecord noeq GazelleVScript(S text) {
  LS tok;
  
  sS helpText = !include string #1033775;

  // A sequence is a sequence of runs,
  // for example in an animation
  class Sequence {
    new MapSO vars;
  }
  
  class Run is Steppable {
    ListAndIndex<S> ptr; // where are we in the script
    S token;
    
    new Stack stack;
    
    // main variables
    settable IBWIntegralImage inputImage;
    IBWIntegralImage integralImage;
    settable BWImage bwImage;
    
    selfType setIntegralImage(IBWIntegralImage ii) {
      push(integralImage = ii);
      this;
    }
      
    *() {
      parse();
      ptr = ListAndIndex(tok, 1);
    }
    
    void push(O o) { stack.add(o); }
    
    O value() { ret last(stack); }
    
    public bool step() {
      token = ptr!;
      if (token == null) false;
      next();
      if (isInteger(token)) {
        int i = parseInt(token);
        token = tpp();
        if (is("p"))
          scaleToHeight(i);
        else if (is("c"))
          posterize(i);
        else if (is("w"))
          scaleWidthOnly(i);
        else
          warn("Unknown token: " + token);
      } else if (is("rasters"))
        push(reproduceRasterBars(bwIntegralImage()));
      else if (is("struct"))
        push(struct(value()));
      else if (is("niceStruct"))
        push(sfuIndent(value()));
      else if (is("className"))
        push(className(value()));
      else if (is("borderImage"))
        push(bwImage = gazelle22_borderImage(bwImage));
      else if (is("gradientImage"))
        push(bwImage = new Gazelle22_GradientImage(bwImage)!);
        
      else if (is("q1")) clip(0, .5, 0, .5);
      else if (is("q2")) clip(.5, 1, 0, .5);
      else if (is("q3")) clip(0, .5, .5, 1);
      else if (is("q4")) clip(.5, 1, .5, 1);
      
      else if (is("original"))
        push(toBufferedImage(inputImage));
        
      else {
        warn("Unknown token: " + token);
      }
      true;
    }
    
    bool is(S t) { ret eqic(token, t); }
    S tpp() { var token = ptr!; next(); ret token; }
    void next { if (!ptr.atEnd()) ptr = ptr.plus(2); }
    
    // operations
    
    void scaleToHeight(int pixelRows) {
      pixelRows = min(pixelRows, 512);
      if (integralImage == null)
        ret with warn("No integral image to scale down");
      push(bwImage = scaledBWImageFromBWIntegralImage_withMeta_height(pixelRows, integralImage));
    }
    
    void posterize(int colors) {
      colors = clamp(colors, 2, 256);
      if (bwImage == null)
        ret with warn("No image to posterize");
      else
        push(bwImage = posterizeBWImage_withMeta(colors, bwImage));
    }
    
    IBWIntegralImage bwIntegralImage() {
      O val = value();
      if (val cast IBWIntegralImage) ret val;
      if (val cast BWImage) {
        var ii = main bwIntegralImage(val);
        setIntegralImage(ii);
        ret ii;
      }
      fail("Not an image on stack");
    }
    
    void scaleWidthOnly(int w) {
      var ii = bwIntegralImage();
      push(bwImage = scaledBWImageFromBWIntegralImage_withMeta(
        ii, w, ii.getHeight());
    }
    
    void clip(double x1, double x2, double y1, double y2) {
      O val = value();
      if (val cast IBWIntegralImage) {
        int w = val.getWidth(), h = val.getHeight();
        Rect r = toRect_round(scaleRect(w, h,
          doubleRectFromPoints(x1, y1, x2, y2)));
        setIntegralImage(new IIBWVirtualClip(val, r));
      }
      else if (val cast BWImage)
        todo();
      else fail("Not an image on stack");
    }
    
    // end of operations
    
    O result() {
      ret last(stack);
    }
    
    run { stepAll(this); }
  }
  
  void parse() {
    tok if null = javaTok(text);
  }
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1033717
Snippet name: GazelleVScript - "Linear" script [v1]
Eternal ID of this version: #1033717/39
Text MD5: 035e55d0cee5283ef6ab04e098f40b3b
Transpilation MD5: 4154db11b34e214882672e20fd02a827
Author: stefan
Category: javax / gazelle v
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-03-08 07:41:34
Source code size: 3894 bytes / 143 lines
Pitched / IR pitched: No / No
Views / Downloads: 254 / 519
Version history: 38 change(s)
Referenced in: [show references]