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

1  
sclass Gazelle22_GradientImage {
2  
  // input
3  
  BWImage posterized;
4  
  double gradientThreshold = .25;
5  
  
6  
  // output
7  
  BWImage out;
8  
  
9  
  // internal
10  
  PosterizeBWImage op;
11  
  BWImage unposterized;
12  
  int w, h, brightnessLevels, singleStep;
13  
  
14  
  *(BWImage *posterized) {}
15  
16  
  bool init() {
17  
    op = (PosterizeBWImage) getMetaSrc(posterized);
18  
    if (op == null) false;
19  
    unposterized = op.img;
20  
    brightnessLevels = op.brightnessLevels;
21  
    
22  
    assertSameSize(unposterized, posterized);
23  
    w = posterized.getWidth();
24  
    h = posterized.getHeight();
25  
    singleStep = iceil(doubleRatio(255, brightnessLevels-1));
26  
    true;
27  
  }
28  
  
29  
  BWImage get() {
30  
    init();
31  
    
32  
    out = new BWImage(w*2+1, h*2+1);
33  
    for y to h:
34  
      for x to w: {
35  
        if (x < w-1) checkPair(x, y, x+1, y);
36  
        if (y < h-1) checkPair(x, y, x, y+1);
37  
      }
38  
      
39  
    ret out;
40  
  }
41  
  
42  
  // also returns true for inner pixels
43  
  // may not work 100% yet (see gazelle22_highlightRegionOutline)
44  
  bool isGradientPoint(int x, int y) {
45  
    if (op == null) false;
46  
    
47  
    new IntMinMax posterizedMinMax;
48  
    new IntMinMax unposterizedMinMax;
49  
    
50  
    // need to grab point and all 8 neighbors to cover all cases
51  
    for (int y2 = max(0, y-1); y2 <= min(h-1, y+1); y2++)
52  
      for (int x2 = max(0, x-1); x2 <= min(w-1, x+1); x2++) {
53  
        posterizedMinMax.add(posterized.getInt(x2, y2));
54  
        unposterizedMinMax.add(unposterized.getInt(x2, y2));
55  
      }
56  
    
57  
    int posterizedContrast = posterizedMinMax.rangeLength();
58  
    if (posterizedContrast > singleStep) false;
59  
    if (posterizedContrast == 0) true;
60  
    int unposterizedContrast = unposterizedMinMax.rangeLength();
61  
    ret unposterizedContrast <= posterizedContrast*gradientThreshold;
62  
  }
63  
64  
  void checkPair(int x, int y, int x2, int y2) {  
65  
    int posterizedContrast = absDiff(posterized.getInt(x, y),
66  
      posterized.getInt(x2, y2));
67  
            
68  
    if (posterizedContrast == 0) ret;
69  
    if (posterizedContrast > singleStep) ret;
70  
    
71  
    int realContrast = absDiff(unposterized.getInt(x, y),
72  
      unposterized.getInt(x2, y2));
73  
    double ratio = doubleRatio(realContrast, singleStep);
74  
    float result = cast (.5-ratio)*2;
75  
    /*if (ratio < .5)*/ {
76  
      out.setPixel(x*2+1-(y2-y), y*2+1, result);
77  
      out.setPixel(x*2+1, y*2+1-(x2-x), result);
78  
    }
79  
  }
80  
}

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: 114 / 244
Version history: 21 change(s)
Referenced in: [show references]