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

80
LINES

< > BotCompany Repo | #1033937 // Gazelle22_GradientImage [OK, makes an image 4 times the resolution of the input to show boundaries]

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

Libraryless. Click here for Pure Java version (7025L/40K).

sclass Gazelle22_GradientImage {
  // input
  BWImage posterized;
  double gradientThreshold = .25;
  
  // output
  BWImage out;
  
  // internal
  PosterizeBWImage op;
  BWImage unposterized;
  int w, h, brightnessLevels, singleStep;
  
  *(BWImage *posterized) {}

  bool init() {
    op = (PosterizeBWImage) getMetaSrc(posterized);
    if (op == null) false;
    unposterized = op.img;
    brightnessLevels = op.brightnessLevels;
    
    assertSameSize(unposterized, posterized);
    w = posterized.getWidth();
    h = posterized.getHeight();
    singleStep = iceil(doubleRatio(255, brightnessLevels-1));
    true;
  }
  
  BWImage get() {
    init();
    
    out = new BWImage(w*2+1, h*2+1);
    for y to h:
      for x to w: {
        if (x < w-1) checkPair(x, y, x+1, y);
        if (y < h-1) checkPair(x, y, x, y+1);
      }
      
    ret out;
  }
  
  // also returns true for inner pixels
  // may not work 100% yet (see gazelle22_highlightRegionOutline)
  bool isGradientPoint(int x, int y) {
    if (op == null) false;
    
    new IntMinMax posterizedMinMax;
    new IntMinMax unposterizedMinMax;
    
    // need to grab point and all 8 neighbors to cover all cases
    for (int y2 = max(0, y-1); y2 <= min(h-1, y+1); y2++)
      for (int x2 = max(0, x-1); x2 <= min(w-1, x+1); x2++) {
        posterizedMinMax.add(posterized.getInt(x2, y2));
        unposterizedMinMax.add(unposterized.getInt(x2, y2));
      }
    
    int posterizedContrast = posterizedMinMax.rangeLength();
    if (posterizedContrast > singleStep) false;
    if (posterizedContrast == 0) true;
    int unposterizedContrast = unposterizedMinMax.rangeLength();
    ret unposterizedContrast <= posterizedContrast*gradientThreshold;
  }

  void checkPair(int x, int y, int x2, int y2) {  
    int posterizedContrast = absDiff(posterized.getInt(x, y),
      posterized.getInt(x2, y2));
            
    if (posterizedContrast == 0) ret;
    if (posterizedContrast > singleStep) ret;
    
    int realContrast = absDiff(unposterized.getInt(x, y),
      unposterized.getInt(x2, y2));
    double ratio = doubleRatio(realContrast, singleStep);
    float result = cast (.5-ratio)*2;
    /*if (ratio < .5)*/ {
      out.setPixel(x*2+1-(y2-y), y*2+1, result);
      out.setPixel(x*2+1, y*2+1-(x2-x), result);
    }
  }
}

Author comment

Began life as a copy of #1033934

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1033937
Snippet name: Gazelle22_GradientImage [OK, makes an image 4 times the resolution of the input to show boundaries]
Eternal ID of this version: #1033937/22
Text MD5: 5aa81b68f27f4c635b3ea7c00ec9314e
Transpilation MD5: 4698d07577456c1ebd84aa959f2a29e6
Author: stefan
Category: javax / gazelle 22
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-01-17 18:52:30
Source code size: 2375 bytes / 80 lines
Pitched / IR pitched: No / No
Views / Downloads: 111 / 238
Version history: 21 change(s)
Referenced in: [show references]