RGB转YUV422,420

  1. #include <stdio.h>           
  2. #include <stdlib.h>           
  3. #include <iostream.h>           
  4. FILE *ifp, *ofp;           
  5. int rgb_2_yuv422(FILE *ifp, FILE *ofp);           
  6. int rgb_2_yuv420(FILE *ifp, FILE *ofp);           
  7.        
  8. int select_option(void);           
  9.        
  10. void main()           
  11. {           
  12. int   result = 0,selectoption = 0;           
  13.        
  14. selectoption = select_option();           
  15.        
  16. switch(selectoption)           
  17. {           
  18. case 1:           
  19. result = rgb_2_yuv422(ifp, ofp);           
  20. break;           
  21.        
  22. case 2:           
  23.        result = rgb_2_yuv420(ifp, ofp);           
  24.       break;           
  25.        
  26. default:           
  27. ;           
  28. }           
  29.        
  30. if (result == -1)           
  31. {           
  32. cout<< "You did not change anything!!/n";           
  33. }           
  34. else if (result == 3)           
  35. {           
  36. cout<< "You convert from RGB to YUV422!!/n";           
  37. }           
  38. else if (result == 4)           
  39. {           
  40. cout<< "You convert from RGB to YUV420!!/n";           
  41. }           
  42.        
  43. }           
  44. int rgb_2_yuv422(FILE *ifp, FILE *ofp)           
  45. {    ifp = fopen("C://fore_cif.raw", "rb");           
  46.      ofp = fopen("C://output.raw", "wb+");           
  47.     int i,j;           
  48.     int R, G, B;           
  49.     int Y, U, V;           
  50.      unsigned char *RGBBuffer = new unsigned char[512*512*3];           
  51.      unsigned char *YBuffer = new unsigned char[512*512];           
  52.      unsigned char *UBuffer = new unsigned char[512*512/2];           
  53.      unsigned char *VBuffer = new unsigned char[512*512/2];           
  54.      unsigned char *ULine = (new unsigned char[512+2])+1;           
  55.      unsigned char *VLine = (new unsigned char[512+2])+1;           
  56.      ULine[-1]=ULine[512]=128;           
  57.      VLine[-1]=VLine[512]=128;           
  58.        
  59.      fread(RGBBuffer, sizeof(char), 512*512*3, ifp);           
  60.        
  61.     for (i=0; i<512; ++i)           
  62. {           
  63.         int RGBIndex = 3*512*i;           
  64.         int YIndex    = 512*i;           
  65.         int UVIndex   = 512*i/2;           
  66.        
  67.         for ( j=0; j<512; ++j)           
  68. {           
  69.           R = RGBBuffer[RGBIndex++];           
  70.           G = RGBBuffer[RGBIndex++];           
  71.           B = RGBBuffer[RGBIndex++];           
  72.        
  73.          //Convert RGB to YUV           
  74.           Y = (unsigned char)( ( 66 * R + 129 * G +   25 * B + 128) >> 8) + 16   ;           
  75.           U = (unsigned char)( ( -38 * R -   74 * G + 112 * B + 128) >> 8) + 128 ;           
  76.           V = (unsigned char)( ( 112 * R -   94 * G -   18 * B + 128) >> 8) + 128 ;           
  77.           YBuffer[YIndex++] = static_cast<unsigned char>( (Y<0) ? 0 : ((Y>255) ? 255 : Y) );           
  78.           VLine[j] = V;           
  79. ULine[j] = U;           
  80.          }           
  81.         for ( j=0; j<512; j+=2)           
  82. {           
  83.          //Filter line           
  84.           V = ((VLine[j-1]+2*VLine[j]+VLine[j+1]+2)>>2);           
  85. U = ((ULine[j-1]+2*ULine[j]+ULine[j+1]+2)>>2);           
  86.        
  87.          //Clip and copy UV to output buffer               
  88.           VBuffer[UVIndex] = static_cast<unsigned char>( (V<0) ? 0 : ((V>255) ? 255 : V) );           
  89.           UBuffer[UVIndex++] = static_cast<unsigned char>( (U<0) ? 0 : ((U>255) ? 255 : U) );           
  90.          }           
  91.      }           
  92. fwrite(YBuffer, sizeof(char), 512*512, ofp);           
  93. fwrite(VBuffer, sizeof(char), 512*512/2, ofp);           
  94. fwrite(UBuffer, sizeof(char), 512*512/2, ofp);           
  95. return 3;           
  96. fclose(ifp);           
  97. fclose(ofp);           
  98. }           
  99.        
  100. int rgb_2_yuv420(FILE *ifp, FILE *ofp)           
  101. {    ifp = fopen("C://news_cif.raw", "rb");           
  102.      ofp = fopen("C://output.raw", "wb+");           
  103. int i,j;           
  104. int R, G, B;           
  105.     int Y, U, V;           
  106.      unsigned char *RGBBuffer = new unsigned char[512*512*3];           
  107.      unsigned char *YBuffer = new unsigned char[512*512];           
  108.      unsigned char *UBuffer = new unsigned char[512*512/4];           
  109.      unsigned char *VBuffer = new unsigned char[512*512/4];           
  110.     int *ULine = (new int[512+2])+1;           
  111.     int *VLine = (new int[512+2])+1;           
  112.      ULine[-1]=ULine[512]=128;           
  113.      VLine[-1]=VLine[512]=128;           
  114.     int *UImage = (new int[(512+2)*(512+2)])+(512+2)+1;           
  115.     int *VImage = (new int[(512+2)*(512+2)])+(512+2)+1;           
  116.      fread(RGBBuffer, sizeof(char), 512*512*3, ifp);           
  117.     for (i=0; i<512; ++i)           
  118. {           
  119.         int RGBIndex = 3*512*i;           
  120.         int YIndex    = 512*i;           
  121.         for ( j=0; j<512; ++j)           
  122. {           
  123.              R = RGBBuffer[RGBIndex++];           
  124.              G = RGBBuffer[RGBIndex++];           
  125.              B = RGBBuffer[RGBIndex++];           
  126.             //Convert RGB to YUV           
  127.           Y = (unsigned char)( ( 66 * R + 129 * G +   25 * B + 128) >> 8) + 16   ;           
  128.           U = (unsigned char)( ( -38 * R -   74 * G + 112 * B + 128) >> 8) + 128 ;           
  129.           V = (unsigned char)( ( 112 * R -   94 * G -   18 * B + 128) >> 8) + 128 ;           
  130.              YBuffer[YIndex++] = static_cast<unsigned char>( (Y<0) ? 0 : ((Y>255) ? 255 : Y) );           
  131.              VLine[j] = V;           
  132. ULine[j] = U;           
  133.          }           
  134.         for ( j=0; j<512; j+=2)           
  135. {           
  136.             //Filter line           
  137.              VImage[i*(512+2)+j] = ((VLine[j-1]+2*VLine[j]+VLine[j+1]+2)>>2);           
  138. UImage[i*(512+2)+j] = ((ULine[j-1]+2*ULine[j]+ULine[j+1]+2)>>2);           
  139.        
  140.          }           
  141. }           
  142.        
  143. for (i=0; i<512; i+=2)           
  144. {           
  145.        int UVIndex = 512*i/4;           
  146.        for ( j=0; j<512; j+=2)           
  147. {           
  148.        
  149.             V = ((VImage[(i-1)*(512+2)+j]+2*VImage[i*(512+2)+j]+VImage[(i+1)*(512+2)+j]+2)>>2);           
  150.             U = ((UImage[(i-1)*(512+2)+j]+2*UImage[i*(512+2)+j]+UImage[(i+1)*(512+2)+j]+2)>>2);           
  151.             VBuffer[UVIndex] = static_cast<unsigned char>( (V<0) ? 0 : ((V>255) ? 255 : V) );           
  152.             UBuffer[UVIndex++] = static_cast<unsigned char>( (U<0) ? 0 : ((U>255) ? 255 : U) );           
  153.        
  154. }           
  155. }           
  156. fwrite(YBuffer, sizeof(char), 512*512, ofp);           
  157. fwrite(VBuffer, sizeof(char), 512*512/4, ofp);           
  158. fwrite(UBuffer, sizeof(char), 512*512/4, ofp);           
  159.        
  160.     return 4;           
  161. fclose(ifp);           
  162. fclose(ofp);           
  163. }           
  164.        
  165.        
  166. int select_option(void)           
  167. {           
  168. int sel = -1;           
  169.        
  170. printf("1. Convert from RGB to YUV422/n");           
  171. printf("2. Convert from RGB to YUV420/n");           
  172.        
  173. printf("Select the option and press enter key: ");           
  174. scanf(" %d", &sel);           
  175.        
  176. return   sel;           
  177. }     

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值