Announcement

Collapse
No announcement yet.

Problems with Bitmap

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problems with Bitmap

    I am creating a bitmap that I can directly edit using this code:

    Code:
      dBitmapDC=CreateCompatibleDC(%NULL)
    
      dBitmapInfo.bmiHeader.biSize=SIZEOF(dBitmapInfo.bmiHeader)
      dBitmapInfo.bmiHeader.biWidth=160
      dBitmapInfo.bmiHeader.biHeight=144
      dBitmapInfo.bmiHeader.biPlanes=1
      dBitmapInfo.bmiHeader.biBitCount=16
      dBitmapInfo.bmiHeader.biCompression=%BI_BITFIELDS
      dBitmapInfo.bmiHeader.biSizeImage=0
      dBitmapInfo.bmiHeader.biXPelsPerMeter=0
      dBitmapInfo.bmiHeader.biYPelsPerMeter=0
      dBitmapInfo.bmiHeader.biClrUsed=0
      dBitmapInfo.bmiHeader.biClrImportant=0
    
      FOR i%=0 TO 256
          dBitmapInfo.bmiColors(i%).rgbBlue=&h001F
          dBitmapInfo.bmiColors(i%).rgbGreen=&h07E0
          dBitmapInfo.bmiColors(i%).rgbRed=&hF800
      NEXT i%
    
      dBitmap = CreateDIBSection(dBitmapDC,dBitmapInfo,%DIB_RGB_COLORS,PIXELS,%NULL,%NULL)
      SelectObject hWndDC, dBitmap
    
      DIM REALPIXELS(160*144) AS WORD AT PIXELS
    PIXELS is a LONG type.

    When I assign a value to an element in REALPIXELS, my program crashes with a write error fault. Any pointers on how to get this to work will be greatly appreciated.

  • #2
    Hi.
    Part of my working code show the whole situation
    ...
    %XP = 4250
    %YP = 7080

    GLOBAL LPBIT AS LONG 'DIB SECTION
    GLOBAL BI AS BITMAPINFO ' ÎÑÍÎÂÍÎÅ ÎÊÍÎ
    ...
    hmem = GlobalAlloc(%GMEM_FIXED,%XP*%YP)
    VARPIX = GlobalLock(hmem)
    DIM PIXX(1:1063) AS GLOBAL RECORD AT VARPIX
    ...
    'ÌÀÑÑÈÂ ÖÂÅÒÎÂÛÕ

    HDC1=GETDC(HCTL1)
    hMemDC1 = CreateCompatibleDC(hDC1)
    ! call palette
    HBMP1=CreateDIBSECTION(HMEMDC1,BI,%DIB_RGB_COLORS,LPBIT,0,0)
    Selectobject HMEMDC1,hbmp1
    .....
    SUB PALETTE
    ! MOV EDX,OFFSET BI
    ! ADD EDX,40
    ! MOV EBX, OFFSET PALDATA
    ! MOV ECX,16
    MPALDAT:
    ! MOV AL,[EBX]
    ! MOV [EDX],AL
    ! MOV AL,[EBX+16]
    ! MOV [EDX+1],AL
    ! MOV AL,[EBX+32]
    ! MOV [EDX+2],AL
    ! ADD EDX,4
    ! INC EBX
    ! LOOP MPALDAT
    BI.BMIHEADER.BISIZE = 40
    BI.BMIHEADER.BIWIDTH = %X_DL
    BI.BMIHEADER.BIHeight = %Y_DL
    BI.BMIHEADER.BIPLANES = 1
    BI.BMIHEADER.BIBITCOUNT = 4
    EXIT SUB
    PALDATA:
    ! DB 0, 0, 0, 76, 0, 0,241,151,239,155,247,107,177,147,182,255
    ! DB 0, 0, 0, 63, 0, 0,218,160,157,172,239,173,238,206,251,255
    ! DB 0, 0, 0,239, 0, 0,160,107,157,198,214,222,168,217,244,255
    END SUB

    ...

    SUB VIDEO
    ! PUSHA
    ! MOV ECX,VARPIX
    ! ADD ECX,REALXPARAM
    ! ADD ECX,%XPM
    ! MOV EAX,%XP
    ! MOV EBX,EAX
    ! MUL REALYPARAM
    ! ADD EAX,ECX
    ! MOV EDI,LPBIT ;<<<<<<<<<<<<<<<<<<<<<<
    ! MOV EDX,%Y_DL
    ! MOV ECX,124
    ! MOVD MM1,ECX
    XXX2:
    ! MOVD ECX,MM1
    ! MOV ESI,EAX
    ! REP MOVSD
    ! SUB EAX,EBX
    ! DEC EDX
    ! CMP EDX,0
    ! JNZ XXX2
    ! POPA
    ! EMMS
    BITBLT HDC1,2,2,%X_DL,%Y_DL,HMEMDC1,0,0,%SRCCOPY
    ...

    END SUB

    ------------------------------

    V.Shulakov



    ------------------
    Spherical Panorama Inc. Virtual Reality for: Real Estate, Tourism Advertisment.

    Comment


    • #3
      I tried the example you gave me but it didn't seem to work. Then I removed the DIM statement in my code and replaced it with the following to try to clear out the buffer:

      Code:
        ! xor EAX,EAX
        ! mov EDI,PIXELS
        ! mov ECX,160*144
        ! STOSW
        ! REP STOSW
      Sure enough it crashed. I then ran it through a debugger and got this dump:

      Code:
      Unhandled exception in OUTPUT.EXE: 0xC0000005: Access Violation.
      
      EAX = 00000000 EBX = 0040A3DC
       ECX = 00005A00 EDX = FFFFFFFF
       ESI = 00000000 EDI = 00000000
       EIP = 00404244 ESP = 0012FDD4
       EBP = 0012FF1C EFL = 00000246 CS = 001B
       DS = 0023 ES = 0023 SS = 0023 FS = 0038
       GS = 0000 OV=0 UP=0 EI=1 PL=0 ZR=1 AC=0
       PE=1 CY=0
       ST0 = 1#SNAN                    
       ST1 = +0.00000000000000000e+0000
       ST2 = +5.65422586806991384e+0315
       ST3 = +0.00000000000000000e+0000
       ST4 = +0.00000000000000000e+0000
       ST5 = +1.00000000000000000e+0008
       ST6 = +1.00000000000000000e+0018
       ST7 = +0.00000000000000000e+0000
       CTRL = 137F STAT = 0000 TAGS = FFFF
       EIP = 00000000 CS = 0000 DS = 0000
       EDO = 00000000
      
      00404237   xor         eax,eax
      00404239   mov         edi,dword ptr ds:[40A3C4h]
      0040423F   mov         ecx,5A00h
      00404244   stos        word ptr [edi] <--- The crash happened here.
      00404246   rep stos    word ptr [edi]
      If it helps any, I'm running Windows 2000 on an AMD K6-2 with 128 MB of ram.

      Comment


      • #4
        FukuSuizui.
        1.My pieces of a code - not an example. My pieces of a code - pieces checked up - working program - for show the whole
        situation!.
        2.I think - You try to make record for a wrong place of memory

        3.I don't use !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        >>>>>>>> DIM REALPIXELS(160*144) AS WORD AT PIXELS
        for
        dBitmap = CreateDIBSection(dBitmapDC,dBitmapInfo,%DIB_RGB_COLORS,PIXELS,%NULL,%NULL)
        ========================================

        My ->
        hmem = GlobalAlloc(%GMEM_FIXED,%XP*%YP)
        VARPIX = GlobalLock(hmem)
        DIM PIXX(1:1063) AS GLOBAL RECORD AT VARPIX
        use - only for "speed" disk file !!!!!!!!!!!!!!!!!!!!!!!!

        4.After opening DIBSECTION, I use first function VIDEO,
        I don't use You variant - DIM REALPIXELS(160*144) AS WORD AT PIXELS !

        5.Look more attentive my first message and find DIBSECTION in
        Forum. I remember some messages on this theme!


        ---------------------------------------------------------
        V.Shulakov



        ------------------
        Spherical Panorama Inc. Virtual Reality for: Real Estate, Tourism Advertisment.

        Comment


        • #5
          Hello FukuSuizui,

          Is this what your looking for? I included two inline assembly routines to draw pixels and lines. This could be modified very easily if you would rather access the data with an PB array.

          Code:
          #compile exe
          #register none
          #include "win32api.inc"
          
          
          %BITMAP_WIDTH           = 640
          %BITMAP_HIGHT           = 480
          %BITMAP_BITS            = 32
          
          type RGBALPHA
            r                     as byte
            g                     as byte
            b                     as byte
            a                     as byte
          end type
          
          
          function WinMain(byval hinst as long, byval hprevinst as long, argv as asciiz ptr, byval flags as long) as long
            dim classname         as local asciiz * 257
            dim wndclass          as local WNDCLASSEX
            dim hwnd              as local long
            dim msg               as local TAGMSG
          
          
            classname = "demo32"
            wndclass.cbsize = sizeof(wndclass)
            wndclass.style = %CS_HREDRAW or %CS_VREDRAW
            wndclass.lpfnwndproc = codeptr(wndproc)
            wndclass.cbclsextra = 0
            wndclass.cbwndextra = 0
            wndclass.hinstance = hinst
            wndclass.hicon = LoadIcon(%NULL, byval %IDI_APPLICATION)
            wndclass.hiconsm = LoadIcon(%NULL, byval %IDI_APPLICATION)
            wndclass.hcursor = LoadCursor(%NULL, byval %IDC_ARROW)
            wndclass.hbrbackground = %COLOR_APPWORKSPACE
            wndclass.lpszclassname = varptr(classname)
            wndclass.lpszmenuname = %NULL
          
          
            if (RegisterClassEx(wndclass) = %FALSE) then
              msgbox "Unable to register window class", %MB_OK, "system message"
              exit function
            end if
          
          
            hwnd = CreateWindow(classname, "demo", %WS_OVERLAPPED or %WS_SYSMENU, %CW_USEDEFAULT, %CW_USEDEFAULT, %BITMAP_WIDTH, %BITMAP_HIGHT, %NULL, %NULL, hinst, byval %NULL)
          
            call ShowWindow(hwnd, flags)
            call UpdateWindow(hwnd)
          
            do while GetMessage(msg, %NULL, 0, 0)
              call TranslateMessage(msg)
              call DispatchMessage(msg)
            loop
          
            function = msg.wParam
          end function
          
          function WndProc(byval hwnd as long, byval wmsg as long, byval wParam as long, byval lParam as long) export as long
            dim hmemory       as static long
            dim bitmap        as static dword
            dim bminfo        as static BITMAPINFO
            dim index         as local long
            dim ps            as local PAINTSTRUCT
          
          
            select case wmsg
              case %WM_CREATE
                hmemory = GlobalAlloc(%GMEM_FIXED or %GMEM_ZEROINIT, %BITMAP_WIDTH * %BITMAP_HIGHT * %BITMAP_BITS / 8)
          
                if hmemory = %NULL then
                  msgbox "Unable to allocate memory for screen buffer", %MB_OK, "system message"
                  call PostMessage(hwnd, %WM_CLOSE, %NULL, %NULL)
                else
                  bitmap = GlobalLock(hmemory)
          
                  bminfo.bmiHeader.biSize = sizeof(bminfo.bmiHeader)
                  bminfo.bmiHeader.biWidth = %BITMAP_WIDTH
                  bminfo.bmiHeader.biheight = -%BITMAP_HIGHT
                  bminfo.bmiHeader.biPlanes = 1
                  bminfo.bmiHeader.biBitCount = 32
                  bminfo.bmiHeader.biCompression = %BI_RGB
                  bminfo.bmiHeader.biSizeImage = %NULL
                  bminfo.bmiHeader.biXPelsPerMeter = %NULL
                  bminfo.bmiHeader.biYPelsPerMeter = %NULL
                  bminfo.bmiHeader.biClrUsed = %NULL
                  bminfo.bmiHeader.biClrImportant = %NULL
          
          
                end if
          
          
              case %WM_ERASEBKGND
                rem process this to get rid of flickering
                
              case %WM_LBUTTONDOWN
                call PostMessage(hwnd, %WM_CLOSE, %NULL, %NULL)
          
          
              case %WM_PAINT
                call BeginPaint(hwnd, ps)
                call SetDIBitsToDevice(ps.hdc, 0, 0, %BITMAP_WIDTH, %BITMAP_HIGHT, 0, 0, 0, %BITMAP_HIGHT, byval bitmap, bmInfo, %DIB_RGB_COLORS)
                call EndPaint(hwnd, ps)
          
                for index = 0 to 255
                  call drawline(bitmap, int(%BITMAP_WIDTH*rnd), int(%BITMAP_HIGHT*rnd), int(%BITMAP_WIDTH*rnd), int(%BITMAP_HIGHT*rnd), rgb(255*rnd,255*rnd,255*rnd))
                next index
          
                call InvalidateRect(hwnd, byval %NULL, %FALSE)
                     
          
              case %WM_DESTROY
                call GlobalUnlock(hmemory)
                call GlobalFree(hmemory)
                call PostQuitMessage(0)
          
          
              case else
                function = DefWindowProc(hwnd, wmsg, wParam, lParam)
          
            end select
          end function
          
          function putpixel(byval buffer as dword, byval x as long, byval y as long, byval c as long) as long
            asm pushad
            asm mov       edi,buffer
            asm mov       eax,y
            asm mov       ebx,%BITMAP_WIDTH
            asm mul       ebx
            asm add       eax,x
            asm shl       eax,2
            asm add       edi,eax
            asm mov       eax,c
            asm mov       [edi],eax
            asm popad
          end function
          
          function drawline(byval buffer as dword, byval x1 as long, byval y1 as long, byval x2 as long, byval y2 as long, byval color as long) as long
            dim deltax    as local long
            dim deltay    as local long
            dim halfx     as local long
            dim halfy     as local long
            dim count     as local long
          
            asm pushad
            asm mov       eax,y2
            asm sub       eax,y1
            asm mov       esi,1
            asm jge       dline1
            asm mov       esi,-1
            asm neg       eax
          dline1:
            asm mov       deltay,eax
            asm mov       eax,x2
            asm sub       eax,x1
            asm mov       edi,1
            asm jge       dline2
            asm mov       edi,-1
            asm neg       eax
          dline2:
            asm mov       deltax,eax
            asm cmp       eax,deltay
            asm jl        dline3
            asm mov       eax,deltax
            asm shr       eax,1
            asm mov       halfx,eax
            asm mov       ecx,x1
            asm mov       edx,y1
            asm xor       ebx,ebx
            asm mov       eax,deltax
            asm mov       count,eax
          dline5: 'draw pixel here ecx = x edx = y
            asm pushad
            asm mov       edi,buffer
            asm mov       eax,edx
            asm mov       ebx,%BITMAP_WIDTH
            asm mul       ebx
            asm add       eax,ecx
            asm shl       eax,2
            asm add       edi,eax
            asm mov       eax,color
            asm mov       [edi],eax
            asm popad
          
            asm add       ecx,edi
            asm add       ebx,deltay
            asm cmp       ebx,halfx
            asm jle       dline6
            asm sub       ebx,deltax
            asm add       edx,esi
          dline6:
            asm dec       count
            asm jge       dline5
            asm jmp       dline4
          dline3:
            asm mov       eax,deltay
            asm shr       eax,1
            asm mov       halfy,eax
            asm mov       ecx,x1
            asm mov       edx,y1
            asm xor       ebx,ebx
            asm mov       eax,deltay
            asm mov       count,eax
          dline7: 'draw pixel here ecx = x edx = y
            asm pushad
            asm mov       edi,buffer
            asm mov       eax,edx
            asm mov       ebx,%BITMAP_WIDTH
            asm mul       ebx
            asm add       eax,ecx
            asm shl       eax,2
            asm add       edi,eax
            asm mov       eax,color
            asm mov       [edi],eax
            asm popad
          
            asm add       edx,esi
            asm add       ebx,deltax
            asm cmp       ebx,halfy
            asm jle       dline8
            asm sub       ebx,deltay
            asm add       ecx,edi
          dline8:
            asm dec       count
            asm jge       dline7
          dline4:
            asm popad
          end function
          ------------------
          Cheers

          Comment

          Working...
          X