c++ - C++:具有缓冲区和指针循环的功能-是否有可能进行优化?

标签 c++ pointers

我编写了一个函数,通过仅占用高位字节,将具有两个字节的图像转换为8位图像。

我仍然需要原始图像,因此无法修改原始数组。

调用此函数来处理视频流的每个帧。

您知道优化速度功能的可能性吗?

谢谢!

QImage* createQImage(uchar *ptr, uint width, uint height, uint channels)
{
   static uint size = width * height * channels;
   static uchar *buffer = new uchar[size];


   //Take every second byte (upper byte)
   for(uint i=0; i<size; i++) {
       buffer[i] = (*(ptr+1));
       ptr = ptr+2;
   }

   static QImage img = QImage(buffer, width, height, QImage::Format_BGR888);
   static QImage *ptr_img = &img;

   return ptr_img;
}

最佳答案

如果只是将输入缓冲区移到输出缓冲区,一次执行一个字节并不是最快的。简单地将uchar *ptr转换为uint64_t*并使用位移/掩码拉出字节可能是优化此方法的方法。最后,您将需要以与已经处理过的相同方式在“末尾”剩下的一些字节进行“手动”处理。假设您的图像是16位,然后16/64 == 4,则速度提高了4倍。

假设您的硬件一次可以加载64位,则可以使用uint32_t将此代码一次加载32位,但这是大多数人都拥有64位硬件的合理假设。该代码假定您使用的是低端字节序硬件。

代码(未经测试)。

QImage* createQImage(uchar* ptr, uint width, uint height, uint channels) {
    uint64_t* nptr = (uint64_t*)ptr;

    static uint size = width * height * channels;
    static uchar *buffer = new uchar[size];
    static uint bytesize = size*2;

    uint idx = 0;
    uint i = 0;
    for(; i + sizeof(uint64_t) <= bytesize; i+=sizeof(uint64_t)) {
        //get 64 bits at a time
        uint64_t val = nptr[i/sizeof(uint64_t)];

        //get 4 bytes at a time
        //note inverted due to little endian assumed (maybe wrong)
        buffer[idx] = (uchar)(val >> (8 * 6));
        ++idx;
        buffer[idx] = (uchar)(val >> (8 * 4));
        ++idx;
        buffer[idx] = (uchar)(val >> (8 * 2));
        ++idx;
        buffer[idx] = (uchar)(val >> (8 * 0));
        ++idx;
    }

    //We have some bytes left at the end, process these a byte at a time
    //Take every second byte (upper byte)
    for(; i < bytesize; i+=2) {
        buffer[idx] = (*(ptr + i + 1));
        ++idx;
    }

    static QImage img = QImage(buffer, width, height, QImage::Format_BGR888);
    static QImage *ptr_img = &img;

    return ptr_img;
}

关于c++ - C++:具有缓冲区和指针循环的功能-是否有可能进行优化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61347383/

相关文章:

c - 错误: too few arguments in sorting void?

python - 模拟 python 类属性的类似指针的行为

c++ - 为变量和激活记录保留空间

c++ - 编译器无法识别自定义 numeric_limits 成员函数

c++ - 如何正确返回具有 A 类元素的 vector ?

c++ - 在链表中,为什么我不能取消引用下一个指针来打印下一个节点的值

c - 为什么 printf() 在 Windows 上打印这个神秘的额外文本?

c - 从指针访问结构体成员

c++ - 如何获取文件的字节数?

c++ - 动态并行性无效文件格式