#include once "window9.bi"

Using GDIPLUS

Dim shared ULONG_PTR_01 AS ULONG_PTR
DIM GDIPLUSSTARTUPINPUT_01 AS GDIPLUSSTARTUPINPUT
GDIPLUSSTARTUPINPUT_01.GdiplusVersion = 1
IF (GDIPLUSSTARTUP(@ULONG_PTR_01, @GDIPLUSSTARTUPINPUT_01, NULL) <> 0) THEN
	PRINT "FAIL"
End If

Function Load_imageA(byref sName as USTRING) as Any Ptr Export
	DIM GPIMAGE_01 AS GPBITMAP PTR
	#ifdef UNICODE
		IF (GDIPLOADIMAGEFROMFILE( *sName, Cast(GpImage ptr Ptr,@GPIMAGE_01)) <> 0) THEN
			PRINT "FAIL"
		End If 	
	#else
		Var blen = (Len(sName)*2)+2
		Dim As WString Ptr wbuf
		wbuf  = Allocate( blen )
		if wbuf = 0 then return 0
		MultiByteToWideChar(CP_ACP, 0, StrPtr(sName), -1, wbuf, blen)
		IF (GDIPLOADIMAGEFROMFILE( *wbuf, Cast(GpImage ptr Ptr,@GPIMAGE_01)) <> 0) THEN
			PRINT "FAIL"
		End If
		DeAllocate(wbuf)  	
	#EndIf
	Return GPIMAGE_01
end Function
Function Copy_imageA(byval GpImage as Any Ptr) as Any Ptr Export
	If GpImage=0 Then Return 0
	DIM GPIMAGE_01 AS Any PTR
	GdipCloneImage(GpImage,@GPIMAGE_01)
	Return GPIMAGE_01
end Function

Function Grab_imageA(byval GpImage as Any Ptr,ByVal x As Single, ByVal y As Single, Byref w As Single, ByRef h As Single) as Any Ptr Export
	If GpImage=0 Then Return 0
	DIM GPIMAGE_01 AS Any Ptr
	dim as long lW = w
	dim as long lH = h
	If lW=0 Then
		GdipGetImageWidth(GPImage,@lW)
	EndIf
	If lH=0 Then
		GdipGetImageHeight(GPImage, @lH)
	EndIf
	GdipCloneBitmapArea(x,y,lW,lH, PixelFormat32bppARGB, GpImage, @GPIMAGE_01)
	Return GPIMAGE_01
end Function

Sub Resize_imageA(ByRef GpImage as Any Ptr, ByRef w As Single, ByRef h As Single) Export
	If GpImage=0 Then Exit Sub
	DIM GPIMAGE_01 AS Any Ptr
	dim as long lW = w
	dim as long lH = h
	If lW=0 Then
		GdipGetImageWidth(GPImage,@lW)
	EndIf
	If lH=0 Then
		GdipGetImageHeight(GPImage, @lH)
	EndIf
	GPIMAGE_01=Create_ImageA(lW,lH)
	ImageStartDrawA(GPIMAGE_01,1,&h00000000)
	ImageDrawA(GpImage,0,0,lW,lH)
	StopDrawA()
	GdipDisposeImage(GpImage)
	GpImage=GPIMAGE_01
End Sub

Function Image_WidthA(byval GpImage as Any Ptr) as Integer Export
	Dim As long w
	GdipGetImageWidth(GPImage,@w)
	Return w
end Function

Function Image_HeightA(byval GpImage as Any Ptr) as Integer Export
	Dim As long h
	GdipGetImageHeight(GPImage,@h)
	Return h
end Function
