Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to selectively apply processing algorithm to certain color channel(s) in Java
    text
    copied!<p>I'm developing an image processing library for Android which has ability to apply adjustments to image.</p> <p>At first, I process each bits like this:</p> <pre><code>public int[] getProcessedPixels(int[] pixels) { int a, r, g, b; for(int i = 0; i &lt; 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 </code></pre> <p>Since I'm developing <strong>a library</strong>, I want to let developers who use it to be able to <strong>apply "doSomethingWithThisChannel" method to any channel(s) as needed</strong></p> <p>I want to change the method like this (it's a copy of the above method, but simplified):</p> <pre><code>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 &lt; pixels.length; i++) { pixels[i] = doSomethingWithTHESEChannels(pixels[i], channels); } return pixels; } // Usage sample int channels = Channel.RED | Channel.GREEN; // ONLY apply processing to RED &amp; GREEN channels int[] pixels = bitmap.getAllPixels(); // each pixels is hexa 0xAARRGGBB int[] resultPixels = getProcessedPixels(pixels, channels); </code></pre> <p>This is my static class to define "bitmasks" (cmiiw) for each ARGB color channel I used in the code above:</p> <pre><code>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; } </code></pre> <p>Do you have any suggestion how should I implement <code>doSomethingWithTHESEChannels</code> method above? I'm pretty sure it will involve bitmask/bit-manipulation.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload