Board index » delphi » Multiplying a colour bitmap by a greyscale bitmap

Multiplying a colour bitmap by a greyscale bitmap

Any suggestions for doing this at high speed?  I.e. is there a single
OS-provided operation like bit-blitting but that has an analog multiply
characteristic....

(I have a multiply of two approximately 1024 x 768 bitmaps down to 36msec on
a Pentium 550 but would like to do better.....  The requirement is to shade
a map of the world between night and day in real-time).

Thanks,
David

 

Re:Multiplying a colour bitmap by a greyscale bitmap


Quote
> (I have a multiply of two approximately 1024 x 768 bitmaps down to 36msec on
> a Pentium 550 but would like to do better.....  The requirement is to shade
> a map of the world between night and day in real-time).

That is, you spend 25 time-cycles on each pixel. No so bad.

- Do you use scanline? (Yes, I think).

- An other way is to use MMX instructions :(((
So you can multiply 8 bits at the same time.

- I can send you a part of code who tranform color to grey scale
picture.

--
Eric SIBERT
http://www.multimania.com/esibert

Re:Multiplying a colour bitmap by a greyscale bitmap


Yes, I spent some time optimising the code already, using Scanline
(essential), skipping multiplies by 1, shift right 8 instead of divide by
255, treating the 24-bit picture as simply an array of bytes rather than
bothering with B G R indices!  I really wanted to be sure I hasn't missed
anything obvious.  I like your idea of MMX - if you have any pointers, that
would be helpful.
Thanks for the offer of the code, but changing colour to greyscale isn't the
problem here.

David

Quote
"Eric SIBERT" <esib...@multimania.com> wrote in message

news:3A1D1163.14189174@multimania.com...
Quote
> > (I have a multiply of two approximately 1024 x 768 bitmaps down to
36msec on
> > a Pentium 550 but would like to do better.....  The requirement is to
shade
> > a map of the world between night and day in real-time).

> That is, you spend 25 time-cycles on each pixel. No so bad.

> - Do you use scanline? (Yes, I think).

> - An other way is to use MMX instructions :(((
> So you can multiply 8 bits at the same time.

> - I can send you a part of code who tranform color to grey scale
> picture.

> --
> Eric SIBERT
> http://www.multimania.com/esibert

Re:Multiplying a colour bitmap by a greyscale bitmap


For MMX, you will find definition of the instructions on Intel web site
(the Instruction Set Reference).
You should use assembler. And the instructions are not coded by Borland,
so you should code it by your self (i.e. hexadecimal code). But there is
something to help you (ni tested) :

http://www.ingjapan.ne.jp/hori/index-e.html
With D5, you can see the MMX register during debugging.

Quote
> Thanks for the offer of the code, but changing colour to greyscale isn't the
> problem here.

No, but the technique are not so far.

Eric SIBERT
http://www.multimania.com/esibert

Re:Multiplying a colour bitmap by a greyscale bitmap


Eric,

If you mean the file image.zip at the address below, I have taken a copy and
will study it carefully.  I'm also grateful for the MMX link.

  http://www.multimania.com/esibert/informatique/programming.htm

Ah, the MMX site has moved to:

  http://www.yks.ne.jp/~hori/index-e.html

Many thanks,
David

Quote
"Eric SIBERT" <esib...@multimania.com> wrote in message

news:3A1D971B.1D7CF0A1@multimania.com...
Quote
> For MMX, you will find definition of the instructions on Intel web site
> (the Instruction Set Reference).
> You should use assembler. And the instructions are not coded by Borland,
> so you should code it by your self (i.e. hexadecimal code). But there is
> something to help you (ni tested) :

> http://www.ingjapan.ne.jp/hori/index-e.html

> With D5, you can see the MMX register during debugging.

> > Thanks for the offer of the code, but changing colour to greyscale isn't
the
> > problem here.

> No, but the technique are not so far.

> Eric SIBERT
> http://www.multimania.com/esibert

Re:Multiplying a colour bitmap by a greyscale bitmap


Quote
> If you mean the file image.zip at the address below, I have taken a copy and
> will study it carefully.  I'm also grateful for the MMX link.

>   http://www.multimania.com/esibert/informatique/programming.htm

Sorry, not this one (which deal meanly with B/W pictures). But you can
have a look at the "Flou" function, which use the same technique (i.e.
moving pointer along a line).

An other question. How does look your greyscale picture? Is it big parts
of white and black with just a fiew area of grey betwen? If yes (as it
is light on earth), you should store for each line, the areas with
constant color (B or W) and only process on grey area. This should be
faster than testing each pixel before proceding. And you can use
FillChar for black area and CopyChar for white area.

--
Eric SIBERT
http://www.multimania.com/esibert

Re:Multiplying a colour bitmap by a greyscale bitmap


Eric,

Sorry, but I do not know in which program your "Flou" function resides.....

The greyscale picture comprises lines which actually have only three
brightnesses - white (full illumination), light-grey (sunrise/sunset), and
grey (night-time).  The final result is shown in this compressed 62KB
screen-shot at:

http://www.david-taylor.pwp.blueyonder.co.uk/software/wxtrack-screens...

So, looking along each line, there are large areas at 100% gain, large areas
at 80% gain (say), and just some small regions at 90% gain.  Every five
minutes, I multiply the original background bitmap by the solar illumination
to get the new displayed bitmap.  I do not recompute the illumination every
time, simply move a pre-computed illumination another few degrees to the
west.  Only when the day changes do I recompute the illumination.

In fact with your suggestions, and replacing the multiply and shift with a
table lookup, I am now reasonably happy with the speed.  The remaining
problem is that every source pixel might be different, so I must process
them all.  However, as I use 24-bit pixels (to save memory), fetching a
24-bit operand (as three separate bytes) is not efficient in a 32-bit
architecture, so I might provide a 32-bit alternative on machines where
plenty of memory is free.
By the way, if anyone is interested the program is free and can be found at:
  http://www.david-taylor.pwp.blueyonder.co.uk/software/wxtrack.htm
and deatils of using it at:

http://www.david-taylor.pwp.blueyonder.co.uk/wxsat/Software/UsingWXtr...

David

Quote
"Eric SIBERT" <esib...@multimania.com> wrote in message

news:3A1E6016.FCB25293@multimania.com...
Quote
> > If you mean the file image.zip at the address below, I have taken a copy
and
> > will study it carefully.  I'm also grateful for the MMX link.

> >   http://www.multimania.com/esibert/informatique/programming.htm

> Sorry, not this one (which deal meanly with B/W pictures). But you can
> have a look at the "Flou" function, which use the same technique (i.e.
> moving pointer along a line).

> An other question. How does look your greyscale picture? Is it big parts
> of white and black with just a fiew area of grey betwen? If yes (as it
> is light on earth), you should store for each line, the areas with
> constant color (B or W) and only process on grey area. This should be
> faster than testing each pixel before proceding. And you can use
> FillChar for black area and CopyChar for white area.

> --
> Eric SIBERT
> http://www.multimania.com/esibert

Re:Multiplying a colour bitmap by a greyscale bitmap


David:

Quote
"David Taylor" <david.j.tay...@baesystems.com> wrote in message news:3a221ced$1_1@dnews...

The final result is shown in this compressed 62KB

Quote
> screen-shot at:

> http://www.david-taylor.pwp.blueyonder.co.uk/software/wxtrack-screens...

What an interesting program!

Quote
> By the way, if anyone is interested the program is free and can be found at:
>   http://www.david-taylor.pwp.blueyonder.co.uk/software/wxtrack.htm
> and deatils of using it at:

> http://www.david-taylor.pwp.blueyonder.co.uk/wxsat/Software/UsingWXtr...

Any chance that you'll ever make your source code available?

I've collected a number of "Sun Clock" links and wanted someday to write a Delphi CGI to put this
SunClock on web pages (and write a "Lab Report" describing the math and the graphics).  It looks
like you've already got the computations and graphics done.

--
efg

Earl F. Glynn     E-mail:  e...@efg2.com
Overland Park, KS  USA

efg's Computer Lab:  http://www.efg2.com/Lab
Mirror:  http://homepages.borland.com/efg2lab/Default.htm

Re:Multiplying a colour bitmap by a greyscale bitmap


Earl,

I've made the source code available to the one or two folks who've asked for
it -  but it's a bit of a pain as the program calls up many routines from a
variety of libraries that I use (Brad Stowers stuff in particular) so the
source code isn't simply in one folder!  I'll contact you directly about
this.  Seeing the results on a Web page would be fun!

The other thing that I did was to make the core orbit prediction stuff
available as an ActiveX library.  The code was originally written by Dr T S
Kelso (you may have seen him on the Weather channel recently -
www.celestrak.com) in Turbo Pascal, and I added a little to turn the code
into Object Pascal with methods and properties etc.  That code is available
from the page mentioned below and can be used with Delphi, Basic, C++,
Windows Scripting Host, Web servers etc.  No graphics, though, just orbit
prediction!

Hope your 3D maths is better than mine, I really struggle with that stuff!

David

"Earl F. Glynn" <EarlGl...@att.net> wrote in message
news:8vu6oj$fei2@bornews.inprise.com...

Quote
> David:
[]
> > By the way, if anyone is interested the program is free and can be found
at:
> >   http://www.david-taylor.pwp.blueyonder.co.uk/software/wxtrack.htm
> > and deatils of using it at:

http://www.david-taylor.pwp.blueyonder.co.uk/wxsat/Software/UsingWXtr...
Quote

> Any chance that you'll ever make your source code available?

> I've collected a number of "Sun Clock" links and wanted someday to write a

Delphi CGI to put this
Quote
> SunClock on web pages (and write a "Lab Report" describing the math and

the graphics).  It looks
Quote
> like you've already got the computations and graphics done.

> --
> efg

> Earl F. Glynn     E-mail:  e...@efg2.com
> Overland Park, KS  USA

> efg's Computer Lab:  http://www.efg2.com/Lab
> Mirror:  http://homepages.borland.com/efg2lab/Default.htm

Re:Multiplying a colour bitmap by a greyscale bitmap


Eric SIBERT <esib...@multimania.com> escreveu na mensagem
news:3A1D1163.14189174@multimania.com...

: - I can send you a part of code who tranform color to grey scale
: picture.

Is it  ((R+G+B) div 3)  ???

--

Have fun!
- Joao Barata [http://jmejm.webjump.com]

Re:Multiplying a colour bitmap by a greyscale bitmap


Not necessarily - it depends on the rendering you want.  Colour TV uses:

  grey = 0.30 * red + 0.59 * green + 0.11 * blue  (off the top of my head,
may be wrong)

David

Quote
"Barata" <n...@spam.com> wrote in message news:3a270c57_1@dnews...

> Is it  ((R+G+B) div 3)  ???

> --

> Have fun!
> - Joao Barata [http://jmejm.webjump.com]

Re:Multiplying a colour bitmap by a greyscale bitmap


Ok! I assumed a bitmap, as a bit-map (like those named *.BMP; in windows or
OS2)
with the extension .BMP and the file-header BM? (where ? stands for 6 or 9)
and a full palette of 256 entries, Whether they are  displayed on Colour TV,
DOS (must div 4 (on native resolutions (256 colours or less))), Windows,
Linux or BeOs... Whatever... (MSX2 must div 8 )

Anyway, gray is the average amount of all colours, so p.e. bright white
(255,255,255) is always the colour 255, AND THEN  you write 255 on the BMP
and let C-TV handle-it!  (or whoever)

Have fun!
- Joao Barata [http://jmejm.webjump.com]

--
David Taylor <david.j.tay...@baesystems.com> escreveu na mensagem
news:3a274e8b$1_1@dnews...
: Not necessarily - it depends on the rendering you want.  Colour TV uses:
:
:   grey = 0.30 * red + 0.59 * green + 0.11 * blue  (off the top of my head,
: may be wrong)
:
: David
:

Quote
: "Barata" <n...@spam.com> wrote in message news:3a270c57_1@dnews...

: >
: > Is it  ((R+G+B) div 3)  ???
: >
: > --
: >
: > Have fun!
: > - Joao Barata [http://jmejm.webjump.com]
: >
: >
:
:

Other Threads