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: 1313
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.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
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)
BarryG
Addict
Addict
Posts: 4173
Joined: Thu Apr 18, 2019 8:17 am

Re: LoadImageEx

Post by BarryG »

I've been using the code at top for a while and it uses a depth of 24 which is good for 99% of images, but it needs a depth of 32 for images with transparency. I've got two files: one is an ICO from Reddit.com, and the other is my own PNG from Paint Shop Pro.

The code at top, when using 24 for the depth, doesn't show the ICO correctly (it has transparency); and when I use 32 for the depth, the PNG image is blank despite return non-zero for success when loading it (it has no transparency).

So how can I tell if a loaded image has transparency, so I can choose a depth of 24 or 32 accordingly? I can't trust the file extension because some files I load don't have one. So I need to test an image for transparent pixels somehow. Thanks.
Post Reply