News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Movement detection source code

Started by Farabi, August 15, 2013, 04:33:10 PM

Previous topic - Next topic

Farabi

I had problem with scanobject functions, I think I messed up the stack again. It was worked before, but now it hanged. I donwloading the remover for DrWatson debugger, and then it stopped working. I guess it was had a bug, but I did not noticed it. Here it is the source code.


mAlloc proc uses esi edi nSize:dword

add nSize,4
invoke GlobalAlloc,GMEM_ZEROINIT or GMEM_FIXED,nSize
.if eax==0
invoke MessageBox,NULL,CADD("Memory Allocation Error"),NULL,MB_OK
xor eax,eax
.endif

ret
mAlloc endp

fNewLayer proc uses esi lpLayerStruct:dword,_width:dword,height:dword

mov esi,lpLayerStruct
invoke CreateCompatibleDC,0
.if eax==0
invoke MessageBox,0,0,0,0
.endif
mov [esi].fLayer.DC,eax
invoke CreateIndependantBitmap,eax,_width,height
mov [esi].fLayer.bmp,eax
invoke SelectObject,[esi].fLayer.DC,[esi].fLayer.bmp
invoke DeleteObject,eax


ret
fNewLayer endp

ScanEdgeXLeft proc uses esi edi lpbits:dword,x:dword,y:dword,_width:dword,height:dword,rslt:dword
LOCAL tx:dword
mov esi,rslt

mov ecx,x
scan_left:
push ecx
push eax
mov eax,ecx
mov tx,eax
invoke GetDIBPixel,tx,y,lpbits,_width,height
.if eax==0
push tx
pop dword ptr[esi]
push y
pop dword ptr[esi+4]
pop ecx
jmp done
.endif
pop eax
pop ecx
dec ecx
cmp ecx,0
jg scan_left
mov dword ptr[esi],0
mov dword ptr[esi+4],0
done:

ret
ScanEdgeXLeft endp

ScanEdgeXRight proc uses esi edi lpbits:dword,x:dword,y:dword,_width:dword,height:dword,rslt:dword
LOCAL tx:dword
mov esi,rslt

mov eax,_width
sub eax,x
xor ecx,ecx
scan_left:
push ecx
push eax
mov eax,x
add eax,ecx
mov tx,eax
invoke GetDIBPixel,tx,y,lpbits,_width,height
.if eax==0
push tx
pop dword ptr[esi]
push y
pop dword ptr[esi+4]
pop ecx
jmp done
.endif
pop eax
pop ecx
inc ecx
cmp ecx,eax
jl scan_left
mov dword ptr[esi],0
mov dword ptr[esi+4],0
done:

ret
ScanEdgeXRight endp

ScanObject proc USES esi edi ebx ecx edx  lpbits:dword,x:dword,y:dword,_width:dword,height:dword,rslt:dword
LOCAL pl:POINT
LOCAL pr:POINT
LOCAL pc:POINT
LOCAL buff[256]:dword


invoke ScanEdgeXLeft,lpbits,x,y,_width,height,addr pl
invoke ScanEdgeXRight,lpbits,x,y,_width,height,addr pr

mov esi,rslt
; This is for X1
mov eax,[esi]
mov ecx,pl.x
.if eax>ecx
mov [esi],ecx
.endif

mov ecx,y
mov [esi+4],ecx

mov eax,[esi+8]
mov ecx,pr.x
.if eax<ecx
mov dword ptr[esi+8],ecx
.endif

mov ecx,pl.x
loop_next:
push ecx
mov edx,y
inc edx

push ecx
pop pc.x
push edx
pop pc.y

invoke GetDIBPixel,pc.x,pc.y,lpbits,_width,height
.if eax!=0
mov ecx,rslt
;invoke SetDIBPixel,x,y,lpbits,640,480,0
invoke ScanObject,lpbits,pc.x,pc.y,_width,height,rslt
mov eax,pc.y
mov [esi+12],eax
pop ecx
jmp done
.endif
pop ecx
inc ecx
cmp ecx,pr.x
jl loop_next
done:

ret
ScanObject endp

CmpPix proc uses esi edi ecx  pix1:dword,pix2:dword,tol:dword
LOCAL r3,g3,b3,r4,g4,b4:dword
LOCAL rr,rg,rb:dword

mov eax,pix1
mov edx,pix2

movzx ecx,al
mov r3,ecx
shr eax,8
movzx ecx,al
mov g3,ecx
shr eax,8
movzx ecx,al
mov b3,ecx

movzx ecx,dl
mov r4,ecx
shr edx,8
movzx ecx,dl
mov g4,ecx
shr edx,8
movzx ecx,dl
mov b4,ecx

mov eax,r3
sub eax,r4
cmp eax,0
jg @f
neg eax
@@:
cmp eax,tol
jle @f
xor eax,eax
mov ecx,0
ret
@@:
mov rr,eax

mov eax,g3
sub eax,g4
cmp eax,0
jg @f
neg eax
@@:
cmp eax,tol
jle @f
xor eax,eax
mov ecx,1
ret
@@:
mov rg,eax

mov eax,b3
sub eax,b4
cmp eax,0
jg @f
neg eax
@@:
cmp eax,tol
jle @f
xor eax,eax
mov ecx,2
ret
@@:


xor eax,eax
inc eax
mov ecx,3

ret
CmpPix endp

GetMinMax proc uses edi esi lpbuff:dword,nCount:dword,pmin:dword,pmax:dword
LOCAL pl:dword

mov pl,4000

mov esi,lpbuff
xor ecx,ecx
getminx:
push ecx
mov eax,[esi+ecx*8]
.if eax<pl
mov pl,eax
.endif
pop ecx
inc ecx
cmp ecx,nCount
jl getminx

mov edi,pmin
mov eax,pl
mov [edi].POINT.x,eax

mov pl,4000
mov esi,lpbuff
xor ecx,ecx
getminy:
push ecx
mov eax,[esi+ecx*8+4]
.if eax<pl
mov pl,eax
.endif
pop ecx
inc ecx
cmp ecx,nCount
jl getminy

mov edi,pmin
mov eax,pl
mov [edi].POINT.y,eax
;--------------------------------------------------------------------------------


mov pl,0
mov esi,lpbuff
xor ecx,ecx
getmaxx:
push ecx
mov eax,[esi+ecx*8]
.if eax>pl
mov pl,eax
.endif
pop ecx
inc ecx
cmp ecx,nCount
jl getmaxx

mov edi,pmax
mov eax,pl
mov [edi].POINT.x,eax

mov pl,0
mov esi,lpbuff
xor ecx,ecx
getmaxy:
push ecx
mov eax,[esi+ecx*8+4]
.if eax>pl
mov pl,eax
.endif
pop ecx
inc ecx
cmp ecx,nCount
jl getmaxy

mov edi,pmax
mov eax,pl
mov [edi].POINT.y,eax


ret
GetMinMax endp

fProcLyr proc uses esi edi ebx lpbits:dword,lpbits2:dword,lpbits3:dword,nOpr:dword
LOCAL r,g,b:dword
LOCAL rc2,gc2,bc2:dword
LOCAL cntr,cntrp,rgbp:dword
LOCAL buff[256]:dword


mov esi,lpbits
mov edi,lpbits2

xor ecx,ecx
mov cntr,ecx
mov cntrp,ecx
mov rgbp,ecx
loop_p:
push ecx

mov edx,[esi+ecx*4]
mov eax,[edi+ecx*4]
push ecx
invoke CmpPix,edx,eax,20
pop ecx
cmp eax,1
jnz not_equal
inc cntr
jmp done1
not_equal:
mov eax,lpbits3
mov dword ptr[eax+ecx*4],0ffffffh
;mov dword ptr [esi+ecx*4],0ffffffh
done1:
pop ecx
inc ecx
cmp ecx,640*480
jl loop_p


mov eax,cntr
mov ecx,100
mul ecx
mov ecx,640*480
div ecx
mov ecx,eax
mov cntr,ecx
mov eax,cntr

ret
fProcLyr endp

fProcLyr2 proc uses esi edi lpbits:dword,buff:Dword
LOCAL sgn:dword
LOCAL x,y:dword
LOCAL buff2[256]:dword
LOCAL tmem:dword
LOCAL bmem:dword
LOCAL bm:BITMAP
LOCAL bm2:BITMAP
LOCAL pmax:POINT
LOCAL ocnt:dword
LOCAL w,h:dword

mov esi,lpbits

invoke mAlloc,128000
mov tmem,eax
mov bmem,eax

push tmem

xor ecx,ecx
mov sgn,ecx
mov ocnt,ecx
loop_y:
push ecx
mov y,ecx
xor ecx,ecx
loop_x:
push ecx
mov x,ecx

invoke GetDIBPixel,x,y,lpbits,640,480
.if eax==0FFFFFFh
.if sgn==0
mov edx,buff
push x
pop [edx]
push y
pop [edx+4]
.endif
inc sgn
.else
.if sgn>20
mov edx,buff
mov ecx,tmem
mov dword ptr[ecx],4000
mov dword ptr[ecx+4],4000
mov dword ptr[ecx+8],0
mov dword ptr[ecx+12],0
push ecx
invoke ScanObject,lpbits,[edx],[edx+4],640,480,ecx
pop ecx
push ecx
invoke GetStockObject,BLACK_PEN
invoke SelectObject,fb.DC,eax
invoke GetStockObject,BLACK_BRUSH
invoke SelectObject,fb.DC,eax
pop ecx
invoke Rectangle,fb.DC,[ecx],[ecx+4],[ecx+8],[ecx+12]
inc ocnt

mov ecx,tmem
add tmem,16
no:
mov sgn,0

.else
mov sgn,0
.endif
.endif
no_need:
pop ecx
inc ecx
cmp ecx,640
jl loop_x
pop ecx
inc ecx
cmp ecx,480
jl loop_y
done:

pop tmem

.if ocnt!=0
invoke GetMinMax,tmem,ocnt,addr pminG,addr pminG+8
; invoke dw2a,ocnt,addr buff
; invoke TextOut,fcr.DC,20,120,addr buff,eax
; xor ecx,ecx
; loop_cnt:
; push ecx
; mov edx,tmem
; shl ecx,4
; add edx,ecx
; invoke DIBRectangle,fcr.bmp,edx,0FF00h,-1
; pop ecx
; inc ecx
; cmp ecx,ocnt
; jl loop_cnt
.endif

; mov eax,pminG.right
; sub eax,pminG.left
; mov w,eax
; mov eax,pminG.bottom
; sub eax,pminG.top
; mov h,eax
;
;
invoke DIBRectangle,fcr.bmp,addr pminG,0FF00h,-1

invoke GlobalFree,tmem
mov eax,sgn

ret
fProcLyr2 endp

fProcessPic proc uses esi edi lpfLayer:dword
LOCAL lpdata:dword
LOCAL b:BITMAP
LOCAL bm2:BITMAP
LOCAL b3:BITMAP
LOCAL frd:BITMAP
LOCAL tgtp:BITMAP
LOCAL buff[256]:dword
LOCAL px:POINT
LOCAL px2:POINT
LOCAL buff2[256]:dword
LOCAL lpimg:dword
LOCAL ang[4]:qword
LOCAL cntr:dword

mov esi,lpfLayer

.if fb.DC==0
invoke fNewLayer,addr fb,640,480
invoke fNewLayer,addr ff,640,480
invoke fNewLayer,addr fcr,640,480
invoke fNewLayer,addr eb,640,480
invoke fNewLayer,addr frslt,640,480
invoke fNewLayer,addr tgt,640,480
invoke fNewLayer,addr forg,640,480
.endif

invoke GetStockObject,BLACK_BRUSH
invoke SelectObject,fb.DC,eax
invoke Rectangle,fb.DC,0,0,1024,768
invoke GetStockObject,BLACK_BRUSH
invoke SelectObject,frslt.DC,eax
invoke Rectangle,frslt.DC,0,0,1024,768

invoke GetObject,[esi].fLayer.bmp,sizeof BITMAP,addr b
invoke GetObject,fcr.bmp,sizeof BITMAP,addr bm2
invoke GetObject,fb.bmp,sizeof BITMAP,addr b3
invoke GetObject,frslt.bmp,sizeof BITMAP,addr frd
invoke GetObject,obj1.bmp,sizeof BITMAP,addr tgtp
invoke BitBlt,forg.DC,0,0,b3.bmWidth,b3.bmHeight,[esi].fLayer.DC,0,0,SRCCOPY


invoke fProcLyr,b.bmBits,bm2.bmBits,b3.bmBits,0
mov cntr,eax
invoke fProcLyr2,b3.bmBits,addr buff2

invoke BitBlt,[esi].fLayer.DC,0,0,b.bmWidth,b.bmHeight,fcr.DC,0,0,SRCCOPY
invoke BitBlt,fcr.fLayer.DC,0,0,640,480,forg.DC,0,0,SRCCOPY

invoke SetBkMode,fcr.DC,TRANSPARENT
invoke SetTextColor,fcr.DC,0FF0000h
invoke TextOut,fcr.DC,20,400,CADD("Target Mode: ON"),15
.if cntr<90
invoke TextOut,fcr.DC,20,10,CADD("Target Moving"),13
.endif

ret
fProcessPic endp



fLayer struct
DC dword 0
bmp dword 0
fLayer ends


Please check the scanobject function since it do recursive. I hate recursive when programming using assembler.
http://farabidatacenter.url.ph/MySoftware/
My 3D Game Engine Demo.

Contact me at Whatsapp: 6283818314165

Farabi

http://farabidatacenter.url.ph/MySoftware/
My 3D Game Engine Demo.

Contact me at Whatsapp: 6283818314165

Farabi

Here it is my plan. If you see your eyes carefully, there is about radius on your eyes about 5 cm, and that circle is where your eyes process object. Outside of it, it was not processed, only as a movement detections. So, to save times for the software to process object, I will only process and recognize everything that moved, remembering the feature, and give a sign on the memory. This will make the software if work as planned very efficient.
http://farabidatacenter.url.ph/MySoftware/
My 3D Game Engine Demo.

Contact me at Whatsapp: 6283818314165

Farabi

Seriously my system is sabotaged. The old code is working until I recompiled it. It seems it target specific name of the software. At first JWas, can compile it, but now it does not worked. THe recursive is fine, the problem is on another else code, which is weird and has nothing to do with the code. Here is a not fully working code but it give you a picture how it works.
http://farabidatacenter.url.ph/MySoftware/
My 3D Game Engine Demo.

Contact me at Whatsapp: 6283818314165

Farabi

Have a look at this code, this code show you that the recursive was fine. It done about 400 recursive

Quote
ScanObject proc USES esi edi ebx ecx edx  lpbits:dword,x:dword,y:dword,_width:dword,height:dword,rslt:dword
   LOCAL pl:POINT
   LOCAL pr:POINT
   LOCAL pc:POINT
   LOCAL buff[256]:dword
   
   mov esi,rslt
   sub dword ptr[esi],20
   .if dword ptr[esi]>0
      invoke ScanObject,lpbits,pc.x,pc.y,_width,height,rslt
   .endif
   ret
   invoke ScanEdgeXLeft,lpbits,x,y,_width,height,addr pl
   invoke ScanEdgeXRight,lpbits,x,y,_width,height,addr pr
   
   mov esi,rslt
   ; This is for X1
   mov eax,[esi]
   mov ecx,pl.x
   .if eax>ecx
      mov [esi],ecx
   .endif
   
   mov ecx,y
   mov [esi+4],ecx
   
   mov eax,[esi+8]
   mov ecx,pr.x
   .if eax<ecx
      mov dword ptr[esi+8],ecx
   .endif
   
   mov ecx,pl.x
   loop_next:
   push ecx
      mov edx,y
      inc edx
      
      push ecx
      pop pc.x
      push edx
      pop pc.y
      
      invoke GetDIBPixel,pc.x,pc.y,lpbits,_width,height ; << Here is that causing the probelm
      .if eax!=0
         mov ecx,rslt
         invoke ScanObject,lpbits,pc.x,pc.y,_width,height,rslt
         mov eax,pc.y
         mov [esi+12],eax
         pop ecx
         jmp done
      .endif
   pop ecx
   inc ecx
   cmp ecx,pr.x
   jl loop_next
   done:
   
   
   ret
ScanObject endp
http://farabidatacenter.url.ph/MySoftware/
My 3D Game Engine Demo.

Contact me at Whatsapp: 6283818314165