【VS开发】【图像处理】 bayer, yuv, RGB转换方法

275 篇文章 40 订阅 ¥39.90 ¥99.00
104 篇文章 49 订阅 ¥39.90 ¥99.00

因为我的STVxxx USB camera输出格式是bayer格式,手头上只有YUVTOOLS这个查看工具,没法验证STVxxx在开发板上是否正常工作。

网上找了很久也没找到格式转换工具,最后放弃了,觉得还是写个转换工具比较快。抄写了部分libv4lconvert的代码, 我只验证了

V4L2_PIX_FMT_SGBRG8到V4L2_PIX_FMT_YUV420的转换。


bayer.c

##################################################################################

[cpp] view plain copy
  1. /* 
  2.  * lib4lconvert, video4linux2 format conversion lib 
  3.  *             (C) 2008 Hans de Goede <hdegoede@redhat.com> 
  4.  * 
  5.  * This library is free software; you can redistribute it and/or 
  6.  * modify it under the terms of the GNU Lesser General Public 
  7.  * License as published by the Free Software Foundation; either 
  8.  * version 2.1 of the License, or (at your option) any later version. 
  9.  * 
  10.  * This library is distributed in the hope that it will be useful, 
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of 
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
  13.  * Lesser General Public License for more details. 
  14.  * 
  15.  * You should have received a copy of the GNU Lesser General Public 
  16.  * License along with this library; if not, write to the Free Software 
  17.  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA  02110-1335  USA 
  18.  * 
  19.  * Note: original bayer_to_bgr24 code from : 
  20.  * 1394-Based Digital Camera Control Library 
  21.  * 
  22.  * Bayer pattern decoding functions 
  23.  * 
  24.  * Written by Damien Douxchamps and Frederic Devernay 
  25.  * 
  26.  * Note that the original bayer.c in libdc1394 supports many different 
  27.  * bayer decode algorithms, for lib4lconvert the one in this file has been 
  28.  * chosen (and optimized a bit) and the other algorithm's have been removed, 
  29.  * see bayer.c from libdc1394 for all supported algorithms 
  30.  */  
  31.   
  32. #include <string.h>  
  33. #include <linux/videodev2.h>  
  34. #include <stdio.h>  
  35.   
  36. #include "convert.h"  
  37.   
  38.   
  39. /************************************************************** 
  40.  *     Color conversion functions for cameras that can        * 
  41.  * output raw-Bayer pattern images, such as some Basler and   * 
  42.  * Point Grey camera. Most of the algos presented here come   * 
  43.  * from http://www-ise.stanford.edu/~tingchen/ and have been  * 
  44.  * converted from Matlab to C and extended to all elementary  * 
  45.  * patterns.                                                  * 
  46.  **************************************************************/  
  47.   
  48. /* inspired by OpenCV's Bayer decoding */  
  49. static void v4lconvert_border_bayer_line_to_bgr24(  
  50.         const unsigned char *bayer, const unsigned char *adjacent_bayer,  
  51.         unsigned char *bgr, int width, int start_with_green, int blue_line)  
  52. {  
  53.     int t0, t1;  
  54.   
  55.     if (start_with_green) {  
  56.         /* First pixel */  
  57.         if (blue_line) {  
  58.             *bgr++ = bayer[1];  
  59.             *bgr++ = bayer[0];  
  60.             *bgr++ = adjacent_bayer[0];  
  61.         } else {  
  62.             *bgr++ = adjacent_bayer[0];  
  63.             *bgr++ = bayer[0];  
  64.             *bgr++ = bayer[1];  
  65.         }  
  66.         /* Second pixel */  
  67.         t0 = (bayer[0] + bayer[2] + adjacent_bayer[1] + 1) / 3;  
  68.         t1 = (adjacent_bayer[0] + adjacent_bayer[2] + 1) >> 1;  
  69.         if (blue_line) {  
  70.             *bgr++ = bayer[1];  
  71.             *bgr++ = t0;  
  72.             *bgr++ = t1;  
  73.         } else {  
  74.             *bgr++ = t1;  
  75.             *bgr++ = t0;  
  76.             *bgr++ = bayer[1];  
  77.         }  
  78.         bayer++;  
  79.         adjacent_bayer++;  
  80.         width -= 2;  
  81.     } else {  
  82.         /* First pixel */  
  83.         t0 = (bayer[1] + adjacent_bayer[0] + 1) >> 1;  
  84.         if (blue_line) {  
  85.             *bgr++ = bayer[0];  
  86.             *bgr++ = t0;  
  87.             *bgr++ = adjacent_bayer[1];  
  88.         } else {  
  89.             *bgr++ = adjacent_bayer[1];  
  90.             *bgr++ = t0;  
  91.             *bgr++ = bayer[0];  
  92.         }  
  93.         width--;  
  94.     }  
  95.   
  96.     if (blue_line) {  
  97.         for ( ; width > 2; width -= 2) {  
  98.             t0 = (bayer[0] + bayer[2] + 1) >> 1;  
  99.             *bgr++ = t0;  
  100.             *bgr++ = bayer[1];  
  101.             *bgr++ = adjacent_bayer[1];  
  102.             bayer++;  
  103.             adjacent_bayer++;  
  104.   
  105.             t0 = (bayer[0] + bayer[2] + adjacent_bayer[1] + 1) / 3;  
  106.             t1 = (adjacent_bayer[0] + adjacent_bayer[2] + 1) >> 1;  
  107.             *bgr++ = bayer[1];  
  108.             *bgr++ = t0;  
  109.             *bgr++ = t1;  
  110.             bayer++;  
  111.             adjacent_bayer++;  
  112.         }  
  113.     } else {  
  114.         for ( ; width > 2; width -= 2) {  
  115.             t0 = (bayer[0] + bayer[2] + 1) >> 1;  
  116.             *bgr++ = adjacent_bayer[1];  
  117.             *bgr++ = bayer[1];  
  118.             *bgr++ = t0;  
  119.             bayer++;  
  120.             adjacent_bayer++;  
  121.   
  122.             t0 = (bayer[0] + bayer[2] + adjacent_bayer[1] + 1) / 3;  
  123.             t1 = (adjacent_bayer[0] + adjacent_bayer[2] + 1) >> 1;  
  124.             *bgr++ = t1;  
  125.             *bgr++ = t0;  
  126.             *bgr++ = bayer[1];  
  127.             bayer++;  
  128.             adjacent_bayer++;  
  129.         }  
  130.     }  
  131.   
  132.     if (width == 2) {  
  133.         /* Second to last pixel */  
  134.         t0 = (bayer[0] + bayer[2] + 1) >> 1;  
  135.         if (blue_line) {  
  136.             *bgr++ = t0;  
  137.             *bgr++ = bayer[1];  
  138.             *bgr++ = adjacent_bayer[1];  
  139.         } else {  
  140.             *bgr++ = adjacent_bayer[1];  
  141.             *bgr++ = bayer[1];  
  142.             *bgr++ = t0;  
  143.         }  
  144.         /* Last pixel */  
  145.         t0 = (bayer[1] + adjacent_bayer[2] + 1) >> 1;  
  146.         if (blue_line) {  
  147.             *bgr++ = bayer[2];  
  148.             *bgr++ = t0;  
  149.             *bgr++ = adjacent_bayer[1];  
  150.         } else {  
  151.             *bgr++ = adjacent_bayer[1];  
  152.             *bgr++ = t0;  
  153.             *bgr++ = bayer[2];  
  154.         }  
  155.     } else {  
  156.         /* Last pixel */  
  157.         if (blue_line) {  
  158.             *bgr++ = bayer[0];  
  159.             *bgr++ = bayer[1];  
  160.             *bgr++ = adjacent_bayer[1];  
  161.         } else {  
  162.             *bgr++ = adjacent_bayer[1];  
  163.             *bgr++ = bayer[1];  
  164.             *bgr++ = bayer[0];  
  165.         }  
  166.     }  
  167. }  
  168.   
  169. /* From libdc1394, which on turn was based on OpenCV's Bayer decoding */  
  170. static void bayer_to_rgbbgr24(const unsigned char *bayer,  
  171.         unsigned char *bgr, int width, int height, unsigned int pixfmt,  
  172.         int start_with_green, int blue_line)  
  173. {  
  174.     /* render the first line */  
  175.     v4lconvert_border_bayer_line_to_bgr24(bayer, bayer + width, bgr, width,  
  176.             start_with_green, blue_line);  
  177.     bgr += width * 3;  
  178.   
  179.     return;  
  180.     /* reduce height by 2 because of the special case top/bottom line */  
  181.     for (height -= 2; height; height--) {  
  182.         int t0, t1;  
  183.         /* (width - 2) because of the border */  
  184.         const unsigned char *bayer_end = bayer + (width - 2);  
  185.   
  186.         if (start_with_green) {  
  187.             /* OpenCV has a bug in the next line, which was 
  188.                t0 = (bayer[0] + bayer[width * 2] + 1) >> 1; */  
  189.             t0 = (bayer[1] + bayer[width * 2 + 1] + 1) >> 1;  
  190.             /* Write first pixel */  
  191.             t1 = (bayer[0] + bayer[width * 2] + bayer[width + 1] + 1) / 3;  
  192.             if (blue_line) {  
  193.                 *bgr++ = t0;  
  194.                 *bgr++ = t1;  
  195.                 *bgr++ = bayer[width];  
  196.             } else {  
  197.                 *bgr++ = bayer[width];  
  198.                 *bgr++ = t1;  
  199.                 *bgr++ = t0;  
  200.             }  
  201.   
  202.             /* Write second pixel */  
  203.             t1 = (bayer[width] + bayer[width + 2] + 1) >> 1;  
  204.             if (blue_line) {  
  205.                 *bgr++ = t0;  
  206.                 *bgr++ = bayer[width + 1];  
  207.                 *bgr++ = t1;  
  208.             } else {  
  209.                 *bgr++ = t1;  
  210.                 *bgr++ = bayer[width + 1];  
  211.                 *bgr++ = t0;  
  212.             }  
  213.             bayer++;  
  214.         } else {  
  215.             /* Write first pixel */  
  216.             t0 = (bayer[0] + bayer[width * 2] + 1) >> 1;  
  217.             if (blue_line) {  
  218.                 *bgr++ = t0;  
  219.                 *bgr++ = bayer[width];  
  220.                 *bgr++ = bayer[width + 1];  
  221.             } else {  
  222.                 *bgr++ = bayer[width + 1];  
  223.                 *bgr++ = bayer[width];  
  224.                 *bgr++ = t0;  
  225.             }  
  226.         }  
  227.   
  228.         if (blue_line) {  
  229.             for (; bayer <= bayer_end - 2; bayer += 2) {  
  230.                 t0 = (bayer[0] + bayer[2] + bayer[width * 2] +  
  231.                     bayer[width * 2 + 2] + 2) >> 2;  
  232.                 t1 = (bayer[1] + bayer[width] + bayer[width + 2] +  
  233.                     bayer[width * 2 + 1] + 2) >> 2;  
  234.                 *bgr++ = t0;  
  235.                 *bgr++ = t1;  
  236.                 *bgr++ = bayer[width + 1];  
  237.   
  238.                 t0 = (bayer[2] + bayer[width * 2 + 2] + 1) >> 1;  
  239.                 t1 = (bayer[width + 1] + bayer[width + 3] + 1) >> 1;  
  240.                 *bgr++ = t0;  
  241.                 *bgr++ = bayer[width + 2];  
  242.                 *bgr++ = t1;  
  243.             }  
  244.         } else {  
  245.             for (; bayer <= bayer_end - 2; bayer += 2) {  
  246.                 t0 = (bayer[0] + bayer[2] + bayer[width * 2] +  
  247.                     bayer[width * 2 + 2] + 2) >> 2;  
  248.                 t1 = (bayer[1] + bayer[width] + bayer[width + 2] +  
  249.                     bayer[width * 2 + 1] + 2) >> 2;  
  250.                 *bgr++ = bayer[width + 1];  
  251.                 *bgr++ = t1;  
  252.                 *bgr++ = t0;  
  253.   
  254.                 t0 = (bayer[2] + bayer[width * 2 + 2] + 1) >> 1;  
  255.                 t1 = (bayer[width + 1] + bayer[width + 3] + 1) >> 1;  
  256.                 *bgr++ = t1;  
  257.                 *bgr++ = bayer[width + 2];  
  258.                 *bgr++ = t0;  
  259.             }  
  260.         }  
  261.   
  262.         if (bayer < bayer_end) {  
  263.             /* write second to last pixel */  
  264.             t0 = (bayer[0] + bayer[2] + bayer[width * 2] +  
  265.                 bayer[width * 2 + 2] + 2) >> 2;  
  266.             t1 = (bayer[1] + bayer[width] + bayer[width + 2] +  
  267.                 bayer[width * 2 + 1] + 2) >> 2;  
  268.             if (blue_line) {  
  269.                 *bgr++ = t0;  
  270.                 *bgr++ = t1;  
  271.                 *bgr++ = bayer[width + 1];  
  272.             } else {  
  273.                 *bgr++ = bayer[width + 1];  
  274.                 *bgr++ = t1;  
  275.                 *bgr++ = t0;  
  276.             }  
  277.             /* write last pixel */  
  278.             t0 = (bayer[2] + bayer[width * 2 + 2] + 1) >> 1;  
  279.             if (blue_line) {  
  280.                 *bgr++ = t0;  
  281.                 *bgr++ = bayer[width + 2];  
  282.                 *bgr++ = bayer[width + 1];  
  283.             } else {  
  284.                 *bgr++ = bayer[width + 1];  
  285.                 *bgr++ = bayer[width + 2];  
  286.                 *bgr++ = t0;  
  287.             }  
  288.             bayer++;  
  289.         } else {  
  290.             /* write last pixel */  
  291.             t0 = (bayer[0] + bayer[width * 2] + 1) >> 1;  
  292.             t1 = (bayer[1] + bayer[width * 2 + 1] + bayer[width] + 1) / 3;  
  293.             if (blue_line) {  
  294.                 *bgr++ = t0;  
  295.                 *bgr++ = t1;  
  296.                 *bgr++ = bayer[width + 1];  
  297.             } else {  
  298.                 *bgr++ = bayer[width + 1];  
  299.                 *bgr++ = t1;  
  300.                 *bgr++ = t0;  
  301.             }  
  302.         }  
  303.   
  304.         /* skip 2 border pixels */  
  305.         bayer += 2;  
  306.   
  307.         blue_line = !blue_line;  
  308.         start_with_green = !start_with_green;  
  309.     }  
  310.   
  311.     /* render the last line */  
  312.     v4lconvert_border_bayer_line_to_bgr24(bayer + width, bayer, bgr, width,  
  313.             !start_with_green, !blue_line);  
  314. }  
  315.   
  316. void v4lconvert_bayer_to_rgb24(const unsigned char *bayer,  
  317.         unsigned char *bgr, int width, int height, unsigned int pixfmt)  
  318. {  
  319.     bayer_to_rgbbgr24(bayer, bgr, width, height, pixfmt,  
  320.             pixfmt == V4L2_PIX_FMT_SGBRG8        /* start with green */  
  321.             || pixfmt == V4L2_PIX_FMT_SGRBG8,  
  322.             pixfmt != V4L2_PIX_FMT_SBGGR8        /* blue line */  
  323.             && pixfmt != V4L2_PIX_FMT_SGBRG8);  
  324. }  
  325.   
  326. void v4lconvert_bayer_to_bgr24(const unsigned char *bayer,  
  327.         unsigned char *bgr, int width, int height, unsigned int pixfmt)  
  328. {  
  329.     bayer_to_rgbbgr24(bayer, bgr, width, height, pixfmt,  
  330.             pixfmt == V4L2_PIX_FMT_SGBRG8        /* start with green */  
  331.             || pixfmt == V4L2_PIX_FMT_SGRBG8,  
  332.             pixfmt == V4L2_PIX_FMT_SBGGR8        /* blue line */  
  333.             || pixfmt == V4L2_PIX_FMT_SGBRG8);  
  334. }  
  335.   
  336. static void v4lconvert_border_bayer_line_to_y(  
  337.         const unsigned char *bayer, const unsigned char *adjacent_bayer,  
  338.         unsigned char *y, int width, int start_with_green, int blue_line)  
  339. {  
  340.     int t0, t1;  
  341.   
  342.     if (start_with_green) {  
  343.         /* First pixel */  
  344.         if (blue_line) {  
  345.             *y++ = (8453 * adjacent_bayer[0] + 16594 * bayer[0] +  
  346.                     3223 * bayer[1] + 524288) >> 15;  
  347.         } else {  
  348.             *y++ = (8453 * bayer[1] + 16594 * bayer[0] +  
  349.                     3223 * adjacent_bayer[0] + 524288) >> 15;  
  350.         }  
  351.         /* Second pixel */  
  352.         t0 = bayer[0] + bayer[2] + adjacent_bayer[1];  
  353.         t1 = adjacent_bayer[0] + adjacent_bayer[2];  
  354.         if (blue_line)  
  355.             *y++ = (4226 * t1 + 5531 * t0 + 3223 * bayer[1] + 524288) >> 15;  
  356.         else  
  357.             *y++ = (8453 * bayer[1] + 5531 * t0 + 1611 * t1 + 524288) >> 15;  
  358.         bayer++;  
  359.         adjacent_bayer++;  
  360.         width -= 2;  
  361.     } else {  
  362.         /* First pixel */  
  363.         t0 = bayer[1] + adjacent_bayer[0];  
  364.         if (blue_line) {  
  365.             *y++ = (8453 * adjacent_bayer[1] + 8297 * t0 +  
  366.                     3223 * bayer[0] + 524288) >> 15;  
  367.         } else {  
  368.             *y++ = (8453 * bayer[0] + 8297 * t0 +  
  369.                     3223 * adjacent_bayer[1] + 524288) >> 15;  
  370.         }  
  371.         width--;  
  372.     }  
  373.   
  374.     if (blue_line) {  
  375.         for ( ; width > 2; width -= 2) {  
  376.             t0 = bayer[0] + bayer[2];  
  377.             *y++ = (8453 * adjacent_bayer[1] + 16594 * bayer[1] +  
  378.                     1611 * t0 + 524288) >> 15;  
  379.             bayer++;  
  380.             adjacent_bayer++;  
  381.   
  382.             t0 = bayer[0] + bayer[2] + adjacent_bayer[1];  
  383.             t1 = adjacent_bayer[0] + adjacent_bayer[2];  
  384.             *y++ = (4226 * t1 + 5531 * t0 + 3223 * bayer[1] + 524288) >> 15;  
  385.             bayer++;  
  386.             adjacent_bayer++;  
  387.         }  
  388.     } else {  
  389.         for ( ; width > 2; width -= 2) {  
  390.             t0 = bayer[0] + bayer[2];  
  391.             *y++ = (4226 * t0 + 16594 * bayer[1] +  
  392.                     3223 * adjacent_bayer[1] + 524288) >> 15;  
  393.             bayer++;  
  394.             adjacent_bayer++;  
  395.   
  396.             t0 = bayer[0] + bayer[2] + adjacent_bayer[1];  
  397.             t1 = adjacent_bayer[0] + adjacent_bayer[2];  
  398.             *y++ = (8453 * bayer[1] + 5531 * t0 + 1611 * t1 + 524288) >> 15;  
  399.             bayer++;  
  400.             adjacent_bayer++;  
  401.         }  
  402.     }  
  403.   
  404.     if (width == 2) {  
  405.         /* Second to last pixel */  
  406.         t0 = bayer[0] + bayer[2];  
  407.         if (blue_line) {  
  408.             *y++ = (8453 * adjacent_bayer[1] + 16594 * bayer[1] +  
  409.                     1611 * t0 + 524288) >> 15;  
  410.         } else {  
  411.             *y++ = (4226 * t0 + 16594 * bayer[1] +  
  412.                     3223 * adjacent_bayer[1] + 524288) >> 15;  
  413.         }  
  414.         /* Last pixel */  
  415.         t0 = bayer[1] + adjacent_bayer[2];  
  416.         if (blue_line) {  
  417.             *y++ = (8453 * adjacent_bayer[1] + 8297 * t0 +  
  418.                     3223 * bayer[2] + 524288) >> 15;  
  419.         } else {  
  420.             *y++ = (8453 * bayer[2] + 8297 * t0 +  
  421.                     3223 * adjacent_bayer[1] + 524288) >> 15;  
  422.         }  
  423.     } else {  
  424.         /* Last pixel */  
  425.         if (blue_line) {  
  426.             *y++ = (8453 * adjacent_bayer[1] + 16594 * bayer[1] +  
  427.                     3223 * bayer[0] + 524288) >> 15;  
  428.         } else {  
  429.             *y++ = (8453 * bayer[0] + 16594 * bayer[1] +  
  430.                     3223 * adjacent_bayer[1] + 524288) >> 15;  
  431.         }  
  432.     }  
  433. }  
  434.   
  435. void v4lconvert_bayer_to_yuv420(const unsigned char *bayer, unsigned char *yuv,  
  436.         int width, int height, unsigned int src_pixfmt, int yvu)  
  437. {  
  438.     int blue_line = 0, start_with_green = 0, x, y;  
  439.     unsigned char *ydst = yuv;  
  440.     unsigned char *udst, *vdst;  
  441.   
  442.     if (yvu) {  
  443.         vdst = yuv + width * height;  
  444.         udst = vdst + width * height / 4;  
  445.     } else {  
  446.         udst = yuv + width * height;  
  447.         vdst = udst + width * height / 4;  
  448.     }  
  449.   
  450.     printf("bayer address(0x%p)", bayer);  
  451.     /* First calculate the u and v planes 2x2 pixels at a time */  
  452.     switch (src_pixfmt) {  
  453.     case V4L2_PIX_FMT_SBGGR8:  
  454.         for (y = 0; y < height; y += 2) {  
  455.             for (x = 0; x < width; x += 2) {  
  456.                 int b, g, r;  
  457.   
  458.                 b  = bayer[x];  
  459.                 g  = bayer[x + 1];  
  460.                 g += bayer[x + width];  
  461.                 r  = bayer[x + width + 1];  
  462.                 *udst++ = (-4878 * r - 4789 * g + 14456 * b + 4210688) >> 15;  
  463.                 *vdst++ = (14456 * r - 6052 * g -  2351 * b + 4210688) >> 15;  
  464.             }  
  465.             bayer += 2 * width;  
  466.         }  
  467.         blue_line = 1;  
  468.         break;  
  469.   
  470.     case V4L2_PIX_FMT_SRGGB8:  
  471.         for (y = 0; y < height; y += 2) {  
  472.             for (x = 0; x < width; x += 2) {  
  473.                 int b, g, r;  
  474.   
  475.                 r  = bayer[x];  
  476.                 g  = bayer[x + 1];  
  477.                 g += bayer[x + width];  
  478.                 b  = bayer[x + width + 1];  
  479.                 *udst++ = (-4878 * r - 4789 * g + 14456 * b + 4210688) >> 15;  
  480.                 *vdst++ = (14456 * r - 6052 * g -  2351 * b + 4210688) >> 15;  
  481.             }  
  482.             bayer += 2 * width;  
  483.         }  
  484.         break;  
  485.   
  486.     case V4L2_PIX_FMT_SGBRG8:  
  487.         for (y = 0; y < height; y += 2) {  
  488.             for (x = 0; x < width; x += 2) {  
  489.                 int b, g, r;  
  490.   
  491.                 g  = bayer[x];  
  492.                 b  = bayer[x + 1];  
  493.                 r  = bayer[x + width];  
  494.                 g += bayer[x + width + 1];  
  495.                 *udst++ = (-4878 * r - 4789 * g + 14456 * b + 4210688) >> 15;  
  496.                 *vdst++ = (14456 * r - 6052 * g -  2351 * b + 4210688) >> 15;  
  497.             }  
  498.             bayer += 2 * width;  
  499.         }  
  500.         blue_line = 1;  
  501.         start_with_green = 1;  
  502.         break;  
  503.   
  504.     case V4L2_PIX_FMT_SGRBG8:  
  505.         for (y = 0; y < height; y += 2) {  
  506.             for (x = 0; x < width; x += 2) {  
  507.                 int b, g, r;  
  508.   
  509.                 g  = bayer[x];  
  510.                 r  = bayer[x + 1];  
  511.                 b  = bayer[x + width];  
  512.                 g += bayer[x + width + 1];  
  513.                 *udst++ = (-4878 * r - 4789 * g + 14456 * b + 4210688) >> 15;  
  514.                 *vdst++ = (14456 * r - 6052 * g -  2351 * b + 4210688) >> 15;  
  515.             }  
  516.             bayer += 2 * width;  
  517.         }  
  518.         start_with_green = 1;  
  519.         break;  
  520.     }  
  521.   
  522.     bayer -= width * height;  
  523.     printf("bayer address(0x%p)", bayer);  
  524.   
  525.     /* render the first line */  
  526.     v4lconvert_border_bayer_line_to_y(bayer, bayer + width, ydst, width,  
  527.             start_with_green, blue_line);  
  528.     ydst += width;  
  529.   
  530.     printf("bayer address(0x%p), height(%d)", bayer, height);  
  531.   
  532.     /* reduce height by 2 because of the border */  
  533.     for (height -= 2; height; height--) {  
  534.         int t0, t1;  
  535.         /* (width - 2) because of the border */  
  536.         const unsigned char *bayer_end = bayer + (width - 2);  
  537.   
  538.         if (start_with_green) {  
  539.             t0 = bayer[1] + bayer[width * 2 + 1];  
  540.             /* Write first pixel */  
  541.             t1 = bayer[0] + bayer[width * 2] + bayer[width + 1];  
  542.             if (blue_line)  
  543.                 *ydst++ = (8453 * bayer[width] + 5516 * t1 +  
  544.                         1661 * t0 + 524288) >> 15;  
  545.             else  
  546.                 *ydst++ = (4226 * t0 + 5516 * t1 +  
  547.                         3223 * bayer[width] + 524288) >> 15;  
  548.   
  549.             /* Write second pixel */  
  550.             t1 = bayer[width] + bayer[width + 2];  
  551.             if (blue_line)  
  552.                 *ydst++ = (4226 * t1 + 16594 * bayer[width + 1] +  
  553.                         1611 * t0 + 524288) >> 15;  
  554.             else  
  555.                 *ydst++ = (4226 * t0 + 16594 * bayer[width + 1] +  
  556.                         1611 * t1 + 524288) >> 15;  
  557.             bayer++;  
  558.         } else {  
  559.             /* Write first pixel */  
  560.             t0 = bayer[0] + bayer[width * 2];  
  561.             if (blue_line) {  
  562.                 *ydst++ = (8453 * bayer[width + 1] + 16594 * bayer[width] +  
  563.                         1661 * t0 + 524288) >> 15;  
  564.             } else {  
  565.                 *ydst++ = (4226 * t0 + 16594 * bayer[width] +  
  566.                         3223 * bayer[width + 1] + 524288) >> 15;  
  567.             }  
  568.         }  
  569.   
  570.         if (blue_line) {  
  571.             for (; bayer <= bayer_end - 2; bayer += 2) {  
  572.                 t0 = bayer[0] + bayer[2] + bayer[width * 2] + bayer[width * 2 + 2];  
  573.                 t1 = bayer[1] + bayer[width] + bayer[width + 2] + bayer[width * 2 + 1];  
  574.                 *ydst++ = (8453 * bayer[width + 1] + 4148 * t1 +  
  575.                         806 * t0 + 524288) >> 15;  
  576.   
  577.                 t0 = bayer[2] + bayer[width * 2 + 2];  
  578.                 t1 = bayer[width + 1] + bayer[width + 3];  
  579.                 *ydst++ = (4226 * t1 + 16594 * bayer[width + 2] +  
  580.                         1611 * t0 + 524288) >> 15;  
  581.             }  
  582.         } else {  
  583.             for (; bayer <= bayer_end - 2; bayer += 2) {  
  584.                 t0 = bayer[0] + bayer[2] + bayer[width * 2] + bayer[width * 2 + 2];  
  585.                 t1 = bayer[1] + bayer[width] + bayer[width + 2] + bayer[width * 2 + 1];  
  586.                 *ydst++ = (2113 * t0 + 4148 * t1 +  
  587.                         3223 * bayer[width + 1] + 524288) >> 15;  
  588.   
  589.                 t0 = bayer[2] + bayer[width * 2 + 2];  
  590.                 t1 = bayer[width + 1] + bayer[width + 3];  
  591.                 *ydst++ = (4226 * t0 + 16594 * bayer[width + 2] +  
  592.                         1611 * t1 + 524288) >> 15;  
  593.             }  
  594.         }  
  595.   
  596.         if (bayer < bayer_end) {  
  597.             /* Write second to last pixel */  
  598.             t0 = bayer[0] + bayer[2] + bayer[width * 2] + bayer[width * 2 + 2];  
  599.             t1 = bayer[1] + bayer[width] + bayer[width + 2] + bayer[width * 2 + 1];  
  600.             if (blue_line)  
  601.                 *ydst++ = (8453 * bayer[width + 1] + 4148 * t1 +  
  602.                         806 * t0 + 524288) >> 15;  
  603.             else  
  604.                 *ydst++ = (2113 * t0 + 4148 * t1 +  
  605.                         3223 * bayer[width + 1] + 524288) >> 15;  
  606.   
  607.             /* write last pixel */  
  608.             t0 = bayer[2] + bayer[width * 2 + 2];  
  609.             if (blue_line) {  
  610.                 *ydst++ = (8453 * bayer[width + 1] + 16594 * bayer[width + 2] +  
  611.                         1661 * t0 + 524288) >> 15;  
  612.             } else {  
  613.                 *ydst++ = (4226 * t0 + 16594 * bayer[width + 2] +  
  614.                         3223 * bayer[width + 1] + 524288) >> 15;  
  615.             }  
  616.             bayer++;  
  617.         } else {  
  618.             /* write last pixel */  
  619.             t0 = bayer[0] + bayer[width * 2];  
  620.             t1 = bayer[1] + bayer[width * 2 + 1] + bayer[width];  
  621.             if (blue_line)  
  622.                 *ydst++ = (8453 * bayer[width + 1] + 5516 * t1 +  
  623.                         1661 * t0 + 524288) >> 15;  
  624.             else  
  625.                 *ydst++ = (4226 * t0 + 5516 * t1 +  
  626.                         3223 * bayer[width + 1] + 524288) >> 15;  
  627.         }  
  628.   
  629.         /* skip 2 border pixels */  
  630.         bayer += 2;  
  631.   
  632.         blue_line = !blue_line;  
  633.         start_with_green = !start_with_green;  
  634.     }  
  635.   
  636.     /* render the last line */  
  637.     v4lconvert_border_bayer_line_to_y(bayer + width, bayer, ydst, width,  
  638.             !start_with_green, !blue_line);  
  639.   
  640. }  


