整数 [0,4095] 12 位到 tube{A,B,C} 是 c++ 中最快的方式

发布于 2025-01-06 14:39:47 字数 1384 浏览 1 评论 0原文

输入:一个整数 [0,4095] 12 位。
输出:一个包含 {A,B,C} all [0,255] 的管子

A,B,C 被指定为 0 到 255,其中 255 映射到 4 位中的 15。原因是我想构造一个 RGB 定义为 0 到 255 的 Color 结构。

我假设解决方案类似于对输入进行位移位以提取 3 组 4 位,然后乘以 17,即 (255/15 | 15 = 1111(二进制))。

你如何计算出这个最快的速度?

我自己的解决方案:

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()

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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜`诱少女 2025-01-13 14:39:47

首先,输入 0...4096 实际上是 12 位,这使得问题更容易理解。这是一种可能的解决方案:

int val; // 0...4096
int red =   ((val&(255<<8))>>8)*17;
int green = ((val&(255<<4))>>4)*17;
int blue =  ((val&(255<<0))>>0)*17;

我也保留了蓝色的位移位,以便您可以发现计算中的相似性。希望这有帮助。

First of all input 0...4096 is in fact 12 bits and this makes the question easier to understand. Here is one possible solution:

int val; // 0...4096
int red =   ((val&(255<<8))>>8)*17;
int green = ((val&(255<<4))>>4)*17;
int blue =  ((val&(255<<0))>>0)*17;

I have kept the bit shifting for blue as well so you can spot the similarity in the calculation. Hope this helps.

空城缀染半城烟沙 2025-01-13 14:39:47

您可以使用联合来更好地解析颜色编码的 12 位值。

union colorCoding
{
  unsigned int val:12;
  struct
  {
    unsigned int red:4;
    unsigned int blue:4;
    unsigned int green:4;
  };
};

You can use unions to better parse your color coded 12 bit value.

union colorCoding
{
  unsigned int val:12;
  struct
  {
    unsigned int red:4;
    unsigned int blue:4;
    unsigned int green:4;
  };
};
叹沉浮 2025-01-13 14:39:47

要从输入中获取前四位,您可以将其与 1111 进行“与”操作,然后将输入向右移位四位并重复该过程。这会得到 0 到 15 范围内的三个整数。

如果您想将其转换为 [0,255] 中的某个值,则将所有内容向左移位 4 位,然后将其与 进行 1111(为简单起见)。

A = (input&15)<<4|15;
input >>= 4;
B = (input&15)<<4|15;
input >>= 4;
C = (input&15)<<4|15;

或者(如果你想 0 映射到 0)

A = input&15;
A = A<<4|A;
input >>= 4;
B = input&15;
B = B<<4|B;
input >>= 4;
C = input&15;
C = C<<4|C;

To get the first four bits from the input, you can AND it with 1111, then bitshift the input to the right by four bits and repeat the process. This gets you three integers in the range of 0 to 15.

If you then want to convert that to something in [0,255], then bitshift everything to the left by four bits and OR it with 1111 (for simplicity).

A = (input&15)<<4|15;
input >>= 4;
B = (input&15)<<4|15;
input >>= 4;
C = (input&15)<<4|15;

or (if you want 0 to map to 0)

A = input&15;
A = A<<4|A;
input >>= 4;
B = input&15;
B = B<<4|B;
input >>= 4;
C = input&15;
C = C<<4|C;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文