0

我正在为 Android 开发一个图像处理库,它能够对图像进行调整。

首先,我像这样处理每个位:

public int[] getProcessedPixels(int[] pixels) {
    int a, r, g, b;
    for(int i = 0; i < pixels.length; i++) {

        a = Color.alpha(pixels[i]);
        r = Color.red(pixels[i]);
        g = Color.green(pixels[i]);
        b = Color.blue(pixels[i]);

        a = doSomethingWithThisChannel(a);
        r = doSomethingWithThisChannel(r);
        g = doSomethingWithThisChannel(g);
        b = doSomethingWithThisChannel(b);

        // Merge back all 4 channels to pixel value
        pixels[i] = Color.argb(a, r, g, b);
    }

    return pixels;
}

// Usage sample
int[] pixels = bitmap.getAllPixels(); // each pixels is hexa 0xAARRGGBB
int[] resultPixels = getProcessedPixels(pixels); // it returns  result pixels

由于我正在开发一个库,我想让使用它的开发人员能够根据需要将“doSomethingWithThisChannel”方法应用于任何通道

我想改变这样的方法(它是上述方法的副本,但简化了):

public int[] getProcessedPixels(int[] pixels) {
    // assume process all channels if not specified
    return getProcessedPixels(pixels, Channel.ALL);
}

public int[] getProcessedPixels(int[] pixels, int channels) {
    int a, r, g, b;
    for(int i = 0; i < pixels.length; i++) {
        pixels[i] = doSomethingWithTHESEChannels(pixels[i], channels);
    }

    return pixels;
}

// Usage sample
int channels = Channel.RED | Channel.GREEN; // ONLY apply processing to RED & GREEN channels
int[] pixels = bitmap.getAllPixels(); // each pixels is hexa 0xAARRGGBB
int[] resultPixels = getProcessedPixels(pixels, channels);

这是我为上面代码中使用的每个 ARGB 颜色通道定义“位掩码”(cmiiw)的静态类:

public class Channel {
    public static final int NONE = 0x00000000;
    public static final int ALL = 0xffffffff;
    public static final int ALPHA = 0xff000000;
    public static final int RED = 0x00ff0000;
    public static final int GREEN = 0x0000ff00;
    public static final int BLUE = 0x000000ff;
}

你有什么建议我应该如何实现doSomethingWithTHESEChannels上面的方法?我很确定它将涉及位掩码/位操作。

4

1 回答 1

1

这是根据您的设计实现的doSomethingWithTHESEChannels方法:

    private int doSomethingWithTHESEChannels(int i, int channels) {

    if (Channel.NONE == channels) {
        return i;
    }

    int a, r, g, b;
    a = (i & Channel.ALPHA) >> 24;
    r = (i & Channel.RED) >> 16;
    g = (i & Channel.GREEN) >> 8;
    b = i & Channel.BLUE;

    int cur = channels & Channel.ALL;
    if (cur == Channel.ALL) {
        System.out.println("all found");
        a = transform(a);
        r = transform(r);
        g = transform(g);
        b = transform(b);

    } else {
        cur = channels & Channel.ALPHA;
        if (cur == Channel.ALPHA) {
            System.out.println("alpha found");
            a = transform(a);
        }
        cur = channels & Channel.RED;
        if (cur == Channel.RED) {
            System.out.println("red found");
            r = transform(r);
        }
        cur = channels & Channel.GREEN;
        if (cur == Channel.GREEN) {
            System.out.println("green found");
            g = transform(g);
        }

        cur = channels & Channel.BLUE;
        if (cur == Channel.BLUE) {
            System.out.println("blue found");
            b = transform(b);
        }

    }

    return (a << 24 | r << 16 | g << 8 | b);

}

变换方法是应用于通道的操作。Sysouts用于测试目的。如果需要,您可以使用适当的类型转换将 a、r、g、b 变量的类型更改为字节。

但是,我建议您将上述通道检测块(如果条件)移动到getProcessedPixels方法本身。然后你可以将整个像素数组传递给一个变换方法。这样,您将消除每个像素的冗余条件检查。如果您需要提示,请回复,我会更新答案。

于 2013-07-26T10:05:42.200 回答