I'm working on a module that handles colormaps and I want to make the mapping of pixel to colors as efficient as possible. It is a performance critical section of our app. Our current solution works like this:
I have a normalized double value x (range [0, 1]) per pixel that denotes the position inside the colormap. The colormap itself is layed out as a vector of arbitrary size of RGBA elements. Now I basically scale my double x*(colormap.size()-1) and access the color like so:
RGBA a = colormap[static_cast<int>(x*(colormap.size()-1))]
This methods has some drawbacks:
- With increasing resolution of the colormap, the vector storage is growing unnecessarily large causing a lot of cache misses as pixel indexes jump seemingly random inside the vector.
- I need to static_cast to integer which again costs me some muops. I guess it doesn't matter in the large scale that much but I question if this can be skipped with a more efficient algorithm
I bet that someone has come up with such a solution over time. I could for example imagine something like a parametric curve through a color cube. However: Fragmented colormaps should also be included (e.g. hard borders between different color areas) should be possible, which would be difficult with this approach.
Is there a more efficient approach?
EDIT: The colormap can either be fragmented or linear but not both. Maybe some optimization could be based on that.