##################################################################################

convert.c

##################################################################################

[cpp] view plain copy
  1. #include <stdio.h>  
  2. #include <string.h>  
  3. #include <stdlib.h>  
  4. #include <fcntl.h>  
  5. #include <sys/stat.h>  
  6. #include <linux/videodev2.h>  
  7. #include "convert.h"  
  8.   
  9. char *g_src_file = NULL;  
  10. char *g_dest_file = NULL;  
  11. int g_in_width = 352;  
  12. int g_in_height = 292;  
  13.   
  14. unsigned int g_src_fmt = V4L2_PIX_FMT_SGBRG8;  
  15. //unsigned int g_dest_fmt = V4L2_PIX_FMT_RGB24;  
  16. unsigned int g_dest_fmt = V4L2_PIX_FMT_YUV420;  
  17.   
  18.   
  19. int process_cmdline(int argc, char **argv)  
  20. {  
  21.     int i;  
  22.     char *tmp;  
  23.   
  24.     for (i = 1; i < argc; i++) {  
  25.         if (strcmp(argv[i], "-s") == 0) {  
  26.             g_src_file = argv[++i];  
  27.         }  
  28.         else if (strcmp(argv[i], "d") == 0) {  
  29.             g_dest_file = strdup(argv[++i]);  
  30.         }  
  31.         else if (strcmp(argv[i], "-sf") == 0) {  
  32.             tmp = argv[++i];  
  33.             if (strlen(tmp) == 4) {  
  34.                 g_src_fmt = v4l2_fourcc(tmp[0], tmp[1], tmp[2], tmp[3]);  
  35.             }  
  36.         }  
  37.         else if (strcmp(argv[i], "-df") == 0) {  
  38.             tmp = argv[++i];  
  39.             if (strlen(tmp) == 4) {  
  40.                 g_dest_fmt = v4l2_fourcc(tmp[0], tmp[1], tmp[2], tmp[3]);  
  41.             }  
  42.         }  
  43.         else if (strcmp(argv[i], "iw") == 0) {  
  44.             g_in_width = atoi(argv[++i]);  
  45.         }  
  46.         else if (strcmp(argv[i], "ih") == 0) {  
  47.             g_in_height = atoi(argv[++i]);  
  48.         }  
  49.     }  
  50.   
  51.     if (g_src_file && g_dest_file == NULL) {  
  52.         g_dest_file = malloc(256);  
  53.         sprintf(g_dest_file, "%s.out", g_src_file);  
  54.     }  
  55.     if (g_in_width == 0 || g_in_height == 0) {  
  56.           
  57.     }  
  58. }  
  59.   
  60. int get_file_size(int fd)  
  61. {  
  62.     int ret;  
  63.     struct stat sb;  
  64.   
  65.     ret = fstat(fd, &sb);  
  66.     if (ret == -1) {  
  67.         return ret;  
  68.     }  
  69.       
  70.     return sb.st_size;  
  71. }  
  72.   
  73. int get_bits_per_pixel(unsigned int fmt)  
  74. {  
  75.     int ret;  
  76.   
  77.     switch (fmt) {  
  78.     case V4L2_PIX_FMT_RGB24:  
  79.         ret = 24;  
  80.         break;  
  81.     case V4L2_PIX_FMT_SBGGR8:  
  82.     case V4L2_PIX_FMT_SGBRG8:  
  83.     case V4L2_PIX_FMT_SGRBG8:  
  84.     case V4L2_PIX_FMT_SRGGB8:  
  85.         ret = 8;  
  86.         break;  
  87.     case V4L2_PIX_FMT_YUV420:  
  88.         ret = 12;  
  89.         break;  
  90.     case V4L2_PIX_FMT_YUYV:  
  91.     case V4L2_PIX_FMT_UYVY:  
  92.         ret = 16;  
  93.         break;  
  94.     default:  
  95.         ret = -1;  
  96.         break;  
  97.     }  
  98.   
  99.     return ret;  
  100. }  
  101.   
  102.   
  103. void convert(void *src, void *dest, int width, int height, unsigned int src_fmt, unsigned int dest_fmt)  
  104. {  
  105.     switch (src_fmt) {  
  106.     case V4L2_PIX_FMT_SGBRG8:  
  107.         if (dest_fmt == V4L2_PIX_FMT_RGB24) {  
  108.             v4lconvert_bayer_to_rgb24(src, dest, width, height, src_fmt);  
  109.         }  
  110.         else if (dest_fmt == V4L2_PIX_FMT_YUV420) {  
  111.             v4lconvert_bayer_to_yuv420(src, dest, width, height, src_fmt, 0);  
  112.         }  
  113.         break;  
  114.     default:  
  115.         break;  
  116.     }  
  117. }  
  118.   
  119. int main(int argc, char *argv[])  
  120. {  
  121.     int ret = 0;  
  122.     int fd_src = 0;  
  123.     int fd_dest = 0;  
  124.     int src_size = 0;  
  125.     int dest_size = 0;  
  126.     int pix_num;  
  127.     void *src = NULL;  
  128.     void *dest = NULL;  
  129.   
  130.     process_cmdline(argc, argv);  
  131.     if (g_src_file == NULL || g_dest_file == NULL) {  
  132.         ret = -1;  
  133.         goto bailout;  
  134.     }  
  135.   
  136.     printf("input file(%s), output file(%s)\n", g_src_file, g_dest_file);  
  137.   
  138.     fd_src = open(g_src_file, O_RDONLY);  
  139.     if (fd_src == -1) {  
  140.         ret = -2;  
  141.         goto bailout;  
  142.     }  
  143.   
  144.     fd_dest = open(g_dest_file, O_RDWR | O_CREAT, 0666);  
  145.     if (fd_dest == -1) {  
  146.         ret = -3;  
  147.         goto bailout;  
  148.     }  
  149.   
  150.     src_size = get_file_size(fd_src);  
  151.     if (src_size == -1) {  
  152.         ret = -4;  
  153.         goto bailout;  
  154.     }  
  155.   
  156.     pix_num = src_size / (get_bits_per_pixel(g_src_fmt)/8) ;  
  157.     //dest_size = pix_num * get_bytes_per_pixel(g_dest_fmt);  
  158.     ret = get_bits_per_pixel(g_dest_fmt);  
  159.     dest_size = pix_num * ret / 8;  
  160.   
  161.     printf("src_size(%d), dest_size(%d)\n", src_size, dest_size);  
  162.     src = malloc(src_size);  
  163.     dest = malloc(dest_size);  
  164.     if (src == NULL || dest == NULL) {  
  165.         ret = -5;  
  166.         goto bailout;  
  167.     }  
  168.   
  169.     ret = read(fd_src, src, src_size);  
  170.     if (ret != src_size) {  
  171.         ret = -6;  
  172.         goto bailout;  
  173.     }  
  174.   
  175.     convert(src, dest, g_in_width, g_in_height, g_src_fmt, g_dest_fmt);  
  176.   
  177.     printf("write out file, size=%d\n", dest_size);  
  178.     ret = write(fd_dest, dest, dest_size);  
  179.     if (ret != dest_size) {  
  180.         ret = -1;  
  181.         goto bailout;  
  182.     }  
  183.   
  184. bailout:  
  185.     if (src)  
  186.         free(src);  
  187.     if (dest)  
  188.         free(dest);  
  189.     if (fd_src)  
  190.         close(fd_src);  
  191.     if (fd_dest)  
  192.         close(fd_dest);  
  193.     if (g_dest_file)  
  194.         free(g_dest_file);  
  195.   
  196.     printf("ret(%d)\n", ret);  
  197.     return ret;  
  198. }  


