• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 295
  • Last Modified:

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
0
andru
Asked:
andru
1 Solution
 
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now