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

1  
srecord noeq GridCodec1(BufferedImage stage0_image) is JavaCodec<BufferedImage> {
2  
  settable int rows = 20;
3  
4  
  static GridCodec1 demo() { ret GridCodec1(defaultScreenshot()); }
5  
6  
  // interim data & searches 
7  
  Matrix<Int> stage1_matrix;
8  
  long dataBytes, dataChars;
9  
  Matrix<Matrix<Int>> stage2_grid;
10  
  
11  
  JE_CompressEachElementIndividually<LInt> strat;
12  
  JECompressionSearch<LLInt> search;
13  
14  
  JECompressionSearch<LLInt> forward() {
15  
    // go from stage 0 (image) to stage 3 (list of list of ints)
16  
17  
    assertNotNull(stage0_image);
18  
    
19  
    var m = stage1_matrix = imageToMatrix(stage0_image);
20  
    dataBytes = m.nCells()*4+2;
21  
    dataChars = dataBytes*2 + l("lInt(\"\")");
22  
    print("Matrix ~ " + shorten_str(60, m));
23  
    print("Trivial comp ~ " + (m.nCells()*8));
24  
    stage2_grid = matrixToMNGrid_rows(m, rows);
25  
    
26  
    // list of lists
27  
    LL<Int> stage3_cells = matrixOfMatricesToListOfLists(stage2_grid);
28  
    
29  
    search = jeCompressionSearch(stage3_cells, dataChars);
30  
    strat = new JE_CompressEachElementIndividually<LInt>()
31  
      .elementCompressionStrategy(-> new JE_RepElement)
32  
      .onCompressionFail(ints -> javaObjectToIJavaExpr(ints);
33  
    
34  
    search.addStrategy(strat);
35  
    ret search;
36  
  }
37  
  
38  
  S stats() { ret strat?.stats(); }
39  
  
40  
  JECompressionSearch<LLInt>.Submission winner() {
41  
    ret search?.bestSubmission();
42  
  }
43  
  
44  
  IJavaExpr<LL<Int>> winnerCodeWithoutPostProcessing() {
45  
    ret winner()?.decompressor();
46  
  }
47  
  
48  
  IJavaExpr<BufferedImage> winnerCode() {
49  
    var innerWinner = winnerCodeWithoutPostProcessing();
50  
    if (innerWinner == null) null;
51  
    ret addPostProcessing(innerWinner);
52  
  }
53  
  
54  
  L<Int> cellWidths() { ret lambdaMapMethod getWidth(rowToList(stage2_grid, 0)); }
55  
  L<Int> cellHeights() { ret lambdaMapMethod getHeight(colToList(stage2_grid, 0)); }
56  
  
57  
  // post processing goes from stage 3 back to stage 0 (see function "forward")
58  
  IJavaExpr<BufferedImage> addPostProcessing(IJavaExpr<LL<Int>> inner) {
59  
    L<Int> cellWidths = cellWidths();
60  
    L<Int> cellHeights = cellHeights();
61  
  
62  
    ret new AbstractJavaXExpr<BufferedImage> {
63  
      public FunctionCall javaConstruct() {
64  
        ret functionCall gridLOLToImage(toJava(cellWidths), toJava(cellHeights), inner);
65  
      }
66  
      
67  
      toString { ret str(javaConstruct()); }
68  
      
69  
      public BufferedImage quickEval() {
70  
        ret gridLOLToImage(cellWidths, cellHeights, inner.quickEval());
71  
      }
72  
    };
73  
  }
74  
75  
  BufferedImage renderCellsLinearly() {
76  
    ret mergeBufferedImagesHorizontally(
77  
      map intMatrixToImage(matrixToList(stage2_grid)));
78  
  }
79  
  
80  
  void drawOverlay(ImageSurface is, Graphics2D g) pcall {
81  
    double zoom = is.getZoomX();
82  
    
83  
    L<Int> cellWidths = cellWidths();
84  
    L<Int> cellHeights = cellHeights();
85  
86  
    L<Int> cellX1s = partialSums_firstIs0(cellWidths);
87  
    L<Int> cellY1s = partialSums_firstIs0(cellHeights);
88  
    
89  
    for y over cellHeights:
90  
      for x over cellWidths:
91  
        if (strat.isElementCompressed(y*l(cellWidths)+x)) {
92  
          Rect r = rectFromPoints(
93  
            cellX1s.get(x), cellY1s.get(y),
94  
            cellX1s.get(x+1), cellY1s.get(y+1));
95  
          r = scaleRect(r, zoom);
96  
          fillRect(g, r, colorWithAlpha(Color.GREEN, .5));
97  
        }
98  
  }
99  
}

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: 162 / 290
Version history: 33 change(s)
Referenced in: [show references]