Author Topic: GetBitmapBits  (Read 5939 times)


  • Moderator
  • Member
  • *****
  • Posts: 1449
  • Assembly is a state of art.
    • RosAsm
« on: September 16, 2013, 03:23:47 AM »
This is a variation of GetBitmapBits function. The parameters and functionality are the same ones as in GDI file

Code: [Select]
; BITMAPINFO structure
; necessary to obtain the pixels of the bitmap
 OutBmpNfo.bmiHeader.biSize: D$ len
 OutBmpNfo.bmiHeader.biWidth: D$ 0
 OutBmpNfo.bmiHeader.biHeight: D$ 0
 OutBmpNfo.bmiHeader.biPlanes: W$ 0
 OutBmpNfo.bmiHeader.biBitCount: W$ 0
 OutBmpNfo.bmiHeader.biCompression: D$ 0
 OutBmpNfo.bmiHeader.biSizeImage: D$ 0
 OutBmpNfo.bmiHeader.biXPelsPerMeter: D$ 0
 OutBmpNfo.bmiHeader.biYPelsPerMeter: D$ 0
 OutBmpNfo.bmiHeader.biClrUsed: D$ 0
 OutBmpNfo.bmiHeader.biClrImportant: D$ 0
 OutBmpNfo.bmiColors.rgbBlue: B$ 0
 OutBmpNfo.bmiColors.rgbGreen: B$ 0
 OutBmpNfo.bmiColors.rgbRed: B$ 0
 OutBmpNfo.bmiColors.rgbReserved: B$ 0]

[Size_of_BITMAPINFO 44]

Proc GetBitmapBits:
    Arguments @hbmp, @cbBuffer, @lpvBits
    Local @byPixel, @hdcImage, @MemData
    Structure @BITMAP 24, @BITMAP.bmTypeDis 0, @BITMAP.bmWidthDis 4, @BITMAP.bmHeightDis 8, @BITMAP.bmWidthBytesDis 12, @BITMAP.bmPlanesDis 16,
              @BITMAP.bmBitsPixelDis 18, @BITMAP.bmBitsDis 20
    Uses ebx, esi, edi, ecx, edx

    ; Allocate the necessary amount of memory to hold the BitMapBits
    lea eax D@MemData | mov D@MemData 0
    call 'RosMem.VMemAlloc' eax, D@cbBuffer | mov D@byPixel eax
    If eax = &NULL
        ; Not enough memory
        xor eax eax

    call 'RosMem.ZeroMemory' OutBmpNfo, Size_Of_BITMAP
    call 'GDI32.GetObjectA' D@hbmp, Size_Of_BITMAP, D@BITMAP ; get informations about the bitmap
    On eax = 0, ExitP ; On error, exit

    ;A bit of information is necessary for GetDIBits to work. So starty filling only what is needed.
    mov D$OutBmpNfo.bmiHeader.biSize Size_of_BITMAPINFO
    move D$OutBmpNfo.bmiHeader.biWidth D@BITMAP.bmWidthDis

    move D$OutBmpNfo.bmiHeader.biHeight D@BITMAP.bmHeightDis

    mov W$OutBmpNfo.bmiHeader.biPlanes 1
    mov eax D@BITMAP.bmBitsPixelDis
    mov W$OutBmpNfo.bmiHeader.biBitCount ax
    mov D$OutBmpNfo.bmiHeader.biCompression &BI_RGB ; compression (no)

    call 'GDI32.CreateCompatibleDC' &NULL | mov D@hdcImage eax
    call 'GDI32.GetDIBits' eax, D@hbmp, 0, D$OutBmpNfo.bmiHeader.biHeight, D@byPixel, OutBmpNfo, &DIB_PAL_COLORS ; obtain the image from HBITMAP
    call 'GDI32.DeleteDC' D@hdcImage

    call 'RosMem.memcpy' D@lpvBits, D@byPixel, D@cbBuffer
    ; release the allocated memory
    call 'RosMem.VMemFree' D@byPixel
    mov eax &TRUE


Note: the function VMemAlloc/VmemFree are only a customized virtualmemory allocation/deallocation inside RosAsm, that i made in the form of a dll (I´m trying to rebuild RosAsm with separated tools) instead forcing it to be on a huge monoblock file.

This function i wrote it as:
Code: [Select]
Proc VMemAlloc::
    Arguments @pMem, @MemSize
    Local @Result

    mov D@Result 0
        VirtualAlloc D@pMem, D@MemSize
        mov D@Result eax

    mov eax D@Result


Code: [Select]
Proc VMemFree::
    Arguments @pMem
    Local @Result

    mov D@Result 0
        VirtualFree D@pMem
        If eax = 0
            mov D@Result &FALSE
            mov D@Result &TRUE

    mov eax D@Result


VirtualAlloc and VirtualFree are the internal macros in RosAsm to handle Virtual Memory. You can find them in the last updated version
Coding in Assembly requires a mix of:
80% of brain, passion, intuition, creativity
10% of programming skills
10% of alcoholic levels in your blood.

My Code Sites: