#include once "win\gdiplus.bi"

using GdiPlus

dim shared as any ptr pMatch(15)

function GDIP_LoadBitmap(sImageFile as string) as BitmapData
  static as integer iStarted
  if iStarted=0 then
    iStarted=1
    CoInitialize(null)
    dim as ulong Token
    dim as GdiplusStartupInput  tInput = type(1)
    dim as GdiplusStartupOutput tOutput
    GdiplusStartup(@Token,@tInput,@tOutput)
  end if  
  dim as wstring*MAX_PATH wFile = sImageFile
  dim as gpBitmap ptr poBitmap
  dim as BitmapData tPixels
  if GdipCreateBitmapFromFile(wFile,@poBitmap) then exit function
  GdipBitmapLockBits(poBitmap,0,ImageLockModeRead,PixelFormat32bppARGB,@tPixels)
  for CNT as integer = 0 to 15 step 2
    if pMatch(CNT)=0 then
      pMatch(CNT) = tPixels.Scan0
      pMatch(CNT+1) = poBitmap
      exit for
    end if
  next CNT  
  return tPixels
end function
sub GDIP_DestroyBitmap(tImage as BitmapData)  
  dim as gpBitmap ptr poBitmap
  for CNT as integer = 0 to 15 step 2
    if pMatch(CNT)=tImage.Scan0 then      
      poBitmap = pMatch(CNT+1)
      pMatch(CNT)=0:pMatch(CNT+1)=0
      exit for
    end if
  next CNT    
  GdipBitmapUnlockBits(poBitmap,@tImage)
  cast(istream ptr,poBitmap)->lpvtbl->Release(cast(any ptr,poBitmap))
  'delete(poBitmap)
end sub
