How to overlay

Just starting out? Need help? Post your questions and find answers here.
eriansa
Enthusiast
Enthusiast
Posts: 277
Joined: Wed Mar 17, 2004 12:31 am
Contact:

How to overlay

Post by eriansa »

What API should I use to establish the effect in the picture (overlay so that the background takes the color of the transparant foreground)

Image
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Reckon it depends upon how you're rendering this image. If you're simply drawing the image directly using gdi, then you should be able to get this effect by xoring different colours.

So perhaps a simple

Code: Select all

DrawingMode(#PB_2DDrawing_XOr)
or

Code: Select all

oldrop=SetROP2_(hdc, #R2_XORPEN)
combined with suitable PB drawing or Windows gdi commands will suffice.
I may look like a mule, but I'm not a complete ass.
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post by Fluid Byte »

*** WARNING ****

THIS ABSOULTE OVERKILL! NO WARRANTY IMPLIED!

Code: Select all

; ***********************************************
; *                                             *
; *      'Overlay Selection'                    *
; *                                             *
; * Author: Christian Kemna                     *
; * Created: Febuary 21, 2007                   *
; * Updated: Febuary 21, 2007                   *
; * Version: PureBasic V4.02                    *
; *                                             *
; * Web: http://codedreality.purearea.net/	   *
; * eMail: ckemna@t-online.de                   *
; *                                             *
; ***********************************************


; // SETUP //
DisableDebugger

#OVRL_WIDTH = 400
#OVRL_HEIGHT = 180
#OVRL_BACKSDWCY = 30
#OVRL_BACKAMPCX = 4
#OVRL_BACKAMPCY = 10
#OVRL_BACKAMPSTEP = 16

Global lpPrevFunc.l
Global clrSelect.l

clrBackgnd1 = RGB(33,33,57)
clrBackgnd2 = RGB(41,41,66)
clrGraph = RGB(214,214,239)
clrSelect = RGB(130,10,25)

; // GRAPH IMAGE //

CreateImage(0,#OVRL_WIDTH,#OVRL_HEIGHT)

hdc = StartDrawing(ImageOutput(0))
Box(0,0,#OVRL_WIDTH,#OVRL_HEIGHT,clrBackgnd1)

Box(0,0,#OVRL_WIDTH,#OVRL_BACKSDWCY,clrBackgnd2)
Box(0,#OVRL_HEIGHT-#OVRL_BACKSDWCY,#OVRL_WIDTH,#OVRL_BACKSDWCY,clrBackgnd2)

For i=0 To #OVRL_WIDTH-1 Step #OVRL_BACKAMPSTEP
	Box(i,#OVRL_BACKSDWCY,#OVRL_BACKAMPCX,#OVRL_BACKAMPCY,clrBackgnd2)
	Box(i,#OVRL_HEIGHT-#OVRL_BACKSDWCY-#OVRL_BACKAMPCY,#OVRL_BACKAMPCX,#OVRL_BACKAMPCY,clrBackgnd2)
Next

For i=0 To #OVRL_WIDTH-1
	Peak = Random(40)	
	Box(i,85,1,-Random(Peak),clrGraph)
	Box(i,85,1,15 + (Random(Peak)),clrGraph)
Next
StopDrawing()

; // OVERLAY IMAGE //

CreateImage(1,#OVRL_WIDTH,#OVRL_HEIGHT)

; // APPLICATION WINDOW //

OpenWindow(0,0,0,#OVRL_WIDTH + 24,#OVRL_HEIGHT + 24,"Audio Graph Selection",#WS_SYSMENU | #WS_CAPTION | 1)
CreateGadgetList(WindowID(0))
ImageGadget(0,10,10,0,0,ImageID(0),#PB_Image_Border)

; // SUBCLASS PROCEDURE //

Procedure ImageProc(hWnd.l,uMsg.l,wParam.l,lParam.l)
	Static bClicked.b, MTX.w
	
    Select uMsg
        Case #WM_LBUTTONDOWN
        MTX = lParam & $FFFF : MTY = lParam >> 16

       	bClicked = #True
 		
 		Case #WM_MOUSEMOVE
 		If bClicked
	        MX = lParam & $FFFF : MY = lParam >> 16	        
	 		
	 		; // OVERLAY MASK //
	 		
			StartDrawing(ImageOutput(1))
			Box(0,0,#OVRL_WIDTH,#OVRL_HEIGHT,$FF00FF)
			Box(MTX,0,MX-MTX,#OVRL_HEIGHT,clrSelect)
			StopDrawing()
	 		
			CopyImage(0,2)
			
			; // COMBINE COLORS //
			
			bmi.BITMAPINFO
			
			hdc = StartDrawing(ImageOutput(0))
			
			; ->> Color Data of Image #1
			lpvBits1 = AllocateMemory(#OVRL_WIDTH * #OVRL_HEIGHT * 4) 
			
			bmi\bmiHeader\biSize = SizeOf(BITMAPINFOHEADER) 
			bmi\bmiHeader\biWidth = #OVRL_WIDTH 
			bmi\bmiHeader\biHeight = #OVRL_HEIGHT 
			bmi\bmiHeader\biPlanes = 1 
			bmi\bmiHeader\biBitCount = 32
			bmi\bmiHeader\biCompression = #BI_RGB 
			
			GetDIBits_(hdc,ImageID(0),0,#OVRL_HEIGHT,lpvBits1,bmi,#DIB_RGB_COLORS) 	
			
			*pxData1.LONG  = lpvBits1
			
			; ->> Color Data of Image #2
			
			lpvBits2 = AllocateMemory(#OVRL_WIDTH * #OVRL_HEIGHT * 4) 
			
			bmi\bmiHeader\biSize = SizeOf(BITMAPINFOHEADER) 
			bmi\bmiHeader\biWidth = #OVRL_WIDTH 
			bmi\bmiHeader\biHeight = #OVRL_HEIGHT 
			bmi\bmiHeader\biPlanes = 1 
			bmi\bmiHeader\biBitCount = 32
			bmi\bmiHeader\biCompression = #BI_RGB 
				
			GetDIBits_(hdc,ImageID(1),0,#OVRL_HEIGHT,lpvBits2,bmi,#DIB_RGB_COLORS) 	
			
			*pxData2.LONG  = lpvBits2
				
			; ->> Combine Color Data (50% Opacity)
			For i=1 To (#OVRL_WIDTH * #OVRL_HEIGHT)
				R1 = *pxData1\l & $FF
				G1 = *pxData1\l >> 8 & $FF
				B1 = *pxData1\l >> 16
				
				R2 = *pxData2\l & $FF
				G2 = *pxData2\l >> 8 & $FF
				B2 = *pxData2\l >> 16		
						
				R = (R1 + R2) / 2 : G = (G1 + G2) / 2 : B = (B1 + B2) / 2
			
				If *pxData2\l ! $FF00FF
					*pxData1\l = R | G << 8 | B << 16	
				EndIf
				
				*pxData2 + 4 : *pxData1 + 4	
			Next
			
			SetDIBits_(hdc,ImageID(2),0,#OVRL_HEIGHT,lpvBits1,bmi,#DIB_RGB_COLORS)
			
			; ->> Clean Up
			FreeMemory(lpvBits1)
			FreeMemory(lpvBits2)
			
			StopDrawing()			
			
			; // GADGET REFRESH //
	 		SetGadgetState(0,ImageID(2))
	 		
	 		FreeImage(2)	 		
 		EndIf
 		
        Case #WM_LBUTTONUP       
       	bClicked = #False	
    EndSelect
     
    ProcedureReturn CallWindowProc_(lpPrevFunc,hWnd.l,uMsg,wParam,lParam)
EndProcedure

lpPrevFunc = SetWindowLong_(GadgetID(0),#GWL_WNDPROC,@ImageProc())

; // EVENT LOOP //

While WaitWindowEvent() ! 16 : Wend
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

Post by traumatic »

Fluid Byte 8)
Good programmers don't comment their code. It was hard to write, should be hard to read.
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Post by Rook Zimbabwe »

Dang Fluid Byte! That is impressive!!!
Num3
PureBasic Expert
PureBasic Expert
Posts: 2812
Joined: Fri Apr 25, 2003 4:51 pm
Location: Portugal, Lisbon
Contact:

Post by Num3 »

[me] gives god mode to FluidByte [/me]
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Man that's good! Nice one Fluid. A great way of doing it. 8)
I may look like a mule, but I'm not a complete ass.
eriansa
Enthusiast
Enthusiast
Posts: 277
Joined: Wed Mar 17, 2004 12:31 am
Contact:

Post by eriansa »

Thanks! :D
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post by Fluid Byte »

Just a reminder:

You may consider drawing the overlay after the selection has been made. As using this technique in realtime with large dimensions (640x480 or even bigger) will result in a slowdown.
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
Post Reply