Page 1 of 1

saving image handle to list not working as expected

Posted: Sat Nov 12, 2022 5:22 am
by TheAutomator

Code: Select all

Structure newstruc
    info.s
    image.i ; save id hande
EndStructure

; Global NewMap imag().newstruc EDIT: made a mistake here while making this post
Global NewMap imag.newstruc()

Procedure load_imgs(name.s)
    imag("name")\info = "test"
    imag()\image = LoadImage(name + ".bmp")
    imag(name + "2")\info = "not working"
    imag()\image = imag(name)\image
    Debug imag()\image
    Debug imag(name)\image
EndProcedure

load_imgs("a")

Debug "--------"

Debug voice("a")\image
Debug voice("a2")\image ;<---- = 0?
why is this 0 outside the proc? :?:

Re: saving image handle to list not working as expected

Posted: Sat Nov 12, 2022 6:38 am
by RASHAD
Hi TheAutomator
Are you sure (as a start) that the next line did not rise any error?

Code: Select all

Global NewMap imag().newstruc

Re: saving image handle to list not working as expected

Posted: Sat Nov 12, 2022 6:53 am
by jacdelad
I'm a bit confused. The image of item "a2" is 0 because it never gets an image assigned. Also, why are you using the map several times without index? Alsoalso, what is the desired outcome?

Re: saving image handle to list not working as expected

Posted: Sat Nov 12, 2022 9:29 am
by netmaestro
Not sure what voice is but I reworked your code to make some kind of crude sense anyway:

Code: Select all

