1

我正在与德尔福合作。我bmp.ScanLine[]在我的代码中使用。我的代码如下:

   bmp := TBitmap.Create;
   bmp.Height := imgMain.Height;
   bmp.Width := imgMain.Width;
   for i := 0 to imgMain.Height - 1 do begin
      prgb := bmp.ScanLine[i];
      p1 := imgMain.ScanLine[i];
      for j := 0 to imgMain.Width - 1 do begin
         //Some code
      end;
   end;

这里,imgMain 是 TBitmap 类型。我的问题是当我执行这段代码时,在行上花费了太多时间

prgb := bmp.ScanLine[i];
p1 := imgMain.ScanLine[i];

请告诉我我错在哪里?

4

1 回答 1

2

嗯,可以获得一些东西(介绍 rowpitch,见下文),但这并不算多。可能将 for 循环更改为 while 循环,该循环执行指针递增并与最后一个像素的指针值进行比较

   // from memory, might need an additional typecast here or there.

   // will typically be negative
   scanline0:=imga.scanline[0];
   rowpitchimga:=integer(imga.scanline[1])-integer(scanline0);  // bytes to jump row.

   prgb1 :=scanline0;
   for i:=0 to imgmain.height-1;
     begin
       prgbend:=prgb1;
       inc(prgbend,width);  // if prgbend, it will be with sizeof(prgb1^)
       while(prgb1<prbend) do // smaller then, since prgb1[] is 0 based.
         begin
           // do your thing
           inc(prgb1);
         end;      
       prgb1:=prgbend;
       inc(pointer(prgb1),rowpitch-width*sizeof(prgb1^));  // skip alignmentbytes
       inc(pointer(prgbend),rowpitch);
     end;

另请参阅旋转位图。在执行此类操作以快速旋转图像的例程的代码中。

一直分配 bmp 也可能很昂贵,特别是如果它们很大,请使用池以避免重复分配。

于 2010-08-17T08:30:25.597 回答