Convert uyvy video format to BMP Image

Source: Internet
Author: User
# Include <windows. h> # include <malloc. h ># include <string> using namespace STD; # define clip (Min, X, max) x = (x <min )? Min: (x> MAX )? MAX: X /////////////////////////////////////// //// // construction/destruction //// //////////////////////////////////////// /// // cuyvy2bmp:: cuyvy2bmp (INT width, int height) {m_nwidth = width; m_nheight = height;} cuyvy2bmp ::~ Cuyvy2bmp () {}// accessor int cuyvy2bmp: getheight () {return m_nheight;} int Height: getwidth () {return m_nwidth;} void cuyvy2bmp: makeconversiontable () {for (long J = 0; j <256; ++ J) {table_uyvy2rgb.ytor [J] = partition [J] = table_uyvy2rgb.ytob [J] = (unsigned short) (j <7); table_uyvy2rgb.vtor [J] = J * 180; // 180 = 1.402*128 table_uyvy2rgb.vtog [J] = J * 91; table_uyvy2rgb.utog [J] = J * 44; // 0.3437 = 44/128 table_uyvy2rgb.utob [J] = J * 226; // 1.772 = 226/128} bool cuyvy2bmp: uyvy2bmp (byte * pdst, byte * psrc) {long m = 0; long K = 0; int n = m_nwidth/2; int dec = m_nwidth * 4; int tmpr0 = 0; int tmpg0 = 0; int tmpb0 = 0; int tmpr1 = 0; int tmpg1 = 0; int tmpb1 = 0; makeconversiontable (); k = (m_nHeight-1) * m_nwidth <1; // K points to the last line for (INT I = m_nHeight-1; I>-1; I --) {for (Int J = 0; j <n; j ++) {tflat 0 = (table_uyvy2rgb.ytor [psrc [k + 1] + table_uyvy2rgb.vtor [psrc [K + 2]-22906)> 7; tmpg0 = (table_uyvy2rgb.ytog [psrc [k + 1]-table_uyvy2rgb.vtog [psrc [K + 2]-example [psrc [K + 0] + 17264)> 7; tmpb0 = (table_uyvy2rgb.ytob [psrc [k + 1] + table_uyvy2rgb.utob [psrc [K + 0]-28928)> 7; tmpr1 = (table_uyvy2rgb.ytor [psrc [K + 3] + table_uyvy2rgb.vtor [psrc [K + 2]-22906)> 7; t Mpg1 = (table_uyvy2rgb.ytog [psrc [K + 3]-table_uyvy2rgb.vtog [psrc [K + 2]-example [psrc [K + 0] + 17264)> 7; tmpb1 = (table_uyvy2rgb.ytob [psrc [K + 3] + table_uyvy2rgb.utob [psrc [K + 0]-28928)> 7; clip (0, tmpr0, 255 ); clip (0, tmpg0, 255); clip (0, tmpb0, 255); clip (0, tmpr1, 255); clip (0, tmpg1, 255); clip (0, tmpb1, 255); pdst [M + 0] = tmpb0; pdst [M + 1] = tmpg0; pdst [M + 2] = Tmpr0; pdst [M + 3] = tmpb1; pdst [M + 4] = tmpg1; pdst [M + 5] = tmpr1; k + = 4; m + = 6 ;} k = k-Dec;} return 1;} bool cuyvy2bmp: writebmpfile (lpstr BMP filename, byte * prgbbuf, int ibitcount) {long rgb_size = m_nwidth * m_nheight * 3; if (ibitcount = 24) {file * FP; Long Count = 0; bitmapfileheader bmpheader; bitmapinfo BMP Info; If (FP = fopen (BMP filename, "WB ")) = NULL) {printf ("can not create BMP File: % s \ n ", BMP filename); Return false;} bmpheader. bftype = 'mb'; bmpheader. bfsize = rgb_size + sizeof (bitmapfileheader) + sizeof (bitmapinfoheader); bmpheader. bfreserved1 = 0; bmpheader. bfreserved2 = 0; bmpheader. bfoffbits = sizeof (bitmapfileheader) + sizeof (bitmapinfoheader); BMP info. bmiheader. bisize = sizeof (bitmapinfoheader); BMP info. bmiheader. biwidth = m_nwidth; BMP info. bmiheader. biheight = M_nheight; BMP info. bmiheader. biplanes = 1; BMP info. bmiheader. bibitcount = 24; BMP info. bmiheader. bicompression = bi_rgb; BMP info. bmiheader. bisizeimage = rgb_size; BMP info. bmiheader. bixpelspermeter = 0; BMP info. bmiheader. biypelspermeter = 0; BMP info. bmiheader. biclrused = 0; BMP info. bmiheader. biclrimportant = 0; If (COUNT = fwrite (& bmpheader, 1, sizeof (bitmapfileheader), FP ))! = Sizeof (bitmapfileheader) printf ("Write BMP file header failed: Count = % d \ n", count); If (COUNT = fwrite (& (BMP info. bmiheader), 1, sizeof (bitmapinfoheader), FP ))! = Sizeof (bitmapinfoheader) printf ("read BMP file info failed: Count = % d \ n", count); If (COUNT = fwrite (prgbbuf, 1, rgb_size, FP ))! = Rgb_size) printf ("Write BMP file data failed: Count = % d \ n", count); fclose (FP); Return true;} else return false ;} int main (){
CUYVY2BMP test(352 ,288);    FILE *pFYUV;int errno=0;    int n=test.GetWidth()/2;
// 123 for YUV video file name if (pfyuv = fopen ("123", "rb") = NULL) {return-1 ;} byte * psrcbuf = (byte *) malloc (test. getheight () * test. getwidth () * 2); byte * pdstbuf = (byte *) malloc (test. getheight () * test. getwidth () * 3); fread (psrcbuf, 1, test. getheight () * test. getwidth () * 2, pfyuv); test. uyvy2bmp (pdstbuf, psrcbuf); test. writebmpfile ("402.bmp", pdstbuf, 24); Return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.