画像に調整を適用できる 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
か? ビットマスク/ビット操作が含まれると確信しています。