Board index » delphi » Quick access to a bitmap????????????

Quick access to a bitmap????????????

Hi there. I wish to access a bitmaps pixels a bit faster than the SLOW
pixels[x,y] function!

Is it perhaps possible to get a pointer to the bitmap area and the alter
the canvas in assembler?

Please e-mail me.

b...@ramboll.dk

 

Re:Quick access to a bitmap????????????


"Brian Fl?e S?rensen" <b...@ramboll.dk> wrote:

Quote
>Hi there. I wish to access a bitmaps pixels a bit faster than the SLOW
>pixels[x,y] function!
>Is it perhaps possible to get a pointer to the bitmap area and the alter
>the canvas in assembler?
>Please e-mail me.
>b...@ramboll.dk

Hi !

Use of GetPixel API with the canvas's handle is faster
then use of pixels[x,y] property, but it's not fast enought.
Here's an example, but you'd better use DIBs.
If you find the best way, thanks telling me !

Regards

procedure TForm1.parseImage(img : TImage) ;
   var
   x,y,dx,dy : word ;
   color : longint ;
   h : HDC ;
   begin
   dx := img.picture.bitmap.width ;
   dy := img.picture.bitmap.height ;
   if (dx > 0) and (dy > 0) then
      begin
      h := img.picture.bitmap.canvas.handle ;
      for y := 0 to dy - 1 do
         begin
         for x := 0 to dx - 1 do
            begin
            color := getPixel(h,x,y) ;
            { DO WHAT YOU WANT WITH COLOR... }
            end ;
         end ;
      end ;
   end ;

Re:Quick access to a bitmap????????????


Quote
JLP wrote:

> "Brian Fl?e S?rensen" <b...@ramboll.dk> wrote:

> >Hi there. I wish to access a bitmaps pixels a bit faster than the SLOW
> >pixels[x,y] function!

> >Is it perhaps possible to get a pointer to the bitmap area and the alter
> >the canvas in assembler?

> >Please e-mail me.

> >b...@ramboll.dk

> Hi !

> Use of GetPixel API with the canvas's handle is faster
> then use of pixels[x,y] property, but it's not fast enought.
> Here's an example, but you'd better use DIBs.
> If you find the best way, thanks telling me !

> Regards

> procedure TForm1.parseImage(img : TImage) ;
>    var
>    x,y,dx,dy : word ;
>    color : longint ;
>    h : HDC ;
>    begin
>    dx := img.picture.bitmap.width ;
>    dy := img.picture.bitmap.height ;
>    if (dx > 0) and (dy > 0) then
>       begin
>       h := img.picture.bitmap.canvas.handle ;
>       for y := 0 to dy - 1 do
>          begin
>          for x := 0 to dx - 1 do
>             begin
>             color := getPixel(h,x,y) ;
>             { DO WHAT YOU WANT WITH COLOR... }
>             end ;
>          end ;
>       end ;
>    end ;

        GetPixel is not going to be any faster than Pixels. Well, actually
it may be a little faster but it's still very slow.

        Back in D1/win3.1 the answer would be GetBitmapBits when it worked
or GetDiBits otherwise (there's also TBitmap.SaveToStream(ATMemoryStream),
more or less a wrapper around getDiBits). Haven't caught up with the
right way to do it in D2/WIN32.

--
David Ullrich

?his ?s ?avid ?llrich's ?ig ?ile
(Someone undeleted it for me...)

Re:Quick access to a bitmap????????????


The Pixels[x,y]  function is slow because it always check a couple of
things (canvas existence and validity) before calling the corrispondent API
function. You speed increase your application by calling directly the
SetPixel API passing the handle to the canvas or better the SetPixelV (only
delphi 2.0) which is even faster. So:
if bmp is a Tbitmap object, to set the pixel at coords 5,7 to black you do
the following:

SetPixel(bmp.canvas.handle,5,7,clBlack);

See the API reference for more info (remeber: under W95/NT use the faster
SetPixelV)

Emanuele

Brian Fl?e S?rensen <b...@ramboll.dk> wrote in article
<01bbaeb3$40a429a0$5b265...@bfs.ramboll.dk>...

Quote
> Hi there. I wish to access a bitmaps pixels a bit faster than the SLOW
> pixels[x,y] function!

> Is it perhaps possible to get a pointer to the bitmap area and the alter
> the canvas in assembler?

> Please e-mail me.

> b...@ramboll.dk

Re:Quick access to a bitmap????????????


On the Delphi Super page in the Freeware 32 bit section, I have a
program called DibDemo, which shows how to create a DIBsection and
access the pixels as fast as poss. Palette handling is also done.
For 2D data plots etc etc it is extremely fast.

John B

Re:Quick access to a bitmap????????????


Quote
Lord of Darkness wrote:

> On the Delphi Super page in the Freeware 32 bit section, I have a
> program called DibDemo, which shows how to create a DIBsection and
> access the pixels as fast as poss. Palette handling is also done.
> For 2D data plots etc etc it is extremely fast.

        You're claiming it's even faster than SetPixelV?

;-)
--
David Ullrich

?his ?s ?avid ?llrich's ?ig ?ile
(Someone undeleted it for me...)

Re:Quick access to a bitmap????????????


Quote
David Ullrich wrote:

> Lord of Darkness wrote:

> > On the Delphi Super page in the Freeware 32 bit section, I have a
> > program called DibDemo, which shows how to create a DIBsection and
> > access the pixels as fast as poss. Palette handling is also done.
> > For 2D data plots etc etc it is extremely fast.

>         You're claiming it's even faster than SetPixelV?

> ;-)
> --
> David Ullrich

Well of course. SetPixelV is a GDI routine and therefore has an overhead
of many clock cycles, using a DIBsection you can simply say

p : Pointer (to memory surface)

PByte(p)^:=255

you now have one white pixel, with 3 clock cycles. My DIBDemo has
bresenhams line, a scanline polygon filler and they are an order of
magnitude faster than GDI. They're not as easy to use, but for creating
data plots and then blitting to screen you can't beat em.

PByte( (longint(p) + x + (cols*y) )^ := 255 for an arbitrary x,y

For plotting pixels, you might want to use SetPixelV, because they will
appear directly on screen, but for drawing to a bitmap, that you can
manipulate at your own leisure DIBsections are very nice.

I'm now using DirectDraw surfaces for direct pixel plotting instead,
because they add another speed factor which is direct access to the
video memory, however they are really only useful for things like
screensavers and games and other animation effects.

I ought to add that I've only implemented 256 colour DIBs.

John B

Re:Quick access to a bitmap????????????


Quote
Lord of Darkness wrote:

> David Ullrich wrote:

> > Lord of Darkness wrote:

> > > On the Delphi Super page in the Freeware 32 bit section, I have a
> > > program called DibDemo, which shows how to create a DIBsection and
> > > access the pixels as fast as poss. Palette handling is also done.
> > > For 2D data plots etc etc it is extremely fast.

> >         You're claiming it's even faster than SetPixelV?

> > ;-)
> > --
> > David Ullrich

> Well of course. [...]

        Thanks. (For years I get in trouble for omitting the ";-)"
part - just once I include it and you missed it. Unless you didn't,
of course ...)

--
David Ullrich

?his ?s ?avid ?llrich's ?ig ?ile
(Someone undeleted it for me...)

Re:Quick access to a bitmap????????????


Quote
> > > ;-)
> > > --
> > > David Ullrich

> > Well of course. [...]

>         Thanks. (For years I get in trouble for omitting the ";-)"
> part - just once I include it and you missed it. Unless you didn't,
> of course ...)

I missed it

:)

John B

Other Threads