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

75
LINES

< > BotCompany Repo | #1033242 // JEImageCompressionTest2 [dev.]

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

Libraryless. Click here for Pure Java version (17569L/113K).

sclass JEImageCompressionTest2 {
  settable BufferedImage originalImage;
  
  settable BufferedImage scaledImage;
  settable BufferedImage restoredScaledImage;
  
  *() { this(screenshot()); }
  *(BufferedImage *originalImage) {}
  
  int rows = 20;
  int imgHeight = 200;
  
  replace m with matrix.
    
  Matrix<Int> matrix;
  long dataBytes, dataChars;
  Matrix<Matrix<Int>> grid;
  LL<Int> flatGrid;
  
  JE_CompressEachElementIndividually<LInt> strat;
  CompressionSearch_AnyType<IJavaExpr, LLInt> search;
  CompressionSearch_AnyType<IJavaExpr, LLInt>.Submission winner;

  S get() {
    // JavaX "null propagation" (handle null gracefully)
    
    if (originalImage == null) null;
    
    // Preprocess input image (scale to imgHeight)
    //   [this step is not reversed]
    
    scaledImage = resizeImageToHeight(originalImage, imgHeight);
    
    // see image as matrix, calculate how big the matrix literal is
    //   (dataBytes/dataChars)

    m = imageToMatrix(scaledImage);
    dataBytes = m.nCells()*4+2;
    dataChars = dataBytes*2 + l("lInt(\"\")");
    
    print("Matrix ~ " + takeFirst_str(60, m));
    print("Matrix literal ~ " + (m.nCells()*8));
    
    // split matrix into cells
    
    grid = matrixToMNGrid_rows(m, rows);
    
    // flatten whole grid & each cell to 1D
    flatGrid = map matrixToList(matrixToList(grid));
    
    search = jeCompressionSearch(flatGrid, dataChars);
    strat = new JE_CompressEachElementIndividually<LInt>()
      .elementCompressionStrategy(-> new JE_RepElement)
      .onCompressionFail(ints -> javaObjectToIJavaExpr(ints);
    
    search.addStrategy(strat);
    
    runProbabilisticForNSeconds(5.0, search);
    winner = search.bestSubmission();
    print(strat.stats());
    if (winner != null) {
      //print("Hard result (score " + result.scoreRatio() + ")", result.checkUnder(hardJERegime());
      print(winner);
    }
    ret winnerCode();
  }
  
  S winnerCode() {
    ret winner?.decompressor_str();
  }
  
  void restoreScaledImage {
    restoredScaledImage = matrixToImage(m);
  }
}

Author comment

Began life as a copy of #1033236

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1033242
Snippet name: JEImageCompressionTest2 [dev.]
Eternal ID of this version: #1033242/7
Text MD5: 8b7f63a493be862a0be4794fb05292ec
Transpilation MD5: 8d36b5605c0025208a57c677b1164a0b
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-22 23:23:54
Source code size: 2140 bytes / 75 lines
Pitched / IR pitched: No / No
Views / Downloads: 148 / 213
Version history: 6 change(s)
Referenced in: [show references]