#include ONCE "window9.bi"

function Catch_Image( pBuf as any ptr , iColorBack as long = 0) as Hbitmap export
	
    dim as GdkPixbufLoader ptr loader
    
    dim as Hbitmap im  , imNew

    dim as GError ptr er_

	dim as long iWidthIm, iHeightIm
	
	dim as HDC cr
	
	dim as cairo_surface_t ptr surf    

    loader = gdk_pixbuf_loader_new ()

    gdk_pixbuf_loader_write (loader, pBuf, UBoundIncBin(pBuf), @er_)
    
	gdk_pixbuf_loader_close(loader,@er_)

    im = gdk_pixbuf_loader_get_pixbuf (loader)	   
	
	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)	
		
		g_object_unref(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