@@ -20,7 +20,7 @@ public class ImageUtils
2020 * @param luminanceThreshold luminance threshold
2121 * @return the dithered 2-color image
2222 */
23- public static BufferedImage desaturateWithDithering (final BufferedImage image , int luminanceThreshold )
23+ public static BufferedImage desaturateWithDithering (final BufferedImage image , final int luminanceThreshold , final int alphaThreshold )
2424 {
2525 // Clone original image
2626 final ColorModel cm = image .getColorModel ();
@@ -31,9 +31,6 @@ public static BufferedImage desaturateWithDithering(final BufferedImage image, i
3131 // Apply dithering
3232 final int width = out .getWidth ();
3333 final int height = out .getHeight ();
34-
35- // Two colors for dithering, typically black and white
36-
3734 for (int y = 0 ; y < height ; y ++) {
3835 for (int x = 0 ; x < width ; x ++) {
3936 final int pixel = out .getRGB (x , y );
@@ -44,7 +41,7 @@ public static BufferedImage desaturateWithDithering(final BufferedImage image, i
4441
4542 // Convert to grayscale (or use other method to choose between color1 and color2)
4643 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 ;
4845
4946 final int error = gray - ((newPixel == BLACK ) ? 0 : 255 );
5047
@@ -54,7 +51,7 @@ public static BufferedImage desaturateWithDithering(final BufferedImage image, i
5451 if (y < height - 1 ) out .setRGB (x , y + 1 , applyError (out .getRGB (x , y + 1 ), Math .round (error * ERROR_FIVE_SIXTEENTHS )));
5552 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 )));
5653
57- out .setRGB (x , y , newPixel | ( alpha << 24 ) );
54+ out .setRGB (x , y , newPixel );
5855 }
5956 }
6057 return out ;
0 commit comments