LoadImageEx

Share your advanced PureBasic knowledge/code with the community.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

LoadImageEx

Post by Dude »

Load BMP, GIF, JPG, PNG (and more) images without the PureBasic image plugins (UseGIFImageDecoder, etc).

Not my code, but no topic exists for it, so I made one. :)

Code: Select all

Procedure LoadImageEx(imgnum,file$,depth=24)
  Interface nIDXSurfaceFactory
    QueryInterface(a,b) : AddRef() : Release() : CreateSurface(a,b,c,d,e,f,g,h) : CreateFromDDSurface(a,b,c,d,e,f) : LoadImage(a.p-bstr,b,c,d,e,f)
    LoadImageFromStream(a,b,c,d,e,f) : CopySurfaceToNewFormat(a,b,c,d,e) : CreateD3DRMTexture(a,b,c,d,e) : BitBlt(a,b,c,d,e)
  EndInterface
  result=CoInitialize_(0)
  If result=#S_FALSE Or result=#S_OK
    #DXLOCKF_READ=0 : #CLSCTX_INPROC_SERVER=1
    CoCreateInstance_(?CLSID_DXTransformFactory,0,#CLSCTX_INPROC_SERVER,?IID_IDXTransformFactory,@dxtf.IDXTransformFactory)
    If dxtf
      dxtf\QueryService(?IID_IDXSurfaceFactory,?IID_IDXSurfaceFactory,@dxsf.nIDXSurfaceFactory)
      If dxsf
        dxsf\LoadImage(file$,0,0,0,?IID_IDXSurface,@surf.IDXSurface)
        If surf
          surf\LockSurfaceDC(0,#INFINITE,#DXLOCKF_READ,@lock.IDXDCLock)
          If lock
            DC=lock\GetDC()
            If DC
              GetClipBox_(DC,re.rect)
              If imgnum=#PB_Any
                result=CreateImage(#PB_Any,re\right,re\bottom,depth) : imgnum=result
              Else
                result=CreateImage(imgnum,re\right,re\bottom,depth)
              EndIf
              If result
                DestDC=StartDrawing(ImageOutput(imgnum))
                If DestDC : ok=BitBlt_(DestDC,0,0,re\right,re\bottom,DC,0,0,#SRCCOPY) : StopDrawing() : EndIf
                If ok=#False : FreeImage(imgnum) : Else : ok=result : EndIf
              EndIf
            EndIf
            Lock\Release()
          EndIf
          surf\Release()
        EndIf
        dxsf\Release()
      EndIf
      dxtf\Release()
    EndIf
  EndIf
  CoUninitialize_()
  ProcedureReturn ok
  DataSection
  CLSID_DXTransformFactory:
    Data.l $D1FE6762
    Data.w $FC48,$11D0
    Data.b $88,$3A,$3C,$8B,$00,$C1,$00,$00
  IID_IDXTransformFactory:
    Data.l $6A950B2B
    Data.w $A971,$11D1
    Data.b $81,$C8,$00,$00,$F8,$75,$57,$DB
  IID_IDXSurfaceFactory:
    Data.l $144946F5
    Data.w $C4D4,$11D1
    Data.b $81,$D1,$00,$00,$F8,$75,$57,$DB
  IID_IDXSurface:
    Data.l $B39FD73F
    Data.w $E139,$11D1
    Data.b $90,$65,$00,$C0,$4F,$D9,$18,$9D
  EndDataSection
EndProcedure
User avatar
Bisonte
Addict
Addict
Posts: 1232
Joined: Tue Oct 09, 2007 2:15 am

Re: LoadImageEx

Post by Bisonte »

Original at german forum from stefan : https://www.purebasic.fr/german/viewtopic.php?t=12155

Also with :
LoadSpriteEx()
CatchSpriteEx()
and CatchImageEx()
PureBasic 6.10 LTS (Windows x86/x64) | Windows10 Pro x64 | Asus TUF X570 Gaming Plus | R9 5900X | 64GB RAM | GeForce RTX 3080 TI iChill X4 | HAF XF Evo | build by vannicom​​
English is not my native language... (I often use DeepL to translate my texts.)
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: LoadImageEx

Post by wilbert »

I also created a module a few years ago to load and save images.
viewtopic.php?f=12&t=66497
But it works in a different way (it uses the normal PB commands).
Windows (x64)
Raspberry Pi OS (Arm64)
Post Reply