Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2151686
  • 博文数量: 438
  • 博客积分: 3871
  • 博客等级: 中校
  • 技术积分: 6075
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-10 00:11
个人简介

邮箱: wangcong02345@163.com

文章分类

全部博文(438)

文章存档

2017年(15)

2016年(119)

2015年(91)

2014年(62)

2013年(56)

2012年(79)

2011年(16)

分类: LINUX

2016-07-04 18:58:01

1. 原理
a.RGB转YUV的公式
  1. Y = 0.299*R + 0.587*G + 0.114*B
  2. U = -0.1687*R - 0.3313*G + 0.5*B + 128
  3. V = 0.5*R - 0.418*G - 0.0813*B + 128
b.说明
对于Y分量,每一个像素代入上面的公式处理即可
对于UV分量,四个像素共用一组UV分量,所以UV分量的计算需要取这四个像素的平均值
  1. (R0,G0,B0) (R1,G1,B1)    -->第1行
  2. (R2,G2,B2) (R3,G3,B3)    -->第2行

  3. Y0 Y1         -->Y0 Y1
  4. Y2 Y3         -->Y2 Y3
  5. U             -->(U0+U1+U2+U3)/4
  6. V             -->(V0+V1+V2+V3)/4
所以 YUV的大小=RGB/2
2.bmp转yuv的代码
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5. #define dbmsg(fmt, args ...) printf("%s:%s[%d]: "fmt"\n", __FILE__,__FUNCTION__, __LINE__,##args)
  6. #pragma pack(1)
  7. typedef struct __BITMAPFILEHEADER__
  8. {
  9.     u_int16_t bfType;
  10.     u_int32_t bfSize;
  11.     u_int16_t bfReserved1;
  12.     u_int16_t bfReserved2;
  13.     u_int32_t bfOffBits;
  14. }BITMAPFILEHEADER;

  15. typedef struct __BITMAPINFOHEADER
  16. {
  17.     u_int32_t biSize;
  18.     u_int32_t biWidth;
  19.     u_int32_t biHeight;
  20.     u_int16_t biPlanes;
  21.     u_int16_t biBitCount;
  22.     u_int32_t biCompression;
  23.     u_int32_t biSizeImage;
  24.     u_int32_t biXPelsPerMeter;
  25.     u_int32_t biYPelsPerMeter;
  26.     u_int32_t biClrUsed;
  27.     u_int32_t biClrImportant;
  28. }BITMAPINFOHEADER;

  29. int32_t rgb_seq_change(unsigned char* rgb, int w, int h, int bpp)
  30. {
  31.     int i;
  32.     unsigned char t;
  33.     unsigned char* pt = NULL;
  34.     pt = (unsigned char*)malloc(w*bpp);
  35.     
  36.     //change pixel form <N--1> to <1--N>
  37.     for(i=0; i<h/2; i++)
  38.     {
  39.         memcpy(pt, &rgb[i*w*bpp], w*bpp);
  40.         memcpy(&rgb[i*w*bpp], &rgb[(h-1-i)*w*bpp], w*bpp);
  41.         memcpy(&rgb[(h-1-i)*w*bpp], pt, w*bpp);
  42.     }
  43.     free(pt);
  44.     //gbr2rgb
  45.     for(i=0; i<h*w*3; i+=3)
  46.     {
  47.         t = rgb[i];
  48.         rgb[i] = rgb[i+2];
  49.         rgb[i+2] = t;
  50.     }
  51.     return 0;
  52. }

  53. #define TY(r,g,b) (( r* 0.2989 + g* 0.5866 + b* 0.1145))
  54. #define TU(r,g,b) (( r*(-0.1688) + g*(-0.3312) + b*0.5000 + 128))
  55. #define TV(r,g,b) (( r* 0.5000 + g*(-0.4184) + b*(-0.0816) + 128))

  56. #define Y(r,g,b) (TY(r,g,b) > 255 ? 255 : (TY(r,g,b) < 0 ? 0 : TY(r,g,b)))
  57. #define U(r,g,b) (TU(r,g,b) > 255 ? 255 : (TU(r,g,b) < 0 ? 0 : TU(r,g,b)))
  58. #define V(r,g,b) (TV(r,g,b) > 255 ? 255 : (TV(r,g,b) < 0 ? 0 : TV(r,g,b)))
  59. int32_t rgb_yuv_convert (unsigned char* yuv, unsigned char* rgb, int w, int h, int bpp)
  60. {
  61.     int i, j, k=0;
  62.     int r, g, b;
  63.     int r0, g0, b0;
  64.     int r1, g1, b1;
  65.     int r2, g2, b2;
  66.     int r3, g3, b3;
  67.     unsigned char *pY = NULL;
  68.     unsigned char *pU = NULL;
  69.     unsigned char *pV = NULL;
  70.     
  71.     pY = yuv;
  72.     pU = yuv + w*h;
  73.     pV = pU + ((w*h)>>2);
  74.     for(i=0; i<h; i++)
  75.     for(j=0; j<w; j++)
  76.     {
  77.         r = rgb[(i*w+j)*bpp];
  78.         g = rgb[(i*w+j)*bpp+1];
  79.         b = rgb[(i*w+j)*bpp+2];

  80.         pY[i*w+j] = Y(r, g, b);
  81.        // dbmsg("i=%d,j=%d", i,j);
  82.         if(i%2==1 && j%2==1)
  83.         {
  84.             //left up
  85.             r0 = rgb[((i-1)*w+j-1)*bpp];
  86.             g0 = rgb[((i-1)*w+j-1)*bpp+1];
  87.             b0 = rgb[((i-1)*w+j-1)*bpp+2];
  88.             //up
  89.             r1 = rgb[((i-1)*w+j)*bpp];
  90.             g1 = rgb[((i-1)*w+j)*bpp+1];
  91.             b1 = rgb[((i-1)*w+j)*bpp+2];

  92.             //right
  93.             r2 = rgb[(i*w+j-1)*bpp];
  94.             g2 = rgb[(i*w+j-1)*bpp+1];
  95.             b2 = rgb[(i*w+j-1)*bpp+2];

  96.             //now
  97.             r3 = r;
  98.             g3 = g;
  99.             b3 = b;

  100.             pU[k] = (U(r0,g0,b0)+ U(r1,g1,b1) + U(r2,g2,b2) + U(r3,g3,b3)) /4;
  101.             pV[k] = (V(r0,g0,b0)+ V(r1,g1,b1) + V(r2,g2,b2) + V(r3,g3,b3)) /4;
  102.             k++;
  103.             //printf("k=%d\n",k );
  104.         }
  105.     }
  106.     return 0;
  107. }

  108. int translate_bmp2yuv(FILE* out_yuv_fp, FILE* bmp_fp)
  109. {
  110.     int i, len;
  111.     int w, h, bpp; //bpp-->byte_per_pix
  112.     char* p_temp = NULL;
  113.     unsigned char* rgb = NULL;
  114.     unsigned char* yuv = NULL;

  115.     BITMAPFILEHEADER bmphead;
  116.     BITMAPINFOHEADER infohead;

  117.     if( (NULL==out_yuv_fp) || (NULL==bmp_fp))
  118.         return -1;
  119.     len = sizeof(bmphead)+sizeof(infohead);
  120.     printf("len=%d\n",len);
  121.     p_temp = (char*)malloc(len*sizeof(char));
  122.     if(p_temp == NULL)
  123.     {
  124.         printf("malloc error\n");
  125.         return -1;
  126.     }
  127.     fread(p_temp, len, 1, bmp_fp);
  128.     memcpy(&bmphead, p_temp, sizeof(bmphead));
  129.     memcpy(&infohead, p_temp+sizeof(bmphead), sizeof(infohead));
  130.     if(bmphead.bfType != 0x4D42) //must be 0x4D42='BM'
  131.     {
  132.         printf("not a bmp file\n");
  133.         return -1;
  134.     }
  135.     if(infohead.biBitCount < 24)
  136.     {
  137.         printf("error: now bitCount=%d < 24bit\n", infohead.biBitCount);
  138.         return -1;
  139.     }
  140.     free(p_temp);
  141.     w = infohead.biWidth;
  142.     h = infohead.biHeight;
  143.     bpp = infohead.biBitCount/8;
  144.     printf("w=%d,h=%d,bitcount=%d\n", w, h, bpp);

  145.     yuv = (char*) malloc(w*h*bpp/2);
  146.     if(yuv== NULL)
  147.     {
  148.         printf("malloc yuv buffer error\n");
  149.         return -1;
  150.     }

  151.     rgb = (char*)malloc(w*h*bpp);
  152.     if(rgb== NULL)
  153.     {
  154.         printf("malloc rgb buffer error\n");
  155.         return -1;
  156.     }
  157.     fread(rgb, w*h*bpp, 1, bmp_fp);
  158.     rgb_seq_change(rgb, w, h, bpp);
  159.     for(i=0; i<100; i++)                        //这儿主要是为了测试,真正用时千万别把这个搞上去
  160.        rgb_yuv_convert(yuv, rgb, w, h, bpp);
  161.     fwrite(yuv, w*h*bpp/2, 1, out_yuv_fp);
  162.     free(rgb);
  163.     free(yuv);
  164.     return 0;
  165. }

  166. int main ( int argc, char *argv[] )
  167. {
  168.     char bmp_file[] = "./640_480.bmp";
  169.     char raw_file[] = "./640_480.yuv";
  170.     FILE * out_yuv_fp = NULL;
  171.     FILE * in_bmp_fp = NULL;

  172.     if (NULL == (in_bmp_fp = fopen(bmp_file,"rb")))
  173.     {
  174.         printf("error: %s not found\n",bmp_file);
  175.         return -1;
  176.     }

  177.     if (NULL == (out_yuv_fp = fopen(raw_file,"wb")))
  178.     {
  179.         printf("error: %s not found\n",raw_file);
  180.         return -1;
  181.     }
  182.     
  183.     translate_bmp2yuv(out_yuv_fp, in_bmp_fp);

  184.     fclose(out_yuv_fp);
  185.     fclose(in_bmp_fp);
  186.     printf("over\n");
  187.     return EXIT_SUCCESS;
  188. }


