ImageLibrary v0.1.1

Share your advanced PureBasic knowledge/code with the community.
Julian
Enthusiast
Enthusiast
Posts: 276
Joined: Tue May 24, 2011 1:36 pm

ImageLibrary v0.1.1

Post by Julian »

Hi,

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
31/05/2015 - v0.1.1
  • 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
Last edited by Julian on Sun May 31, 2015 9:53 pm, edited 4 times in total.
Julian
Enthusiast
Enthusiast
Posts: 276
Joined: Tue May 24, 2011 1:36 pm

Re: ImageLibrary v0.1.0

Post by Julian »

Reserved for future use.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: ImageLibrary v0.1.0

Post by ts-soft »

Code: Select all

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
    Filename = ReplaceString(Filename, "/", "\")
  CompilerElse
    Filename = ReplaceString(Filename, "\", "/") ; -> \ is part of name on linux, no separatur
  CompilerEndIf
Don't replace the backslash on Linux, is part of name, no separatur. I have made the same mistake in many sources,
but i will corrected it :oops:

Greetings
Thomas
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
Little John
Addict
Addict
Posts: 4791
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: ImageLibrary v0.1.0

Post by Little John »

And the following is not necessary

Code: Select all

  CompilerIf #PB_Compiler_OS = #PB_OS_Windows
    Filename = ReplaceString(Filename, "/", "\")
because Windows treats both "\" and "/" as path separator.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: ImageLibrary v0.1.0

Post by ts-soft »

@Little John
But in some dialogs is / not allowed and you can't mix this.
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: ImageLibrary v0.1.0

Post by Josh »

BTW, instead of:

Code: Select all

Filename = ReplaceString(Filename, "/", "\")
Filename = ReplaceString(Filename, "\", "/")
i would use:

Code: Select all

ReplaceString(Filename, "/", "\", #PB_String_InPlace)
ReplaceString(Filename, "\", "/", #PB_String_InPlace)
sorry for my bad english
Julian
Enthusiast
Enthusiast
Posts: 276
Joined: Tue May 24, 2011 1:36 pm

Re: ImageLibrary v0.1.1

Post by Julian »

New version.

31/05/2015 - v0.1.1
  • Removed path separator correction from linux and implemented #PB_String_InPlace (both suggested in thread)
Thanks for the suggestions everyone.
Post Reply