我正在为 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
上面的方法?我很确定它将涉及位掩码/位操作。