如何重新排序原始图像颜色数据,以实现特定的2 × 2格式从四张图像?(c++)



我有四个图像的原始颜色数据,我们称它们为1、2、3和4。我将数据存储在分配内存的unsigned char *中。我可以单独操作或编码图像,但当试图将它们连接或排序为单个图像时,它可以工作,但需要比我想要的更多的时间。

我想创建一个2 × 2的原始图像数据编码为单个图像。

1 2
3 4

对于我的例子,每个图像是400 × 225的RGBA(360000字节)。我正在用memcpy做for循环,其中

for (int j = 0; j < 225; j++)
{
std::memcpy(dest + (j * (400 + 400) * 4), src + (j * 400 * 4), 400 * 4); //
}

为每个图像添加了起始位置的偏移量(当然上面的例子只适用于左上角)。

这是有效的,但我想知道这是否是一个解决问题的更好的解决方案,无论是在某处描述的算法或一个小库。

#include <iostream>
const int width  = 6;
const int height = 4;
constexpr int n  = width * height;
int main()
{
unsigned char a[n], b[n], c[n], d[n];
unsigned char dst[n * 4];
int i = 0, j = 0;
/* init data */
for (; i < n; i++) {
a[i] = 'a';
b[i] = 'b';
c[i] = 'c';
d[i] = 'd';
}
/* re-order */
i = 0;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++, i++, j++) {
dst[i                ] = a[j];
dst[i         + width] = b[j];
dst[i + n * 2        ] = c[j];
dst[i + n * 2 + width] = d[j];
}
i += width;
}
/* print result */
i = 0;
for (int y = 0; y < height * 2; y++) {
for (int x = 0; x < width * 2; x++, i++)
std::cout << dst[i];
std::cout << 'n';
}
return 0;
}

相关内容