srecord noeq GridCodec1(BufferedImage stage0_image) is JavaCodec { settable int rows = 20; static GridCodec1 demo() { ret GridCodec1(defaultScreenshot()); } // interim data & searches Matrix stage1_matrix; long dataBytes, dataChars; Matrix> stage2_grid; JE_CompressEachElementIndividually strat; JECompressionSearch search; JECompressionSearch 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 stage3_cells = matrixOfMatricesToListOfLists(stage2_grid); search = jeCompressionSearch(stage3_cells, dataChars); strat = new JE_CompressEachElementIndividually() .elementCompressionStrategy(-> new JE_RepElement) .onCompressionFail(ints -> javaObjectToIJavaExpr(ints); search.addStrategy(strat); ret search; } S stats() { ret strat?.stats(); } JECompressionSearch.Submission winner() { ret search?.bestSubmission(); } IJavaExpr> winnerCodeWithoutPostProcessing() { ret winner()?.decompressor(); } IJavaExpr winnerCode() { var innerWinner = winnerCodeWithoutPostProcessing(); if (innerWinner == null) null; ret addPostProcessing(innerWinner); } L cellWidths() { ret lambdaMapMethod getWidth(rowToList(stage2_grid, 0)); } L cellHeights() { ret lambdaMapMethod getHeight(colToList(stage2_grid, 0)); } // post processing goes from stage 3 back to stage 0 (see function "forward") IJavaExpr addPostProcessing(IJavaExpr> inner) { L cellWidths = cellWidths(); L cellHeights = cellHeights(); ret new AbstractJavaXExpr { 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 cellWidths = cellWidths(); L cellHeights = cellHeights(); L cellX1s = partialSums_firstIs0(cellWidths); L 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)); } } }