现在的位置: 首页 > 综合 > 正文

RGB轉YUV422420 但執行時出現了9個分割,無法顯示像原圖一樣的圖案

2013年10月02日 ⁄ 综合 ⁄ 共 6459字 ⁄ 字号 评论关闭
 

  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. }     
【上篇】
【下篇】

抱歉!评论已关闭.