Board index » delphi » Fast Drawing Grid on Canvas?

Fast Drawing Grid on Canvas?

Hi,

I have Bitmap on which I have to draw a grid and I find out that if Bitmap
gets big, let say 3000x3000, it takes a long time to draw a grid if I am
using
.....
Canvas.MoveTo(x,y);
Canvas.LineTo(x+1,y+1);
..........

So I was wondering if there any faster way to do that?

Thanks.

 

Re:Fast Drawing Grid on Canvas?


Look up ScanLine under TBitmap...

--

Paul Nicholls (Delphi 5 Professional)
"Life is like a road - occasionally you run into potholes or get
blowouts." -
Paul Nicholls

HomePage: www.southcom.com.au/~phantom
Email: paul-nicho...@hotmail.com
(Replace "-" with "_" to reply)

Quote
"Hayk Petrosyan" <hayk_petros...@yahoo.com> wrote in message

news:3d2b68ce$1_2@dnews...
Quote
> Hi,

> I have Bitmap on which I have to draw a grid and I find out that if Bitmap
> gets big, let say 3000x3000, it takes a long time to draw a grid if I am
> using
> .....
> Canvas.MoveTo(x,y);
> Canvas.LineTo(x+1,y+1);
> ..........

> So I was wondering if there any faster way to do that?

> Thanks.

Re:Fast Drawing Grid on Canvas?


Hello,

This link should help.
http://community.borland.com/article/0,1410,22939,00.html

Quote
"Paul Nicholls" <paul-nicho...@hotmail.com> wrote in message

news:3d2b6dbb_2@dnews...
Quote
> Look up ScanLine under TBitmap...

> --

> Paul Nicholls (Delphi 5 Professional)
> "Life is like a road - occasionally you run into potholes or get
> blowouts." -
> Paul Nicholls

> HomePage: www.southcom.com.au/~phantom
> Email: paul-nicho...@hotmail.com
> (Replace "-" with "_" to reply)

> "Hayk Petrosyan" <hayk_petros...@yahoo.com> wrote in message
> news:3d2b68ce$1_2@dnews...
> > Hi,

> > I have Bitmap on which I have to draw a grid and I find out that if
Bitmap
> > gets big, let say 3000x3000, it takes a long time to draw a grid if I am
> > using
> > .....
> > Canvas.MoveTo(x,y);
> > Canvas.LineTo(x+1,y+1);
> > ..........

> > So I was wondering if there any faster way to do that?

> > Thanks.

Re:Fast Drawing Grid on Canvas?


Quote
> "Life is like a road - occasionally you run into potholes or get
> blowouts." -

or...
Life is like a road trip. Sometimes you're the bug; sometimes you're the
windshield.

Sorry. I couldn't resist.
--
Jon
http://home1.gte.net/jqjacobs/index.htm
This posting was made entirely from recycled electrons. No innocent atoms
were harmed.

Re:Fast Drawing Grid on Canvas?


"Jon Q. Jacobs" <jqjac...@gte.net> wrote in message
news:3d2ba676$1_1@dnews...

Quote
> > "Life is like a road - occasionally you run into potholes or get
> > blowouts." -
> or...
> Life is like a road trip. Sometimes you're the bug; sometimes you're the
> windshield.

> Sorry. I couldn't resist.

LOL! You're the first person to comment about my signature <G>

Cheers,
Paul Nicholls (Delphi 5 Professional)
"Life is like a road - occasionally you run into potholes or get
blowouts." -
Paul Nicholls

HomePage: www.southcom.com.au/~phantom
Email: paul-nicho...@hotmail.com
(Replace "-" with "_" to reply)

Re:Fast Drawing Grid on Canvas?


Quote
"Hayk Petrosyan" <hayk_petros...@yahoo.com> wrote in message

news:3d2b68ce$1_2@dnews...

Quote
> Hi,

I think the polyline functions may be faster than numerous MoveTo - LineTo
calls.

Regards

Re:Fast Drawing Grid on Canvas?


ScanLine works really fast, like 20 times probably. But it's not trivial to
draw a Grid where you need to draw only every 6 pixel when I am using pf1bit
mode. It's easy if scale is like 8, 16, 24 but for other numbers I need some
other algorithm.

Anyone have ready algorithm to do that?
..............
   Scale := 6;
   while k < Bitmap.Height do begin
      P := Bitmap.ScanLine[k];
      i := 0;
      while i < Bitmap.Width do begin
        P[i div 8] := "here have to be some Bit operation";
        Inc(i, Scale);
      end;
      Inc(k, Scale);
    end;
.....................

Thanks!

Quote
"Paul Nicholls" <paul-nicho...@hotmail.com> wrote in message

news:3d2b6dbb_2@dnews...
Quote
> Look up ScanLine under TBitmap...

> --

> Paul Nicholls (Delphi 5 Professional)
> "Life is like a road - occasionally you run into potholes or get
> blowouts." -
> Paul Nicholls

> HomePage: www.southcom.com.au/~phantom
> Email: paul-nicho...@hotmail.com
> (Replace "-" with "_" to reply)

> "Hayk Petrosyan" <hayk_petros...@yahoo.com> wrote in message
> news:3d2b68ce$1_2@dnews...
> > Hi,

> > I have Bitmap on which I have to draw a grid and I find out that if
Bitmap
> > gets big, let say 3000x3000, it takes a long time to draw a grid if I am
> > using
> > .....
> > Canvas.MoveTo(x,y);
> > Canvas.LineTo(x+1,y+1);
> > ..........

