#include Once "window9.bi"

Function Load_image (sPath As String , iColorBack As Long = 0) As Hbitmap Export
	
	Dim As Hbitmap im ,  imNew
	
	Dim As Long iWidthIm, iHeightIm
	
	Dim As HDC cr
	
	Dim As Gerror Ptr err_
	
	Dim As cairo_surface_t Ptr surf
	
	im = gdk_pixbuf_new_from_file(sPath , @err_)
	
	If im = 0 Then Return 0
	
	If gdk_pixbuf_get_has_alpha (im) = 0 Orelse ((iColorBack And &hff000000) = 0) Then	
		
		iWidthIm = gdk_pixbuf_get_width(im)
		
		iHeightIm = gdk_pixbuf_get_height(im)
		
		If 	iWidthIm = 0 Orelse iHeightIm = 0 Then Return 0
		
		imNew =  gdk_pixbuf_new (GDK_COLORSPACE_RGB, True, 8 , iWidthIm, iHeightIm)
		
		gdk_pixbuf_fill(imNew , &h000000FF)
		
		surf = cairo_image_surface_create_for_data(gdk_pixbuf_get_pixels(imNew),CAIRO_FORMAT_ARGB32,iWidthIm,iHeightIm,gdk_pixbuf_get_rowstride(imNew))
		
		cr = cairo_create (surf)
		
		cairo_set_source_rgba (cr, (iColorBack And &h0000FF) /&h0000ff , (iColorBack And &h00FF00)/&h00ff00 , (iColorBack And &hFF0000)/&hFF0000 ,1)
		
		cairo_paint(cr)
		
		gdk_cairo_set_source_pixbuf(cr, im, 0, 0)
		
		cairo_paint(cr)
		
		cairo_destroy (cr)
		
		cairo_surface_destroy(surf)	
		
		Free_image(im)
		
		Dim As Ubyte Ptr pPixels = gdk_pixbuf_get_pixels(imNew)
		
		Dim As Integer iStride = gdk_pixbuf_get_rowstride(imNew)	
		
		For y As Long = 0 To iHeightIm  -1
			
			Dim As Long Ptr row = Cast(Any Ptr,pPixels)
			
			For x As Long = 0 To iWidthIm-1
				
				row[x] = (row[x] And &hFF00FF00) Or ((row[x] And &h00FF0000) Shr 16) Or ((row[x] And &h000000FF) Shl 16)
				
			Next
			
			pPixels += iStride
			
		Next	
		
		Return imNew
		
	Else
	
		Return im
		
	Endif
	
End Function
