News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

how use getdibbits

Started by costy35, March 11, 2010, 10:43:39 AM

Previous topic - Next topic

costy35

I give my getdibbits error function and not know where the mistake!

.Const

.Data?

.Data
lpProc DD 0
library_name DB "avicap32.dll", 0
hLib DD 0
proc_name DB "capCreateCaptureWindowW", 0
_camtitle DB "FASMWEBCAM", 0
hWndC DD 0
nDevice DD 0
nFPS DD 25
sr DD 0
nx DD 100
ny DD 100
sr_kopya DD 0
bmp_kopya DD 0
bi BITMAPINFO <>
costy DB 1000 Dup(0)
hdc DD 0
hMemory DD 0
pbmfh DD 0
.Code

Window1Procedure Proc Private hWnd:HWND, uMsg:ULONG, wParam:WPARAM, lParam:LPARAM
   .If uMsg == WM_CREATE
      Invoke GetDlgItem, hWnd, IDC_WINDOW1_PICTURE1
      Invoke GetDC, Eax
      Mov hdc, Eax

      Invoke LoadLibrary, Addr library_name
              Mov Ecx, Eax
                Invoke GetProcAddress, Eax, Addr proc_name
                Mov lpProc, Eax
                Invoke FreeLibrary, Ecx
               Push 0
            Push hWnd
            Push 144
            Push 176
            Push 10
            Push 10
            Push WS_VISIBLE Or WS_CHILD
            Push Offset _camtitle
            Call lpProc
            Mov hWndC, Eax
            Invoke GetDC, hWndC
            Mov sr, Eax
               Invoke SendMessage, hWndC, WM_CAP_DRIVER_CONNECT, nDevice, 0
                   Invoke SendMessage, hWndC, WM_CAP_SET_SCALE, TRUE, 0
               Invoke SendMessage, hWndC, WM_CAP_SET_PREVIEWRATE, nFPS, 0
               Invoke SendMessage, hWndC, WM_CAP_SET_PREVIEW, TRUE, 0
                   Invoke SendMessage, hWndC, WM_CAP_DLG_VIDEOFORMAT, nDevice, 0

   Invoke SetTimer, hWnd, 1, 1, 0
         .ElseIf uMsg == WM_TIMER
Invoke BitBlt, hdc, 0, 0, 150, 150, sr, 10, 10, SRCCOPY
         .ElseIf uMsg == WM_COMMAND
            .If wParam == IDC_WINDOW1_BUTTON1
               Invoke GlobalAlloc, GMEM_MOVEABLE Or GMEM_ZEROINIT, 1000
        Mov hMemory, Eax         ; Handle to memory
   
        invoke GlobalLock,hMemory
        mov  pbmfh,eax            ; got pointer to BITMAPFILEHEADER

Invoke CreateCompatibleDC, hdc
Mov sr_kopya, Eax
Invoke CreateCompatibleBitmap, sr_kopya, nx, ny
  Mov bmp_kopya, Eax
Invoke GetDIBits, sr_kopya, bmp_kopya, 0, 1, pbmfh, Offset bi, NULL
.If !Eax
   Invoke MessageBox, 0, 0, 0, 0
   .EndIf
            .EndIf
         .ElseIf uMsg == WM_CLOSE
      Invoke IsModal, hWnd
      .If Eax
         Invoke EndModal, hWnd, IDCANCEL
         Return TRUE
      .EndIf
   .EndIf
   Return FALSE
Window1Procedure EndP

qWord

Invoke LoadLibrary, Addr library_name
Mov Ecx, Eax
Invoke GetProcAddress, Eax, Addr proc_name
Mov lpProc, Eax
Invoke FreeLibrary, Ecx

This construction make not much sense! The only reason why it works is using ECX for saving the module handle (which will be overwritten by GetProcAddress), so that an invalid handle is passed to Freelibrary - the dll is not unloaded (before any use!).

