我需要将表示RGB字节顺序的字节数组中图像的内容复制到另一个RGBA(每个像素4个字节)缓冲区中。透明度通道稍后会被填充。最快的实现方式是什么?
我需要将表示RGB字节顺序的字节数组中图像的内容复制到另一个RGBA(每个像素4个字节)缓冲区中。透明度通道稍后会被填充。最快的实现方式是什么?
void fast_unpack(char* rgba, const char* rgb, const int count) {
if(count==0)
return;
for(int i=count; --i; rgba+=4, rgb+=3) {
*(uint32_t*)(void*)rgba = *(const uint32_t*)(const void*)rgb;
}
for(int j=0; j<3; ++j) {
rgba[j] = rgb[j];
}
}
我的简单实验确实证实这在我的 x86 机器上至少略微更快。以下是基准测试:
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <time.h>
void fast_unpack(char* rgba, const char* rgb, const int count) {
if(count==0)
return;
for(int i=count; --i; rgba+=4, rgb+=3) {
*(uint32_t*)(void*)rgba = *(const uint32_t*)(const void*)rgb;
}
for(int j=0; j<3; ++j) {
rgba[j] = rgb[j];
}
}
void simple_unpack(char* rgba, const char* rgb, const int count) {
for(int i=0; i<count; ++i) {
for(int j=0; j<3; ++j) {
rgba[j] = rgb[j];
}
rgba += 4;
rgb += 3;
}
}
int main() {
const int count = 512*512;
const int N = 10000;
char* src = (char*)malloc(count * 3);
char* dst = (char*)malloc(count * 4);
clock_t c0, c1;
double t;
printf("Image size = %d bytes\n", count);
printf("Number of iterations = %d\n", N);
printf("Testing simple unpack....");
c0 = clock();
for(int i=0; i<N; ++i) {
simple_unpack(dst, src, count);
}
c1 = clock();
printf("Done\n");
t = (double)(c1 - c0) / (double)CLOCKS_PER_SEC;
printf("Elapsed time: %lf\nAverage time: %lf\n", t, t/N);
printf("Testing tricky unpack....");
c0 = clock();
for(int i=0; i<N; ++i) {
fast_unpack(dst, src, count);
}
c1 = clock();
printf("Done\n");
t = (double)(c1 - c0) / (double)CLOCKS_PER_SEC;
printf("Elapsed time: %lf\nAverage time: %lf\n", t, t/N);
return 0;
}
最快的方法是使用实现转换的库,而不是自己编写。您要面向哪个平台?
如果出于某些原因坚持自己编写,请先编写简单且正确的版本。使用该版本。如果性能不足,然后再考虑优化。一般来说,这种转换最好使用向量排列,但确切的最佳序列取决于目标架构。
struct rgb {
char r;
char g;
char b;
};
struct rgba {
char r;
char g;
char b;
char a;
}
void convert(struct rgba * dst, const struct rgb * src, size_t num)
{
size_t i;
for (i=0; i<num; i++) {
dst[i].r = src[i].r;
dst[i].g = src[i].g;
dst[i].b = src[i].b;
}
}
这是更加简洁的解决方案,但由于你提到了字节数组,你应该使用这个:
// num is still the size in pixels. So dst should have space for 4*num bytes,
// while src is supposed to be of length 3*num.
void convert(char * dst, const char * src, size_t num)
{
size_t i;
for (i=0; i<num; i++) {
dst[4*i] = src[3*i];
dst[4*i+1] = src[3*i+1];
dst[4*i+2] = src[3*i+2];
}
}
我记得有一篇Nehe的教程,讲述了如何快速做到这样的事情。
它在 这里。
有趣的部分在这里:
void flipIt(void* buffer) // Flips The Red And Blue Bytes (256x256)
{
void* b = buffer; // Pointer To The Buffer
__asm // Assembler Code To Follow
{
mov ecx, 256*256 // Set Up A Counter (Dimensions Of Memory Block)
mov ebx, b // Points ebx To Our Data (b)
label: // Label Used For Looping
mov al,[ebx+0] // Loads Value At ebx Into al
mov ah,[ebx+2] // Loads Value At ebx+2 Into ah
mov [ebx+2],al // Stores Value In al At ebx+2
mov [ebx+0],ah // Stores Value In ah At ebx
add ebx,3 // Moves Through The Data By 3 Bytes
dec ecx // Decreases Our Loop Counter
jnz label // If Not Zero Jump Back To Label
}
}
它的功能相当自我解释,将其转换为添加阿尔法字节应该很容易。
只需创建大小为源数组的4/3的数组。读取整个数组并将其写入RGBA数组,但在每3个字节后插入255作为alpha通道。