I had to code this up to provide certain functionality for my BetterButtons library.
The main feature needed was to get the #Image given the ImageID - B8_ImageNumber(ImageID)
Let me know if its any use or if you need additional features.
Thanks to everyone on this thread their help and tips especially mk-soft.
Version History
30/5/2015 - v0.1.0
- Initial release
- New ImageLibrary overloads all image related calls With the following additional functionality:
- B8_ImageList() is a map of all loaded images keyed by ImageID
- B8_ImageNumber(ImageID) returns the #Image from the ImageID
- B8_ImageIDByFilename(Filename$) searches currently loaded images for Filename$ and returns ImageID if found, 0 if not
- Removed path separator correction from linux and implemented #PB_String_InPlace (both suggested in thread)
Code: Select all
;{
;==============================
;ImageLibrary
;==============================
; Version 0.1.1
;
; Copyright (c) 2015 by Julian
; http://www.purebasic.fr/english/memberlist.php?mode=viewprofile&u=7685
; All Rights Reserved.
;
; The contents of this file are subject To the Mozilla Public License Version
; 2.0 (the "License"); you may not use this file except in compliance with
; the License. You may obtain a copy of the License at
; http://www.mozilla.org/MPL/
;
; Software distributed under the License is distributed on an "AS IS" basis,
; WITHOUT WARRANTY OF ANY KIND, either express Or implied. See the License
; For the specific language governing rights And limitations under the
; License.
;==============================
;VERSION HISTORY
;==============================
;v0.1.0 - Initial release
; New ImageLibrary overloads all image related calls With the following additional functionality:
; B8_ImageList() is a map of all loaded images keyed by ImageID
; B8_ImageNumber(ImageID) returns the #Image from the ImageID
; B8_ImageIDByFilename(Filename$) searches currently loaded images for Filename$ and returns ImageID if found, 0 if not
;v0.1.1 - Removed path separator correction from linux and implemented #PB_String_InPlace (both suggested in thread)
;}
;EnableExplicit
;-ImageData structure
Structure B8_ImageData
id.i
number.i
filename.s
EndStructure
;-B8_ImageList global
Global NewMap B8_ImageList.B8_ImageData()
;-Public Procedures
Procedure.i B8_ImageNumber(ImageID.i)
;=== B8_ImageNumber
;Returns the #Image of an image given its ImageID and sets the current position of B8_ImageList() to the found entry
;=== PARAMETERS
;ImageID.i : ImageID
;=== RETURN
;Returns the #Image of an image or -1 if nothing was found
;===
Protected result = -1
If FindMapElement(B8_ImageList(), Str(ImageID))
result = B8_ImageList()\number
EndIf
ProcedureReturn result
EndProcedure
Procedure.i B8_ImageIDByFilename(Filename.s)
;=== B8_ImageIDByFilename
;Returns the #Image of an image given its filename and sets the current position of B8_ImageList() to the found entry
;=== PARAMETERS
;Filename.s : Filename of image (including path as it was use when loading the file originally)
;=== RETURN
;Returns the #Image of an image or -1 if nothing was found
;===
ResetMap(B8_ImageList())
While NextMapElement(B8_ImageList())
If Filename = B8_ImageList()\filename
; found an image in the list that matches filename
ProcedureReturn B8_ImageList()\number
EndIf
Wend
ProcedureReturn -1
EndProcedure
;-Overloaded Procedures
Procedure _B8_StoreImageData(result.i, Image.i, Filename.s)
Protected id.i, number.i
If result
If Image = #PB_Any
id = ImageID(result)
number = result
Else
id = result
number = Image
EndIf
AddMapElement(B8_ImageList(), Str(id))
With B8_ImageList()
\id = id
\number = number
\filename = Filename
EndWith
EndIf
EndProcedure
Procedure.i _B8_CatchImage(Image, *MemoryAddress, Size = 0)
Protected result.i
result = CatchImage(Image, *MemoryAddress, Size)
_B8_StoreImageData(result, Image, ":CatchImage:" + Str(result)) ; Note the custom filename
ProcedureReturn result
EndProcedure
Procedure.i _B8_CopyImage(Image1, Image2)
Protected result.i
result = CopyImage(Image1, Image2)
_B8_StoreImageData(result, Image2, ":CopyImage:")
ProcedureReturn result
EndProcedure
Procedure.i _B8_CreateImage(Image, Width, Height, Depth = 24, BackColor = 0)
Protected result.i
result = CreateImage(Image, Width, Height, Depth, BackColor)
_B8_StoreImageData(result, Image, ":CreateImage:")
ProcedureReturn result
EndProcedure
Procedure.i _B8_GrabDrawingImage(Image, x, y, Width, Height)
Protected result.i
result = GrabDrawingImage(Image, x, y, Width, Height)
_B8_StoreImageData(result, Image, ":GrabDrawingImage:")
ProcedureReturn result
EndProcedure
Procedure.i _B8_GrabImage(Image1, Image2, x, y, Width, Height)
Protected result.i
result = GrabImage(Image1, Image2, x, y, Width, Height)
_B8_StoreImageData(result, Image2, ":GrabImage:")
ProcedureReturn result
EndProcedure
Procedure.i _B8_LoadImage(Image.i, Filename.s, Flags.i = 0, UseExisting.i = 0)
Protected result.i
Protected found.i
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
ReplaceString(Filename, "/", "\", #PB_String_InPlace)
CompilerEndIf
If UseExisting
found = B8_ImageIDByFilename(Filename)
If found = -1 ; create a new image
result = LoadImage(Image, Filename, Flags)
_B8_StoreImageData(result, Image, Filename)
Else
result = found
EndIf
Else
result = LoadImage(Image, Filename, Flags)
_B8_StoreImageData(result, Image, Filename)
EndIf
ProcedureReturn result
EndProcedure
Procedure _B8_FreeImage(Image.i) ; we can either find the image using #Image or the handle
If IsImage(Image) ; find using #Image
DeleteMapElement(B8_ImageList(), Str(ImageID(Image)))
FreeImage(Image)
Else ; find using handle
If FindMapElement(B8_ImageList(), Str(Image))
If IsImage(B8_ImageList()\number)
FreeImage(B8_ImageList()\number)
DeleteMapElement(B8_ImageList())
EndIf
EndIf
EndIf
EndProcedure
;-Macros
Macro CatchImage(Image, MemoryAddress, Size = 0)
_B8_CatchImage(Image, MemoryAddress, Size)
EndMacro
Macro CopyImage(Image1, Image2)
_B8_CopyImage(Image1, Image2)
EndMacro
Macro CreateImage(Image, Width, Height, Depth = 24, BackColor = 0)
_B8_CreateImage(Image, Width, Height, Depth, BackColor)
EndMacro
Macro GrabDrawingImage(Image, x, y, Width, Height)
_B8_GrabDrawingImage(Image, x, y, Width, Height)
EndMacro
Macro GrabImage(Image1, Image2, x, y, Width, Height)
_B8_GrabImage(Image1, Image2, x, y, Width, Height)
EndMacro
Macro LoadImage(Image, Filename, Flags = 0, UseExisting = 0)
_B8_LoadImage(Image, Filename, Flags, UseExisting)
EndMacro
Macro FreeImage(Image)
_B8_FreeImage(Image)
EndMacro