?
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
?
755 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month9 days, 15 hours left to enroll

762 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