2 ms·
Here is a generic function that will turn any bit-depth into any other bit-depth: uint64_t convert(uint64_t color, uint sourceDepth, uint targetDepth) {
by byuu 7y ago
Here is a generic function that will turn any bit-depth into any other bit-depth:
uint64_t convert(uint64_t color, uint sourceDepth, uint targetDepth) {
if(sourceDepth == 0 || targetDepth == 0) return 0;
while(sourceDepth < targetDepth) {
color = (color << sourceDepth) | color;
sourceDepth += sourceDepth;
}
if(targetDepth < sourceDepth) color >>= (sourceDepth - targetDepth);
return color;
}
In fact, you probably should use normalization when converting arbitrary levels. A floating point multiply is almost certainly cheaper than a while loop. But hey, why not? Just don't use it for any real-time stuff unless you build a color palette cache.