Search code examples
image-processingassemblyx86masm32

Bitmap image processing with masm32


I am working with masm 32. I want to process bitmap images. But I don't know how to get an image or save an image. I have no idea about processing images with masm. Any information will help me. Thanks


Solution

  • This might get you started... This is basic code to load in a bitmap and display it on a dialog box. I'm not sure what mean by 'processing' bitmap images, I suspect you need something more complex. However, this might get you started...

    .386
    .model flat,stdcall
    option casemap:none
    
    includelib user32.lib
    includelib kernel32.lib
    includelib shell32.lib
    includelib comctl32.lib
    includelib comdlg32.lib
    includelib gdi32.lib
    
    WinMain             PROTO :DWORD,:DWORD,:DWORD,:DWORD
    WndProc             PROTO :DWORD,:DWORD,:DWORD,:DWORD
    
    IDD_DIALOG          equ 1000
    
    IDC_BITMAP          equ 100
    
    IDM_MENU            equ 10000
    
    .const
    
    ClassName           db 'DLGCLASS',0
    
    
    .data?
    
    hInstance           dd ?
    CommandLine         dd ?
    hWnd                dd ?
    hBitmap             dd ?
    
    
    
    .code
    
    start:
    
        invoke GetModuleHandle,NULL
        mov    hInstance,eax
        invoke GetCommandLine
        invoke InitCommonControls
        mov     CommandLine,eax
        invoke WinMain,hInstance,NULL,CommandLine,SW_SHOWDEFAULT
        invoke ExitProcess,eax
    
    WinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:DWORD
        LOCAL   wc:WNDCLASSEX
        LOCAL   msg:MSG
    
        mov     wc.cbSize,sizeof WNDCLASSEX
        mov     wc.style,CS_HREDRAW or CS_VREDRAW
        mov     wc.lpfnWndProc,offset WndProc
        mov     wc.cbClsExtra,NULL
        mov     wc.cbWndExtra,DLGWINDOWEXTRA
        push    hInst
        pop     wc.hInstance
        mov     wc.hbrBackground,COLOR_BTNFACE+1
        mov     wc.lpszMenuName, NULL
        mov     wc.lpszClassName,offset ClassName
        invoke LoadIcon,NULL,IDI_APPLICATION
        mov     wc.hIcon,eax
        mov     wc.hIconSm,eax
        invoke LoadCursor,NULL,IDC_ARROW
        mov     wc.hCursor,eax
        invoke RegisterClassEx,addr wc
        invoke CreateDialogParam,hInstance,IDD_DIALOG,NULL,addr WndProc,NULL
        invoke ShowWindow,hWnd,SW_SHOWNORMAL
        invoke UpdateWindow,hWnd
        .while TRUE
            invoke GetMessage,addr msg,NULL,0,0
          .BREAK .if !eax
            invoke TranslateMessage,addr msg
            invoke DispatchMessage,addr msg
        .endw
        mov     eax,msg.wParam
        ret
    
    WinMain endp
    
    WndProc proc hWin:HWND,uMsg:UINT,wParam:WPARAM,lParam:LPARAM
        LOCAL   ps:PAINTSTRUCT
        LOCAL   hdc:HDC
        LOCAL   hMemDC:HDC
        LOCAL   rect:RECT
    
        mov     eax,uMsg
        .if eax==WM_INITDIALOG
            push    hWin
            pop     hWnd
    
            ; Load up the bitmap
            invoke  LoadBitmap, hInstance, IDC_BITMAP
            mov     hBitmap, eax
    
        .elseif eax==WM_COMMAND
            mov     eax,wParam
            mov     edx, eax
            shr     edx, 16
            .if lParam==0 
                .if eax==IDM_FILE_EXIT
                    invoke SendMessage,hWin,WM_CLOSE,0,0
                .elseif eax==IDM_HELP_ABOUT
                    invoke ShellAbout,hWin,addr AppName,addr AboutMsg,NULL
                .endif
            .else
    
            .endif
    
        .elseif eax==WM_PAINT
            invoke  BeginPaint, hWnd, addr ps
            mov     hdc, eax
            invoke  CreateCompatibleDC, hdc
            mov     hMemDC, eax
            invoke  SelectObject, hMemDC, hBitmap
            invoke  GetClientRect, hWnd, addr rect
            invoke  BitBlt, hdc, 10, 10, rect.right, rect.bottom, hMemDC, 0, 0, SRCAND
            invoke  DeleteDC, hMemDC
            invoke  EndPaint, hWnd, addr ps
        .elseif eax==WM_CLOSE
            invoke DestroyWindow,hWin
        .elseif uMsg==WM_DESTROY
            invoke  DeleteObject, hBitmap
            invoke PostQuitMessage,NULL
        .else
            invoke DefWindowProc,hWin,uMsg,wParam,lParam
            ret
        .endif
        xor    eax,eax
        ret
    
    WndProc endp
    
    end start
    

    Note the LoadBitmap API call and the WM_PAINT routine.

    ..Forgot the .rc file...

    #define IDD_DIALOG 1000
    
    #define IDC_BITMAP 100
    
    
    IDC_BITMAP BITMAP DISCARDABLE "myfile.bmp"