CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jan 2010
    Posts
    6

    Analyzing 70.000 pixels color as fast as is possible.

    Hi, after a big research I finally found a solution to make sreenshots;p But what is bothering me - when I do some tests it shows up that analizing about 66.000 pixels takes 50-60% of my CPU resources(Intel Core Duo 1.66 GHz). Is there a faster way to do that?
    Here is the code:

    Code:
    //---------------------------------------------------------------------------
    	#include <vcl.h>
    	#pragma hdrstop
    	#include "Unit1.h"
    	//---------------------------------------------------------------------------
    	#pragma package(smart_init)
    	#pragma resource "*.dfm"
    	#include <windows.h>
    	#include <stdio.h>
    	#include <winuser.h>
    	#include "fstream.h"
    	 
    	String dir;
    	String czas;
    	String data;
    	int b=0;
    	int s=0;
    	 	 
    	TForm1 *Form1;
    	//---------------------------------------------------------------------------
    	__fastcall TForm1::TForm1(TComponent* Owner)
    	        : TForm(Owner)
    	{
    	}
    	//---------------------------------------------------------------------------
    	 
    	 
    	void __fastcall TForm1::Timer1Timer(TObject *Sender)
    	{
    	 
    	b++;
    	if(b>1000) return;
    	 
    	         TCanvas &PulpitCanvas = *new TCanvas();
    	         Graphics::TBitmap *bitmap = new Graphics::TBitmap;
    	         COLORREF k[116][114];
    	         HWND Wnd=FindWindow(NULL,"WINDOW");
    	         if(Wnd==0)
                     {MessageBox(0,"Didnt found the window!",0,0);}
    	         PulpitCanvas.Handle = GetDC(Wnd);
    	         bitmap->Width = 800;
    	         bitmap->Height = 600;
    	 
    	bitmap->Canvas->CopyRect(Rect(0, 0, bitmap->Width,    bitmap->Height), &PulpitCanvas, Rect(0, 0, bitmap->Width, bitmap->Height));
    	 
    	for(int i=0; i<116; i++)
    	{
    	    for(int j=0; j<114; j++)
    	    {
    	       if(bitmap->Canvas->Pixels[i][j]==RGB(0,0,0));
    	       if(bitmap->Canvas->Pixels[i][j]==RGB(0,0,0));
    	       if(bitmap->Canvas->Pixels[i][j]==RGB(0,0,0));
    	       if(bitmap->Canvas->Pixels[i][j]==RGB(0,0,0));
    	       if(bitmap->Canvas->Pixels[i][j]==RGB(0,0,0));
    	    }
    	}
    	 
    	ReleaseDC(0, PulpitCanvas.Handle);
    	delete &PulpitCanvas;
    	delete bitmap;
    	   
    	 }
    	 
    	//---------------------------------------------------------------------------
    Last edited by Marc G; January 28th, 2010 at 09:34 AM. Reason: fixed code tags

  2. #2
    Join Date
    Jan 2010
    Posts
    6

    Re: Analyzing 70.000 pixels color as fast as is possible.

    Sorry, for the code but there was no option to get it into the block. And I have no option to edit my post. Is it normal?

  3. #3
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Analyzing 70.000 pixels color as fast as is possible.

    you used the wrong ending code tag.
    The proper syntax is: [ code ]...[ /code ] without the spaces.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  4. #4
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Analyzing 70.000 pixels color as fast as is possible.

    You say it uses 50-60&#37; CPU.
    That doesn't say anything actually.
    Does it use 50-60% CPU for half a second, 1 second, 1 minute?
    Are you sure you are running a RELEASE build?
    If you are running a DEBUG build that might explain the low performance.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  5. #5
    Join Date
    Nov 2007
    Posts
    613

    Re: Analyzing 70.000 pixels color as fast as is possible.

    Professionally, pixel-by-pixel image processing is done using low level Assembler code to access directly the memory where the pixels are stored. That greatly speedes up the work. You don't have to be an Assembler guru, you only need to know how to write embedded loops, access the stack, the memory and the registers. The _asm keyword allows you to insert directly Assembler code in a C/C++ program.

    You will need to rewrite several times the same algorithm, one time for each possible color deepth. The differences are caused by the fact that in a 32 bit per pixel bitmap you will have four bytes defining each pixel, in a 24 bit per pixel you will have 3 bytes defining each pixel and so on.

    Also, you'll need to create your bitmap with the CreateDIBSection function.

  6. #6
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Analyzing 70.000 pixels color as fast as is possible.

    Quote Originally Posted by srelu View Post
    Professionally, pixel-by-pixel image processing is done using low level Assembler code to access directly the memory where the pixels are stored. That greatly speedes up the work. You don't have to be an Assembler guru, you only need to know how to write embedded loops, access the stack, the memory and the registers. The _asm keyword allows you to insert directly Assembler code in a C/C++ program.
    This is a popular misconception. Even if you are an ASM guru, you will have a very hard time competing with modern optimized compilers. And if you are not a guru – don’t even bother.
    However, you do NOT need assembler to get a direct access to your pixels.


    Quote Originally Posted by srelu View Post
    Also, you'll need to create your bitmap with the CreateDIBSection function.
    The OP’s code uses Borland’s VCL library. I am not familiar with it, but guessing (based on the fact that it provides per-pixel access) that it has a DIB section.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  7. #7
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Analyzing 70.000 pixels color as fast as is possible.

    Is the algorithm taking a long time? The fact that it uses 50-60&#37; of your CPU really means nothing. Your Central Processing Unit is there to do one thing, process. If it's not processing (idle), you're just wasting energy.

    Viggy

  8. #8
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Analyzing 70.000 pixels color as fast as is possible.

    Quote Originally Posted by arva View Post
    Hi, after a big research I finally found a solution to make sreenshots;p But what is bothering me - when I do some tests it shows up that analizing about 66.000 pixels takes 50-60% of my CPU resources(Intel Core Duo 1.66 GHz). Is there a faster way to do that?
    As Marc pointed out, 50% is NOT a measure of speed, it’s CPU utilization.
    For your dual-core processor, it likely means 100% utilization of one core by a single thread. In other words, your app constantly does something CPU-intensive.
    What is your timer interval? May be the work in your TForm1::Timer1Timer simply can’t be completed in such interval?
    Also, your code does nothing useful. It appears to copy part of the screen content into the bitmap, and then discards that bitmap.
    And your nested loops do nothing at all. Five times!

    As for the optimization, I suggest few things:
    1. Create your bitmap once, outside of that timer handler.
    2. Move FindWindow() call to the top of the function, and return immediately if not found. There is no reason to copy anything from non-existing window.
    3. If that window was found, save its handle in a member variable and don’t call FindWindow() anymore.
    4. Same applies to your Canvas. It could be created only once, when the window was found.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  9. #9
    Join Date
    Dec 2009
    Posts
    44

    Re: Analyzing 70.000 pixels color as fast as is possible.

    If I may butt in with a off-topic request, could you share how you made your code take screenshots? I found some code by Napalm @ rohitlab.com but it freezes the screen for a small period of time and id like screenshotting to be much smoother, unless theres no way to escape this tiny delay.
    New to C++ and Visual C++ 2008 Express Edition.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured