public static void applyFloydSteinbergDithering(final BufferedImage image, final Palette palette) throws ImagingException {
        for (int y = 0; y < image.getHeight(); y++) {
            for (int x = 0; x < image.getWidth(); x++) {
                final int argb = image.getRGB(x, y);
                final int index = palette.getPaletteIndex(argb);
                final int nextArgb = palette.getEntry(index);
                image.setRGB(x, y, nextArgb);

                final int a = argb >> 24 & 0xff;
                final int r = argb >> 16 & 0xff;
                final int g = argb >> 8 & 0xff;
                final int b = argb & 0xff;

                final int na = nextArgb >> 24 & 0xff;
                final int nr = nextArgb >> 16 & 0xff;
                final int ng = nextArgb >> 8 & 0xff;
                final int nb = nextArgb & 0xff;

                final int errA = a - na;
                final int errR = r - nr;
                final int errG = g - ng;
                final int errB = b - nb;

                if (x + 1 < image.getWidth()) {
                    int update = adjustPixel(image.getRGB(x + 1, y), errA, errR, errG, errB, 7);
                    image.setRGB(x + 1, y, update);
                    if (y + 1 < image.getHeight()) {
                        update = adjustPixel(image.getRGB(x + 1, y + 1), errA, errR, errG, errB, 1);
                        image.setRGB(x + 1, y + 1, update);
                    }
                }
                if (y + 1 < image.getHeight()) {
                    int update = adjustPixel(image.getRGB(x, y + 1), errA, errR, errG, errB, 5);
                    image.setRGB(x, y + 1, update);
                    if (x - 1 >= 0) {
                        update = adjustPixel(image.getRGB(x - 1, y + 1), errA, errR, errG, errB, 3);
                        image.setRGB(x - 1, y + 1, update);
                    }

                }
            }
        }
    }
Changes the given image to only use colors from the given palette, applying Floyd-Steinberg dithering in the process. Ensure that your alpha values in
the image and in the palette are consistent.

@param image   the image to change
@param palette the palette to use
@throws ImagingException if it fails to read the palette index
src/main/java/org/apache/commons/imaging/palette/Dithering.java