本文共 3176 字,大约阅读时间需要 10 分钟。
笔者注:CIF文件内存放的是352*288真彩色数据
- #include <stdio.h>
- #include <stdlib.h>
-
- #define WIDTH 352
- #define HEIGHT 288
-
-
- double YuvToRgb[3][3] = {1, 0 , 1.4022 ,
- 1, -0.3456, -0.7145 ,
- 1, 1.771 , 0 };
-
-
- int WriteBmp(int width, int height, unsigned char *R,unsigned char *G,unsigned char *B, char *BmpFileName);
-
-
- int Convert(char *file, int width, int height, int n)
- {
-
- int i = 0;
- int temp = 0;
- int x = 0;
- int y = 0;
- int fReadSize = 0;
- int ImgSize = width*height;
- FILE *fp = NULL;
- unsigned char* yuv = NULL;
- unsigned char* rgb = NULL;
- unsigned char* cTemp[6];
- char BmpFileName[256];
-
-
- int FrameSize = ImgSize + (ImgSize >> 1);
- yuv = (unsigned char *)malloc(FrameSize);
- rgb = (unsigned char *)malloc(ImgSize*3);
-
- if((fp = fopen(file, "rb")) == NULL)
- return 0;
- fseek(fp, FrameSize*(n-1), SEEK_CUR);
- fReadSize = fread(yuv, 1, FrameSize, fp);
- fclose(fp);
- if(fReadSize < FrameSize)
- return 0;
-
- cTemp[0] = yuv;
- cTemp[1] = yuv + ImgSize;
- cTemp[2] = cTemp[1] + (ImgSize>>2);
- cTemp[3] = rgb;
- cTemp[4] = rgb + ImgSize;
- cTemp[5] = cTemp[4] + ImgSize;
- for(y=0; y < height; y++)
- for(x=0; x < width; x++)
- {
-
- temp = cTemp[0][y*width+x] + (cTemp[2][(y/2)*(width/2)+x/2]-128) * YuvToRgb[0][2];
- cTemp[3][y*width+x] = temp<0 ? 0 : (temp>255 ? 255 : temp);
-
- temp = cTemp[0][y*width+x] + (cTemp[1][(y/2)*(width/2)+x/2]-128) * YuvToRgb[1][1]
- + (cTemp[2][(y/2)*(width/2)+x/2]-128) * YuvToRgb[1][2];
- cTemp[4][y*width+x] = temp<0 ? 0 : (temp>255 ? 255 : temp);
-
- temp = cTemp[0][y*width+x] + (cTemp[1][(y/2)*(width/2)+x/2]-128) * YuvToRgb[2][1];
- cTemp[5][y*width+x] = temp<0 ? 0 : (temp>255 ? 255 : temp);
- }
-
-
- sprintf(BmpFileName, "%s%05d.bmp", file, n);
- WriteBmp(width, height, cTemp[3], cTemp[4], cTemp[5], BmpFileName);
-
- free(yuv);
- free(rgb);
- return n;
- }
-
- void main()
- {
- int i=1;
- for( i=0; i<260; i++)
- Convert("C://Documents and Settings//Administrator//桌面//cif//tempete.cif", WIDTH, HEIGHT, i);//获取文件的第i帧,此目录自定义
- }
-
-
- int WriteBmp(int width, int height, unsigned char *R,unsigned char *G,unsigned char *B, char *BmpFileName)
- {
- int x=0;
- int y=0;
- int i=0;
- int j=0;
- FILE *fp;
- unsigned char *WRGB;
- unsigned char *WRGB_Start;
- int yu = width*3%4;
- int BytePerLine = 0;
-
- yu = yu!=0 ? 4-yu : yu;
- BytePerLine = width*3+yu;
-
- if((fp = fopen(BmpFileName, "wb")) == NULL)
- return 0;
- WRGB = (unsigned char*)malloc(BytePerLine*height+54);
- memset(WRGB, 0, BytePerLine*height+54);
-
-
- WRGB[0] = 'B';
- WRGB[1] = 'M';
- *((unsigned int*)(WRGB+2)) = BytePerLine*height+54;
- *((unsigned int*)(WRGB+10)) = 54;
- *((unsigned int*)(WRGB+14)) = 40;
- *((unsigned int*)(WRGB+18)) = width;
- *((unsigned int*)(WRGB+22)) = height;
- *((unsigned short*)(WRGB+26)) = 1;
- *((unsigned short*)(WRGB+28)) = 24;
- *((unsigned short*)(WRGB+34)) = BytePerLine*height;
-
- WRGB_Start = WRGB + 54;
-
- for(y=height-1,j=0; y >= 0; y--,j++)
- {
- for(x=0,i=0; x<width; x++)
- {
- WRGB_Start[y*BytePerLine+i++] = B[j*width+x];
- WRGB_Start[y*BytePerLine+i++] = G[j*width+x];
- WRGB_Start[y*BytePerLine+i++] = R[j*width+x];
- }
- }
-
- fwrite(WRGB, 1, BytePerLine*height+54, fp);
- free(WRGB);
- fclose(fp);
- return 1;
- }
运行结束后在cif目录里会有BMP文件
转载地址:http://xnuab.baihongyu.com/