Board index » delphi » Help on Pixels.....

Help on Pixels.....

Hi All,
I'm working with Bitmaps and want to manipulate pixels, first I'd like
to output the value that ColorToRGB returns.. to
a MEMO component
I have found the BITMAP.SCANLINE[x] to do most of the work already and
it returns  PByteArray,  now I have tried
something like

var
P : PByteArray;
x : LongInt;
y,w : int;
PixStr : String;
begin

for w := 0 to Image1.heigth - 1 do
begin
P := Image1.Picture.Bitmap.Scanline[0]; // scan row
     for y := 0 to Image1.width -1 do
          begin
              X:= ColorToRGB(P[y]);
             PixStr  := PixStr  +',' + IntToStr(X);// output a pixel to
the memo

     end;
     Memo1.Lines.Insert(w,PixStr);
end;
end;

which seems to map just for part of the bitmap .(.only maps 1/4 of the
bitmap) eventhough I get  the  total
pixels mapped in the memo...  I think I'm loosing bytes in the
ColorToRGB..
Anyone knows of a better way to acces the pixels and bytes for each
pixel...??..

also how could i write the value of a PByteArray directly to the Memo
component?
something like $02FFFFFF; ???...
( sorry I'm new to graphics)

Thanks in Advance...

 

Re:Help on Pixels.....


Quote
youNameHere wrote in message <361A91B8.E30E1...@sercer.get>...
>I'm working with Bitmaps and want to manipulate pixels, first I'd like
>to output the value that ColorToRGB returns.. to
>a MEMO component
>I have found the BITMAP.SCANLINE[x] to do most of the work already and
>it returns  PByteArray
>Anyone knows of a better way to acces the pixels and bytes for each
>pixel...??..

You've got to take a look at the PixelFormat before you know what the
format of each Scanline is.

Take a look at the PixelFormat and Scanline information under "TBitmap"
on the Delphi Graphics VCL & RTL page:
www.infomaster.net/external/efg/Library/Delphi/Graphics/VCLRTL.htm

There is at least one Scanline example for each PixelFormat there.

efg
_________________________________________
efg's Computer Lab:  http://infomaster.net/external/efg

Earl F. Glynn                 E-Mail:  EarlGl...@att.net
Overland Park, KS  USA

Re:Help on Pixels.....


  I have better luck using rgbTriple, and making sure my bitmap is 24bit:

type

  TRGBArray    = ARRAY[0..32767] OF TRGBTriple;
  pRGBArray    = ^TRGBArray;

var
  sline : pRGBArray;
  r, g, b : word;
  w, h : integer;
begin
  myBMP.PixelFormat := pf24bit;
  for h := myBMP.Height-1 do begin
    sline := myBMP.Scanline[h];
    for w := 0 to myBMP.Width-1 do begin
      r := sline[w].rgbtRed;
      g := sline[w].rgbtGreen;
      b := sline[w].rgbtBlue;
      Memo1.Lines.Add('$' + (format('%x', [sline[w]]));
    end;
  end;

end;

  I'm not sure I understand the second part of the question, but it sounded
like you wanted the hex representation of the value.  However, with a
bitmap of any size, you will soon blow the memo stack limit.  The r, g, b
values in the code above are not referenced, I just threw them in to show
how to extract the individual values, all in the 0-255 range.

  HTH :)

Quote
youNameHere wrote:
> Hi All,
> I'm working with Bitmaps and want to manipulate pixels, first I'd like
> to output the value that ColorToRGB returns.. to
> a MEMO component
> I have found the BITMAP.SCANLINE[x] to do most of the work already and
> it returns  PByteArray,  now I have tried
> something like

> var
> P : PByteArray;
> x : LongInt;
> y,w : int;
> PixStr : String;
> begin

> for w := 0 to Image1.heigth - 1 do
> begin
> P := Image1.Picture.Bitmap.Scanline[0]; // scan row
>      for y := 0 to Image1.width -1 do
>           begin
>               X:= ColorToRGB(P[y]);
>              PixStr  := PixStr  +',' + IntToStr(X);// output a pixel to
> the memo

>      end;
>      Memo1.Lines.Insert(w,PixStr);
> end;
> end;

> which seems to map just for part of the bitmap .(.only maps 1/4 of the
> bitmap) eventhough I get  the  total
> pixels mapped in the memo...  I think I'm loosing bytes in the
> ColorToRGB..
> Anyone knows of a better way to acces the pixels and bytes for each
> pixel...??..

> also how could i write the value of a PByteArray directly to the Memo
> component?
> something like $02FFFFFF; ???...
> ( sorry I'm new to graphics)

> Thanks in Advance...

--
Harm  (remove thebaragain for email)

"Two major products came out of Berkely in the 70's;
Unix and LSD.  It's not a coincidence."

Re:Help on Pixels.....


Try this it works for 24 bit bitmaps.

var
 i : Integer;
 j : Integer;
 TempA : Integer;
 Tempj : Integer;
 SLine : ^TByteArray;
begin
 TempA := 0;
 Tempj := 0;
for i := 0 to (ABitmap.Height-1) do // Loop through Height (i) of the bitmap
  begin
   SLine := ABitmap.ScanLine[i]; // Grab Scan Line i of ABitmap and store to
SLine
   for j := 1 to ABitmap.Width do  // Loop on the width of the bitmap
    begin
     Tempj := (j -1)*3;
     TempA :=
ThreeByteToInt(Byte(SLine^[Tempj]),Byte(SLine^[Tempj+1]),Byte(SLine^[Tempj+2
]));
     //add your code to handle integer(color value) of TempA
    end;
     Tempj := 0; // Reset Tempj
     TempA := 0; // Reset byte value sum to 0
     end;
  end;
end;

function ThreeByteToInt(A,B,C: Byte): Integer;
begin
 Result := (65536*A)+(256*B)+C;
end;

Jim P.

Quote
youNameHere wrote in message <361A91B8.E30E1...@sercer.get>...
>Hi All,
>I'm working with Bitmaps and want to manipulate pixels, first I'd like
>to output the value that ColorToRGB returns.. to
>a MEMO component
>I have found the BITMAP.SCANLINE[x] to do most of the work already and
>it returns  PByteArray,  now I have tried
>something like

>var
>P : PByteArray;
>x : LongInt;
>y,w : int;
>PixStr : String;
>begin

>for w := 0 to Image1.heigth - 1 do
>begin
>P := Image1.Picture.Bitmap.Scanline[0]; // scan row
>     for y := 0 to Image1.width -1 do
>          begin
>              X:= ColorToRGB(P[y]);
>             PixStr  := PixStr  +',' + IntToStr(X);// output a pixel to
>the memo

>     end;
>     Memo1.Lines.Insert(w,PixStr);
>end;
>end;

>which seems to map just for part of the bitmap .(.only maps 1/4 of the
>bitmap) eventhough I get  the  total
>pixels mapped in the memo...  I think I'm loosing bytes in the
>ColorToRGB..
>Anyone knows of a better way to acces the pixels and bytes for each
>pixel...??..

>also how could i write the value of a PByteArray directly to the Memo
>component?
>something like $02FFFFFF; ???...
>( sorry I'm new to graphics)

>Thanks in Advance...

Re:Help on Pixels.....


Thanks to you Both.. I tried both scripts and they worked great.

except for   Memo1.Lines.Add('$' + (format('%x', [sline[w]]));
somehow it didnt compile.. I think it's the   [sline[w]]

Are there any books that anyone could recommend for graphics processing with
Delphi?

Thanks Again.

Re:Help on Pixels.....


In article <361A91B8.E30E1...@sercer.get>,
  youNameHere <YourN...@sercer.get> wrote:

Quote
> Hi All,
> I'm working with Bitmaps and want to manipulate pixels, first I'd like
> to output the value that ColorToRGB returns.. to
> a MEMO component
> I have found the BITMAP.SCANLINE[x] to do most of the work already and
> it returns  PByteArray,  now I have tried
> something like

> var
> P : PByteArray;
> x : LongInt;
> y,w : int;
> PixStr : String;
> begin

> for w := 0 to Image1.heigth - 1 do
> begin
> P := Image1.Picture.Bitmap.Scanline[0]; // scan row
>      for y := 0 to Image1.width -1 do
>           begin
>               X:= ColorToRGB(P[y]);
>              PixStr  := PixStr  +',' + IntToStr(X);// output a pixel to
> the memo

>      end;
>      Memo1.Lines.Insert(w,PixStr);
> end;
> end;

> which seems to map just for part of the bitmap .(.only maps 1/4 of the
> bitmap) eventhough I get  the  total
> pixels mapped in the memo...  I think I'm loosing bytes in the
> ColorToRGB..
> Anyone knows of a better way to acces the pixels and bytes for each
> pixel...??..

> also how could i write the value of a PByteArray directly to the Memo
> component?
> something like $02FFFFFF; ???...
> ( sorry I'm new to graphics)

> Thanks in Advance...

First of all,  mapping the bitmap pixels in memo , it depends on your choise.
if your bitmap is a 256color bitmap, it is difficult to get the bitmap pixel
values. Because the pixels in 256 color bitmap are represented as only one
byte. Because of the ColorToRGB function returns a 4 byte lenght hexadecimal
value, the first byte of this value represent the related logical palette
($02,$01,$00), so you should call GetRValue,GetBValue and GetGValue function
to access the color intensity levels of entire pixels. it your bitmap had a
gray level pixels you probably had all the pixel values in memo. I recomended
that if you use TRGBTriple array type and a pointer pointing the RGBTriple
array instead of PByte array, and use 15,16 or 24Bit pixel formated bitmap,
you can do whatever you want. I met the same problem and I solved it using
24Bit bitmaps.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    

Other Threads