##################################################################################


covert.h

##################################################################################

[cpp] view plain copy
  1. #ifndef __LIBV4LCONVERT_PRIV_H  
  2. #define __LIBV4LCONVERT_PRIV_H  
  3.   
  4. #define V4L2_PIX_FMT_SRGGB8 v4l2_fourcc('R', 'G', 'G', 'B')  
  5.   
  6. void v4lconvert_bayer_to_rgb24(const unsigned char *bayer,  
  7.         unsigned char *bgr, int width, int height, unsigned int pixfmt);  
  8.   
  9. void v4lconvert_bayer_to_bgr24(const unsigned char *bayer,  
  10.         unsigned char *bgr, int width, int height, unsigned int pixfmt);  
  11.   
  12. void v4lconvert_bayer_to_yuv420(const unsigned char *bayer, unsigned char *yuv,  
  13.         int width, int height, unsigned int src_pixfmt, int yvu);  
  14.   
  15.   
  16. #endif /* __LIBV4LCONVERT_PRIV_H */  


##################################################################################

Makefile

##################################################################################

[cpp] view plain copy
  1. CONVERT := convert  
  2. CFLAGS := -static  
  3.   
  4. CC = gcc  
  5.   
  6. $(CONVERT): convert.c bayer.c  
  7.   
  8.     $(CC) -o $@ $^ $(CFLAGS)  
  9. clean:  
  10.     rm -f *.o $(CONVERT)  


