|
and getting pixels?
Every new thing you learn,Gives you a new personality.
|
|
|
|
|
Look at the members for CImage...
CImage::GetBits Retrieves a pointer to the actual pixel values of the bitmap.
|
|
|
|
|
I need a few c++ code optimizing tips. Anybody knows where I can find good ones?
regards
hint_54
|
|
|
|
|
The first rule is to measure first. You need to pinpoint a specific piece of code that is slower then could be and is worth the trouble of changing (you want some bang for your buck). It is a well known fact that when developers try to guess they get it wrong more times then not and you end up with code that is more confusing but not noticeably faster. This is best stated by the following oft quoted saying:
"Premature optimization is the root of all evil"
Steve
|
|
|
|
|
Joseph Newcomer's take on optimization[^]
Also, Herb Sutter's "101 Coding Standards" has a few good tips on when/where you should and when/where you shouldn't. (I have issues with the book as I only found I can live with roughly 1/4 of the suggestions in the book but that's about 25 more good coding techniques nonetheless.)
Meyer's "More Effective C++" has a small bit to say indirectly about optimization in terms of understanding polymorphic behavior and late binding and what it means in terms of the overhead added.
However, I think your gonna find more people are focused on readability and correctness nowadays for most programs, unless your the person writing the next GDI+ (please make it faster if you are;P).
Threads will continue to be the best place to achieve "apparent" improvements for the end user while allowing you to retain the readability and correctness everybody's preaching.
|
|
|
|
|
bob16972 wrote: unless your the person writing the next GDI+ (please make it faster if you are).
lol. No, that's not it. It's a lot more simple. I'm running for a coding contest and programs have to be very optimized (if they aren't you might get a "Time Limit Exceeded" error). Besides, I think I should learn this things too.
regards
hint_54
|
|
|
|
|
I was flipping through Meyer's "Effective C++" this morning trying to remember how to do something and I noticed he's not directly concerned with "efficiency" (per se) but he does get to the heart of portable C++ and mentions "efficiency" quite a bit as a secondary argument for many program design recommendations. I remembered your post and thought I'd add this comment in hindsight.
Some tips will yield you more efficient code directly and some will point out where designing for robustness, clarity, and caution will yield more maintainable code at the cost of efficiency but these same tips can be reversed to point you to places where you could squeeze out some more speed.
The book is a keeper in so many ways it isn't funny so if you don't already have it. Get it. It's also listed here at CP as a recommended book by the masses (or at least whoever put that list together)
Take care and good luck in your competition.
|
|
|
|
|
hint_54 wrote: I need a few c++ code optimizing tips. Anybody knows where I can find good ones?
As already mentioned, measure first. Always keep in mind the 80/20 rule (depending on who is talking this might even be referred to as a 90/10 rule -- I prefer 80/20 because that usually assumes you have not optimized yet, ): 80% of the time is spent in 20% of the code, when you have optimized you might actually reach the 90/10.
Optimize the code that is A) called the most often B) more time is spent in the code. These could be two different areas, "A" could be a short module that even saving 2% of the overhead within will make a difference because of how often it is called. "B" could be a long routine that could be redesigned to spend less time within.
Reduce redundancy. If you call cos(angle) 10 times for the same angle, consider making a local variable to hold the result and reuse the result 10 times rather than calculate the cosine of the angle 10 different times for the same angle.
That may sound obvious, but you would be surprised at how many people don't think about it. Also don't replace a single point of reduncany without a check, usually it is still faster to allocate the local variable and store the result, but for some CPU math instructions it is easier to calculate twice than to allocate the memory use it and deallocate at the end.
That is why measuring is extremely important. You will discover faster ways yourself, especially in algorithms, because you will get to know your own code better.
_________________________
Asu no koto o ieba, tenjo de nezumi ga warau.
Talk about things of tomorrow and the mice in the ceiling laugh. (Japanese Proverb)
|
|
|
|
|
How to calculate the Red, gree, Blue values of each and every pixel (of say around 50000 pixles) in a bitmap?
|
|
|
|
|
Hi,
You should use CDC::GetPixel() which returns the RGB value of a specific
pixel of the bitmap.
Regards,
Eli
|
|
|
|
|
that should be your last resort as it is very slow
|
|
|
|
|
Hi nikhilsai,
maybe it is some helpful to you
You can use CImage for load bitmap and use GetPixel that retun COLORREF
|
|
|
|
|
GetRValue(rgb value)
GetGVaiue(rgb value)
GetBValue(rgb value)
|
|
|
|
|
whenever I use it, following "build error" shows up:
F:\hu\DD\DetailDesign\ProfileFeatureCmd.cpp(2850) : error C2248: 'Release' : cannot access private member declared in class 'ATL::_NoAddRefReleaseOnCComPtr<struct sketchpoint="">'
d:\program files\microsoft visual studio\vc98\atl\include\atlbase.h(420) : see declaration of 'Release'
Thank you very much!!!
-------------------
I am learning C++ and English
|
|
|
|
|
Instead of calling the underlying interface's Release function, call the class's Release method.
So, instead of :
<br />
pMyInterface->Release();<br />
Try:
<br />
pMyInterface.Release();<br />
<EDIT>
But the ATL class will do this automatically for you when it goes out of scope, so you don't need to explicitly do this<
;EDIT>
I Dream of Absolute Zero
-- modified at 9:07 Sunday 16th April, 2006
|
|
|
|
|
This is by design. The reason in that when you call Release through a smart pointer you've probably just introduced a bug into your code - So ATL disallows it. He's some code which shows why this is the case:
{
CComPtr<IDispatch> spDisp;
CoCreateInstance(CLSID_SomeObject, NULL, CLSCTX_ALL, IID_IDispatch, reinterpret_cast<void**>(&spDisp));
spDisp->Release();
}
Steve
|
|
|
|
|
The reason why I have been using the operator "->" is that I always treat a smart pointer as a pointer , now I think it through that it is a object class CComPtr<...>
Thank you all very much!!!
-------------------
I am learning C++ and English
|
|
|
|
|
Hi.
I was wonder how can I set multiple instances of one dll file,
see the following:
<br />
<br />
typedef void type_SetX(int x);<br />
typedef int type_GetX();<br />
<br />
type_SetX *SetX;<br />
type_GetX *GetX;<br />
<br />
HINSTANCE h1 = LoadLibrary("foo.dll");<br />
HINSTANCE h2 = LoadLibrary("foo.dll");<br />
<br />
SetX = (type_SetX *)GetProcAddress(h1, "SetX");<br />
GetX = (type_GetX *)GetProcAddress(h1, "GetX");<br />
<br />
SetX(5);<br />
Print( GetX() );
<br />
<br />
SetX = (type_SetX *)GetProcAddress(h2, "SetX");<br />
<br />
SetX(8);
<br />
Print( GetX() );
The var X inside the dll is not static of course.
Any idia how can I manage multiple instances of the same dll ?
Thanks,
David
-- modified at 8:39 Sunday 16th April, 2006
|
|
|
|
|
Hi David,
I'm not sure , but have you tried to use different function pointers for each instance of the DLL?
Regards,
Eli
|
|
|
|
|
Hi Eli.
Yes, but it doesn't matters because LoadLibrary() always return a pointer to the same instance.
I wrote a simple test code:
<br />
<br />
HINSTANCE h1 = LoadLibrary("Foo.dll");<br />
HINSTANCE h2 = LoadLibrary("Foo.dll");<br />
<br />
bool IsSame = (h1 == h2);<br />
<br />
well, IsSame = true...
Maybe the rule states: One instance of a DLL-File per process.
And my only way is to inject it to a thread...
Thanks,
David
|
|
|
|
|
Virtek wrote: HINSTANCE h1 = LoadLibrary("Foo.dll");
HINSTANCE h2 = LoadLibrary("Foo.dll");
bool IsSame = (h1 == h2);
You're seeing the correct behavior, that's how DLLs work. The second LoadLibrary() sees that the DLL is already loaded, so it just increments the reference count.
--Mike--
Visual C++ MVP
LINKS~! Ericahist | NEW!! PimpFish | CP SearchBar v3.0 | C++ Forum FAQ
|
|
|
|
|
Hi,
I need to find all files in a certain directory that are in the format of: file.001, file.002, etc (the numbers in the file extensions can go up to infinity...).
I am using the functions FindFile and FindNextFile to do this (my code is below). The problem is that in the same directory there might also be files named file.x or file.y (for example) which I don't need to find.
I only need the files with the number extensions.
Is there a way to do this?
this is my code:
sSearchPath.Format("file.*");
BOOL bWorking = f.FindFile(sSearchPath);
while (bWorking)
{
bWorking = f.FindNextFile();
CString strFileName = f.GetFileName();
}
Thanks
|
|
|
|
|
I dont know if this code work with but this what I used
int i=0;
WIN32_FIND_DATA info;
HANDLE hp;
sprintf(fileFound, "%s\\*.tga", folderPath);
hp = FindFirstFile(fileFound, &info);
if(hp== INVALID_HANDLE_VALUE)
return;
do
{
sprintf(fileFound,"%s\\%s", folderPath, info.cFileName);
// DeleteFile(fileFound);
i++;
}while(FindNextFile(hp, &info));
FindClose(hp);
ShowMessage(i);
|
|
|
|
|
I don't understand, are u looking for all files that have a .tga extension?
|
|
|
|
|