What is the proper way to use Scanline?

Hi

I'm using the Scanline function to count pixels in a bitmap rather that use the pixels property which takes donkeys years to finish. But I'm not sure how Scanline works. The documentation in Delphi is pretty unclear as to what format the Scanline array is. So is the following code right?

const
  max_width=640;

type
  p_colour_array=^colour_array;
  colour_array=array[0..max_width-1] of longint;

  p_byte_array=^byte_array;
  byte_array=array[0..2] of byte;

procedure count_pixels(
  bitmap:tbitmap;
  colour:tcolor);
var
  py,px:integer;
  b:p_byte_array;
  l:p_colour_array;
begin
  pixel_count:=0;
  with bitmap do
     for py:=0 to height-1 do
    begin
       l:=scanline[py];
      for px:=0 to width-1 do
        begin
          b:=@l[px];
          if tcolor(rgb(b^[2],b^[1],b^[0]))=colour then
            inc(pixel_count);
        end;
    end;
 end;

TIA
andruAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mokuleCommented:
Well
I'm using it as follows for example

procedure TForm1.Bmp2YUV(bmp:TBitmap);
var
  h,w: integer;
  i,j: integer;
  row: pByteArray;
  Y,U,V: Byte;
begin
  h := bmp.Height;
  w := bmp.Width;
  for i := 0 to h-1 do
    begin
    row := pByteArray(bmp.ScanLine[i]);
    for j := 0 to w-1 do
      begin
      BGR2YUV(row[3*j],row[3*j+1],row[3*j+2],Y,U,V);
//      some code using Y, U and V
      end;
    end;
end;
0
Russell LibbySoftware Engineer, Advisory Commented:
For a good overview of scanline handling, take a look at:

http://www.efg2.com/Lab/ImageProcessing/Scanline.htm#Scanline

Scan line is the way to go when you need fast read/write capability to the pixel data. BUT.... (here is the gotcha), you MUST pay attention to the pixel format of the bitmap in order to use the scan line (array of bytes) data.

For pf1Bit, there are actually 8 pixels (black/white) in every byte, so the valid scanline data width is actually = (bitmap.width div 8). For pf4Bit, its 2 pixels per byte, for pf8bit, its a one-to-one, etc.

Anyways, needless to say the width for the array of byte data (scanline) is very dependant on the pixel format, and ignoring this can cause numerous errors (including access violations). This means that you will need to:

a). handle each pixel format individually
or
b). convert the bitmap to a common pixel format (eg pf24Bit) and then access the scanline data.


Hope this helps,
Russell


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.