독자는 코드가 없지만 답변을 드렸습니다.
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)
루프의 두 배 당신이 그 요소의 수에 O (N)하고 이길 수없는 것입니다 나는 당신이 좋아하는 STH 필요가 있다고 생각 이들을 모두 for 루프 내에서 복사하려면 매번 수행 할 필요가없는 곱셈이있을 수 있습니다. 성능 차이는 거의 없을 것입니다. 물론 이미지가 클 경우 프로세스를 여러 스레드로 분할 할 수 있습니다. – CashCow
[캐시 효율 매트릭스 변환 프로그램?] (http://stackoverflow.com/questions/5200338/a-cache-efficient-matrix-transpose-program) –
복제본을 반드시 복사해야합니까? 네가해야한다면, 더 빠른 길은 없다.실제로 모든 요소를 방문해야하므로 복사 알고리즘이 최적입니다. 복사하지 않는다면 색인을 교환 할 필요가 있습니다. 즉, 색인 할 필요가있을 때마다 (i, j) 대신 (j, i)를 사용하여 색인을 생성하십시오. 그럴 수있어? O (1) 시간 (아마도 O (1) 공간)이 필요하다는 것을 쉽게 알 수 있습니다. – mrm