> > So I was wondering if there any faster way to do that?

> > Thanks.

Re:Fast Drawing Grid on Canvas?


Quote
>       i := 0;
>       while i < Bitmap.Width do begin
>         P[i shr 3] := 1 shl (i and 7);
>         Inc(i, Scale);
>       end;

Have not tested it, it could also be:

Quote
>         P[i shr 3] := 1 shl (7-(i and 7));

(I don't remember the order of the bits right now)

Jens

P.S. I use [i shr 3] instead of [i div 8], the result is the same, but
it's more "assembler-like" (although the compiler should take care about
it and generate the same code).

Re:Fast Drawing Grid on Canvas?


Thanks!

I will try that.

Some other strange thing is happening too. If my Scale is something other
than 2^x I am getting very strange results. Like not all bitmap is filled,
some pixels missed etc.

Did you experienced anything like that?

Quote
"Jens Gruschel" <j...@pegtop.net> wrote in message

news:3D2C8494.EC9EEF49@pegtop.net...
Quote
> >       i := 0;
> >       while i < Bitmap.Width do begin
> >         P[i shr 3] := 1 shl (i and 7);
> >         Inc(i, Scale);
> >       end;

> Have not tested it, it could also be:

> >         P[i shr 3] := 1 shl (7-(i and 7));

> (I don't remember the order of the bits right now)

> Jens

> P.S. I use [i shr 3] instead of [i div 8], the result is the same, but
> it's more "assembler-like" (although the compiler should take care about
> it and generate the same code).

Re:Fast Drawing Grid on Canvas?


Quote
> Some other strange thing is happening too. If my Scale is something other
> than 2^x I am getting very strange results. Like not all bitmap is filled,
> some pixels missed etc.

Oups, I didn't think of that before. Of course (i div 8) and ((i+scale)
div 8) can result in the same value (for example if i = 0 and scale =
3). In such a case you set the same byte twice. The most simple way to
avoid this would be...

P[i shr 3] := P[i shr 3] OR (1 SHL ...

Note that this cannot happen if scale >= 8 (if I'm not missing something
again).

Jens

Re:Fast Drawing Grid on Canvas?


Actually I wasn't talking about thath :)

I wrote some algorithm to handle that thing. The point of my last message
was that lines are not showing and not rows. So let say if I need to fill
every 7 line it's actually not doing that and does some wired things.

Thanks.

Quote
"Jens Gruschel" <j...@pegtop.net> wrote in message

news:3D2DFF62.222F8637@pegtop.net...
Quote
> > Some other strange thing is happening too. If my Scale is something
other
> > than 2^x I am getting very strange results. Like not all bitmap is
filled,
> > some pixels missed etc.

> Oups, I didn't think of that before. Of course (i div 8) and ((i+scale)
> div 8) can result in the same value (for example if i = 0 and scale =
> 3). In such a case you set the same byte twice. The most simple way to
> avoid this would be...

> P[i shr 3] := P[i shr 3] OR (1 SHL ...

> Note that this cannot happen if scale >= 8 (if I'm not missing something
> again).

> Jens

Re:Fast Drawing Grid on Canvas?


Hi Hayk,

Quote
>       while i < Bitmap.Width do begin
>         P[i div 8] := "here have to be some Bit operation";
>         Inc(i, Scale);
>       end;

Before making your Bit operation, you need to test (e.g. with i mod 8) which Bit
operation you must apply. Not only the line may be in different positions inside
the byte, but in some cases you might have to draw more than one line inside the
same byte.

Let's say that B is the limit between two bytes, x is a bit that must not be
drawn, I is a bit that must be drawn. Here is what you should have with a Scale :=
6:

BIxxxxxIxBxxxxIxxxBxxIxxxxxBIxxxxxIxB  etc.

In this case you have 3 different patterns, but if Scale is 7 you will have more!

Thrse

Re:Fast Drawing Grid on Canvas?


Hi Hayk,

Quote
> ScanLine works really fast, like 20 times probably. But it's not trivial to
> draw a Grid where you need to draw only every 6 pixel when I am using pf1bit
> mode. It's easy if scale is like 8, 16, 24 but for other numbers I need some
> other algorithm.

> Anyone have ready algorithm to do that?

Here some untested pseudocode:

    Scale := 6;
    while k < Bitmap.Height do
       begin
       P := Bitmap.ScanLine[k];
       i := 0;
       PreviousByte := 0;
       while i < Bitmap.Width do
         begin
         CurrentByte := i div 8;
         If CurrentByte <> PreviousByte then
           P[CurrentByte] := 1 shl (i mod 8) else
           P[CurrentByte] := P[CurrentByte] or 1 shl (i mod 8);
         Inc(i, Scale);
         PreviousByte := CurrentByte;
         end;
       Inc(k, Scale);
       end;

Thrse

Re:Fast Drawing Grid on Canvas?


Quote
>          If CurrentByte <> PreviousByte then
>            P[CurrentByte] := 1 shl (i mod 8) else
>            P[CurrentByte] := P[CurrentByte] or 1 shl (i mod 8);

Well I am not used to bit operations, so maybe the last line is enough (and maybe
the same as Jens's suggestion?)...

Thrse

Re:Fast Drawing Grid on Canvas?


Hi Hayk,

Quote
> The point of my last message
> was that lines are not showing and not rows.

You mean you don't have any pixels for some of the horizontal lines?
Is your Bitmap.Width a multiple of 8?

Thrse

Go to page: [1] [2]

Other Threads