TBitmap.ScanLine[] takes too much time for execution

933 views Asked by At

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?

1

There are 1 answers

4
Marco van de Voort On

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.