saving image handle to list not working as expected

Just starting out? Need help? Post your questions and find answers here.
User avatar
TheAutomator
Enthusiast
Enthusiast
Posts: 112
Joined: Tue Dec 01, 2020 8:33 pm

saving image handle to list not working as expected

Post 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? :?:
Last edited by TheAutomator on Sat Nov 12, 2022 4:51 pm, edited 1 time in total.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 5011
Joined: Sun Apr 12, 2009 6:27 am

Re: saving image handle to list not working as expected

Post 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
Egypt my love
User avatar
jacdelad
Addict
Addict
Posts: 2044
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: saving image handle to list not working as expected

Post 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?
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: saving image handle to list not working as expected

Post 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?
BERESHEIT
User avatar
TheAutomator
Enthusiast
Enthusiast
Posts: 112
Joined: Tue Dec 01, 2020 8:33 pm

Re: saving image handle to list not working as expected

Post 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
User avatar
Caronte3D
Addict
Addict
Posts: 1379
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: saving image handle to list not working as expected

Post 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.
User avatar
TheAutomator
Enthusiast
Enthusiast
Posts: 112
Joined: Tue Dec 01, 2020 8:33 pm

Re: saving image handle to list not working as expected

Post 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).
User avatar
Caronte3D
Addict
Addict
Posts: 1379
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: saving image handle to list not working as expected

Post 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).
User avatar
TheAutomator
Enthusiast
Enthusiast
Posts: 112
Joined: Tue Dec 01, 2020 8:33 pm

Re: saving image handle to list not working as expected

Post 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?
User avatar
mk-soft
Always Here
Always Here
Posts: 6404
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: saving image handle to list not working as expected

Post 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
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
TheAutomator
Enthusiast
Enthusiast
Posts: 112
Joined: Tue Dec 01, 2020 8:33 pm

Re: saving image handle to list not working as expected

Post 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.
User avatar
mk-soft
Always Here
Always Here
Posts: 6404
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: saving image handle to list not working as expected

Post 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
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
TheAutomator
Enthusiast
Enthusiast
Posts: 112
Joined: Tue Dec 01, 2020 8:33 pm

Re: saving image handle to list not working as expected

Post 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)
User avatar
mk-soft
Always Here
Always Here
Posts: 6404
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: saving image handle to list not working as expected

Post by mk-soft »

That's right ...

Exit program or reload image with same static image constant (0 .. 4999) ;)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply