JM中的有用程序

JM中的有用程序

#if 0
/*
 * 1. dct方法
 
*/
 #include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define M 16    
#define BLOCK_SIZE 4
static int tmp[M];
void forward4x4(int (*block)[M], int (*tblock)[M], int pos_y, int pos_x)
{
  int i, ii;  
  int *pTmp = tmp, *pblock;
  static int p0,p1,p2,p3;
  static int t0,t1,t2,t3;
  // Horizontal
  for (i=pos_y; i < pos_y + BLOCK_SIZE; i++)
  {
    pblock = &block[i][pos_x];
    p0 = *(pblock++);
    p1 = *(pblock++);
    p2 = *(pblock++);
    p3 = *(pblock  );
    t0 = p0 + p3;
    t1 = p1 + p2;
    t2 = p1 - p2;
    t3 = p0 - p3;
    *(pTmp++) =  t0 + t1;
    *(pTmp++) = (t3 << 1) + t2;
    *(pTmp++) =  t0 - t1;    
    *(pTmp++) =  t3 - (t2 << 1);
  }
  // Vertical 
  for (i=0; i < BLOCK_SIZE; i++)
  {
    pTmp = tmp + i;
    p0 = *pTmp;
    p1 = *(pTmp += BLOCK_SIZE);
    p2 = *(pTmp += BLOCK_SIZE);
    p3 = *(pTmp += BLOCK_SIZE);
    t0 = p0 + p3;
    t1 = p1 + p2;
    t2 = p1 - p2;
    t3 = p0 - p3;
    ii = pos_x + i;
    tblock[pos_y    ][ii] = t0 +  t1;
    tblock[pos_y + 1][ii] = t2 + (t3 << 1);
    tblock[pos_y + 2][ii] = t0 -  t1;
    tblock[pos_y + 3][ii] = t3 - (t2 << 1);
  }
}
void print_arr(int arr[][M],int n)
{
    int j,i;
    for(j=0;j<n;j++)
    {
        for(i=0;i<M;i++)
            printf("%8d",arr[j][i]);
        printf("\n");
    }
}
int main()
{
   int orig[M][M];
   int dct[M][M];
   int j,i;
//   srand((int)time(NULL));
   for(j=0;j<M;j++)
       for(i=0;i<M;i++)
           orig[j][i]=rand()%100;
   print_arr(orig,M);
   for(j=0;j<M;j+=4)
   {
       for(i=0;i<M;i+=4)
           forward4x4(orig,dct,j,i);
   }
   print_arr(dct,M);


}
#endif
#if 0

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define M 16    
#define BLOCK_SIZE 4
static int tmp[16];
void forward4x4(int **block, int **tblock, int pos_y, int pos_x)
{
  int i, ii;  
  int *pTmp = tmp, *pblock;
  static int p0,p1,p2,p3;
  static int t0,t1,t2,t3;
  // Horizontal
  for (i=pos_y; i < pos_y + BLOCK_SIZE; i++)
  {
    pblock = &block[i][pos_x];
    p0 = *(pblock++);
    p1 = *(pblock++);
    p2 = *(pblock++);
    p3 = *(pblock  );
    t0 = p0 + p3;
    t1 = p1 + p2;
    t2 = p1 - p2;
    t3 = p0 - p3;
    *(pTmp++) =  t0 + t1;
    *(pTmp++) = (t3 << 1) + t2;
    *(pTmp++) =  t0 - t1;    
    *(pTmp++) =  t3 - (t2 << 1);
  }
  // Vertical 
  for (i=0; i < BLOCK_SIZE; i++)
  {
    pTmp = tmp + i;
    p0 = *pTmp;
    p1 = *(pTmp += BLOCK_SIZE);
    p2 = *(pTmp += BLOCK_SIZE);
    p3 = *(pTmp += BLOCK_SIZE);
    t0 = p0 + p3;
    t1 = p1 + p2;
    t2 = p1 - p2;
    t3 = p0 - p3;
    ii = pos_x + i;
    tblock[pos_y    ][ii] = t0 +  t1;
    tblock[pos_y + 1][ii] = t2 + (t3 << 1);
    tblock[pos_y + 2][ii] = t0 -  t1;
    tblock[pos_y + 3][ii] = t3 - (t2 << 1);
  }
}
void print_arr(int **arr,int n,int m)
{
    int j,i;
    for(j=0;j<n;j++)
    {
        for(i=0;i<m;i++)
            printf("%8d",arr[j][i]);
        printf("\n");
    }
}
int main()
{
   int **orig;
   int **dct;
   int j,i;
//   srand((int)time(NULL));
orig=malloc(sizeof(int*)*M);
dct=malloc(sizeof(int*)*M);
for(j=0;j<M;j++)
{
    orig[j]=malloc(sizeof(int)*M);
    dct[j]=malloc(sizeof(int)*M);
}
   for(j=0;j<M;j++)
       for(i=0;i<M;i++)
           orig[j][i]=rand()%100;
   print_arr(orig,M,M);
   for(j=0;j<M;j+=4)
   {
       for(i=0;i<M;i+=4)
           forward4x4(orig,dct,j,i);
   }
   print_arr(dct,M,M);
   for(j=0;j<M;j++)
{
    free(orig[j]);
    free(dct[j]);
}
free(orig);
free(dct);


}


