我写的图像放大线性插入在执行时用的时间太长了,比人家的卷积法时间都长。
那位高手能帮我看看代码吗?
function Scale2(cnv:TCanvas;scale:double):TBitmap;
var
  w,h: Integer;
  i,j,m,n: Integer;
  x,y:integer;
  neww,newh:integer;
  xt,yt:double;
  color: LongInt;
  xx,yy: array[0..1] of integer;
  r,g,b: array[0..1,0..1] of byte;
  red,green,blue :byte;
  u,v:double;
  bmp:TBitmap;
begin
  bmp:=TBitmap.Create ;  w := cnv.ClipRect.Right -cnv.ClipRect.Left;
  h := cnv.Cliprect.Bottom-cnv.Cliprect.Top ;  neww:=Trunc(w*scale+0.5);
  newh:=Trunc(h*scale+0.5);  bmp.Width :=neww-1;
  bmp.Height :=newh-1;  for i:=0 to newh-1 do
  begin
    yt:=i/scale;
    y:=Trunc(yt);
    v:=yt-y;    for j:=0 to neww-1 do
    begin
      xt:=j/scale;
      x:=Trunc(xt);
      u:=xt-x;      xx[0]:=x;
      xx[1]:=x+1;
      if x=neww-1 then     xx[1]:=x;      yy[0]:=y;
      yy[1]:=y+1;
      if y=newh-1 then     yy[1]:=y;      for m:=0 to 1 do
      begin
        for n:=0 to 1 do
        begin
          color := ColorToRGB(cnv.Pixels[xx[n], yy[m]]);
          b[n,m] := (Color and $FF0000) shr 16;
          g[n,m] := (Color and $FF00) shr 8;
          r[n,m] := (Color and $FF);
        end;
      end ;
      red:=Trunc((1-u)*(1-v)*r[0][0]+(1-u)*v*r[0][1]+u*(1-v)*r[1][0]+u*v*r[1][1]);
      green:=Trunc((1-u)*(1-v)*g[0][0]+(1-u)*v*g[0][1]+u*(1-v)*g[1][0]+u*v*g[1][1]);
      blue:=Trunc((1-u)*(1-v)*b[0][0]+(1-u)*v*b[0][1]+u*(1-v)*b[1][0]+u*v*b[1][1]);
      bmp.Canvas.Pixels[j,i]:=RGB(red, green, blue);
    end;
  end ;
  result:=bmp;
end;

解决方案 »

  1.   

    首先
              color := ColorToRGB(cnv.Pixels[xx[n], yy[m]]);
              b[n,m] := (Color and $FF0000) shr 16;
              g[n,m] := (Color and $FF00) shr 8;
              r[n,m] := (Color and $FF);
    这么写的读取RGB代码本身效率就低,更别说后面的运算了。
    想节省时间必须用Scanline方法,详细请看图形版的斑竹置顶的帖子。
      

  2.   

    其实Windows本身已经提供了支持:procedure ScaleCanvas(SrcCanvas,DestCanvas: TCanvas; SrcRect,DestRect: TRect);
    begin
      SetStretchBltMode(DestCanvas.Handle,HALFTONE); // !!!
      DestCanvas.CopyRect(DestRect,SrcCanvas,SrcRect);
    end;