Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
example: loading images using FreeImage
#1
Code:
#include "freeimage.bi"
#include "crt.bi"

' code example for loading pretty much any image type using FreeImage
' by yetifoot
'
' credits
'
' jofers for the ImageCreate stuff as seen in his example to load jpegs
' me! for code from my VB project 'FImage' shameless plug follows
'
'  http://sourceforge.net/projects/fimage
'  check it out for code to save, blt to dc, stretch, screencap, multipage etc
'  

Option Explicit

Declare Function FI_Load(filename As String) As Any ptr

'::::
Function FI_Load(filename As String) As Any ptr
  Dim FIF As FREE_IMAGE_FORMAT
  Dim dib As FIBITMAP Ptr
  Dim dib32 As FIBITMAP Ptr
  Dim DIBWidth As uInteger
  Dim DIBHeight As uInteger
  Dim flags As uInteger
  Dim Sprite As Any Ptr
  Dim Bits As Any Ptr
  
    'Get File Format
    FIF = FreeImage_GetFileType(strptr(filename), 0)
    If FIF = FIF_UNKNOWN Then
      FIF = FreeImage_GetFIFFromFilename(strptr(filename))
    End If
    
    'Exit If Unknown
    If FIF = FIF_UNKNOWN Then
      FI_Load = NULL
      Exit Function
    End If
    
    'Always load jpegs accurately
    If FIF = FIF_JPEG Then flags = JPEG_ACCURATE
    
    'Load Image
    dib = FreeImage_Load(FIF, strptr(filename), flags)
    
    If dib = 0 Then
      'Problem During Load
      FI_Load = NULL
      Exit Function
    End If
    
    'Get Size
    DIBWidth = FreeImage_GetWidth(dib)
    DIBHeight = FreeImage_GetHeight(dib)

    'Flip and force 32 bits
    FreeImage_FlipVertical Dib
    Dib32 = FreeImage_ConvertTo32Bits(Dib)

    'make our sprite and get a ptr to the FI dib
    Sprite = ImageCreate(DIBWidth, DIBHeight)
    Bits = FreeImage_GetBits(Dib32)
  
    'copy from dib to sprite
    MemCpy CPtr(ZString Ptr, Sprite) + 4, Bits, DIBWidth * DIBHeight * 4
    
    'Unload dibs
    FreeImage_Unload dib
    FreeImage_Unload dib32
    
    FI_Load = Sprite
End Function

Sub Main()
  Dim Image As Any Ptr
  
    ScreenRes 640, 480, 32
    
    Image = FI_Load("yf.jpg")
    
    If Image Then
      Put (0, 0), Image
    Else
      Print "File not found"
    End If
    
End Sub

'Program entry
Main
Sleep
EVEN MEN OF STEEL RUST.
[Image: chav.gif]
Reply
#2
Keep it up, yeti. I'll be sure to grab this when I get home, even if most of my programming uses ASCII graphics or text. 8)
Reply
#3
nice! I'll definately be using this!
Reply
#4
This is what I've been trying to figure out. Thanks for sharing! Smile
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)