@@ -53,37 +53,15 @@ public static void renderFromImage(final BufferedImage scaled, final MapRenderer
5353 if (Math .abs (diff ) > 0.18f ) {
5454 centerB = centerB * 0.4f + median * 0.6f ;
5555 }
56+ centerB = getBrightness (neighB , idx , centerB );
5657
57- float bilateral = 0f , weightSum = 0f ;
58- for (int dy = -1 ; dy <= 1 ; dy ++) {
59- for (int dx = -1 ; dx <= 1 ; dx ++) {
60- if (dx == 0 && dy == 0 ) continue ;
61-
62- float nb = neighB [idx ++];
63- float distW = (dx == 0 || dy == 0 ) ? 1f : 0.7f ;
64- float diffB = Math .abs (centerB - nb );
65- float colorW = (diffB < 0.07f ) ? 1f : 0.2f ;
66- float w = distW * colorW ;
67-
68- bilateral += nb * w ;
69- weightSum += w ;
70- }
71- }
72-
73- if (weightSum > 0 ) {
74- float smoothB = bilateral / weightSum ;
75- centerB = centerB * 0.85f + smoothB * 0.15f ;
76- }
77-
78- int rgb = HSBtoRGB (hsb [0 ], hsb [1 ], clamp (centerB , 0f , 1f ));
58+ int rgb = convertHSBtoRGB (hsb [0 ], hsb [1 ], clamp (centerB , 0f , 1f ));
7959 filtered .setRGB (x , y , rgb );
8060 }
8161 }
82-
8362 addPixels (mapRendererData , height , width , filtered );
8463 }
8564
86-
8765 /**
8866 * Converts a color from HSB components into packed RGB format (0xAARRGGBB).
8967 * This matches the RGB format used by {@link Color#getRGB()} and can be directly
@@ -94,8 +72,10 @@ public static void renderFromImage(final BufferedImage scaled, final MapRenderer
9472 * @param brightness value in range 0.0–1.0
9573 * @return a packed ARGB integer representing the color
9674 */
97- public static int HSBtoRGB (float hue , float saturation , float brightness ) {
98- int r = 0 , g = 0 , b = 0 ;
75+ public static int convertHSBtoRGB (float hue , float saturation , float brightness ) {
76+ int r = 0 ;
77+ int g = 0 ;
78+ int b = 0 ;
9979 if (saturation == 0 ) {
10080 r = g = b = (int ) (brightness * 255.0f + 0.5f );
10181 } else {
@@ -135,6 +115,8 @@ public static int HSBtoRGB(float hue, float saturation, float brightness) {
135115 g = (int ) (p * 255.0f + 0.5f );
136116 b = (int ) (q * 255.0f + 0.5f );
137117 break ;
118+ default :
119+ break ;
138120 }
139121 }
140122 return 0xff000000 | (r << 16 ) | (g << 8 ) | (b << 0 );
@@ -154,7 +136,7 @@ public static int HSBtoRGB(float hue, float saturation, float brightness) {
154136 * @param hsbvals optional array to store the result; may be {@code null}
155137 * @return an array of three floats containing hue, saturation, and brightness (in that order)
156138 */
157- public static float [] getHSB (final int rgb , final float [] hsbvals ) {
139+ public static float [] getHSB (final int rgb , final float [] hsbvals ) {
158140 int r = (rgb >> 16 ) & 0xFF ;
159141 int g = (rgb >> 8 ) & 0xFF ;
160142 int b = rgb & 0xFF ;
@@ -175,13 +157,14 @@ public static float[] getHSB(final int rgb, final float[] hsbvals) {
175157 * @param b blue component (0–255)
176158 * @param hsbvals optional array to store the result; may be {@code null}
177159 * @return an array containing hue, saturation, and brightness (in that order).
178- *
179160 * @see java.awt.Color#getRGB()
180161 * @see java.awt.Color#Color(int)
181162 * @see java.awt.Color#RGBtoHSB(int, int, int, float[])
182163 */
183164 public static float [] retrieveRGBToHSB (int r , int g , int b , float [] hsbvals ) {
184- float hue , saturation , brightness ;
165+ float hue ;
166+ float saturation ;
167+ float brightness ;
185168 if (hsbvals == null ) {
186169 hsbvals = new float [3 ];
187170 }
@@ -190,7 +173,7 @@ public static float[] retrieveRGBToHSB(int r, int g, int b, float[] hsbvals) {
190173 int cmin = (r < g ) ? r : g ;
191174 if (b < cmin ) cmin = b ;
192175
193- brightness = (( float ) cmax ) / 255.0f ;
176+ brightness = (float ) ( cmax / 255.0 ) ;
194177 if (cmax != 0 )
195178 saturation = ((float ) (cmax - cmin )) / ((float ) cmax );
196179 else
@@ -261,9 +244,41 @@ private static float getMedianBrightness(float[] v) {
261244 * @return a value within the inclusive range {@code [min, max]}
262245 */
263246 private static float clamp (float v , float min , float max ) {
264- return v < min ? min : (v > max ? max : v );
247+ if (v < min ) {
248+ return min ;
249+ }
250+ if (v > max ) return max ;
251+
252+ return v ;
265253 }
266254
255+ private static float getBrightness (final float [] neighB , int idx , float brightness ) {
256+ float bilateral = 0f ;
257+ float weightSum = 0f ;
258+
259+ for (int dy = -1 ; dy <= 1 ; dy ++) {
260+ for (int dx = -1 ; dx <= 1 ; dx ++) {
261+ if (dx == 0 && dy == 0 ) continue ;
262+
263+ float nb = neighB [idx ++];
264+ float distW = (dx == 0 || dy == 0 ) ? 1f : 0.7f ;
265+ float diffB = Math .abs (brightness - nb );
266+ float colorW = (diffB < 0.07f ) ? 1f : 0.2f ;
267+ float w = distW * colorW ;
268+
269+ bilateral += nb * w ;
270+ weightSum += w ;
271+ }
272+ }
273+
274+ if (weightSum > 0 ) {
275+ float smoothB = bilateral / weightSum ;
276+ brightness = brightness * 0.85f + smoothB * 0.15f ;
277+ }
278+ return brightness ;
279+ }
280+
281+
267282 private static void addPixels (final MapRendererData mapRendererData , final int height , final int width , final BufferedImage filtered ) {
268283 for (int y = 0 ; y < height ; y ++) {
269284 for (int x = 0 ; x < width ; x ++) {
0 commit comments