#endif
#if 0
2. splityuv 函数
#if 1
/*
 * 13. 截取YUV中的某些帧
 
*/
#include <stdio.h>
#include <stdlib.h>
typedef unsigned char byte;

int main(int argc,char*argv[])
{
    char *yuvpath;
    char outpath[100];
    int from,to;
    FILE *p_in,*p_out;
    int height, width;
    int framebytes;
    byte *buf;
    if(argc<2) {
        printf("Usage:splityuv xxx.yuv from to \n");
        exit(-1);
    }
    yuvpath=argv[1];
    if(argc<3)
        from=0;
    else {
        int tmp=atoi(argv[2]);
        from=tmp>=0?tmp:0;
    }
    if(argc<4)
        to=130;
    else {
        int tmp=atoi(argv[3]);
        to=tmp>0?tmp:130;
    }
    if(argc<5) {
        height=144;
        width=176;
    }
    else {
        int tmph=atoi(argv[4]);
        int tmpw=atoi(argv[5]);
        height=tmph>0?tmph:144;
        width=tmpw>0?tmpw:176;
    }
    framebytes=height*width+height/2*width/2*2;
    if(NULL==(buf=(byte*)calloc(framebytes,sizeof(byte)))) {
        printf("Merrory allocate error!\n");
        exit(-1);
    }
    sprintf(outpath,"%s_%d_%d",yuvpath,from,to);
    if(NULL==(p_in=fopen(yuvpath,"r"))) {
        printf("open file error\n");
        exit(-4);
    }
    if(NULL==(p_out=fopen(outpath,"w"))) {
        printf("open file error\n");
        exit(-4);
    }
    {
        int i;
        for(i=from;i<to;++i)
        {
          if(-1==fseek(p_in,framebytes*i,SEEK_SET))
          {
            printf("read yuv file error\n");
            exit(10);
          }
         if(framebytes!=fread(buf,sizeof(byte),framebytes,p_in))
         {
           printf("read file error\n");
           exit(-3);
          }
          if(-1==fseek(p_out,framebytes*i,SEEK_SET))
          {
            printf("read yuv file error\n");
            exit(10);
          }
         if(framebytes!=fwrite(buf,sizeof(byte),framebytes,p_out))
         {
           printf("write file error\n");
           exit(-7);
          }

        }

    }
        fclose(p_in);
        fclose(p_out);
        free(buf);

    return 0;
}


