@@ -20,7 +20,7 @@ public class ImageUtils
20
20
* @param luminanceThreshold luminance threshold
21
21
* @return the dithered 2-color image
22
22
*/
23
- public static BufferedImage desaturateWithDithering (final BufferedImage image , int luminanceThreshold )
23
+ public static BufferedImage desaturateWithDithering (final BufferedImage image , final int luminanceThreshold , final int alphaThreshold )
24
24
{
25
25
// Clone original image
26
26
final ColorModel cm = image .getColorModel ();
@@ -31,9 +31,6 @@ public static BufferedImage desaturateWithDithering(final BufferedImage image, i
31
31
// Apply dithering
32
32
final int width = out .getWidth ();
33
33
final int height = out .getHeight ();
34
-
35
- // Two colors for dithering, typically black and white
36
-
37
34
for (int y = 0 ; y < height ; y ++) {
38
35
for (int x = 0 ; x < width ; x ++) {
39
36
final int pixel = out .getRGB (x , y );
@@ -44,7 +41,7 @@ public static BufferedImage desaturateWithDithering(final BufferedImage image, i
44
41
45
42
// Convert to grayscale (or use other method to choose between color1 and color2)
46
43
final int gray = (int )(0.299 * red + 0.587 * green + 0.114 * blue );
47
- final int newPixel = gray < luminanceThreshold ? BLACK : WHITE ;
44
+ final int newPixel = gray <= luminanceThreshold && alpha >= alphaThreshold ? BLACK : WHITE ;
48
45
49
46
final int error = gray - ((newPixel == BLACK ) ? 0 : 255 );
50
47
@@ -54,7 +51,7 @@ public static BufferedImage desaturateWithDithering(final BufferedImage image, i
54
51
if (y < height - 1 ) out .setRGB (x , y + 1 , applyError (out .getRGB (x , y + 1 ), Math .round (error * ERROR_FIVE_SIXTEENTHS )));
55
52
if (x < width - 1 && y < height - 1 ) out .setRGB (x + 1 , y + 1 , applyError (out .getRGB (x + 1 , y + 1 ), Math .round (error * ERROR_ONE_SIXTEENTH )));
56
53
57
- out .setRGB (x , y , newPixel | ( alpha << 24 ) );
54
+ out .setRGB (x , y , newPixel );
58
55
}
59
56
}
60
57
return out ;
0 commit comments