📄 morton_tans.c
字号:
/*----------Morton ordering---------------------------------
*Transposition
*aim:
* 矩阵向左旋转90度.
*function:
* Block_Roate32: 小块矩阵旋转
* trc: 划分函数
*
*parameter:
* WIDTH: 图像宽度
* HEIGHT:图像高度
* BLOCK_W:块的宽度
* BLOCK_H:块的高度
* BLOCK_NUM:块的划分,分四块时 BLOCK_NUM = 4,
* 分16块时 BLOCK_NUM = 8,分32块时 BLOCK_NUM = 8,
*
*arithmetic advantage:
* 能过充利用cache的临近访问优势,提高cache的命中率
*
*note:
* 1:图像宽度和高度必须为4的倍数
* 2:函数只处理16色的图像,垂直分辨71*71
* 3:源图像内存和目标内存不应当是同一个内存块]
* 4:划分的块的大小也必须是4的倍数
*
*time: 2005-12-23 整理
*author: fred-liu@qq.com
*-------------------------------------------------------*/
//PIC 352*288
#define BLOCK_NUM 8 //划分成八部分,共 4*4块
#define WIDTH 352
#define HEIGHT 288
#define BLOCK_W 72 // 352/8
#define BLOCK_H 88 // 288/8
/*
*块转换函数1
*pack16lsb:两个低16位转换成一个整数
*pack16msb:两个高16位转换成一个整数
*4字节转换
*/
void Block_Roate32(unsigned int * restrict image_in,
unsigned int * restrict image_out,
int width,
int height,
int block_w,
int block_h)
{
int m,n;
for( m =0; m<(block_h>>1); m ++){
for( n =0; n<(block_w>>1); n ++){
image_out[(block_h>>1)-1 - m + n*height] =
pack16lsb(image_in[n + m*width],image_in[n + m*width +(width>>1)]);
image_out[(block_h>>1)-1 - m + (height>>1) + n*height] =
pack16msb(image_in[n + m*width],image_in[n + m*width +(width>>1)]);
}
}
}
/*
*块转换函数2
*对每个像素进行转换
*双字节转换
*/
void block_trans16(unsigned short int* restrict matrix_in,
unsigned short int* restrict matrix_out,
int width,
int heigh,
int block_w,
int block_h)
{
int m ;
int n ;
for(m =0; m < block_h; m ++){
for(n =0; n < block_w; n ++){
matrix_out[block_h - m + n*heigh -1] = matrix_in[n + m*width];
}
}
}
/*
*块转换函数3
*对每个像素进行转换
*单字节转换
*/
void block_trans(unsigned char * restrict matrix_in,
unsigned char * restrict matrix_out,
int width,
int heigh,
int block_w,
int block_h)
{
int m =0;
int n =0;
for(m =0; m < block_h<<1; m +=2){
for(n =0; n < block_w<<1; n +=2){
matrix_out[(block_h<<1) - m + n*heigh-2 ] = matrix_in[n + m*width];
matrix_out[(block_h<<1) - m + n*heigh- 1] = matrix_in[n + m*width +1];
}
}
}
/*
*入口函数1
*src:源图像
*dst:目标图像
*n:块划分参数
*说明:
* 块的划分必须是4的倍数
*/
void trc(char unsigned *src, char unsigned *dst,int n)
{
int nn1 = n/2;
if(nn1 == 1)
{//块转换
Block_Roate32((unsigned int *)src,(unsigned int *)dst, WIDTH,HEIGHT,BLOCK_W,BLOCK_H);
}
else
{
trc(&src[0], &dst[HEIGHT], nn1);//3
trc(&src[WIDTH], &dst[HEIGHT*WIDTH + HEIGHT], nn1);//4
trc(&src[WIDTH*HEIGHT], &dst[0] , nn1);//1
trc(&src[WIDTH*HEIGHT+WIDTH], &dst[HEIGHT*WIDTH ] , nn1); //2
}
}
/*
*入口函数2
*说明:
* 块的划分必须是4的倍数
*/
void BlockRoate32(unsigned int * restrict image_in,
unsigned int * restrict image_out,
int width,
int height,
int block_w,
int block_h)
{
int i,j ;
for(i =0; i<height/block_h ; i ++){
for(j =0; j<width/block_w ; j ++){
Block_Roate32(&image_in[i*(width>>1)*block_h + j*(block_w>>1)],
&image_out[(height>>1)-i*(block_h>>1)+ j*block_w*(height>>1) -(block_h>>1)],
width,height,block_w,block_h);
}
}
}
/*
*入口函数3
*说明:
* 块的划分必须是4的倍数
*/
void BlockRoate(unsigned char * restrict image_in,
unsigned char * restrict image_out,
int width,
int height,
int block_w,
int block_h)
{
int i;
int j;
for(i =0; i<height/block_h ; i ++){
for(j =0; j<width/block_w ; j ++){
//单字节转换
block_trans(&image_in[i*(width<<1)*block_h + j*(block_w<<1)],
&image_out[(height<<1)- i*(block_h<<1)+ j*(height<<1)*block_w - (block_h<<1)],
width,height,block_w, block_h);
}
}
}
//
void main()
{
//
//trc(src, dst, 4) //分4块
//trc(src, dst, 8) //分16块
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -