【问题标题】:TBitmap.ScanLine[] takes too much time for executionTBitmap.ScanLine[] 执行时间过长
【发布时间】:2010-08-17 06:36:22
【问题描述】:

我正在与德尔福合作。我在我的代码中使用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];

请告诉我哪里错了?

【问题讨论】:

  • 这些行需要多长时间?你期待什么?

标签: delphi tbitmap


【解决方案1】:

嗯,可以获得一些东西(介绍 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;

另请参阅rotating bitmaps. In code,了解执行此类操作以快速旋转图像的例程。

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

【讨论】:

  • 就是这样。你是对的。如果您希望您的代码可以使用 64 位编译器,请不要忘记使用 NativeInt 而不是 Integer!这里没有巨魔:rowpitchimga:=NativeInt(imga.scanline[1])-NativeInt(scanline0);在 Delphi 2007 之前,您必须定义类型 NativeInt=integer;
  • 它实际上被定义为 ptruint,并且已经在 64 位上运行。它最初是使用 Free Pascal 开发的。为什么 Embarcadero 选择了不同的标识符,我不知道
  • 嘿@Marco,抱歉回复晚了。是的,这可以是一个解决方案。但我观察到的另一件事是,如果我只写p1 := imgMain.Scanline[i];,那么它不会花费太多时间。所以,这只是在prgb := bmp.Scanline[i]; 上花费时间。这里,imgMain 是使用TBitmap.Assign(); 从 TImage 位图复制的位图。
  • Delphi 位图是基于窗口的(由窗口分配的句柄)而不是(与设备无关的 iirc)。延迟的可能是两种类型之间的转换。
猜你喜欢
  • 2018-01-19
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
  • 2021-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多