Thread: To replace GetPixel(), attemping to use bitmap + pointers to speed things up

  1. #1
    Registered User
    Join Date
    Apr 2016
    Posts
    10

    To replace GetPixel(), attemping to use bitmap + pointers to speed things up

    Hi everyone!

    Getpixel() is too slow so I am trying to use bitmap to load the RGB values into a pointer to access the RGB values rapidly, but it seems
    either I messed up on the bitmap operations or I can't get the pointer right!

    I use Pixelcolor() to compare so I know I have it right if the returned RGB values matches one another.

    Can anyone help me out?

    thx!



    Code:
    #include <iostream>
    #include <Windows.h>
    using namespace std;
    
    void cout_colour();
    void get_pixel();
    
    void main()
    {
       cout_colour();
       get_pixel();
    }
    
    void cout_colour()
    {
       HDC hdc, hdcTemp;
       RECT rect;
       BYTE* bitPointer;
    
       int red = 0;
       int green = 0;
       int blue = 0;
    
       hdc = GetDC(GetDesktopWindow());
       GetWindowRect(GetDesktopWindow(), &rect);
    
       int MAX_WIDTH = rect.right;
       int MAX_HEIGHT = rect.bottom;
    
       cout << "MAX_WIDTH" << MAX_WIDTH << endl;
       cout << "MAX_HEIGHT" << MAX_HEIGHT << endl;
    
       hdcTemp = CreateCompatibleDC(hdc);
    
       BITMAPINFO bitmap;
       bitmap.bmiHeader.biSize = sizeof(bitmap.bmiHeader);
       bitmap.bmiHeader.biWidth = MAX_WIDTH;
       bitmap.bmiHeader.biHeight = MAX_HEIGHT;
       bitmap.bmiHeader.biPlanes = 1;
       bitmap.bmiHeader.biBitCount = 32;
       bitmap.bmiHeader.biCompression = BI_RGB;
       bitmap.bmiHeader.biSizeImage = 0;   
       bitmap.bmiHeader.biClrUsed = 0;
       bitmap.bmiHeader.biClrImportant = 0;
    
    
       HBITMAP hBitmap2 = CreateDIBSection(hdcTemp, &bitmap, DIB_RGB_COLORS, (void**)(&bitPointer), NULL, NULL);
       SelectObject(hdcTemp, hBitmap2);
       BitBlt(hdcTemp, 0, 0, MAX_WIDTH, MAX_HEIGHT, hdc, 0, 0, SRCCOPY);
    
    
       int i = 1;
    
       int x = 1;
       int y = 1;
       SetCursorPos(x, y);
    
       red = (int)bitPointer[i];
       green = (int)bitPointer[i + 1];
       blue = (int)bitPointer[i + 2];
    
       cout << red << " " << green << " " << blue << endl;
    
       DeleteDC(hdc);
    }
    
    void get_pixel()
    {
       int x = 1;
       int y = 1;
       COLORREF hex_color;
       int red;
       int green;
       int blue;
       HDC dc = GetDC(GetDesktopWindow());
    
       hex_color = GetPixel(dc, x, y);
       red = GetRValue(hex_color);
       green = GetGValue(hex_color);
       blue = GetBValue(hex_color);
    
       cout << "Super Slow RGB cout!" << endl;
       cout << red << " " << green << " " << blue << endl;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    At a guess....

    https://msdn.microsoft.com/en-us/lib...=vs.85%29.aspx

    biHeight

    The height of the bitmap, in pixels. If biHeight is positive, the bitmap is a bottom-up DIB and its origin is the lower-left corner. If biHeight is negative, the bitmap is a top-down DIB and its origin is the upper-left corner.

    If biHeight is negative, indicating a top-down DIB, biCompression must be either BI_RGB or BI_BITFIELDS. Top-down DIBs cannot be compressed.
    In short, your idea of 1,1 might be the difference between the top-left being the origin, or the bottom-left being the origin.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2016
    Posts
    10
    got it, thanks!

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Note also that the R, G, B values are reversed in a DIBSection.

    The least significant byte in 32 bit DIBSection DWORD is blue, not red as it is in a COLORREF value.

    You might try using a 32 bit pointer to access the color values in the bitmap. The functions for operating on COLORREF values (eg GetRValue(), RGB(), etc) could be used and are still considerably faster than GetPixel() and SetPixel().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help me clear some things up about pointers.
    By camel-man in forum C Programming
    Replies: 13
    Last Post: 04-17-2011, 08:46 PM
  2. Replace button with a bitmap
    By Swarvy in forum Windows Programming
    Replies: 10
    Last Post: 09-12-2008, 11:43 AM
  3. Replies: 2
    Last Post: 06-28-2008, 08:30 PM
  4. Speed of pointers vs. speed of arrays/structs
    By Kempelen in forum C Programming
    Replies: 32
    Last Post: 06-27-2008, 10:16 AM
  5. pattern replace using pointers
    By Tintu in forum C Programming
    Replies: 7
    Last Post: 03-05-2008, 04:21 AM