【转】YUV值对应的颜色

版权声明:本文为博主原创文章,未经博主允许不得转载。欢迎大家积极评论,博主会一一答复!

最近有人在网上问我,YUV的值对应的颜色是如何的

下面给出YUV值对应的颜色关系

256张图512x512,每张对应的Y为0~255   每4x4对应的是同一颜色区域  横坐标 U  纵坐标V

yuv下载地址

http://download.csdn.net/detail/cabbage2008/9314683

这里截取了Y=0,10,20,60,128,250,255几张

Y=0

Y=10

Y=20

Y=60

Y=128

Y=250

Y=255

生成代码:

  1. #include <stdlib.h>  
  2. #include <stdio.h>  
  3. #include <string.h>  
  4.   
  5. typedef       void                Void;  
  6. typedef       bool                Bool;  
  7. typedef       int                 Int;  
  8. typedef       unsigned char       UChar;  
  9. typedef       UChar               Pel;        ///< 16-bit pixel type  
  10.   
  11. class TComPicYuv  
  12. {  
  13. private:  
  14.   
  15.     // ------------------------------------------------------------------------------------------------  
  16.     //  YUV buffer  
  17.     // ------------------------------------------------------------------------------------------------  
  18.   
  19.     Pel*  m_apiPicBufY;           ///< Buffer (including margin)  
  20.     Pel*  m_apiPicBufU;  
  21.     Pel*  m_apiPicBufV;  
  22.   
  23.   
  24.     // ------------------------------------------------------------------------------------------------  
  25.     //  Parameter for general YUV buffer usage  
  26.     // ------------------------------------------------------------------------------------------------  
  27.   
  28.     Int   m_iPicWidth;            ///< Width of picture  
  29.     Int   m_iPicHeight;           ///< Height of picture  
  30.   
  31. public:  
  32.     TComPicYuv         ();  
  33.     virtual ~TComPicYuv();  
  34.   
  35.     // ------------------------------------------------------------------------------------------------  
  36.     //  Memory management  
  37.     // ------------------------------------------------------------------------------------------------  
  38.   
  39.     Bool  create      ( Int iPicWidth, Int iPicHeight );  
  40.     Void  destroy     ();  
  41.   
  42.     Void  setYuv      (Pel pixelY,Pel pixelU,Pel pixelV);  
  43.     Void  setPixel    (int posX, int posY, int width, Pel pixelY,Pel pixelU,Pel pixelV);  
  44.   
  45.     // ------------------------------------------------------------------------------------------------  
  46.     //  Get information of picture  
  47.     // ------------------------------------------------------------------------------------------------  
  48.     Pel*  getPicBufY  ()     { return m_apiPicBufY ; }  
  49.     Pel*  getPicBufU  ()     { return m_apiPicBufU ; }  
  50.     Pel*  getPicBufV  ()     { return m_apiPicBufV ; }  
  51.   
  52.     Int   getWidth    ()     { return  m_iPicWidth;    }  
  53.     Int   getHeight   ()     { return  m_iPicHeight;   }  
  54.   
  55.     Int   getStride   ()     { return  m_iPicWidth ;    }  
  56.     Int   getCStride  ()     { return  (m_iPicWidth >> 1); }  
  57.   
  58. };// END CLASS DEFINITION TComPicYuv  
  59.   
  60. TComPicYuv::TComPicYuv()  
  61. {  
  62.     m_apiPicBufY      = NULL;   // Buffer (including margin)  
  63.     m_apiPicBufU      = NULL;  
  64.     m_apiPicBufV      = NULL;  
  65. }  
  66.   
  67. TComPicYuv::~TComPicYuv()  
  68. {  
  69. }  
  70.   
  71. Bool TComPicYuv::create( Int iPicWidth, Int iPicHeight )  
  72. {  
  73.     m_iPicWidth       = iPicWidth;  
  74.     m_iPicHeight      = iPicHeight;  
  75.   
  76.   
  77.     m_apiPicBufY      = (Pel*)malloc(m_iPicWidth * m_iPicHeight * sizeof(Pel));  
  78.     m_apiPicBufU      = (Pel*)malloc((m_iPicWidth >>1) * (m_iPicHeight>>1) * sizeof(Pel));  
  79.     m_apiPicBufV      = (Pel*)malloc((m_iPicWidth >>1) * (m_iPicHeight>>1) * sizeof(Pel));  
  80.   
  81.     return true;  
  82. }  
  83.   
  84. Void TComPicYuv::destroy()  
  85. {  
  86.   
  87.     if( m_apiPicBufY ){ free( m_apiPicBufY );    m_apiPicBufY = NULL; }  
  88.     if( m_apiPicBufU ){ free( m_apiPicBufU );    m_apiPicBufU = NULL; }  
  89.     if( m_apiPicBufV ){ free( m_apiPicBufV );    m_apiPicBufV = NULL; }  
  90.   
  91. }  
  92.   
  93. Void TComPicYuv::setYuv(Pel pixelY,Pel pixelU,Pel pixelV)  
  94. {  
  95.     if( m_apiPicBufY ){ memset(m_apiPicBufY, pixelY,m_iPicWidth * m_iPicHeight * sizeof(Pel)) ; }  
  96.     if( m_apiPicBufU ){ memset(m_apiPicBufU, pixelU,(m_iPicWidth >>1) * (m_iPicHeight>>1) * sizeof(Pel)) ; }  
  97.     if( m_apiPicBufV ){ memset(m_apiPicBufV, pixelV,(m_iPicWidth >>1) * (m_iPicHeight>>1) * sizeof(Pel)) ; }  
  98. }  
  99.   
  100. Void TComPicYuv::setPixel(int posX, int posY, int width, Pel pixelY,Pel pixelU,Pel pixelV)  
  101. {  
  102.     Pel *Y = m_apiPicBufY + posY*m_iPicWidth+posX;  
  103.     for(int h = 0; h<width; h++)  
  104.     {  
  105.         for(int w = 0; w<width;w++)  
  106.         {  
  107.             Y[h*m_iPicWidth + w] = pixelY;  
  108.         }  
  109.     }  
  110.   
  111.     Pel *U = m_apiPicBufU + (posY>>1)*(m_iPicWidth>>1)+(posX>>1);  
  112.     Pel *V = m_apiPicBufV + (posY>>1)*(m_iPicWidth>>1)+(posX>>1);  
  113.       
  114.     for(int h = 0; h<(width>>1); h++)  
  115.     {  
  116.         for(int w = 0; w<(width>>1);w++)  
  117.         {  
  118.             U[h*(m_iPicWidth>>1) + w] = pixelU;  
  119.             V[h*(m_iPicWidth>>1) + w] = pixelV;  
  120.         }  
  121.     }  
  122. }  
  123.   
  124. int main()  
  125. {  
  126.     TComPicYuv *frame = new TComPicYuv;  
  127.     TComPicYuv *clourMap = new TComPicYuv;  
  128.   
  129.     frame->create(8,8);  
  130.     clourMap->create(512,512);  
  131.   
  132.     FILE *FrameFile = fopen("D:\clourFrame_8x8.yuv","wb");  
  133.     FILE *clourFile = fopen("D:\clourMap_512x512.yuv","wb");  
  134.   
  135.     for(int Y = 0; Y<256; Y++)  
  136.     {  
  137.         for(int U = 0; U<256; U++)  
  138.         {  
  139.             for(int V = 0; V<256; V++)  
  140.             {  
  141.                 frame->setYuv((Pel)Y,(Pel)U,(Pel)V);  
  142.                 fwrite(frame->getPicBufY(),1,frame->getWidth()*frame->getHeight(),FrameFile);  
  143.                 fwrite(frame->getPicBufU(),1,(frame->getWidth()*frame->getHeight())>>2,FrameFile);  
  144.                 fwrite(frame->getPicBufV(),1,(frame->getWidth()*frame->getHeight())>>2,FrameFile);  
  145.   
  146.                 int  count = Y*256*256 + U*256 + V;  
  147.                 if(count%1000000==0)  
  148.                 {  
  149.                     printf("=");  
  150.                 }  
  151.             }  
  152.         }  
  153.     }  
  154.   
  155.     for(int Y = 0; Y<256; Y++)  
  156.     {  
  157.         for(int U = 0; U<256; U++)  
  158.         {  
  159.             for(int V = 0; V<256; V++)  
  160.             {  
  161.                 clourMap->setPixel(U*2,V*2,2,Y,U,V);  
  162.             }  
  163.         }  
  164.   
  165.         fwrite(clourMap->getPicBufY(),1,clourMap->getWidth()*clourMap->getHeight(),clourFile);  
  166.         fwrite(clourMap->getPicBufU(),1,(clourMap->getWidth()*clourMap->getHeight())>>2,clourFile);  
  167.         fwrite(clourMap->getPicBufV(),1,(clourMap->getWidth()*clourMap->getHeight())>>2,clourFile);  
  168.           
  169.         printf("*");  
  170.     }  
  171.   
  172.     fclose(clourFile);  
  173.     fclose(FrameFile);  
  174.   
  175.     frame->destroy();  
  176.     clourMap->destroy();  
  177. }  

 转载:http://blog.csdn.net/cabbage2008/article/details/50117671

原文地址:https://www.cnblogs.com/cslunatic/p/6074031.html