Indexed color using Android's graphics API

316 views Asked by At

I'm making a drawing program for Android and one of the features I would like to support is the ability to adjust colors and see the color changing in real time in the drawing.

To represent the drawing, the program internally stores a 2D int array of indexes (the "color index array") that reference a limited color palette array. When a part of the drawing is adjusted, the color index array is adjusted along with the bitmap that is actually drawn to the screen (the "screen bitmap").

Using this method, when a color is adjusted, I have to loop through the whole color index array and re-render the entire bitmap. I'd like to avoid having to loop through the entire array.

One thing that I've tried is keeping a separate ALPHA_8 bitmap for each color in the palette. Each bitmap contains 0xff where a pixel is set in that color and 0x00 where it is not set. When a color is changed, I draw the ALPHA_8 bitmap for the color that is changing on top of the screen bitmap using a Paint with the new color.

This solution works, and it's more efficient than the naive looping solution, but the issue I'm having is that setPixel does not work on ALPHA_8 bitmaps. It silently fails. Instead, I need to maintain a separate ByteBuffer for each ALPHA_8 bitmap and then use copyPixelsFromBuffer to update the actual bitmap. This is undesirable because it turns what should be a O(1) operation into a O(n) operation and doubles the required storage for my ALPHA_8 bitmaps.

I feel like I'm not seeing an obvious solution. Is there something that I've missed?

0

There are 0 answers