?
Solved

How to use Scanline property of bitmap to read an entire line of pixels into a 2d array?

Posted on 2003-03-10
11
Medium Priority
?
782 Views
Last Modified: 2012-08-14
I am trying to read in the RGB Values and currently am using this piece of code using standard image1.canvas.pixels[22,33] = ...
The image i am using is in greyscale so basically i take one of the RGB values and convert it into an integer in the range 0-255.  Below all that Valueofpix function does is do this conversion into 256 grey value.

i.e:

for N := 2 to 511 do                      
  begin
        for M := 2 to 383 do
        begin
        p1 := ValueofPix(image1.Canvas.Pixels[n-1,m-1]);
        p2 := ValueofPix(image1.Canvas.Pixels[n,m-1]);
        p3 := ValueofPix(image1.Canvas.Pixels[n+1,m-1]);
        p4 := ValueofPix(image1.Canvas.Pixels[n-1,m]);
        p5 := ValueofPix(image1.Canvas.Pixels[n+1,m]);
        p6 := ValueofPix(image1.Canvas.Pixels[n-1,m+1]);
        p7 := ValueofPix(image1.Canvas.Pixels[n,m+1]);
        p8 := ValueofPix(image1.Canvas.Pixels[n+1,m+1]);
        Facefinder[n,m] := ((p1+p2+p3+p4+p5+p6+p7+p8)/8);
        end;
end;


I have been told that scan line is so much more quicker albeit less easier (as i have found) but i would like to use scanline for the speed issue regarding my program.  Any help would be helpful heh

Thx alot i also need an answer fairly soon i.e. by tuesday night.  Thx
0
Comment
Question by:DarkGant
  • 6
  • 4
11 Comments
 
LVL 1

Expert Comment

by:rfedyk
ID: 8107044
How have you drawn to the canvas?

If you have loaded a bitmap then the Scanline property is available with the Bitmap property of TImage.Picture and is the way to access the bitmap data of a Windows DIB associated with the Image(BMP files or files that are converted to a DIB on reading). You read the scanline like any other array.  Delphi Help on Scanline has a very good coding example.



Regards
Roger Fedyk
0
 
LVL 9

Expert Comment

by:mocarts
ID: 8109961
type
  TRGB32 = packed record
    B, G, R, A: Byte;
  end;
  TRGB32Array = packed array[0..MaxInt div SizeOf(TRGB32)-1] of TRGB32;
  PRGB32Array = ^TRGB32Array;

  TRGB24 = packed record
    B, G, R: Byte;
  end;
  TRGB24Array = packed array[0..MaxInt div SizeOf(TRGB24)-1] of TRGB24;
  PRGB24Array = ^TRGB24Array;

function Pixel32Color(bmp: TBitmap; const X, Y: integer): TColor;
begin
  with PRGB32Array(bmp.ScanLine[Y])[X] do
    Result := TColor(RGB(R, G, B));
end;

function Pixel24Color(bmp: TBitmap; const X, Y: integer): TColor;
begin
  with PRGB24Array(bmp.ScanLine[Y])[X] do
    Result := TColor(RGB(R, G, B));
end;

var
  bmp: TBitmap;
begin
  bmp := Image1.Picture.Bitmap;
  bmp.PixelFormat := pf24bit; // or pf32bit; or comment out if you have already
  for N := 2 to 511 do begin
    for M := 2 to 383 do begin
      p1 := ValueofPix(Pixel24Color(bmp, n-1, m-1));
      p2 := ValueofPix(Pixel24Color(bmp, n, m-1));
      p3 := ValueofPix(Pixel24Color(bmp, n+1, m-1));
      p4 := ValueofPix(Pixel24Color(bmp, n-1, m));
      p5 := ValueofPix(Pixel24Color(bmp, n+1, m));
      p6 := ValueofPix(Pixel24Color(bmp, n-1, m+1));
      p7 := ValueofPix(Pixel24Color(bmp, n, m+1));
      p8 := ValueofPix(Pixel24Color(bmp, n+1,m+1));
      Facefinder[n,m] := ((p1+p2+p3+p4+p5+p6+p7+p8)/8);
    end;
  end;
end;

mo.
0
 
LVL 9

Expert Comment

by:mocarts
ID: 8110323
to get 8bit (256) pixel color using scanlines:

uses Windows;

type
  TPal256EntryArray = array[0..255] of TPaletteEntry;
  PPalEntryArray = ^TPal256EntryArray;

function Pixel8Color(bmp: TBitmap; pal: PPalEntryArray; const X, Y: integer): TColor;
begin
  with pal[PByteArray(bmp.ScanLine[Y])[X]] do
     Result := TColor(RGB(peRed, peGreen, peBlue));
end;

