求DELPHI版的YV12ToRGB代码?YV12 To RGBDelphi

解决方案 »

  1.   


    unit RGBYUV;interface
    uses Windows,Classes,Graphics,Math;
    function RGBToColor(R,G,B: Byte): TColor;overload;
    function RGBToColor(Color : Longint): TColor;overload;procedure ColorToYUV(RGB: TColor; out Y, Cb, Cr: byte);
    function YUVtoColor(Y, Cb, Cr: Byte): TColor;
    implementationfunction RGBToColor(R,G,B: Byte): TColor;
    begin
      Result := R or (G shl 8) or (B shl 16);
    end;function RGBToColor(Color : Longint): TColor;
    var
      R,G,B,A : Byte;
    begin
      R := Color and $FF;
      G := (Color shr 8) and $FF;
      B := (Color shr 16) and $FF;
      A := $00;
      Result := R or (G shl 8) or (B shl 16) or (A shl 24);
    end;procedure ColorToRGB(Color: TColor; out R, G, B: Byte);
    begin
      R := Color and $FF;
      G := (Color shr 8) and $FF;
      B := (Color shr 16) and $FF;
    end; procedure ColorToCMY(Color: TColor; out C, M, Y: Byte);
    var
      R,G,B : Byte;
    begin
      ColorToRGB(Color, R, G, B);
      C := 255 - R;
      M := 255 - G;
      Y := 255 - B;
    end; function CMYToColor(C, M, Y: Byte): TColor;
    begin
      Result := (255 - C) or ((255 - M) shl 8) or ((255 - Y) shl 16);
    end; function HSLtoColor(H, S, L: Double): TColor;
    var
      M1, M2: double;
      function HueToColourValue (Hue: double) : byte;
      var
        V : double;
      begin
        if Hue < 0 then
          Hue := Hue + 1
        else
          if Hue > 1 then
            Hue := Hue - 1;    if 6 * Hue < 1 then
          V := M1 + (M2 - M1) * Hue * 6
        else
          if 2 * Hue < 1 then
            V := M2
          else
            if 3 * Hue < 2 then
              V := M1 + (M2 - M1) * (2/3 - Hue) * 6
            else
              V := M1;
        Result := round (255 * V)
      end;
    var
      R, G, B: byte;
    begin
      if S = 0 then
      begin
        R := round (255 * L);
        G := R;
        B := R
      end
      else
      begin
        if L <= 0.5 then
          M2 := L * (1 + S)
        else
          M2 := L + S - L * S;
        M1 := 2 * L - M2;
        R := HueToColourValue (H + 1/3);
        G := HueToColourValue (H);
        B := HueToColourValue (H - 1/3)
      end;
      Result := RGB (R, G, B)
    end; 
    procedure ColortoHSL(RGB: TColor; out H, S, L: double);
    var
      R, G, B : double;
      D, Cmax, Cmin: double;
    begin
      R := GetRValue (RGB) / 255;
      G := GetGValue (RGB) / 255;
      B := GetBValue (RGB) / 255;
      Cmax := Max (R, Max (G, B));
      Cmin := Min (R, Min (G, B));
      // calculate luminosity
      L := (Cmax + Cmin) / 2; 
      if Cmax = Cmin then
      begin
        H := 0;// it's actually undefined
        S := 0;
      end
      else
      begin
        D := Cmax - Cmin;
        // calculate Saturation
        if L < 0.5 then
          S := D / (Cmax + Cmin)
        else
          S := D / (2 - Cmax - Cmin);    // calculate Hue
        D_R := (((Cmax - R) / 6) + (D / 2)) / D;
        D_G := (((Cmax - G) / 6) + (D / 2)) / D;
        D_B := (((Cmax - B) / 6) + (D / 2)) / D;    if      R = Cmax then  H := (D_B - D_G)
        else if G = Cmax then  H := (1 / 3) + D_R - D_B
        else if B = Cmax then  H := (2 / 3) + D_G - D_R;    if H < 0 then H := H + 1;
        if H > 1 then H := H - 1;
      end;
    end;
    procedure ColorToYUV(RGB: TColor; out Y, Cb, Cr: byte);
    {
    Lum (Y) ranges from 16 to 235.
    Cb  (U) ranges from 16 to 240.
    Cr  (V) ranges from 16 to 240.
    }
    var
      R,G,B : byte;
      t: Double;
    begin
      R := GetRValue(RGB);
      G := GetGValue(RGB);
      B := GetBValue(RGB);  Y  := Round( (0.257 * R) + (0.504 * G) + (0.098 * B) + 16 );
      Cb := Round( (-0.148 * R) - (0.291 * G) + (0.439 * B) + 128 );
      Cr := Round( (0.439 * R) - (0.368 * G) - (0.071 * B) + 128 );
      {y := Round(0.299*R + 0.587*G + 0.114*B);
      Cb := Round(0.564*(B - Y ));
      Cr := Round(0.713*(R - Y )) }
    end;  
    function YUVtoColor(Y, Cb, Cr: Byte): TColor;
    {
    Lum (Y) ranges from 16 to 235.
    Cb  (U) ranges from 16 to 240.
    Cr  (V) ranges from 16 to 240.
    }
    var
      R,G,B: Byte;
    begin
      G := Round( (1.164 * (Y-16)) - (0.391 * (Cb-128)) - (0.813 * (Cr-128)) );
      R := Round( (1.164 * (Y-16)) + (1.596 * (Cr-128)) );
      B := Round( (1.164 * (Y-16)) + (2.018 * (Cb-128)) );  Result := RGBToColor(R, G, B);
    end;  
    end.
      

  2.   

    function YV12ToRGB(yv12:TMyByteArray;nWidth,nHeight:integer;var rgb24:TMyByteArray):boolean;
    const
     Table_fv1:array[0..255] of integer=( -180, -179, -177, -176, -174, -173, -172, -170, -169, -167, -166, -165, -163, -162, -160, -159, -158, -156, -155, -153,
        -152, -151, -149, -148, -146, -145, -144, -142, -141, -139, -138, -137, -135, -134, -132, -131, -130, -128, -127, -125, -124, -123, -121, -120, -118,
        -117, -115, -114, -113, -111, -110, -108, -107, -106, -104, -103, -101, -100, -99, -97, -96, -94, -93, -92, -90, -89, -87, -86, -85, -83, -82, -80, -79,
        -78, -76, -75, -73, -72, -71, -69, -68, -66, -65, -64, -62, -61, -59, -58, -57, -55, -54, -52, -51, -50, -48, -47, -45, -44, -43, -41, -40, -38, -37, -36,
         -34, -33, -31, -30, -29, -27, -26, -24, -23, -22, -20, -19, -17, -16, -15, -13, -12, -10, -9, -8, -6, -5, -3, -2, 0, 1, 2, 4, 5, 7, 8, 9, 11, 12, 14, 15,
         16, 18, 19, 21, 22, 23, 25, 26, 28, 29, 30, 32, 33, 35, 36, 37, 39, 40, 42, 43, 44, 46, 47, 49, 50, 51, 53, 54, 56, 57, 58, 60, 61, 63, 64, 65, 67, 68, 70,
         71, 72, 74, 75, 77, 78, 79, 81, 82, 84, 85, 86, 88, 89, 91, 92, 93, 95, 96, 98, 99, 100, 102, 103, 105, 106, 107, 109, 110, 112, 113, 114, 116, 117, 119,
        120, 122, 123, 124, 126, 127, 129, 130, 131, 133, 134, 136, 137, 138, 140, 141, 143, 144, 145, 147, 148, 150, 151, 152, 154, 155, 157, 158, 159, 161, 162,
         164, 165, 166, 168, 169, 171, 172, 173, 175, 176, 178 );
     Table_fv2:array[0..255] of integer=( -92, -91, -91, -90, -89, -88, -88, -87, -86, -86, -85, -84, -83, -83, -82, -81, -81, -80, -79, -78, -78, -77, -76, -76,
        -75, -74, -73, -73, -72, -71, -71, -70, -69, -68, -68, -67, -66, -66, -65, -64, -63, -63, -62, -61, -61, -60, -59, -58, -58, -57, -56, -56, -55, -54, -53,
        -53, -52, -51, -51, -50, -49, -48, -48, -47, -46, -46, -45, -44, -43, -43, -42, -41, -41, -40, -39, -38, -38, -37, -36, -36, -35, -34, -33, -33, -32, -31,
        -31, -30, -29, -28, -28, -27, -26, -26, -25, -24, -23, -23, -22, -21, -21, -20, -19, -18, -18, -17, -16, -16, -15, -14, -13, -13, -12, -11, -11, -10, -9,
        -8, -8, -7, -6, -6, -5, -4, -3, -3, -2, -1, 0, 0, 1, 2, 2, 3, 4, 5, 5, 6, 7, 7, 8, 9, 10, 10, 11, 12, 12, 13, 14, 15, 15, 16, 17, 17, 18, 19, 20, 20, 21,
        22, 22, 23, 24, 25, 25, 26, 27, 27, 28, 29, 30, 30, 31, 32, 32, 33, 34, 35, 35, 36, 37, 37, 38, 39, 40, 40, 41, 42, 42, 43, 44, 45, 45, 46, 47, 47, 48,
        49, 50, 50, 51, 52, 52, 53, 54, 55, 55, 56, 57, 57, 58, 59, 60, 60, 61, 62, 62, 63, 64, 65, 65, 66, 67, 67, 68, 69, 70, 70, 71, 72, 72, 73, 74, 75, 75,
        76, 77, 77, 78, 79, 80, 80, 81, 82, 82, 83, 84, 85, 85, 86, 87, 87, 88, 89, 90, 90 );
     Table_fu1:array[0..255] of integer=( -44, -44, -44, -43, -43, -43, -42, -42, -42, -41, -41, -41, -40, -40, -40, -39, -39, -39, -38, -38, -38, -37, -37, -37,
        -36, -36, -36, -35, -35, -35, -34, -34, -33, -33, -33, -32, -32, -32, -31, -31, -31, -30, -30, -30, -29, -29, -29, -28, -28, -28, -27, -27, -27, -26, -26,
        -26, -25, -25, -25, -24, -24, -24, -23, -23, -22, -22, -22, -21, -21, -21, -20, -20, -20, -19, -19, -19, -18, -18, -18, -17, -17, -17, -16, -16, -16, -15,
        -15, -15, -14, -14, -14, -13, -13, -13, -12, -12, -11, -11, -11, -10, -10, -10, -9, -9, -9, -8, -8, -8, -7, -7, -7, -6, -6, -6, -5, -5, -5, -4, -4, -4, -3,
         -3, -3, -2, -2, -2, -1, -1, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 11, 11, 11, 12, 12, 12, 13,
        13, 13, 14, 14, 14, 15, 15, 15, 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 20, 20, 20, 21, 21, 22, 22, 22, 23, 23, 23, 24, 24, 24, 25, 25, 25, 26, 26,
         26, 27, 27, 27, 28, 28, 28, 29, 29, 29, 30, 30, 30, 31, 31, 31, 32, 32, 33, 33, 33, 34, 34, 34, 35, 35, 35, 36, 36, 36, 37, 37, 37, 38, 38, 38, 39, 39, 39,
         40, 40, 40, 41, 41, 41, 42, 42, 42, 43, 43 );
     Table_fu2:array[0..255] of integer=( -227, -226, -224, -222, -220, -219, -217, -215, -213, -212, -210, -208, -206, -204, -203, -201, -199, -197, -196, -194,
        -192, -190, -188, -187, -185, -183, -181, -180, -178, -176, -174, -173, -171, -169, -167, -165, -164, -162, -160, -158, -157, -155, -153, -151, -149, -148,
        -146, -144, -142, -141, -139, -137, -135, -134, -132, -130, -128, -126, -125, -123, -121, -119, -118, -116, -114, -112, -110, -109, -107, -105, -103, -102,
         -100, -98, -96, -94, -93, -91, -89, -87, -86, -84, -82, -80, -79, -77, -75, -73, -71, -70, -68, -66, -64, -63, -61, -59, -57, -55, -54, -52, -50, -48, -47,
         -45, -43, -41, -40, -38, -36, -34, -32, -31, -29, -27, -25, -24, -22, -20, -18, -16, -15, -13, -11, -9, -8, -6, -4, -2, 0, 1, 3, 5, 7, 8, 10, 12, 14, 15,
        17, 19, 21, 23, 24, 26, 28, 30, 31, 33, 35, 37, 39, 40, 42, 44, 46, 47, 49, 51, 53, 54, 56, 58, 60, 62, 63, 65, 67, 69, 70, 72, 74, 76, 78, 79, 81, 83, 85,
        86, 88, 90, 92, 93, 95, 97, 99, 101, 102, 104, 106, 108, 109, 111, 113, 115, 117, 118, 120, 122, 124, 125, 127, 129, 131, 133, 134, 136, 138, 140, 141, 143,
         145, 147, 148, 150, 152, 154, 156, 157, 159, 161, 163, 164, 166, 168, 170, 172, 173, 175, 177, 179, 180, 182, 184, 186, 187, 189, 191, 193, 195, 196, 198,
        200, 202, 203, 205, 207, 209, 211, 212, 214, 216, 218, 219, 221, 223, 225 );
    var length,
        v,//v值的起始位置
        u,  //u值的起始位置
        rdif, invgdif, bdif,
        m, n, i, j, hfWidth,
        py,
        pos, pos1//dopod 595 图像调整
        ,x,y,t
        :integer;
        rgb:array[0..2] of integer;
        addHalf:boolean;
        temp:byte;
    begin
        length := nWidth * nHeight;
        v := length;
        u := (length * 5) shr 2;
        hfWidth := nWidth shr 1;
       // if (yv12=nil) then result:=false;
        m := -nWidth;
        n := -hfWidth;
        addHalf := true;
        for y:=0 to nHeight-1 do
        begin
            m :=m+nWidth;
            if addHalf then
            begin
                n:=n+hfWidth;
                addHalf := false;
            end else
            begin
                addHalf := true;
            end;
            for x:=0 to nWidth-1 do
            begin
                i := m + x;
                j := n + (x shr 1);
                py := integer(yv12[i]);
                rdif := Table_fv1[integer(yv12[v + j])];
                invgdif := Table_fu1[integer(yv12[u + j])] + Table_fv2[integer(yv12[v + j])];
                bdif := Table_fu2[integer(yv12[u + j])];
                rgb[2] := py + rdif;
                rgb[1] := py - invgdif;
                rgb[0] := py + bdif;
                j := m + x;
                i := (j shl 1) + j;            for j:=0 to 2 do
                begin
                  rgb24[i+j]:=byte(ifthen((rgb[j] < 0),0,ifthen((rgb[j] > 255),255,rgb[j])));
                end;
               { if (x mod 3 = 1) then
                begin
                  pos := (m + x - 1) * 3;
                  pos1 := (m + x) * 3;              temp := rgb24[pos];
                  rgb24[pos] := rgb24[pos1];
                  rgb24[pos1] := temp;              temp := rgb24[pos + 1];
                  rgb24[pos + 1] := rgb24[pos1 + 1];
                  rgb24[pos1 + 1] := temp;              temp := rgb24[pos + 2];
                  rgb24[pos + 2] := rgb24[pos1 + 2];
                  rgb24[pos1 + 2] := temp;
                end;   }
            end;
        end;
        result:=true;
    end;调用:
    procedure TDecCBFun(nPort:LongInt;pBuf:pchar;nSize:dword;pFrameInfo:PHik_FRAME_INFO;nReserved1,nReserved2:LongInt); stdcall;
    var nType:integer;
        Bmp: TBitmap;
        pBMIInfo: PBITMAPINFO;
        pRGBBuf:pchar;
    begin
        pBMIInfo := AllocMem(sizeof(TBITMAPINFO)+ (255 * sizeof(TRGBQuad)));
        //设定BMP的头信息
        pBMIInfo^.bmiHeader.biSize := sizeof(BITMAPINFOHEADER);
        pBMIInfo^.bmiHeader.biWidth := pFrameInfo.nWidth;
        pBMIInfo^.bmiHeader.biHeight := pFrameInfo.nHeight;
        pBMIInfo^.bmiHeader.biPlanes := 1;
        pBMIInfo^.bmiHeader.biBitCount := 24;
        pBMIInfo^.bmiHeader.biCompression := BI_RGB;
        pBMIInfo^.bmiHeader.biSizeImage := 0;
        pBMIInfo^.bmiHeader.biXPelsPerMeter := 0;
        pBMIInfo^.bmiHeader.biYPelsPerMeter := 0;
        pBMIInfo^.bmiHeader.biClrUsed := 0;
        pBMIInfo^.bmiHeader.biClrImportant := 0;    Bmp := TBitmap.Create;
        Bmp.Width:= pFrameInfo.nWidth;
        Bmp.Height := pFrameInfo.nHeight;
        Bmp.PixelFormat := pf24bit;
            pRGBBuf :=AllocMem(999999);
        SetStretchBltMode(Bmp.Canvas.Handle, COLORONCOLOR);
        try
          Application.ProcessMessages;
          YV12ToRGB(PTMyByteArray(pBuf)^,pFrameInfo.nWidth,pFrameInfo.nHeight,PTMyByteArray(pRGBBuf)^);
          SetDIBitsToDevice(Bmp.Canvas.Handle, 0, 0, Bmp.Width, Bmp.Height,
                    0, 0, 0, pBMIInfo^.bmiHeader.biHeight,
                    pRGBBuf,
                    pBMIInfo^, DIB_RGB_COLORS);
          ImageRotate90(Bmp);
          ImageRotate90(Bmp);
          frmHikHightVideo.Image1.Picture.Bitmap.Assign(Bmp);
          frmHikHightVideo.Image1.Refresh;
        finally
          FreeMem(pRGBBuf);
          FreeMem(pBMIInfo);
          Bmp.Free;
        end;       frmHikHightVideo.Memo1.Lines.Add(formatdatetime('hh:mm:ss:zzz',now())+'帧数:'+IntToStr(pFrameInfo.dwFrameNum)+',帧率:'+intToStr(pFrameInfo.nFrameRate)+'时标:'+intToStr(pFrameInfo.nStamp));
    end;
    有个问题,图像是反的,怎么改正?(海康实时预)
      

  3.   

    自已解决,见http://blog.csdn.net/ccnccyj/article/details/9131413