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

99
LINES

< > BotCompany Repo | #1033357 // GridCodec1 [grid-based still image codec, OK, but doesn't use integral image]

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

Libraryless. Click here for Pure Java version (23983L/156K).

srecord noeq GridCodec1(BufferedImage stage0_image) is JavaCodec<BufferedImage> {
  settable int rows = 20;

  static GridCodec1 demo() { ret GridCodec1(defaultScreenshot()); }

  // interim data & searches 
  Matrix<Int> stage1_matrix;
  long dataBytes, dataChars;
  Matrix<Matrix<Int>> stage2_grid;
  
  JE_CompressEachElementIndividually<LInt> strat;
  JECompressionSearch<LLInt> search;

  JECompressionSearch<LLInt> forward() {
    // go from stage 0 (image) to stage 3 (list of list of ints)

    assertNotNull(stage0_image);
    
    var m = stage1_matrix = imageToMatrix(stage0_image);
    dataBytes = m.nCells()*4+2;
    dataChars = dataBytes*2 + l("lInt(\"\")");
    print("Matrix ~ " + shorten_str(60, m));
    print("Trivial comp ~ " + (m.nCells()*8));
    stage2_grid = matrixToMNGrid_rows(m, rows);
    
    // list of lists
    LL<Int> stage3_cells = matrixOfMatricesToListOfLists(stage2_grid);
    
    search = jeCompressionSearch(stage3_cells, dataChars);
    strat = new JE_CompressEachElementIndividually<LInt>()
      .elementCompressionStrategy(-> new JE_RepElement)
      .onCompressionFail(ints -> javaObjectToIJavaExpr(ints);
    
    search.addStrategy(strat);
    ret search;
  }
  
  S stats() { ret strat?.stats(); }
  
  JECompressionSearch<LLInt>.Submission winner() {
    ret search?.bestSubmission();
  }
  
  IJavaExpr<LL<Int>> winnerCodeWithoutPostProcessing() {
    ret winner()?.decompressor();
  }
  
  IJavaExpr<BufferedImage> winnerCode() {
    var innerWinner = winnerCodeWithoutPostProcessing();
    if (innerWinner == null) null;
    ret addPostProcessing(innerWinner);
  }
  
  L<Int> cellWidths() { ret lambdaMapMethod getWidth(rowToList(stage2_grid, 0)); }
  L<Int> cellHeights() { ret lambdaMapMethod getHeight(colToList(stage2_grid, 0)); }
  
  // post processing goes from stage 3 back to stage 0 (see function "forward")
  IJavaExpr<BufferedImage> addPostProcessing(IJavaExpr<LL<Int>> inner) {
    L<Int> cellWidths = cellWidths();
    L<Int> cellHeights = cellHeights();
  
    ret new AbstractJavaXExpr<BufferedImage> {
      public FunctionCall javaConstruct() {
        ret functionCall gridLOLToImage(toJava(cellWidths), toJava(cellHeights), inner);
      }
      
      toString { ret str(javaConstruct()); }
      
      public BufferedImage quickEval() {
        ret gridLOLToImage(cellWidths, cellHeights, inner.quickEval());
      }
    };
  }

  BufferedImage renderCellsLinearly() {
    ret mergeBufferedImagesHorizontally(
      map intMatrixToImage(matrixToList(stage2_grid)));
  }
  
  void drawOverlay(ImageSurface is, Graphics2D g) pcall {
    double zoom = is.getZoomX();
    
    L<Int> cellWidths = cellWidths();
    L<Int> cellHeights = cellHeights();

    L<Int> cellX1s = partialSums_firstIs0(cellWidths);
    L<Int> cellY1s = partialSums_firstIs0(cellHeights);
    
    for y over cellHeights:
      for x over cellWidths:
        if (strat.isElementCompressed(y*l(cellWidths)+x)) {
          Rect r = rectFromPoints(
            cellX1s.get(x), cellY1s.get(y),
            cellX1s.get(x+1), cellY1s.get(y+1));
          r = scaleRect(r, zoom);
          fillRect(g, r, colorWithAlpha(Color.GREEN, .5));
        }
  }
}

Author comment

Began life as a copy of #1033236

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1033357
Snippet name: GridCodec1 [grid-based still image codec, OK, but doesn't use integral image]
Eternal ID of this version: #1033357/34
Text MD5: adb447e4fb0821ba9920663d42d554ba
Transpilation MD5: c666c2077c578a8b538028fa00a748b5
Author: stefan
Category: javax / recognition by compression
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-10-30 18:13:02
Source code size: 3291 bytes / 99 lines
Pitched / IR pitched: No / No
Views / Downloads: 220 / 372
Version history: 33 change(s)
Referenced in: #1033364 - JavaCodec
#1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)