#endif
#if 0
3. 读取一帧图像
/*
 *    Description: 读取yuv视频文件中的某一帧 
 
*/
#include <stdio.h>
#include <stdlib.h>
typedef unsigned char byte;
byte**y;
byte**u;
byte**v;
void getimgdata(FILE*p_in,int fno,int height,int width);
void memalloc(byte***src,int row,int col);
void freemem(byte**src,int row);
int main(int argc,char*argv[])
{
    char *yuvpath;
    int framenumber;
    FILE *p_in;
    int height;
    int width;
    if(argc<2)
    {
        printf("Usage:readimg xxx.yuv fno height width \n");
        exit(-1);
    }
    yuvpath=argv[1];
    if(argc<3)
    {
        framenumber=0;
    }
    else
    {
        framenumber=atoi(argv[2]);
    }
    if(argc<5)
    {
        height=144;
        width=176;
    }
    else
    {
        height=atoi(argv[3]);
        width=atoi(argv[4]);
    }
    memalloc(&y,height,width);
    memalloc(&u,height/2,width/2);
    memalloc(&v,height/2,width/2);
    if(NULL==(p_in=fopen(yuvpath,"r")))
    {
        printf("open file error\n");
        exit(-4);
    }

    getimgdata(p_in,framenumber,height,width);

    {
        FILE *p_y,*p_u,*p_v;
        FILE *p_y2,*p_u2,*p_v2;
        int i,j;
        char yname[20]="y";
        char yname2[20]="y";
        char uname[20]="u";
        char uname2[20]="u";
        char vname[20]="v";
        char vname2[20]="v";

        sprintf(yname,"%s_%d.txt",yname,framenumber);
        sprintf(yname2,"%s_%d.bin",yname2,framenumber);
        sprintf(uname,"%s_%d.txt",uname,framenumber);
        sprintf(uname2,"%s_%d.bin",uname2,framenumber);
        sprintf(vname,"%s_%d.txt",vname,framenumber);
        sprintf(vname2,"%s_%d.bin",vname2,framenumber);


    if(NULL==(p_y=fopen(yname,"w")))
    {
        printf("open file error\n");
        exit(-4);
    }
    if(NULL==(p_u=fopen(uname,"w")))
    {
        printf("open file error\n");
        exit(-4);
    }
    if(NULL==(p_v=fopen(vname,"w")))
    {
        printf("open file error\n");
        exit(-4);
    }
    if(NULL==(p_y2=fopen(yname2,"w")))
    {
        printf("open file error\n");
        exit(-4);
    }
    if(NULL==(p_u2=fopen(uname2,"w")))
    {
        printf("open file error\n");
        exit(-4);
    }
    if(NULL==(p_v2=fopen(vname2,"w")))
    {
        printf("open file error\n");
        exit(-4);
    }
    for(j=0;j<height;j++)
    {
        for(i=0;i<width;i++)
        {
            fprintf(p_y,"%d ",y[j][i]);
        }
        fprintf(p_y,"\n");
    fwrite(y[j],sizeof(byte),width,p_y2);
    }
    for(j=0;j<height/2;j++)
    {
        for(i=0;i<width/2;i++)
        {
            fprintf(p_u,"%d ",u[j][i]);
            fprintf(p_v,"%d ",v[j][i]);
        }
        fprintf(p_u,"\n");
        fprintf(p_v,"\n");
    fwrite(u[j],sizeof(byte),width/4,p_u2);
    fwrite(v[j],sizeof(byte),width/4,p_v2);
    }


    fclose(p_y);
    fclose(p_u);
    fclose(p_v);
    fclose(p_y2);
    fclose(p_u2);
    fclose(p_v2);
    }


    freemem(y,height);
    freemem(u,height/2);
    freemem(v,height/2);

    return 0;

}
void getimgdata(FILE*p_in,int fno,int height,int width)
{
    int bytes_y=height*width;
    int bytes_uv=height/2*width/2;
    int frame_bytes=bytes_y+bytes_uv*2;
    byte*buf;
    byte*buf_y;
    byte*buf_u;
    byte*buf_v;

    if(-1==fseek(p_in,frame_bytes*fno,SEEK_SET))
    {
        printf("read yuv file error\n");
        exit(10);
    }
    if(NULL==y||NULL==u||NULL==v)
    {
        printf("y/u/v:no memory allocation\n");
        exit(-2);
    }
    if(NULL==(buf=(byte*)calloc(frame_bytes,sizeof(byte))))
    {
        printf("memorry allocation error\n");
        exit(-1);
    }
    if(NULL==(buf_y=(byte*)calloc(bytes_y,sizeof(byte))))
    {
        printf("memorry allocation error\n");
        exit(-1);
    }
    if(NULL==(buf_u=(byte*)calloc(bytes_uv,sizeof(byte))))
    {
        printf("memorry allocation error\n");
        exit(-1);
    }
    if(NULL==(buf_v=(byte*)calloc(bytes_uv,sizeof(byte))))
    {
        printf("memorry allocation error\n");
        exit(-1);
    }
    if(frame_bytes!=fread(buf,sizeof(byte),frame_bytes,p_in))
    {
        printf("read file error\n");
        exit(-3);
    }
    memcpy(buf_y,buf,bytes_y);
    memcpy(buf_u,buf+bytes_y,bytes_uv);
    memcpy(buf_v,buf+bytes_y+bytes_uv,bytes_uv);
    {
        int j;
        for(j=0;j<height;j++)
            memcpy(y[j],buf_y+j*width,width);
        
        for(j=0;j<height/2;j++)
        {
            memcpy(u[j],buf_u+j*width/2,width/2);
            memcpy(v[j],buf_v+j*width/2,width/2);
        }
    }
    free(buf);
    free(buf_y);
    free(buf_u);
    free(buf_v);
}
void memalloc(byte***src,int row,int col)
{
    int i;
    *src=(byte**)calloc(row,sizeof(byte*));
    for(i=0;i<row;i++)
        (*src)[i]=(byte*)calloc(col,sizeof(byte));

}
void freemem(byte**src,int row)
{
    int i;
    for(i=0;i<row;i++)
        free(src[i]);
    free(src);
}


/*
 * 17. 根据坐标来计算角度
 
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define PI 3.1415926
int main(int argc,char*argv[])
{
    int y,x;
    double dge;
    double ddd;
    double ddd2;
    if(argc<3)
    {
        printf("wrong\n");
        exit(-1);
    }
    y=atoi(argv[1]);
    x=atoi(argv[2]);
    if(x==0&&y==0)
    {
        printf("bad input \n");
        exit(1);
    }
    if(x==0&&y>0)
       ddd=PI/2;
    else if(x==0&&y<0)
       ddd=3*PI/2;
    else if(y==0&&x>0)
       ddd=0;
    else if(y==0&&x<0)
       ddd=PI;
    else
    {
    dge=((double)y)/x;
    printf("dge=%f\n",dge);
    ddd=atan(dge);
    if(ddd<0)
        ddd+=PI;
    }
    printf("ddd=%f\n",ddd);
    ddd2=ddd*180/PI;
    printf("ddd2=%f\n",ddd2);
    return 0;

}
#endif

 

posted @ 2012-08-24 10:37  Mr.Rico  阅读(214)  评论(0编辑  收藏  举报