Intput: An integer [0,4095] 12bits.
Output: A tuble of {A,B,C} all [0,255]
The A,B,C are given as 0 to 255, where 255 maps to 15 in the 4 bits. Reason are that I want to construct a Color struct having RGB defined from 0 to 255.
I assume the solution to be something like bit shifting the input to extract the 3 sets of 4bits and then multiply by 17 as (255/15 | 15 = 1111(binary)).
How would you compute this fastest?
my own solution:
QColor mycolor(int value)
{
if(value > 0xFFF)
value = 0xFFF;
int a=0,b=0,c=0;
a = (value & 0xF) * 17;
b = ((value&(0xF<<4))>>4) *17;
c = ((value&(0xF<<8))>>8) *17;
return QColor(c,b,a);
}
cv::Mat cv_image(10,10,CV_16U,cv::Scalar::all(1));
QImage image(cv_image.data, 10,10,QImage::Format_RGB444);
QPainter p(&image);
p.setPen(mycolor(255));
p.drawLine(0,0,9,0);
p.setPen(mycolor(4095));
p.drawLine(0,1,9,1);
p.setPen(mycolor(0));
p.drawLine(0,2,9,2);
p.setPen(mycolor(10000));
p.drawLine(0,3,9,3);
********* Start testing of Test1 *********
Config: Using QTest library 4.7.4, Qt 4.7.4
PASS : Test1::initTestCase()
[255, 255, 255, 255, 255, 255, 255, 255, 255, 255;
4095, 4095, 4095, 4095, 4095, 4095, 4095, 4095, 4095, 4095;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
4095, 4095, 4095, 4095, 4095, 4095, 4095, 4095, 4095, 4095;
1, 1, 1, 1, 1, 1, 1, 1, 1, 1;
1, 1, 1, 1, 1, 1, 1, 1, 1, 1;
1, 1, 1, 1, 1, 1, 1, 1, 1, 1;
1, 1, 1, 1, 1, 1, 1, 1, 1, 1;
1, 1, 1, 1, 1, 1, 1, 1, 1, 1;
1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
PASS : Test1::test1()
First of all input 0…4096 is in fact 12 bits and this makes the question easier to understand. Here is one possible solution:
I have kept the bit shifting for blue as well so you can spot the similarity in the calculation. Hope this helps.