var
 bmp: TBitmap;
 pe: TPal256EntryArray;
begin
 bmp := Image1.Picture.Bitmap;
 bmp.PixelFormat := pf8bit;
 Res := GetPaletteEntries(bmp.Palette, 0, 256, pe);
 if res > 0 then begin
   for N := 2 to 511 do begin
     for M := 2 to 383 do begin
       p1 := ValueofPix(Pixel8Color(bmp, @pe, n-1, m-1));
       p2 := ValueofPix(Pixel8Color(bmp, @pe, n, m-1));
       p3 := ValueofPix(Pixel8Color(bmp, @pe, n+1, m-1));
       p4 := ValueofPix(Pixel8Color(bmp, @pe, n-1, m));
       p5 := ValueofPix(Pixel8Color(bmp, @pe, n+1, m));
       p6 := ValueofPix(Pixel8Color(bmp, @pe, n-1, m+1));
       p7 := ValueofPix(Pixel8Color(bmp, @pe, n, m+1));
       p8 := ValueofPix(Pixel8Color(bmp, @pe, n+1,m+1));
       Facefinder[n,m] := ((p1+p2+p3+p4+p5+p6+p7+p8)/8);
     end;
   end;
 end;
end;
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:DarkGant
ID: 8110511
mocarts i tried the 8 bit version which works fine for the first loop i.e. it gets all the pixel data correctly when N= 2 and M= 2 to 383 for the first line but when it tries to go onto the 2nd line it says:

Scan Line Index out of Range

Can u please help?
0
 
LVL 9

Expert Comment

by:mocarts
ID: 8110535
scanlines have zero based index, try this:
  for N := 1 to bmp.height -2 do begin
    for M := 1 to bmp.width -2 do begin

mo.
0
 
LVL 9

Expert Comment

by:mocarts
ID: 8110563
oo..
you should exchange N and M in loop declarations

for M := 1 to bmp.height -2 do begin
  for N := 1 to bmp.width -2 do begin

mo.
0
 

Author Comment

by:DarkGant
ID: 8110718
isnt there a quicker way to just place all RGB values into a 2 d array so that i can access them easier?.
0
 
LVL 9

Accepted Solution

by:
mocarts earned 200 total points
ID: 8110871
type
 TPal256EntryArray = array[0..255] of TPaletteEntry;
 TColorMatrix = array of array of TColor;  // [Row, Col]
// TColorMatrix = array of array of byte; // if you want to convert in ColorMatrix8 to gray scale (to do it once)

function ColorMatrix8(bmp: TBitmap): TColorMatrix;
var
  pe: TPal256EntryArray;
  Line: PByteArray;
  x, y, res: integer;
begin
  if bmp.PixelFormat <> pf8bit then exit;
  Res := GetPaletteEntries(bmp.Palette, 0, 256, pe);
  if res > 0 then begin
    SetLength(Result, bmp.Height);
    for y := 0 to bmp.Height -1 do begin
      Line := bmp.ScanLine[y];
      SetLength(Result[y], bmp.Width);
      for x := 0 to bmp.Width -1 do
       with pe[line[x]] do
          Result[y, x] := TColor(RGB(peRed, peGreen, peBlue));
          // or
          // Result[y, x] := ValueOfPix(TColor(RGB(peRed, peGreen, peBlue));
    end;
  end;
end;

0
 

Author Comment

by:DarkGant
ID: 8113117
Thx alot for all your help i believe i understand how scanline works a bit better and your code worked wonderfully.  Thx again
0
 
LVL 9

Expert Comment

by:mocarts
ID: 8118200
some comments to my comments :)
I wrote stupid inefficient code... :(
Performance will increase only if we get ScanLine and work with it as long as we need it. and then get next.
my functions Pixel32(24,8)Color probably will work slowly than Canvas.Pixel[x,y].. as in every call of these functions they retrieve whole scanline instead of one pixel..
right way is to pass to Pixel32Color scanLine not bitmap.

function Pixel32Color(ScanLine: PRGB32Array; const X: integer): TColor;
begin
 with ScanLine[X] do
   Result := TColor(RGB(R, G, B));
end;

mo. :(
0
 

Author Comment

by:DarkGant
ID: 8123484
thx alot anyways u made my code run fast enough for me (it increased the speed by a factor of 2 :) )

So thx alot if id have had more points id have given u more
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
This is an update to some code that someone else posted on Experts Exchange. It is an alternate approach, I think a little easier to use, & makes sure that things like the Task Bar will update.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
There may be issues when you are trying to access Outlook or send & receive emails or due to Outlook crash which leads to corrupt or damaged PST file. To eliminate the corruption from your PST file, you need to repair the corrupt Outlook PST file. U…
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question