Solved

Speed Up Graphics Routine

Posted on 1998-10-30
23
213 Views
Last Modified: 2010-04-02
I am doing some Pixel manipulation in a for loop - the routine works but is very slow.. What Can I do to speed it up? The routine is a 1-1 pixel manipulation to fade the image.  I need it to work for a 640x480 16-bit image.
0
Comment
Question by:gpaige
  • 9
  • 6
  • 4
  • +3
23 Comments
 

Expert Comment

by:lestat007
ID: 1176577
If all you're doing is fading, you could modify the palette registers instead of each individual pixel.
0
 

Author Comment

by:gpaige
ID: 1176578
Unless I'm wrong a 16 and 24 bit image doesn't have a palette.  If it does - How do I realize it as the logical palette to manipulate ?
0
 

Expert Comment

by:lestat007
ID: 1176579
Sorry.  I assumed that since you were using 640x480 you were also using 256 colors.  This is the only mode that will allow you an array of palette registers.
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1176580
Let's see what you have so far...
0
 
LVL 3

Expert Comment

by:altena
ID: 1176581
Using an off-screen, in mamory bitmap in stead of the "real" screen should speed things up.

Good luck
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1176582
How do you figure?

"Speed it up" is a relative term.  In order to find out how to do this, we need to have something to compare it to, i.e. the original source code.
0
 
LVL 3

Expert Comment

by:altena
ID: 1176583
>"Speed it up" is a relative term.
>  In order to find out how to do this,
>  we need to have something to compare it to,
>  i.e. the original source code.

We could set up a special commitee to solve and tackle
the problem. complete with monthly status report meetings,
status report tracking reports and 4 layers of management.
(common approach in most shops...sigh)

Or we can use some common sense..
e.g.: manipulating data in memory is faster than on screen.

Let gpaige be the judge please. The answer may or may not be
enough to solve the problem.
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1176584
>Or we can use some common sense..
>e.g.: manipulating data in memory is faster than on screen.

Sure, but it's not all gain...you must consider the time it takes to copy the area in memory back to the screen.

If you say that this is common sense, then the fact that you suggested it means that you assume gpaige has no common sense.

0
 
LVL 3

Expert Comment

by:altena
ID: 1176585
Scrapdog,

Your record at the E-E is quite impressive, you should
now better than to make remakrs like that.

I am pointing out the in memory processing as an option to
gpaige. He'll be the judge.

And since we seem to be in a flame war,
here's some more oil for the fire... :-)
(who said we experts cannot have fun in here)

> Sure, but it's not all gain...you must consider the time it
> takes to copy the area in memory back to the screen.

Have you ever done this for real Scrapdog ?
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1176586
It is not my intention to start a flame war!!  (even though I like to have fun too) =) !  :) !

BUT

How do you know that gpaige isn't ALREADY doing this (manipulating memory rather than the screen)?

I do agree that this is the best way to do it, but it can't be *faster* if he is already doing it!!!???

Compare to this question:

"How do I speed up my car?"

Answer: "Pump up your tires."  

It is obvious we need more info from gpaige!!

>Have you ever done this for real Scrapdog ?

Yes.
0
 
LVL 3

Expert Comment

by:altena
ID: 1176587
hehehe, This is fun...

>How do you know that gpaige isn't ALREADY doing this
>(manipulating memory rather than the screen)?

We will be the first ones to know as soon as he hits the
"reject" button... :-)