CreateImage(0, 32,32,24,#Red)
SaveImage(0,"a.bmp",#PB_ImagePlugin_BMP)

Structure newstruc
    info.s
    image.i ; save id hande
EndStructure

Global NewMap imag.newstruc()

Procedure load_imgs(name.s)
    imag(name)\info = "test"
    imag(name)\image = ImageID(LoadImage(#PB_Any, name + ".bmp"))
    imag(name + "2")\info = "not working"
    imag(name + "2")\image = imag(name)\image
    Debug imag(name + "2")\image
    Debug imag(name)\image
EndProcedure

load_imgs("a")

Debug "--------"

Debug imag("a")\image
Debug imag("a2")\image ;<---- = 0?

Re: saving image handle to list not working as expected

Posted: Sat Nov 12, 2022 4:46 pm
by TheAutomator
Woops, okay, first things first..
"Are you sure (as a start) that the next line did not rise any error?"
Sorry about my first code sample here,
it was very late and I made a quick little version of my code for this post to explain the problem.
I misplaced the '()' on the new map, and mistakenly used a name from my original code 'voice'.. :oops:
"why are you using the map several times without index?"
Accessing an assigned map key can be done without putting in the key-string every time because PB remembers the last accessed key:

Code: Select all

imag("name")\info = "test"
imag()\image = LoadImage(name + ".bmp")
The second line here works because 'LoadImage' returns the image handle by default.
No need for imageID() that way..

I think the error came from the fact that the last accessed key in this part is "a" instead of "a2":

Code: Select all

imag(name + "2")\info = "not working"
Debug imag()\info     
imag()\image = imag(name)\image
Debug imag()\info
Hence my mistake. Thanks for helping me debug this confusing part of code :D

Re: saving image handle to list not working as expected

Posted: Sat Nov 12, 2022 6:17 pm
by Caronte3D
From the LoadImage help:
Return value
Returns nonzero if the image was loaded successfully and zero if the image could not be loaded. If #PB_Any was specified as the #Image parameter then the auto-generated number is returned on success.
So... better if you use the ImageID() function in case you need (I don't know) a Windows Handle to that Image.

Re: saving image handle to list not working as expected

Posted: Sat Nov 12, 2022 6:21 pm
by TheAutomator
But in my program it always returns the windows handle as a result, without imageid()..

Code: Select all

debug LoadImage(0, "a.bmp")
debug imageID(0)
The way I load and use the images 'imageID()' is only an unnecessary extra step because it is stored in the map anyway..
I only need to display the images on a GUI one after the other in a loop (with timing and what image to display depending on some data).

Re: saving image handle to list not working as expected

Posted: Sat Nov 12, 2022 6:59 pm
by Caronte3D
I think the reason for not saying that a handle is returned is because it can change in the future, so it's not something that is going to be returned with absolute certainty.
An unofficial feature (I think).

Re: saving image handle to list not working as expected

Posted: Sat Nov 12, 2022 7:47 pm
by TheAutomator
So an image handle can randomly change in memory?
I thought handles were static? Sort of a constant so to, speak until released / altered?

Or do you mean something else?

Re: saving image handle to list not working as expected

Posted: Sat Nov 12, 2022 8:22 pm
by mk-soft
You must keep both IDs.

One is the image number that PB returns to you when you use #PB_Any to delete the image with FreeImage (otherwise there is a memory leak when you reuse the same variable to save the image number without FreeImage).
Once the ImageID for quick access to the Handle (Windows) or Object (Linux, MacOS)

See PB help 'PureBasic objects overview'.

Example memory leak

Code: Select all

;- Memoryleak with #PB_Any

Structure udtMyData
  Name.s
  Image.i
  Array Values.i(10)
EndStructure

Global NewList MyData.udtMyData()

AddElement(MyData())
MyData()\Name = "My Pitures 1"
MyData()\Image = CreateImage(#PB_Any, 32, 32, 32, #Red)

AddElement(MyData())
MyData()\Name = "My Pitures 2"
MyData()\Image = CreateImage(#PB_Any, 32, 32, 32, #Green)

AddElement(MyData())
MyData()\Name = "My Pitures 3"
MyData()\Image = CreateImage(#PB_Any, 32, 32, 32, #Blue)

SelectElement(MyData(), 1)
; FreeImage(MyData()\Image) ; FreeImage before delete element
; String, lists, arrays automated release
DeleteElement(MyData())

ForEach MyData()
  Debug MyData()\Name
Next

ShowLibraryViewer("image")

CallDebugger

Re: saving image handle to list not working as expected

Posted: Sat Nov 12, 2022 8:45 pm
by TheAutomator
- An object that is associated with an index is automatically freed when reusing that index.

so replacing an image e.g. image 0 to another .bmp file wil not create a memory leak if I understand correctly?

I don't use #PB_Any in my original code, the index is stored in the map to change the image later on in the code if needed.

LoadImage() returns nonzero if the image was loaded successfully -> image id in my case.
If #PB_Any was specified as the #Image parameter then the auto-generated number is returned on success.-> not the case here.

Re: saving image handle to list not working as expected

Posted: Sat Nov 12, 2022 9:11 pm
by mk-soft
Loading an image with #PB_Any works perfectly here. Maybe the path to the image is not correct

P.S.
The returned handle is not static. It can change after each LoadImage(0, ...).

Code: Select all

;- Memoryleak with #PB_Any

Structure udtMyData
  Name.s
  Path.s
  Image.i
  ImageID.i
  Array Values.i(10)
EndStructure

Global NewList MyData.udtMyData()

AddElement(MyData())
MyData()\Name = "My Pitures 1"
MyData()\Path = #PB_Compiler_Home + "Examples/Sources/Data/AlphaChannel.bmp"
MyData()\Image = LoadImage(#PB_Any, MyData()\Path)
MyData()\ImageID = ImageID(MyData()\Image)

AddElement(MyData())
MyData()\Name = "My Pitures 2"
MyData()\Path = #PB_Compiler_Home + "Examples/Sources/Data/Background.bmp"
MyData()\Image = LoadImage(#PB_Any, MyData()\Path)
MyData()\ImageID = ImageID(MyData()\Image)

AddElement(MyData())
MyData()\Name = "My Pitures 3"
MyData()\Path = #PB_Compiler_Home + "Examples/Sources/Data/PureBasic.bmp"
MyData()\Image = LoadImage(#PB_Any, MyData()\Path)
MyData()\ImageID = ImageID(MyData()\Image)

SelectElement(MyData(), 1)
; FreeImage(MyData()\Image) ; FreeImage before delete element
; String, lists, arrays automated release
DeleteElement(MyData())

ForEach MyData()
  Debug MyData()\Name + " PB-ID = " + MyData()\Image + " Handle = " + MyData()\ImageID
Next

ShowLibraryViewer("image")

CallDebugger

Re: saving image handle to list not working as expected

Posted: Sat Nov 12, 2022 9:32 pm
by TheAutomator
I mean I don't use #PB_any :)
and I reassign the map()\image part when I reload an image but in the helpfile it says unloading is not needed (automatically)

Re: saving image handle to list not working as expected

Posted: Sat Nov 12, 2022 9:38 pm
by mk-soft
That's right ...

Exit program or reload image with same static image constant (0 .. 4999) ;)