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

33
LINES

< > BotCompany Repo | #1037264 // BinaryImageToString

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

Libraryless. Click here for Pure Java version (14364L/83K).

srecord noeq BinaryImageToString(IBinaryImage img) {
  settable S format = "$(width)x$(height)-$(hex)";
  
  simplyCached Image2B renderedImage() {
    ret img == null ?: img.toImage2B();
  }
  
  private S lazyReplace(S s, S pattern, IF0 replacement) {
    if (contains(s, pattern))
      s = replace(s, pattern, strOrEmpty(replacement!));
    ret s;
  }
  
  S get() {
    if (img == null) null;
    
    S s = format;
    s = lazyReplace(s, "$(width)", -> img.getWidth());
    s = lazyReplace(s, "$(height)", -> img.getHeight());
    s = lazyReplace(s, "$(hex)", -> hexPixels());
    ret s;
  }
  
  S hexPixels() {
    var img = this.renderedImage();
    byte[] pixels = img.pixels;
    int n = pixels.length;
    
    while (n > 1 && pixels[n-1] == 0) --n;

    ret bytesToHex(pixels, 0, n);
  }
}

Author comment

Began life as a copy of #1035835

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): gjtlkbvenryc, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1037264
Snippet name: BinaryImageToString
Eternal ID of this version: #1037264/10
Text MD5: 7f1042812dd8f1d2581a0b922a254682
Transpilation MD5: c3255843384925aec68756429fb44064
Author: stefan
Category: javax / imaging
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2023-11-13 19:34:42
Source code size: 834 bytes / 33 lines
Pitched / IR pitched: No / No
Views / Downloads: 128 / 228
Version history: 9 change(s)
Referenced in: [show references]