Nearest Neighbor、Bilinear、Bicubic算法的Delphi代码[2]

[入库:2006年2月23日] [更新:2007年3月24日]

本文简介:

procedure ResizeBilinear(Src: TBitmap; var Dest: TBitmap;
  DestWidth, DestHeight, SrcWidth, SrcHeight: integer);
var
  hScale, wScale: double;
  f1, f2, f3, f4, f12, f34, fNew: TRGBTriple;
  temp1, temp2, tempDst: PRGBTripleArray;
  x, y: double;
  x1, x2, y1, y2, i, j: integer;
begin
  Dest := TBitmap.Create;
  Dest.PixelFormat := pf24Bit;
  Dest.Width := DestWidth;
  Dest.Height := DestHeight;

  Src.PixelFormat := pf24Bit;
  Src.Width := SrcWidth;
  Src.Height := SrcHeight;

  hScale := DestHeight / SrcHeight;
  wScale := DestWidth / SrcWidth;

  for i := 0 to DestHeight - 1 do
  begin
    x := i / hScale;
    x1 := trunc(x);
    x2 := x1 + 1;
    if x2 > SrcHeight - 1 then x2 := SrcHeight - 1;

    temp1 := Src.ScanLine[x1];
    temp2 := Src.ScanLine[x2];
    tempDst := Dest.ScanLine[i];

    for j := 0 to DestWidth - 1 do
    begin
      y := j / wScale;
      y1 := trunc(y);
      y2 := y1 + 1;
      if y2 > SrcWidth - 1 then y2 := SrcWidth - 1;

      f1 := temp1^[y1];
      f2 := temp1^[y2];
      f3 := temp2^[y1];
      f4 := temp2^[y2];

      f12.rgbtRed := trunc(f1.rgbtRed + (y - y1) * (f2.rgbtRed - f1.rgbtRed));
      f12.rgbtGreen := trunc(f1.rgbtGreen + (y - y1) * (f2.rgbtGreen - f1.rgbtGreen));
      f12.rgbtBlue := trunc(f1.rgbtBlue + (y - y1) * (f2.rgbtBlue - f1.rgbtBlue));

      f34.rgbtRed := trunc(f3.rgbtRed + (y - y1) * (f4.rgbtRed - f3.rgbtRed));
      f34.rgbtGreen := trunc(f3.rgbtGreen + (y - y1) * (f4.rgbtGreen - f3.rgbtGreen));
      f34.rgbtBlue := trunc(f3.rgbtBlue + (y - y1) * (f4.rgbtBlue - f3.rgbtBlue));

      fNew.rgbtRed := trunc(f12.rgbtRed + (x - x1) * (f34.rgbtRed - f12.rgbtRed));
      fNew.rgbtGreen := trunc(f12.rgbtGreen + (x - x1) * (f34.rgbtGreen - f12.rgbtGreen));
      fNew.rgbtBlue := trunc(f12.rgbtBlue + (x - x1) * (f34.rgbtBlue - f12.rgbtBlue));

      tempDst^[j] := fNew;
    end;
  end;
end;

procedure ResizeNearestNeighbor(Src: TBitmap; var Dest: TBitmap;
  DestWidth, DestHeight, SrcWidth, SrcHeight: integer);
var
  hScale, wScale: double;
  fNew: TRGBTriple;
  tempSrc, tempDst: PRGBTripleArray;
  x, y, i, j: integer;
begin
  Dest := TBitmap.Create;
  Dest.PixelFormat := pf24Bit;
  Dest.Width := DestWidth;
  Dest.Height := DestHeight;

  Src.PixelFormat := pf24Bit;
  Src.Width := SrcWidth;
  Src.Height := SrcHeight;

  hScale := DestHeight / SrcHeight;
  wScale := DestWidth / SrcWidth;

  for i := 0 to DestHeight - 1 do
  begin
    x := round(i / hScale);
    if x > SrcHeight - 1 then x := SrcHeight - 1;

    tempSrc := Src.ScanLine[x];
    tempDst := Dest.ScanLine[i];

    for j := 0 to DestWidth - 1 do
    begin
      y := round(j / wScale);
      if y > SrcWidth - 1 then y := SrcWidth - 1;

      fNew := tempSrc^[y];
      tempDst^[j] := fNew;
    end;
  end;
end;

本文关键:Nearest Neighbor、Bilinear、Bicubic算法的Delphi代码
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top