rgb2yuv444.cpp
上传用户:ycbin163
上传日期:2013-11-06
资源大小:1k
文件大小:1k
源码类别:

图形图像处理

开发平台:

Visual C++

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <iostream.h>
  4. #define INPUT "C:\akiyo_cif.raw"
  5. FILE *ifp, *ofp;
  6. int rgb_2_yuv(FILE *ifp, FILE *ofp);
  7. void main()
  8. {
  9.   int  result = 0;
  10.   ifp = fopen(INPUT, "rb");
  11.   ofp = fopen("C://output.raw", "wb+");
  12.   result = rgb_2_yuv(ifp, ofp);
  13.   if (result == -1) 
  14.   {
  15.   cout<< "We did not change the colorn";
  16.   }
  17.   else if (result == 1) 
  18.   {
  19.   cout<< "We converted RGB into YUV<4:4:4>!!n";
  20.   }
  21.   cout<<"Please press any key to get out ."<<endl;
  22.   getchar();
  23. }
  24. int rgb_2_yuv(FILE *ifp, FILE *ofp)
  25. {
  26.         int    modifyItem = 0;
  27. //        int    itemsRead = 0;
  28.         int i,j;
  29. unsigned char* RgbPixe;
  30. unsigned char  RgbPixel24[288*352*3] = {0};
  31. unsigned char  YuvPixel24[288*352*3] = {0};
  32. RgbPixe = RgbPixel24;
  33.         fread(RgbPixel24, sizeof(char), 288*352*3, ifp);
  34.         for ( i = 0 ; i < 288 ; i++ )
  35. {
  36. for ( j = 0 ; j <352 ; j++ )
  37. {
  38. YuvPixel24[i * 352 + j]  =  (unsigned char) (0.299 * RgbPixe[0] + 0.587 * RgbPixe[1] +  0.114 * RgbPixe[2] );    //set Y
  39. YuvPixel24[(i + 288) * 352 + j]  = (unsigned char)  (0.5 * RgbPixe[0] -  0.4187 * RgbPixe[1] - 0.0813 * RgbPixe[2] + 128);   //set U 
  40. YuvPixel24[(i + 2 * 288) * 352 + j]  =  (unsigned char)(-0.1687 * RgbPixe[0] -  0.3313 * RgbPixe[1] +  0.5 * RgbPixe[2] + 128);  //set V
  41. RgbPixe = RgbPixe + 3 ;
  42. }
  43.        fwrite(YuvPixel24, sizeof(char), 288*352*3, ofp);
  44.  
  45.        return 1;  // 1 means it is done well
  46. }