⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 aicinvcolorconversion.pas

📁 很好的源代码
💻 PAS
字号:
unit AICInvColorConversion;
{ Advanced Image Coding (AIC)
  ===========================
  YCbCr to RGB color conversion.
  Parts of the code in this unit are based on the IJG JPEG reference software.
  See JPEG.txt for license details. }

interface

uses
  AICCommon, Windows, Graphics;

procedure ConvertGreyscaleChannelToBitmap(const Channel: TAICChannel;
  const Bitmap: TBitmap; const Width, Height: Integer);
{ Converts the luminance channel in Channel to a greyscale bitmap }

procedure ConvertYCbCrChannelsToBitmap(const Channels: TAICChannels;
  const Bitmap: TBitmap; const Width, Height: Integer);
{ Converts the Y, Cb and Cr channels in Channels to a true color RGB bitmap }
implementation

const
  ScaleBits = 16;
  OneHalf   = 1 shl (ScaleBits - 1);

var
  CrRTab, CbBTab, CrGTab, CbGTab: array [0..255] of Integer;

function Fix(const Value: Double): Integer;
begin
  Result := Trunc((Value * (1 shl ScaleBits)) + 0.5);
end;

function RightShift(Value, Bits: Integer): Integer; assembler;
asm
  mov ecx,edx
  sar eax,cl
end;

procedure InitTables;
var
  I, J: Integer;
begin
  J := -128;
  for I := 0 to 255 do begin
    CrRTab[I] := RightShift(Fix(1.40200) * J + OneHalf,ScaleBits);
    CbBTab[I] := RightShift(Fix(1.77200) * J + OneHalf,ScaleBits);
    CrGTab[I] := (-Fix(0.71414)) * J;
    CbGTab[I] := (-Fix(0.34414)) * J + OneHalf;
    Inc(J);
  end;
end;

function CreateGreyscalePalette: HPalette;
var
  Pal: TMaxLogPalette;
  I: Integer;
begin
  Pal.palVersion := $300;
  Pal.palNumEntries := 256;
  for I := 0 to 255 do
    with Pal.palPalEntry[I] do begin
      peRed   := I;
      peGreen := I;
      peBlue  := I;
      peFlags := 0;
    end;
  Result := CreatePalette(PLogPalette(@Pal)^);
end;

procedure ConvertGreyscaleChannelToBitmap(const Channel: TAICChannel;
  const Bitmap: TBitmap; const Width, Height: Integer);
var
  Y: Integer;
begin
  Bitmap.PixelFormat := pf8Bit;
  Bitmap.Width := Width;
  Bitmap.Height := Height;
  Bitmap.Palette := CreateGreyscalePalette;
  for Y := 0 to Height - 1 do
    Move(Channel[Y,0],Bitmap.Scanline[Y]^,Width);
end;

procedure ConvertYCbCrChannelsToBitmap(const Channels: TAICChannels;
  const Bitmap: TBitmap; const Width, Height: Integer);
{ R = Y             + 1.40200Cr
  G = Y - 0.34414Cb - 0.71414Cr
  B = Y + 1.77200Cb }
var
  Row, Col, Y, Cb, Cr: Integer;
  BGR: PAICBGR;
begin
  Bitmap.PixelFormat := pf24Bit;
  Bitmap.Width := Width;
  Bitmap.Height := Height;

  for Row := 0 to Height - 1 do begin
    BGR := Bitmap.ScanLine[Row];
    for Col := 0 to Width - 1 do begin
      Y  := Channels[ctY,Row,Col];
      Cb := Channels[ctCb,Row,Col];
      Cr := Channels[ctCr,Row,Col];

      BGR.R := AICByteClip[Y + CrRTab[Cr]];
      BGR.G := AICByteClip[Y + RightShift(CbGTab[Cb] + CrGTab[Cr],ScaleBits)];
      BGR.B := AICByteClip[Y + CbBTab[Cb]];

      Inc(BGR);
    end;
  end;
end;

initialization
  InitTables;
  
end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -