DarkGant
asked on
How to use Scanline property of bitmap to read an entire line of pixels into a 2d array?
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.P ixels[n-1, m-1]);
p2 := ValueofPix(image1.Canvas.P ixels[n,m- 1]);
p3 := ValueofPix(image1.Canvas.P ixels[n+1, m-1]);
p4 := ValueofPix(image1.Canvas.P ixels[n-1, m]);
p5 := ValueofPix(image1.Canvas.P ixels[n+1, m]);
p6 := ValueofPix(image1.Canvas.P ixels[n-1, m+1]);
p7 := ValueofPix(image1.Canvas.P ixels[n,m+ 1]);
p8 := ValueofPix(image1.Canvas.P ixels[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
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.P
p2 := ValueofPix(image1.Canvas.P
p3 := ValueofPix(image1.Canvas.P
p4 := ValueofPix(image1.Canvas.P
p5 := ValueofPix(image1.Canvas.P
p6 := ValueofPix(image1.Canvas.P
p7 := ValueofPix(image1.Canvas.P
p8 := ValueofPix(image1.Canvas.P
Facefinder[n,m] := ((p1+p2+p3+p4+p5+p6+p7+p8)
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
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(bm p, n-1, m-1));
p2 := ValueofPix(Pixel24Color(bm p, n, m-1));
p3 := ValueofPix(Pixel24Color(bm p, n+1, m-1));
p4 := ValueofPix(Pixel24Color(bm p, n-1, m));
p5 := ValueofPix(Pixel24Color(bm p, n+1, m));
p6 := ValueofPix(Pixel24Color(bm p, n-1, m+1));
p7 := ValueofPix(Pixel24Color(bm p, n, m+1));
p8 := ValueofPix(Pixel24Color(bm p, n+1,m+1));
Facefinder[n,m] := ((p1+p2+p3+p4+p5+p6+p7+p8) /8);
end;
end;
end;
mo.
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
Result := TColor(RGB(R, G, B));
end;
function Pixel24Color(bmp: TBitmap; const X, Y: integer): TColor;
begin
with PRGB24Array(bmp.ScanLine[Y
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(bm
p2 := ValueofPix(Pixel24Color(bm
p3 := ValueofPix(Pixel24Color(bm
p4 := ValueofPix(Pixel24Color(bm
p5 := ValueofPix(Pixel24Color(bm
p6 := ValueofPix(Pixel24Color(bm
p7 := ValueofPix(Pixel24Color(bm
p8 := ValueofPix(Pixel24Color(bm
Facefinder[n,m] := ((p1+p2+p3+p4+p5+p6+p7+p8)
end;
end;
end;
mo.
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.ScanLin e[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.Pale tte, 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;
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.ScanLin
Result := TColor(RGB(peRed, peGreen, peBlue));
end;
var
bmp: TBitmap;
pe: TPal256EntryArray;
begin
bmp := Image1.Picture.Bitmap;
bmp.PixelFormat := pf8bit;
Res := GetPaletteEntries(bmp.Pale
if res > 0 then begin
for N := 2 to 511 do begin
for M := 2 to 383 do begin
p1 := ValueofPix(Pixel8Color(bmp
p2 := ValueofPix(Pixel8Color(bmp
p3 := ValueofPix(Pixel8Color(bmp
p4 := ValueofPix(Pixel8Color(bmp
p5 := ValueofPix(Pixel8Color(bmp
p6 := ValueofPix(Pixel8Color(bmp
p7 := ValueofPix(Pixel8Color(bmp
p8 := ValueofPix(Pixel8Color(bmp
Facefinder[n,m] := ((p1+p2+p3+p4+p5+p6+p7+p8)
end;
end;
end;
end;
ASKER
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?
Scan Line Index out of Range
Can u please help?
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.
for N := 1 to bmp.height -2 do begin
for M := 1 to bmp.width -2 do begin
mo.
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.
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.
ASKER
isnt there a quicker way to just place all RGB values into a 2 d array so that i can access them easier?.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thx alot for all your help i believe i understand how scanline works a bit better and your code worked wonderfully. Thx again
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. :(
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. :(
ASKER
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
So thx alot if id have had more points id have given u more
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