TBitmap.ScanLine[] 执行时间过长

发布于 2024-09-14 15:16:13 字数 527 浏览 3 评论 0原文

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

,请告诉我哪里错了?

I am working with Delphi. I am using bmp.ScanLine[] in my code. My code is as follows :

   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;

Here, imgMain is of TBitmap type. My problem is when I execute this code, it takes too much time on the lines

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

Please, tell me where I am wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

横笛休吹塞上声 2024-09-21 15:16:13

嗯,可以获得一些东西(介绍 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 也可能很昂贵,特别是如果它们很大,请使用池来避免重复分配。

Hmm, something can be gained (introducing rowpitch, see below) but that is not too much. Probably changing the for loop to a while loop that does pointer increment and compares with a pointer value of the last pixel

   // 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;

See also rotating bitmaps. In code for a routine that does things like this to rotate an image fast.

Allocating bmps all the time can be expensive too, specially if they are large, use pools to avoid repeated allocation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文