YUV420p旋轉代碼

 從網上找的一團亂糟糟 索性自己寫個吧 其實也很簡單的

 

很多人對YUV數據格式不清楚,以至於在做視頻的時候出現了一些不可預知的錯誤(比如說圖像帶有點、顏色不對等)。今晚是週末放假,我就抽點時間來給大家介紹一下。

       提示: 讀下面的文字時,希望大家結合圖片看,這樣更易理解
       在YUV420中,一個像素點對應一個Y,一個2X2的小方塊對應一個U和V。對於所有YUV420圖像,它們的Y值排列是完全相同的,因爲只有Y的圖像就是灰度圖像。YUV420sp與YUV420p的數據格式它們的UV排列在原理上是完全不同的。420p它是先把U存放完後,再存放V,也就是說UV它們是連續的。而420sp它是UV、UV這樣交替存放的。(見下圖)
有了上面的理論,我就可以準確的計算出一個YUV420在內存中存放的大小。
width * hight =Y(總和)
U = Y / 4  
V = Y / 4


所以YUV420 數據在內存中的長度是 width * hight * 3 / 2,

假設一個分辨率爲8X4的YUV圖像,它們的格式如下圖:


                                   YUV420sp格式如下圖                                                                    YUV420p數據格式如下圖

                         

  1. class yuv420_rotate  
  2. {  
  3. public:  
  4.     yuv420_rotate(void);  
  5.     ~yuv420_rotate(void);  
  6.   
  7. public:  
  8.     //rotate clockwise  
  9.     static void yuv_rotate_90(BYTE *des,BYTE *src,int width,int height);  
  10.     static void yuv_rotate_180(BYTE *des,BYTE *src,int width,int height);  
  11.     static void yuv_rotate_270(BYTE *des,BYTE *src,int width,int height);  
  12.   
  13.     //flip  
  14.     static void yuv_flip_horizontal(BYTE *des,BYTE *src,int width,int height);  
  15.     static void yuv_flip_vertical(BYTE *des,BYTE *src,int width,int height);  
  16.   
  17. };  


 

  1. yuv420_rotate::yuv420_rotate(void)  
  2. {  
  3. }  
  4.   
  5. yuv420_rotate::~yuv420_rotate(void)  
  6. {  
  7. }  
  8.   
  9. void yuv420_rotate::yuv_rotate_90(BYTE *des,BYTE *src,int width,int height)  
  10. {  
  11.     int n = 0;  
  12.     int hw = width / 2;  
  13.     int hh = height / 2;  
  14.     //copy y  
  15.     for(int j = 0; j < width;j++)  
  16.     {  
  17.         for(int i = height - 1; i >= 0; i--)  
  18.         {  
  19.             des[n++] = src[width * i + j];  
  20.         }  
  21.     }  
  22.   
  23.     //copy u  
  24.     BYTE *ptemp = src + width * height;  
  25.     for(int j = 0;j < hw;j++)  
  26.     {  
  27.         for(int i = hh - 1;i >= 0;i--)  
  28.         {  
  29.             des[n++] = ptemp[ hw*i + j ];  
  30.         }  
  31.     }  
  32.   
  33.     //copy v  
  34.     ptemp += width * height / 4;  
  35.     for(int j = 0; j < hw; j++)  
  36.     {  
  37.         for(int i = hh - 1;i >= 0;i--)  
  38.         {  
  39.             des[n++] = ptemp[hw*i + j];  
  40.         }  
  41.     }  
  42. }  
  43.   
  44. void yuv420_rotate::yuv_rotate_180(BYTE *des,BYTE *src,int width,int height)  
  45. {  
  46.     int n = 0;  
  47.     int hw = width / 2;  
  48.     int hh = height / 2;  
  49.     //copy y  
  50.     for(int j = height - 1; j >= 0; j--)  
  51.     {  
  52.         for(int i = width; i > 0; i--)  
  53.         {  
  54.             des[n++] = src[width*j + i];  
  55.         }  
  56.     }  
  57.   
  58.     //copy u  
  59.     BYTE *ptemp = src + width * height;  
  60.     for(int j = hh - 1;j >= 0; j--)  
  61.     {  
  62.         for(int i = hw; i > 0; i--)  
  63.         {  
  64.             des[n++] = ptemp[hw * j + i];  
  65.         }  
  66.     }  
  67.   
  68.     //copy v  
  69.     ptemp += width * height / 4;  
  70.     for(int j = hh - 1;j >= 0; j--)  
  71.     {  
  72.         for(int i = hw; i > 0; i--)  
  73.         {  
  74.             des[n++] = ptemp[hw * j + i];  
  75.         }  
  76.     }  
  77. }  
  78.   
  79. void yuv420_rotate::yuv_rotate_270(BYTE *des,BYTE *src,int width,int height)  
  80. {  
  81.     int n = 0;  
  82.     int hw = width / 2;  
  83.     int hh = height / 2;  
  84.     //copy y  
  85.     for(int j = width; j > 0; j--)  
  86.     {  
  87.         for(int i = 0; i < height;i++)  
  88.         {  
  89.             des[n++] = src[width*i + j];  
  90.         }  
  91.     }  
  92.   
  93.     //copy u  
  94.     BYTE *ptemp = src + width * height;  
  95.     for(int j = hw; j > 0;j--)  
  96.     {  
  97.         for(int i = 0; i < hh;i++)  
  98.         {  
  99.             des[n++] = ptemp[hw * i + j];  
  100.         }  
  101.     }  
  102.   
  103.     //copy v  
  104.     ptemp += width * height / 4;  
  105.     for(int j = hw; j > 0;j--)  
  106.     {  
  107.         for(int i = 0; i < hh;i++)  
  108.         {  
  109.             des[n++] = ptemp[hw * i + j];  
  110.         }  
  111.     }  
  112. }  
  113.   
  114. void yuv420_rotate::yuv_flip_horizontal(BYTE *des,BYTE *src,int width,int height)  
  115. {  
  116.     int n = 0;  
  117.     int hw = width / 2;  
  118.     int hh = height / 2;  
  119.     //copy y  
  120.     for(int j = 0; j < height; j++)  
  121.     {  
  122.         for(int i = width - 1;i >= 0;i--)  
  123.         {  
  124.             des[n++] = src[width * j + i];  
  125.         }  
  126.     }  
  127.   
  128.     //copy u  
  129.     BYTE *ptemp = src + width * height;  
  130.     for(int j = 0; j < hh; j++)  
  131.     {  
  132.         for(int i = hw - 1;i >= 0;i--)  
  133.         {  
  134.             des[n++] = ptemp[hw * j + i];  
  135.         }  
  136.     }  
  137.       
  138.     //copy v  
  139.     ptemp += width * height / 4;  
  140.     for(int j = 0; j < hh; j++)  
  141.     {  
  142.         for(int i = hw - 1;i >= 0;i--)  
  143.         {  
  144.             des[n++] = ptemp[hw * j + i];  
  145.         }  
  146.     }  
  147. }  
  148.   
  149. void yuv420_rotate::yuv_flip_vertical(BYTE *des,BYTE *src,int width,int height)  
  150. {  
  151.     int n = 0;  
  152.     int hw = width / 2;  
  153.     int hh = height / 2;  
  154.     //copy y  
  155.     for(int j = 0; j < width;j++)  
  156.     {  
  157.         for(int i = height - 1; i >= 0;i--)  
  158.         {  
  159.             des[n++] = src[width * i + j];  
  160.         }  
  161.     }  
  162.   
  163.     //copy u  
  164.     BYTE *ptemp = src + width * height;  
  165.     for(int j = 0; j < hw; j++)  
  166.     {  
  167.         for(int i = hh - 1; i >= 0;i--)  
  168.         {  
  169.             des[n++] = ptemp[ hw * i + j];  
  170.         }  
  171.     }  
  172.   
  173.     //copy v  
  174.     ptemp += width * height / 4;  
  175.     for(int j = 0; j < hw; j++)  
  176.     {  
  177.         for(int i = hh - 1; i >= 0; i--)  
  178.         {  
  179.             des[n++] = ptemp[ hw * i + j];  
  180.         }  
  181.     }  
  182. }  



http://blog.csdn.net/codefoxtiger/article/details/23549553
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章