你有种回答自己,但没有代码。我认为你需要某事像:
typedef struct
{
unsigned char r;
unsigned char g;
unsigned char b;
}somePixelFormat;
#define HEIGHT 2
#define WIDTH 4
// let's say this is original image width=4 height=2 expresed as one dimentional
// array of structs that adhere to your pixel format
somePixelFormat src[ WIDTH * HEIGHT ] =
{
{0,0,0}, {1,1,1}, {2,2,2}, {3,3,3},
{4,4,4}, {5,5,5}, {6,6,6}, {7,7,7}
};
somePixelFormat dst[ WIDTH * HEIGHT ];
void printImage(void *img, int width, int height, int pixelByteCount)
{
for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
printf("(%02d,%02d,%02d) ", ((somePixelFormat*)img + width * row + col)->r,
((somePixelFormat*)img + width * row + col)->g,
((somePixelFormat*)img + width * row + col)->b);
}
printf ("\n");
}
printf("\n\n");
}
void flip(void *dstImg, void *srcImg, int srcWidth, int srcHeight, int pixelByteCount)
{
for (int row = 0; row < srcHeight; row++)
{
for (int col = 0; col < srcWidth; col++)
{
*((somePixelFormat*)dstImg + srcHeight * col + row) = *((somePixelFormat*)srcImg + srcWidth * row + col);
}
}
}
int main()
{
printImage(src, 4, 2, sizeof(somePixelFormat));
flip(dst, src, 4, 2, sizeof(somePixelFormat));
printImage(dst, 2, 4, sizeof(somePixelFormat));
getchar();
return 0;
}
下面是输出示例:
(00,00,00) (01,01,01) (02,02,02) (03,03,03)
(04,04,04) (05,05,05) (06,06,06) (07,07,07)
(00,00,00) (04,04,04)
(01,01,01) (05,05,05)
(02,02,02) (06,06,06)
(03,03,03) (07,07,07)
双for循环将是对元素的数量O(N),你不能击败,因为你有将它们全部复制,但是在你的for循环中,可能会有一些你不需要每次执行的乘法。几乎不可能产生任何性能差异。如果图像很大,你当然可以将进程分成多个线程。 – CashCow 2012-02-01 18:01:33
[缓存高效矩阵移调程序?]的可能重复(http://stackoverflow.com/questions/5200338/a-cache-efficient-matrix-transpose-program) – 2012-02-01 18:02:52
您是否绝对需要复制它?如果你必须的话,没有更快的方法。你的复制算法是最优的,因为每个元素确实需要被访问。如果您不复制它,请考虑交换索引 - 也就是说,无论何时您需要索引索引,都用(i,j)而不是(i,j)索引它。你能做到吗?你可以很容易地看到这需要O(1)时间(也许O(1)空间)。 – mrm 2012-02-01 18:03:00