Here an example for using GetDIBits:
include masm32rt.inc
...
    ; 3 things are needed:
    ; hBmp     = handle to bitmap
    ; bmWidth  = width of bitmap (you can use GetObject() to obtain this information)
    ; bmHeight = hight of bitmap

    .data?
        bi      BITMAPINFO <>
        pBits   DWORD ?
        hdc     HDC ?
    .code
   
    mov hdc,rv(CreateCompatibleDC,rv(GetDC,0)) ; create some DC (this one is compatibel to desktop-DC)
   
    mov edx,bmWidth     ; width of bitmap
    mov eax,bmHeight    ; hight of bitmap

    mov bi.bmiHeader.biSize,SIZEOF BITMAPINFO
    mov bi.bmiHeader.biWidth,edx
    mov bi.bmiHeader.biHeight,eax
    mov bi.bmiHeader.biPlanes,1
    mov bi.bmiHeader.biCompression,BI_RGB
    mov bi.bmiHeader.biBitCount,32          ; 4 byte per pixel
    mov bi.bmiHeader.biSizeImage,0
    mov bi.bmiHeader.biXPelsPerMeter,0
    mov bi.bmiHeader.biYPelsPerMeter,0
    mov bi.bmiHeader.biClrUsed,0
    mov bi.bmiHeader.biClrImportant,0
   
    mul edx         ; bmWidth*bmHeight
    lea edx,[eax*4] ; calc buffer size

    mov pBits,rv(GlobalAlloc,GPTR,edx)
   
    ; write the Bits to buffer (pBits)
    invoke GetDIBits,hdc,hBmp,0,bmHeight,pBits,OFFSET bi,DIB_RGB_COLORS


BTW: welcome to forum
FPU in a trice: SmplMath
It's that simple!

costy35

Thanks for the reply! I did what you said, but Getdibbits not write anything in the buffer ! Where I wrong again?Thanks!


include \masm32\include\masm32rt.inc
include \masm32\include\debug.inc
includelib \masm32\lib\debug.lib


    ; 3 things are needed:
    ; hBmp     = handle to bitmap
    ; bmWidth  = width of bitmap (you can use GetObject() to obtain this information)
    ; bmHeight = hight of bitmap

    .data?
        bi      BITMAPINFO <>
        pBits   DWORD ?
        hdc     HDC ?
        hbmp    DWORD ?
    .data
        bmWidth  dd 100
        bmHeight dd 100
    .code
    start:
    mov hdc,rv(CreateCompatibleDC,rv(GetDC,0)) ; create some DC (this one is compatibel to desktop-DC)
   
    mov hbmp,rv(CreateCompatibleBitmap,hdc,bmWidth,bmWidth)

    mov edx,bmWidth     ; width of bitmap
    mov eax,bmHeight    ; hight of bitmap

    mov bi.bmiHeader.biSize,SIZEOF BITMAPINFO
    mov bi.bmiHeader.biWidth,edx
    mov bi.bmiHeader.biHeight,eax
    mov bi.bmiHeader.biPlanes,1
    mov bi.bmiHeader.biCompression,BI_RGB
    mov bi.bmiHeader.biBitCount,32          ; 4 byte per pixel
    mov bi.bmiHeader.biSizeImage,0
    mov bi.bmiHeader.biXPelsPerMeter,0
    mov bi.bmiHeader.biYPelsPerMeter,0
    mov bi.bmiHeader.biClrUsed,0
    mov bi.bmiHeader.biClrImportant,0
   
    mul edx         ; bmWidth*bmHeight
    lea edx,[eax*4] ; calc buffer size

    mov pBits,rv(GlobalAlloc,GPTR,edx)
   
    ; write the Bits to buffer (pBits)
    invoke GetDIBits,hdc,hbmp,0,bmHeight,pBits,OFFSET bi,DIB_RGB_COLORS

DumpMem [pBits], 100
invoke ExitProcess, 0
END start

qWord

mov hbmp,rv(CreateCompatibleBitmap,hdc,bmWidth,bmWidth)
What did you expected to get by this? -> you get an black bitmap (each pixel has the value 0). Also it is an monochrome bitmap - use the desktop DC as referenc instead of hdc
FPU in a trice: SmplMath
It's that simple!

costy35

#4
...use the desktop DC as referenc instead of hdc... :'(
I do not understand where I wrong!
I changed this:
    mov hdc,rv(GetDC,rv(GetDesktopWindow))
and still not working!
Where can I find an example?
I have read many tutorials, I'm doing less with asm, but not fully understand these functions.my job is carpenter. :dance:
I want to do a CNC controlled with webcam.but I saw that with win32 is difficult access to bitmap point array.
I already have built one...http://www.youtube.com/bobyca2003
thanks again!

qWord

When creating an DC with CreateCompatibleDC(), you get an DC with default bitmap,brush,pen... The bitmap is then an monochrome (1Bit: black or white). When using this DC for CreateCompatibleBitmap(), you get also an Monochrome Bitmap - that why I've said to use the desktop DC (which has commonly a 32 or 16 Bit Bitmap).
The example code you've post works! You simply get an array of zero filled DWORD's.

Look at this thread, there are some full working examples on using webcam - I'm sure, that what your are looking for. For further questions pleas first use the forum search and then, if doesn't found the answer, open an new thread in the The Campus or The Workshop - there you will get more feedback than in this IDE-specific subforum. 
FPU in a trice: SmplMath
It's that simple!