0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:gpaige
ID: 1176588
Wow - I didn't what to cause all this..
I am researching graphics programming and have found numerous cases of how to manipulate a paletted image - but I was trying to work on non-paletted images.  This is what I am doing so far.
I load a bitmap into memory then based on the size of the image I check each pixel with GetPixel(hdc,x,y) and do a alpha blend on it then use SetPixel(hdc,x,y) to place the new value back.  I have found that the calls to Get and SetPixel are the choke point.  
So I have been trying to get a pointer to the actual bitmap bits in memory to manipulate them - which I hope will speed it up..
Here is a pseudo cade segment :
 for (int inc = 0; inc<10; inc++)
      {
      for (int x=0; x<sizex; x++)
      {
            for (int y=0; y<sizey; y++)
            {
            Pixel = GetPixel(hMemDC,x,y);
            Red = GetRValue(Pixel);
            Green = GetGValue(Pixel);
            Blue = GetBValue(Pixel);
// Try just using alpha blending...
            SetPixel(hMemDC,x,y,RGB(AlphaArr[Red],AlphaArr[Green],AlphaArr[Blue]));
// Note AlphaArr[] is a pre-calculated array of pixel values.            
            }
      }
      //Finally Blt to the Screen
        // Code Deleted - but I use a simple StretchBlt
      }

So, could anyone tell me if I'm on the right track here and maybe an idea of how to start - I know there must be a way..
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1176589
Are GetPixel, GetRValue, GetGValue, GetBValue, and SetPixel functions or macros?

If they are functions, they will be many times slower than macros due to the overhead of the calls.  Tasks such as this are probably done with only one or two assembler statements...if you add the overhead of a function call you can slow them down as much as 5-10 times!!  This wouldn't really matter if you only called it once or twice, but you are calling it repeatedly.

(Note:  I normally don't use C, so I don't know if GetPixel etc. is contained in a library, etc.)
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1176590
Maybe using the xlat assembler opcode could speed it up a bit.  Since each component of the color is a byte, you could circumvent having to use GetXValue and RGB(xxx,xxx,xxx).
0
 

Author Comment

by:gpaige
ID: 1176591
I've figured it out - well a way that works for me.
I load the bitmap and then malloc a buffer large enough for the bitmap bits.  Then manipulate the buffer.  This is much faster.


0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1176592
I think using xlat on the buffer you created would be faster yet!!
0
 

Author Comment

by:gpaige
ID: 1176593
thanks scrapdog -

I am not too familiar with the x86 assembly - but I have started to look into it.  My next step to increase the speed is to try some inline assembly so I will look into the xlat opcode.
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1176594
Show me the source code you have so far, and I will try to help!
0
 
LVL 2

Expert Comment

by:harrys
ID: 1176595
You should get the data to a memory chunk with GetDIBits() as RGB for example, manipulate the bytes directly and copy it back to the bitmap with SetDIBits(), this
should go a lot faster than calling 5 WinAPI functions in your inner loop like you examples
does, these routines are not designed for manipulating large data structures...
0
 

Accepted Solution

by:
shudecek earned 80 total points
ID: 1176596
Try pre-building the fade in an array of offscreen buffers.
If you want a 30 frame fade this will require 640x480x2x30=18.4Meg, not a whole lot
for todays machines
have buffer[0] contain your final image, buffer[1] the image with the RGB values reduced to 29/30th's buffer[2] 28/30... until buffer[29] contains black or the value you are fading to.
Do this all in advance before you start the fade, Make sure there is enough physical memory availible.
When it's time to do your fade, copy the buffers one at a time to your screen, DC, or bitmap.
The more pre-processing you do (the less you do during the actual fade), the less the CPU has to do during the actual fade. (memory moves ie.  MOVSD are usually very fast)



0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1176597
18.4 MB is a big price to pay just to do a fade...

But if you decided to take this route, you could cut back the number of buffers needed to 1/2 with clever use of shr.

If you wanted to do a fade in 32 frames, you could make 4 buffers (640x480x2x16 = 9.3 MB).  For frames 32-17, the buffer would be copied, but frames 16-1 each byte of the buffer would be shifted while it is being copied.

16 would be the buffer for 32, shifted right by 1
15 would be the buffer for 30, shifted right by 1, etc.

The only cost would be the shr in the inner loop, and this is not too expensive, especially for a fade.


0
 

Author Comment

by:gpaige
ID: 1176598
Thanks for all of your help...

0
 

Author Comment

by:gpaige
ID: 1176599
Thanks for all of your help...

0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now