在看jpeg解码,里面有对8x8数组进行重排。里面直接提供了unzig表:
int unzig[] = {
0, 1, 8, 16, 9, 2, 3, 10,
17, 24, 32, 25, 18, 11, 4, 5,
12, 19, 26, 33, 40, 48, 41, 34,
27, 20, 13, 6, 7, 14, 21, 28,
35, 42, 49, 56, 57, 50, 43, 36,
29, 22, 15, 23, 30, 37, 44, 51,
58, 59, 52, 45, 38, 31, 39, 46,
53, 60, 61, 54, 47, 55, 62, 63,
};
然后自己写了个产生unzig表的程序。
:-)
代码如下:
#include
#include
#include
typedef void (*fn)(int, int, int, int, void*);
printpos(int x, int y, int n, int i, void *arr)
{
// printf("%2d%c", x+y*n, i%n==(n-1)?'\n':' ');
int *a;
a = (int*)arr;
printf("%2d%c", a[i], i%n==(n-1)?'\n':' ');
}
unzigasgn(int x, int y, int n, int i, void *arr)
{
int *a;
a = (int*)arr;
a[i] = x+y*n;
}
zigasgn(int x, int y, int n, int i, void *arr)
{
int *a;
a = (int*)arr;
a[x+y*n] = i;
}
zigzag(int n, fn f1, void *arr)
{
int i, x, y;
i = 0;
x = y = 0;
f1(x, y, n, i, arr);
for(;;) {
/* right, or down */
if(++i >= n*n)
return;
if(x+1 < n){
x++;
f1(x, y, n, i, arr);
}else{
y++;
f1(x, y, n, i, arr);
}
/* left down */
while(x-1 >= 0 && y+1 < n){
x--;
y++;
if(++i >= n*n)
return;
f1(x, y, n, i, arr);
}
/* down, or right */
if(++i >= n*n)
return;
if(y+1 < n){
y++;
f1(x, y, n, i, arr);
}else{
x++;
f1(x, y, n, i, arr);
}
/* right up */
while(x+1 < n && y-1 >= 0){
x++;
y--;
if(++i >= n*n)
return;
f1(x, y, n, i, arr);
}
}
}
testzigzag(int n)
{
int i;
int n2;
int *arr;
n2 = n*n;
arr = malloc(n*n*sizeof(*arr));
zigzag(n, (fn)zigasgn, arr);
zigzag(n, (fn)printpos, arr);
printf("\n\n");
zigzag(n, (fn)unzigasgn, arr);
zigzag(n, (fn)printpos, arr);
}
main(int argc, char **argv)
{
int n;
n = 8;
if(argc > 1){
n = atoi(argv[1]);
}
testzigzag(n);
return 0;
}