int er = (r1 - r2) << ERROR_SHIFT; int eg = (g1 - g2) << ERROR_SHIFT; int eb = (b1 - b2) << ERROR_SHIFT; int ea = (a1 - a2) << ERROR_SHIFT;
int er = r1 - r2; int eg = g1 - g2; int eb = b1 - b2; int ea = a1 - a2;
    public BufferedImage filter(BufferedImage src, BufferedImage dst) {        // If there's no dest. create one        if (dst == null)            dst = createCompatibleDestImage(src, null);        // Otherwise check that the provided dest is an indexed image        else if (dst.getType() != BufferedImage.TYPE_BYTE_INDEXED) {            throw new IllegalArgumentException("Wrong Destination Buffer type");        }        DataBufferByte dstBuffer = (DataBufferByte) dst.getRaster().getDataBuffer();        byte dstData[] = dstBuffer.getData();        // Other things to test are pixel bit strides, scanline stride and transfer type        // Same goes for the source image        IndexColorModel icm = (IndexColorModel) dst.getColorModel();        colorMap = new int[icm.getMapSize()];        icm.getRGBs(colorMap);        int width = src.getWidth();        int height = src.getHeight();        DataBufferInt srcBuffer = (DataBufferInt) src.getRaster().getDataBuffer();        int srcData[] = srcBuffer.getData();        // This is the offset into the buffer of the current source pixel        int index = 0;        // Loop through each pixel        for (int y = 0; y < height; y++) {            boolean reverse = serpentine && (y & 1) == 1;            int direction;            if (reverse) {                index = y * width + width - 1;                direction = -1;            } else {                index = y * width;                direction = 1;            }            for (int x = 0; x < width; x++) {                int rgb1 = srcData[index];                int a1 = (rgb1 >> 24) & 0xff;                int r1 = (rgb1 >> 16) & 0xff;                int g1 = (rgb1 >> 8) & 0xff;                int b1 = rgb1 & 0xff;                int idx = findIndex(r1, g1, b1, a1);                dstData[index] = (byte) idx;                int rgb2 = colorMap[idx];                int a2 = (rgb2 >> 24) & 0xff;                int r2 = (rgb2 >> 16) & 0xff;                int g2 = (rgb2 >> 8) & 0xff;                int b2 = rgb2 & 0xff;                int er = (r1 - r2) << ERROR_SHIFT;                int eg = (g1 - g2) << ERROR_SHIFT;                int eb = (b1 - b2) << ERROR_SHIFT;                int ea = (a1 - a2) << ERROR_SHIFT;                for (int i = -1; i <= 1; i++) {                    int iy = i + y;                    if (0 <= iy && iy < height) {                        for (int j = -1; j <= 1; j++) {                            int jx = j + x;                            if (0 <= jx && jx < width) {                                int w;                                if (reverse)                                    w = matrix[(i + 1) * 3 - j + 1];                                else                                    w = matrix[(i + 1) * 3 + j + 1];                                if (w != 0) {                                    int k = reverse ? index - j : index + j;                                    rgb1 = srcData[k];                                    a1 = ((rgb1 >> 24) & 0xff) + ea * w / sum;                                    r1 = ((rgb1 >> 16) & 0xff) + er * w / sum;                                    g1 = ((rgb1 >> 8) & 0xff) + eg * w / sum;                                    b1 = (rgb1 & 0xff) + eb * w / sum;                                    srcData[k] = ((clamp(a1) << 24) | clamp(r1) << 16)                                        | (clamp(g1) << 8) | clamp(b1);                                }                            }                        }                    }                }                index += direction;            }        }        return dst;    }