CIF格式文件转BMP文件

来源:互联网 发布:c语言打印字母图形 编辑:程序博客网 时间:2024/05/01 08:26

笔者注:CIF文件内存放的是352*288真彩色数据

[cpp] view plaincopyprint?
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3.   
  4. #define WIDTH 352  
  5. #define HEIGHT 288  
  6.   
  7. //转换矩阵  
  8. double YuvToRgb[3][3] = {1,   0      ,  1.4022   ,  
  9.                          1,   -0.3456,   -0.7145 ,  
  10.                          1,   1.771  ,   0       };  
  11.   
  12. //根据RGB三分量写BMP,不必关注  
  13. int WriteBmp(int width, int height, unsigned char *R,unsigned char *G,unsigned char *B, char *BmpFileName);  
  14.   
  15. //转换函数  
  16. int Convert(char *file, int width, int height, int n)  
  17. {  
  18.     //变量声明  
  19.     int i = 0;  
  20.     int temp = 0;  
  21.     int x = 0;  
  22.     int y = 0;  
  23.     int fReadSize = 0;  
  24.     int ImgSize = width*height;  
  25.     FILE *fp = NULL;  
  26.     unsigned char* yuv = NULL;  
  27.     unsigned char* rgb = NULL;  
  28.     unsigned char* cTemp[6];  
  29.     char BmpFileName[256];  
  30.   
  31.     //申请空间  
  32.     int FrameSize = ImgSize + (ImgSize >> 1);  
  33.     yuv = (unsigned char *)malloc(FrameSize);  
  34.     rgb = (unsigned char *)malloc(ImgSize*3);  
  35.     //读取指定文件中的指定帧  
  36.     if((fp = fopen(file, "rb")) == NULL)  
  37.         return 0;  
  38.     fseek(fp, FrameSize*(n-1), SEEK_CUR);  
  39.     fReadSize = fread(yuv, 1, FrameSize, fp);  
  40.     fclose(fp);  
  41.     if(fReadSize < FrameSize)  
  42.         return 0;  
  43.     //转换指定帧  如果你不是处理文件 主要看这里  
  44.     cTemp[0] = yuv;                        //y分量地址  
  45.     cTemp[1] = yuv + ImgSize;              //u分量地址  
  46.     cTemp[2] = cTemp[1] + (ImgSize>>2);    //v分量地址  
  47.     cTemp[3] = rgb;                        //r分量地址  
  48.     cTemp[4] = rgb + ImgSize;              //g分量地址  
  49.     cTemp[5] = cTemp[4] + ImgSize;         //b分量地址  
  50.     for(y=0; y < height; y++)  
  51.         for(x=0; x < width; x++)  
  52.         {  
  53.             //r分量  
  54.             temp = cTemp[0][y*width+x] + (cTemp[2][(y/2)*(width/2)+x/2]-128) * YuvToRgb[0][2];  
  55.             cTemp[3][y*width+x] = temp<0 ? 0 : (temp>255 ? 255 : temp);  
  56.             //g分量  
  57.             temp = cTemp[0][y*width+x] + (cTemp[1][(y/2)*(width/2)+x/2]-128) * YuvToRgb[1][1]  
  58.                                        + (cTemp[2][(y/2)*(width/2)+x/2]-128) * YuvToRgb[1][2];  
  59.             cTemp[4][y*width+x] = temp<0 ? 0 : (temp>255 ? 255 : temp);  
  60.             //b分量  
  61.             temp = cTemp[0][y*width+x] + (cTemp[1][(y/2)*(width/2)+x/2]-128) * YuvToRgb[2][1];  
  62.             cTemp[5][y*width+x] = temp<0 ? 0 : (temp>255 ? 255 : temp);  
  63.         }  
  64.   
  65.     //写到BMP文件中  
  66.     sprintf(BmpFileName, "%s%05d.bmp", file, n);  
  67.     WriteBmp(width, height, cTemp[3], cTemp[4], cTemp[5], BmpFileName);  
  68.   
  69.     free(yuv);  
  70.     free(rgb);  
  71.     return n;  
  72. }  
  73. //入口 没啥东西  
  74. void main()  
  75. {  
  76.     int i=1;  
  77.     for( i=0; i<260; i++)  
  78.         Convert("C://Documents and Settings//Administrator//桌面//cif//tempete.cif", WIDTH, HEIGHT, i);//获取文件的第i帧,此目录自定义  
  79. }  
  80.   
  81. //写BMP  不必关注  
  82. int WriteBmp(int width, int height, unsigned char *R,unsigned char *G,unsigned char *B, char *BmpFileName)  
  83. {  
  84.     int x=0;  
  85.     int y=0;  
  86.     int i=0;  
  87.     int j=0;  
  88.     FILE *fp;  
  89.     unsigned char *WRGB;  
  90.     unsigned char *WRGB_Start;  
  91.     int yu = width*3%4;  
  92.     int BytePerLine = 0;  
  93.   
  94.     yu = yu!=0 ? 4-yu : yu;  
  95.     BytePerLine = width*3+yu;  
  96.   
  97.     if((fp = fopen(BmpFileName, "wb")) == NULL)  
  98.         return 0;  
  99.     WRGB = (unsigned char*)malloc(BytePerLine*height+54);  
  100.     memset(WRGB, 0, BytePerLine*height+54);  
  101.       
  102.     //BMP头  
  103.     WRGB[0] = 'B';  
  104.     WRGB[1] = 'M';  
  105.     *((unsigned int*)(WRGB+2)) = BytePerLine*height+54;  
  106.     *((unsigned int*)(WRGB+10)) = 54;  
  107.     *((unsigned int*)(WRGB+14)) = 40;  
  108.     *((unsigned int*)(WRGB+18)) = width;  
  109.     *((unsigned int*)(WRGB+22)) = height;  
  110.     *((unsigned short*)(WRGB+26)) = 1;  
  111.     *((unsigned short*)(WRGB+28)) = 24;  
  112.     *((unsigned short*)(WRGB+34)) = BytePerLine*height;  
  113.   
  114.     WRGB_Start = WRGB + 54;  
  115.   
  116.     for(y=height-1,j=0; y >= 0; y--,j++)  
  117.     {  
  118.         for(x=0,i=0; x<width; x++)  
  119.         {  
  120.             WRGB_Start[y*BytePerLine+i++] = B[j*width+x];  
  121.             WRGB_Start[y*BytePerLine+i++] = G[j*width+x];  
  122.             WRGB_Start[y*BytePerLine+i++] = R[j*width+x];  
  123.         }  
  124.     }  
  125.   
  126.     fwrite(WRGB, 1, BytePerLine*height+54, fp);  
  127.     free(WRGB);  
  128.     fclose(fp);  
  129.     return 1;  
  130. }  

运行结束后在cif目录里会有BMP文件


原创粉丝点击