你有種回答自己,但沒有代碼。我認爲你需要某事像:
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