##################################################################################


RGB32 RGB565转换为YUV444

另外为了在PC显示framebuffer中的数据,写了一个ARGB32或者RGB565到YUV444的转换工具。利用YUVtools显示YUV444图像。

其实可以不用这么麻烦,使用ddms截屏也行。

YUV444是 Y U V分量比为4:4:4,每个像素用24bits表示。


##################################################################################

[cpp] view plain copy
  1. #include <stdio.h>  
  2. #include <string.h>  
  3. #include <stdlib.h>  
  4. #include <fcntl.h>  
  5. #include <sys/stat.h>  
  6. #include <linux/videodev2.h>  
  7.   
  8. char *g_src_file = NULL;  
  9. char *g_dest_file = NULL;  
  10. int g_in_width = 720;  
  11. int g_in_height = 480;  
  12.   
  13. /* V4L2_PIX_FMT_BGR32 */  
  14. //unsigned int g_src_fmt = V4L2_PIX_FMT_RGB32;  
  15. unsigned int g_src_fmt = V4L2_PIX_FMT_RGB565;  
  16. /* V4L2_PIX_FMT_YUYV V4L2_PIX_FMT_YVYU V4L2_PIX_FMT_UYVY V4L2_PIX_FMT_VYUY */  
  17. unsigned int g_dest_fmt = V4L2_PIX_FMT_YUV444;  
  18.   
  19. static unsigned char get_R(unsigned char *src, unsigned int fmt)  
  20. {  
  21.     unsigned char R;  
  22.   
  23.     switch (fmt) {  
  24.         case V4L2_PIX_FMT_RGB32:  
  25.         case V4L2_PIX_FMT_BGR32:  
  26.         case V4L2_PIX_FMT_RGB24:  
  27.             R = *(src);  
  28.             break;  
  29.         case V4L2_PIX_FMT_RGB565:  
  30.             R = (*(src) & 0x1F) << 3;  
  31.             break;  
  32.         case V4L2_PIX_FMT_SBGGR8:  
  33.         case V4L2_PIX_FMT_SGBRG8:  
  34.         case V4L2_PIX_FMT_SGRBG8:  
  35.         case V4L2_PIX_FMT_YUV420:  
  36.         case V4L2_PIX_FMT_YUV444:  
  37.         default:  
  38.             R = 0;  
  39.             break;  
  40.     }  
  41.   
  42.     return R;  
  43. }  
  44.   
  45. static unsigned char get_G(unsigned char *src, unsigned int fmt)  
  46. {  
  47.     unsigned char G;  
  48.   
  49.     switch (fmt) {  
  50.         case V4L2_PIX_FMT_RGB32:  
  51.         case V4L2_PIX_FMT_BGR32:  
  52.         case V4L2_PIX_FMT_RGB24:  
  53.             G = *(src + 1);  
  54.             break;  
  55.         case V4L2_PIX_FMT_RGB565:  
  56.             G = ((*(src) & 0xE0) >> 3) | ((*(src + 1) & 0x07) << 5);  
  57.             break;  
  58.         case V4L2_PIX_FMT_SBGGR8:  
  59.         case V4L2_PIX_FMT_SGBRG8:  
  60.         case V4L2_PIX_FMT_SGRBG8:  
  61.         case V4L2_PIX_FMT_YUV420:  
  62.         case V4L2_PIX_FMT_YUV444:  
  63.         default:  
  64.             G = 0;  
  65.             break;  
  66.     }  
  67.   
  68.     return G;  
  69. }  
  70.   
  71. static unsigned char get_B(unsigned char *src, unsigned int fmt)  
  72. {  
  73.     unsigned char B;  
  74.   
  75.     switch (fmt) {  
  76.         case V4L2_PIX_FMT_RGB32:  
  77.         case V4L2_PIX_FMT_BGR32:  
  78.         case V4L2_PIX_FMT_RGB24:  
  79.             B = *(src + 2);  
  80.             break;  
  81.         case V4L2_PIX_FMT_RGB565:  
  82.             B = (*(src + 1) & 0xF8);  
  83.             break;  
  84.         case V4L2_PIX_FMT_SBGGR8:  
  85.         case V4L2_PIX_FMT_SGBRG8:  
  86.         case V4L2_PIX_FMT_SGRBG8:  
  87.         case V4L2_PIX_FMT_YUV420:  
  88.         case V4L2_PIX_FMT_YUV444:  
  89.         default:  
  90.             B = 0;  
  91.             break;  
  92.     }  
  93.   
  94.     return B;  
  95. }  
  96.   
  97.   
  98. rgb2yuv(char *rgb, char *yuv)  
  99. {  
  100.     int i, j;  
  101.     unsigned char R, G, B, *y, *u, *v, *alpha;  
  102.     double RR, GG, BB;  
  103.     unsigned char *src, *dest;  
  104.   
  105.     for (i = 0; i < g_in_height; i++) {  
  106.         for (j = 0; j < g_in_width; j++) {  
  107.             src = rgb + (i * g_in_width + j) * get_bpp(g_src_fmt) / 8;  
  108.             dest = yuv + (i * g_in_width + j) * get_bpp(g_dest_fmt) / 8;  
  109.   
  110.             R = get_R(src, g_src_fmt);  
  111.             G = get_G(src, g_src_fmt);  
  112.             B = get_B(src, g_src_fmt);  
  113.             /* normalize to 16 ..235 */  
  114.             RR = 219 * R / 255 + 16;  
  115.             GG = 219 * G / 255 + 16;  
  116.             BB = 219 * B / 255 + 16;  
  117.               
  118.             y = dest;  
  119.             u = dest + 1;  
  120.             v = dest + 2;  
  121.             //alpha = dest + 3;  
  122.   
  123.             *y = (unsigned char)(0.2991*RR + 0.5849*GG + 0.1159*BB + 0.5);  
  124.             *u = (unsigned char)(-0.1725*RR - 0.3372*GG + 0.5097*BB + 128.5);  
  125.             *v = (unsigned char)(0.5097*RR - 0.4254*GG - 0.0843*BB + 128.5);  
  126.             //*alpha = 255;  
  127.         }      
  128.     }  
  129. }  
  130.   
  131. int process_cmdline(int argc, char **argv)  
  132. {  
  133.     int i;  
  134.     char *tmp;  
  135.   
  136.     for (i = 1; i < argc; i++) {  
  137.         if (strcmp(argv[i], "-s") == 0) {  
  138.             g_src_file = argv[++i];  
  139.         }  
  140.         else if (strcmp(argv[i], "-d") == 0) {  
  141.             g_dest_file = strdup(argv[++i]);  
  142.         }  
  143.         else if (strcmp(argv[i], "-sf") == 0) {  
  144.             tmp = argv[++i];  
  145.             if (strlen(tmp) == 4) {  
  146.                 g_src_fmt = v4l2_fourcc(tmp[0], tmp[1], tmp[2], tmp[3]);  
  147.             }  
  148.         }  
  149.         else if (strcmp(argv[i], "-df") == 0) {  
  150.             tmp = argv[++i];  
  151.             if (strlen(tmp) == 4) {  
  152.                 g_dest_fmt = v4l2_fourcc(tmp[0], tmp[1], tmp[2], tmp[3]);  
  153.             }  
  154.         }  
  155.         else if (strcmp(argv[i], "iw") == 0) {  
  156.             g_in_width = atoi(argv[++i]);  
  157.         }  
  158.         else if (strcmp(argv[i], "ih") == 0) {  
  159.             g_in_height = atoi(argv[++i]);  
  160.         }  
  161.     }  
  162.   
  163.     if (g_src_file && g_dest_file == NULL) {  
  164.         g_dest_file = malloc(256);  
  165.         sprintf(g_dest_file, "%s.out", g_src_file);  
  166.     }  
  167.     if (g_in_width == 0 || g_in_height == 0) {  
  168.   
  169.     }  
  170. }  
  171.   
  172. int get_bpp(unsigned int fmt)  
  173. {  
  174.     int ret;  
  175.   
  176.     switch (fmt) {  
  177.         case V4L2_PIX_FMT_RGB32:  
  178.         case V4L2_PIX_FMT_BGR32:  
  179.             ret = 32;  
  180.             break;  
  181.         case V4L2_PIX_FMT_RGB24:  
  182.             ret = 24;  
  183.             break;  
  184.         case V4L2_PIX_FMT_RGB565:  
  185.             ret = 16;  
  186.             break;  
  187.         case V4L2_PIX_FMT_SBGGR8:  
  188.         case V4L2_PIX_FMT_SGBRG8:  
  189.         case V4L2_PIX_FMT_SGRBG8:  
  190.             ret = 8;  
  191.             break;  
  192.         case V4L2_PIX_FMT_YUV420:  
  193.             ret = 12;  
  194.             break;  
  195.         case V4L2_PIX_FMT_YUYV:  
  196.         case V4L2_PIX_FMT_UYVY:  
  197.             ret = 16;  
  198.             break;  
  199.         case V4L2_PIX_FMT_YUV444:  
  200.             ret = 24;  
  201.             break;  
  202.         default:  
  203.             ret = -1;  
  204.             break;  
  205.     }  
  206.   
  207.     return ret;  
  208. }  
  209.   
  210. main(int argc, char *argv[])  
  211. {  
  212.     int ret;  
  213.     int src_fd, dest_fd;  
  214.     int src_size, dest_size;  
  215.     char *src_buf;  
  216.     char *dest_buf;  
  217.   
  218.     process_cmdline(argc, argv);  
  219.     if (g_src_file == NULL || g_dest_file == NULL) {  
  220.         ret = -1;  
  221.         goto bailout;  
  222.     }  
  223.   
  224.     src_fd = open(g_src_file, O_RDONLY);  
  225.     if (src_fd == -1) {  
  226.         ret = -2;  
  227.         goto bailout;  
  228.     }  
  229.   
  230.     dest_fd = open(g_dest_file, O_RDWR | O_CREAT, 0666);  
  231.     if (dest_fd == -1) {  
  232.         ret = -3;  
  233.         goto bailout;  
  234.     }  
  235.   
  236.     src_size = g_in_width * g_in_height * get_bpp(g_src_fmt) / 8;  
  237.     dest_size = g_in_width * g_in_height * get_bpp(g_dest_fmt) / 8;  
  238.   
  239.     src_buf = malloc(src_size);  
  240.     dest_buf = malloc(dest_size);  
  241.   
  242.     ret = read(src_fd, src_buf, src_size);  
  243.     if (ret != src_size) {  
  244.         printf("src_size=%d, ret=%d\n", src_size, ret);  
  245.         ret = -4;  
  246.         goto bailout;  
  247.     }  
  248.   
  249.     rgb2yuv(src_buf, dest_buf);  
  250.   
  251.     printf("write out file, size=%d\n", dest_size);  
  252.     ret = write(dest_fd, dest_buf, dest_size);  
  253.     if (ret != dest_size) {  
  254.         ret = -5;  
  255.         goto bailout;  
  256.     }  
  257.   
  258. bailout:  
  259.     printf("ret(%d)\n", ret);  
  260.     return ret;  
  261.   
  262. }  

##################################################################################
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值