rgb2yuv444.cpp
上传用户:ycbin163
上传日期:2013-11-06
资源大小:1k
文件大小:1k
- #include <stdio.h>
- #include <stdlib.h>
- #include <iostream.h>
- #define INPUT "C:\akiyo_cif.raw"
- FILE *ifp, *ofp;
- int rgb_2_yuv(FILE *ifp, FILE *ofp);
- void main()
- {
- int result = 0;
- ifp = fopen(INPUT, "rb");
- ofp = fopen("C://output.raw", "wb+");
- result = rgb_2_yuv(ifp, ofp);
- if (result == -1)
- {
- cout<< "We did not change the colorn";
- }
- else if (result == 1)
- {
- cout<< "We converted RGB into YUV<4:4:4>!!n";
- }
- cout<<"Please press any key to get out ."<<endl;
- getchar();
- }
- int rgb_2_yuv(FILE *ifp, FILE *ofp)
- {
- int modifyItem = 0;
- // int itemsRead = 0;
- int i,j;
- unsigned char* RgbPixe;
- unsigned char RgbPixel24[288*352*3] = {0};
- unsigned char YuvPixel24[288*352*3] = {0};
- RgbPixe = RgbPixel24;
- fread(RgbPixel24, sizeof(char), 288*352*3, ifp);
- for ( i = 0 ; i < 288 ; i++ )
- {
- for ( j = 0 ; j <352 ; j++ )
- {
- YuvPixel24[i * 352 + j] = (unsigned char) (0.299 * RgbPixe[0] + 0.587 * RgbPixe[1] + 0.114 * RgbPixe[2] ); //set Y
- YuvPixel24[(i + 288) * 352 + j] = (unsigned char) (0.5 * RgbPixe[0] - 0.4187 * RgbPixe[1] - 0.0813 * RgbPixe[2] + 128); //set U
- YuvPixel24[(i + 2 * 288) * 352 + j] = (unsigned char)(-0.1687 * RgbPixe[0] - 0.3313 * RgbPixe[1] + 0.5 * RgbPixe[2] + 128); //set V
- RgbPixe = RgbPixe + 3 ;
- }
- }
- fwrite(YuvPixel24, sizeof(char), 288*352*3, ofp);
-
- return 1; // 1 means it is done well
- }