2.查表法
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5. #define dbmsg(fmt, args ...) printf("%s:%s[%d]: "fmt"\n", __FILE__,__FUNCTION__, __LINE__,##args)
  6. #pragma pack(1)
  7. typedef struct __BITMAPFILEHEADER__
  8. {
  9.     u_int16_t bfType;
  10.     u_int32_t bfSize;
  11.     u_int16_t bfReserved1;
  12.     u_int16_t bfReserved2;
  13.     u_int32_t bfOffBits;
  14. }BITMAPFILEHEADER;

  15. typedef struct __BITMAPINFOHEADER
  16. {
  17.     u_int32_t biSize;
  18.     u_int32_t biWidth;
  19.     u_int32_t biHeight;
  20.     u_int16_t biPlanes;
  21.     u_int16_t biBitCount;
  22.     u_int32_t biCompression;
  23.     u_int32_t biSizeImage;
  24.     u_int32_t biXPelsPerMeter;
  25.     u_int32_t biYPelsPerMeter;
  26.     u_int32_t biClrUsed;
  27.     u_int32_t biClrImportant;
  28. }BITMAPINFOHEADER;

  29. int32_t rgb_seq_change(unsigned char* rgb, int w, int h, int bpp)
  30. {
  31.     int i;
  32.     unsigned char t;
  33.     unsigned char* pt = NULL;
  34.     pt = (unsigned char*)malloc(w*bpp);
  35.     
  36.     //change pixel form <N--1> to <1--N>
  37.     for(i=0; i<h/2; i++)
  38.     {
  39.         memcpy(pt, &rgb[i*w*bpp], w*bpp);
  40.         memcpy(&rgb[i*w*bpp], &rgb[(h-1-i)*w*bpp], w*bpp);
  41.         memcpy(&rgb[(h-1-i)*w*bpp], pt, w*bpp);
  42.     }
  43.     free(pt);
  44.     //gbr2rgb
  45.     for(i=0; i<h*w*3; i+=3)
  46.     {
  47.         t = rgb[i];
  48.         rgb[i] = rgb[i+2];
  49.         rgb[i+2] = t;
  50.     }
  51.     return 0;
  52. }
  53. #define Y(a) ((a)> 255 ? 255 : (a) < 0 ? 0 : (a))
  54. #define U(a) ((a) > 255 ? 255 : (a) < 0 ? 0 : (a))
  55. #define V(a) ((a) > 255 ? 255 : (a) < 0 ? 0 : (a))

  56. #define RGB_RED 0 /* Offset of Red in an RGB scanline element */
  57. #define RGB_GREEN 1 /* Offset of Green */
  58. #define RGB_BLUE 2 /* Offset of Blue */
  59. #define GETJSAMPLE(value) ((int) (value) & 0xFF)
  60. #define MAXJSAMPLE 255
  61. #define CENTERJSAMPLE 128
  62. #define SCALEBITS    16    /* speediest right-shift on some machines */
  63. #define FIX(x) ((int32_t) ((x) * (1L<<SCALEBITS) + 0.5))
  64. #define CBCR_OFFSET    ((int32_t) CENTERJSAMPLE << SCALEBITS)
  65. #define ONE_HALF    ((int32_t) 1 << (SCALEBITS-1))
  66. #define FIX(x)        ((int32_t) ((x) * (1L<<SCALEBITS) + 0.5))

  67. /* We allocate one big table and divide it up into eight parts, instead of
  68.  * doing eight alloc_small requests. This lets us use a single table base
  69.  * address, which can be held in a register in the inner loops on many
  70.  * machines (more than can hold all eight addresses, anyway).
  71.  */

  72. #define R_Y_OFF        0            /* offset to R => Y section */
  73. #define G_Y_OFF        (1*(MAXJSAMPLE+1))    /* offset to G => Y section */
  74. #define B_Y_OFF        (2*(MAXJSAMPLE+1))    /* etc. */
  75. #define R_CB_OFF    (3*(MAXJSAMPLE+1))
  76. #define G_CB_OFF    (4*(MAXJSAMPLE+1))
  77. #define B_CB_OFF    (5*(MAXJSAMPLE+1))
  78. #define R_CR_OFF    B_CB_OFF        /* B=>Cb, R=>Cr are the same */
  79. #define G_CR_OFF    (6*(MAXJSAMPLE+1))
  80. #define B_CR_OFF    (7*(MAXJSAMPLE+1))
  81. #define TABLE_SIZE    (8*(MAXJSAMPLE+1))


  82. /* * Initialize for RGB->YCC colorspace conversion. */
  83. int32_t* rgb_yuv_start()
  84. {
  85.     int32_t * rgb_yuv_tab;
  86.     int32_t i;
  87.     dbmsg("rgb_yuv_start");
  88.     rgb_yuv_tab = (int32_t*) malloc(TABLE_SIZE * sizeof(int32_t));
  89.     if(NULL==rgb_yuv_tab)
  90.         return NULL;

  91.     for (i = 0; i <= MAXJSAMPLE; i++) {
  92.         rgb_yuv_tab[i+R_Y_OFF] = FIX(0.29900) * i;
  93.         rgb_yuv_tab[i+G_Y_OFF] = FIX(0.58700) * i;
  94.         rgb_yuv_tab[i+B_Y_OFF] = FIX(0.11400) * i + ONE_HALF;
  95.         rgb_yuv_tab[i+R_CB_OFF] = (-FIX(0.16874)) * i;
  96.         rgb_yuv_tab[i+G_CB_OFF] = (-FIX(0.33126)) * i;
  97.         rgb_yuv_tab[i+B_CB_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1;
  98.         rgb_yuv_tab[i+G_CR_OFF] = (-FIX(0.41869)) * i;
  99.         rgb_yuv_tab[i+B_CR_OFF] = (-FIX(0.08131)) * i;
  100.     }
  101.     return rgb_yuv_tab;
  102. }

  103. void rgb_yuv_convert(char* yuv, char* rgb, int w, int h,int bpp, int32_t* ctab)
  104. {
  105.     int r, g, b;
  106.     int r0, g0, b0;
  107.     int r1, g1, b1;
  108.     int r2, g2, b2;
  109.     int r3, g3, b3;
  110.     unsigned char *pY = NULL;
  111.     unsigned char *pU = NULL;
  112.     unsigned char *pV = NULL;
  113.     unsigned int i, j, k=0;
  114.     pY = yuv;
  115.     pU = yuv + w*h;
  116.     pV = pU + ((w*h)>>2);

  117.     for(i=0; i<h; i++)
  118.     for(j=0; j<w; j++)
  119.     {
  120.         r = GETJSAMPLE(rgb[(i*w+j)*bpp+RGB_RED]);
  121.         g = GETJSAMPLE(rgb[(i*w+j)*bpp+RGB_GREEN]);
  122.         b = GETJSAMPLE(rgb[(i*w+j)*bpp+RGB_BLUE]);
  123.         pY[i*w+j] = Y((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF]) >> SCALEBITS);
  124.         if(i%2==1 && j%2==1)
  125.         {
  126.             //left up
  127.             r0 = GETJSAMPLE(rgb[((i-1)*w+j-1)*bpp]);
  128.             g0 = GETJSAMPLE(rgb[((i-1)*w+j-1)*bpp+1]);
  129.             b0 = GETJSAMPLE(rgb[((i-1)*w+j-1)*bpp+2]);
  130.             //up
  131.             r1 = GETJSAMPLE(rgb[((i-1)*w+j)*bpp]);
  132.             g1 = GETJSAMPLE(rgb[((i-1)*w+j)*bpp+1]);
  133.             b1 = GETJSAMPLE(rgb[((i-1)*w+j)*bpp+2]);

  134.             //right
  135.             r2 = GETJSAMPLE(rgb[(i*w+j-1)*bpp]);
  136.             g2 = GETJSAMPLE(rgb[(i*w+j-1)*bpp+1]);
  137.             b2 = GETJSAMPLE(rgb[(i*w+j-1)*bpp+2]);

  138.             r3 = r;
  139.             g3 = g;
  140.             b3 = b;
  141.             pU[k] = (
  142.                     U((ctab[r0+R_CB_OFF] + ctab[g0+G_CB_OFF] + ctab[b0+B_CB_OFF]) >> SCALEBITS) +
  143.                     U((ctab[r1+R_CB_OFF] + ctab[g1+G_CB_OFF] + ctab[b1+B_CB_OFF]) >> SCALEBITS) +
  144.                     U((ctab[r2+R_CB_OFF] + ctab[g2+G_CB_OFF] + ctab[b2+B_CB_OFF]) >> SCALEBITS) +
  145.                     U((ctab[r3+R_CB_OFF] + ctab[g3+G_CB_OFF] + ctab[b3+B_CB_OFF]) >> SCALEBITS)
  146.                     )/4;
  147.                 
  148.             pV[k] = (
  149.                     V((ctab[r0+R_CR_OFF] + ctab[g0+G_CR_OFF] + ctab[b0+B_CR_OFF]) >> SCALEBITS) +
  150.                     V((ctab[r1+R_CR_OFF] + ctab[g1+G_CR_OFF] + ctab[b1+B_CR_OFF]) >> SCALEBITS) +
  151.                     V((ctab[r2+R_CR_OFF] + ctab[g2+G_CR_OFF] + ctab[b2+B_CR_OFF]) >> SCALEBITS) +
  152.                     V((ctab[r3+R_CR_OFF] + ctab[g3+G_CR_OFF] + ctab[b3+B_CR_OFF]) >> SCALEBITS)
  153.                     )/4;
  154. #if 0
  155.             dbmsg("(%d+%d+%d+%d)/4=%d",
  156.                     U((ctab[r0+R_CB_OFF] + ctab[g0+G_CB_OFF] + ctab[b0+B_CB_OFF]) >> SCALEBITS) ,
  157.                     U((ctab[r1+R_CB_OFF] + ctab[g1+G_CB_OFF] + ctab[b1+B_CB_OFF]) >> SCALEBITS) ,
  158.                     U((ctab[r2+R_CB_OFF] + ctab[g2+G_CB_OFF] + ctab[b2+B_CB_OFF]) >> SCALEBITS) ,
  159.                     U((ctab[r3+R_CB_OFF] + ctab[g3+G_CB_OFF] + ctab[b3+B_CB_OFF]) >> SCALEBITS), pU[k]
  160.                  );
  161.             dbmsg("(%d+%d+%d+%d)/4=%d",
  162.                     V((ctab[r0+R_CR_OFF] + ctab[g0+G_CR_OFF] + ctab[b0+B_CR_OFF]) >> SCALEBITS) ,
  163.                     V((ctab[r1+R_CR_OFF] + ctab[g1+G_CR_OFF] + ctab[b1+B_CR_OFF]) >> SCALEBITS) ,
  164.                     V((ctab[r2+R_CR_OFF] + ctab[g2+G_CR_OFF] + ctab[b2+B_CR_OFF]) >> SCALEBITS) ,
  165.                     V((ctab[r3+R_CR_OFF] + ctab[g3+G_CR_OFF] + ctab[b3+B_CR_OFF]) >> SCALEBITS),pV[k]
  166.                     );
  167. #endif
  168.             k++;
  169.         }
  170.    }
  171. }

  172. int translate_bmp2yuv(FILE* out_yuv_fp, FILE* bmp_fp)
  173. {
  174.     int i, len;
  175.     int w, h, bpp; //bpp-->byte_per_pix
  176.     char* p_temp = NULL;
  177.     unsigned char* rgb = NULL;
  178.     unsigned char* yuv = NULL;
  179.     int32_t* p_color_tab = NULL;
  180.     BITMAPFILEHEADER bmphead;
  181.     BITMAPINFOHEADER infohead;

  182.     if( (NULL==out_yuv_fp) || (NULL==bmp_fp))
  183.         return -1;
  184.     len = sizeof(bmphead)+sizeof(infohead);
  185.     printf("len=%d\n",len);
  186.     p_temp = (char*)malloc(len*sizeof(char));
  187.     if(p_temp == NULL)
  188.     {
  189.         printf("malloc error\n");
  190.         return -1;
  191.     }
  192.     fread(p_temp, len, 1, bmp_fp);
  193.     memcpy(&bmphead, p_temp, sizeof(bmphead));
  194.     memcpy(&infohead, p_temp+sizeof(bmphead), sizeof(infohead));
  195.     if(bmphead.bfType != 0x4D42) //must be 0x4D42='BM'
  196.     {
  197.         printf("not a bmp file\n");
  198.         return -1;
  199.     }
  200.     if(infohead.biBitCount < 24)
  201.     {
  202.         printf("error: now bitCount=%d < 24bit\n", infohead.biBitCount);
  203.         return -1;
  204.     }
  205.     free(p_temp);
  206.     w = infohead.biWidth;
  207.     h = infohead.biHeight;
  208.     bpp = infohead.biBitCount/8;
  209.     printf("w=%d,h=%d,bitcount=%d\n", w, h, bpp);

  210.     yuv = (char*) malloc(w*h*bpp/2);
  211.     if(yuv== NULL)
  212.     {
  213.         printf("malloc yuv buffer error\n");
  214.         return -1;
  215.     }

  216.     rgb = (char*)malloc(w*h*bpp);
  217.     if(rgb== NULL)
  218.     {
  219.         printf("malloc rgb buffer error\n");
  220.         return -1;
  221.     }
  222.     fread(rgb, w*h*bpp, 1, bmp_fp);
  223.     rgb_seq_change(rgb, w, h, bpp);
  224.     p_color_tab = rgb_yuv_start();
  225.     for(i=0; i<100; i++)                        //这儿主要是为了测试,真正用时千万别把这个搞上去
  226.         rgb_yuv_convert(yuv, rgb, w, h, bpp, p_color_tab);
  227.     fwrite(yuv, w*h*bpp/2, 1, out_yuv_fp);
  228.     free(rgb);
  229.     free(yuv);
  230.     return 0;
  231. }

  232. int main ( int argc, char *argv[] )
  233. {
  234.     char bmp_file[] = "./640_480.bmp";
  235.     char raw_file[] = "./640_480.yuv";
  236.     FILE * out_yuv_fp = NULL;
  237.     FILE * in_bmp_fp = NULL;

  238.     if (NULL == (in_bmp_fp = fopen(bmp_file,"rb")))
  239.     {
  240.         printf("error: %s not found\n",bmp_file);
  241.         return -1;
  242.     }

  243.     if (NULL == (out_yuv_fp = fopen(raw_file,"wb")))
  244.     {
  245.         printf("error: %s not found\n",raw_file);
  246.         return -1;
  247.     }
  248.     
  249.     translate_bmp2yuv(out_yuv_fp, in_bmp_fp);

  250.     fclose(out_yuv_fp);
  251.     fclose(in_bmp_fp);
  252.     printf("over\n");
  253.     return EXIT_SUCCESS;
  254. }
3. 测试
分别对两个rgb_yuv_convert进行测试,执行100次发现
 -->直接转的耗时
real 0m2.183s
user 0m2.179s
sys 0m0.000s    

-->查表法的耗时
real 0m0.679s
user 0m0.674s
sys 0m0.004s    
发现查表法是有一定的提高。



阅读(1329) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~