QB64 Phoenix Edition
_WIDTH(_LOADIMAGE()) Ok? - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1)
+--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3)
+---- Forum: Help Me! (https://qb64phoenix.com/forum/forumdisplay.php?fid=10)
+---- Thread: _WIDTH(_LOADIMAGE()) Ok? (/showthread.php?tid=1396)



_WIDTH(_LOADIMAGE()) Ok? - TerryRitchie - 01-13-2023

I recently saw some code that did this:

ImageWidth = _WIDTH(_LOADIMAGE("Image.png", 32))

Will this leave the image's contents in RAM with no way of freeing it?

-or-

Will the image be discarded since the handle was not assigned to a variable?

It's a neat trick and one that I could happen to use on a current project but not if the image is orphaned in RAM.


RE: _WIDTH(_LOADIMAGE()) Ok? - mnrvovrfc - 01-13-2023

If I were you, I'd assign a LONG variable to _LOADIMAGE result. To load an image only to get its width, it's actually not being "used" according to the Wiki description.

From:

https://qb64phoenix.com/qb64wiki/index.php/LOADIMAGE

Quote:It is important to free unused or discarded images with _FREEIMAGE to prevent CPU memory overflow errors.



RE: _WIDTH(_LOADIMAGE()) Ok? - mnrvovrfc - 01-13-2023

Running this program on Manjaro MATE:

Code: (Select All)
dim lh as long, i as long
dim apic$
apic$ = environ$("HOME") + "/Pictures/linux-distros-wikipedia.png"
for i = 1 to 10
    print i
    lh = _width(_loadimage(apic$, 32))
next
print "Press any key to get away..."
do : ke$ = inkey$ : loop until ke$ = ""
do : _limit 20 : ke$ = inkey$ : loop while ke$ = ""
system

For this picture converted by GIMP to PNG file, which is 4.5MB in size:

https://en.wikipedia.org/wiki/Linux_distribution#/media/File:Linux_Distribution_Timeline_21_10_2021.svg

Observe the screenshot:

[Image: memory-hog-width-loadimage.png]


RE: _WIDTH(_LOADIMAGE()) Ok? - TerryRitchie - 01-13-2023

(01-13-2023, 02:07 PM)mnrvovrfc Wrote: If I were you, I'd assign a LONG variable to _LOADIMAGE result. To load an image only to get its width, it's actually not being "used" according to the Wiki description.

From:

https://qb64phoenix.com/qb64wiki/index.php/LOADIMAGE

Quote:It is important to free unused or discarded images with _FREEIMAGE to prevent CPU memory overflow errors.

Yep, I understand all of that. That's why I'm asking if something like the example is advisable or not.


RE: _WIDTH(_LOADIMAGE()) Ok? - RokCoder - 01-13-2023

I have no idea how good the garbage collection / smart pointers / whatever is so there's bound to be someone better versed with the source code to answer this. That said, I would be more tempted to create a helper function if this kind of thing is needed often. Then you'd get the benefits of a simple call to a function that would do the loading and calculation and also clean up after itself.

And, with that, I haven't answered your question at all  Big Grin


RE: _WIDTH(_LOADIMAGE()) Ok? - bplus - 01-13-2023

Clearly a bad idea IMO. Why load image without getting handle?

It's width is easy from handle.


RE: _WIDTH(_LOADIMAGE()) Ok? - bplus - 01-13-2023

Can do it with _LoadFont sorta
Code: (Select All)
Screen _NewImage(800, 600, 32)
_Font _LoadFont("arial.ttf", 20)
Print "Hello World"
fh = _Font
Print fh



RE: _WIDTH(_LOADIMAGE()) Ok? - SMcNeill - 01-13-2023

(01-13-2023, 01:45 PM)TerryRitchie Wrote: I recently saw some code that did this:

ImageWidth = _WIDTH(_LOADIMAGE("Image.png", 32))

Will this leave the image's contents in RAM with no way of freeing it?

-or-

Will the image be discarded since the handle was not assigned to a variable?

It's a neat trick and one that I could happen to use on a current project but not if the image is orphaned in RAM.

How is one going to _FREEIMAGE that?  As you suspect, chances are that's an image lost in RAM forevermore.

The only reason I say "chances are", is because image handles start at -10 and go downwards from there, so it's always possible some odd coder wrote something like:

FOR i = -10 TO -1000 STEP -1
  _FREEIMAGE i
NEXT


RE: _WIDTH(_LOADIMAGE()) Ok? - TerryRitchie - 01-13-2023

Thanks for the replies guys. Yeah, I figured this was a bad idea, just wanted to get the opinions of others to make sure. I'm writing some code that scans directories for images and I thought this might be a simple method of getting widths and heights without actually having to load, get the info, then free each image.


RE: _WIDTH(_LOADIMAGE()) Ok? - TempodiBasic - 01-21-2023

Quote:ImageWidth = _WIDTH(_LOADIMAGE("Image.png", 32))
about this I see another point of weakness, different from the RAM garbage coming because image.pgn lasts in RAM.

If in the _Loadimage action something goes wrong, what arrive to _WIDTH? 
Think about a wrong path, a failure to read the file, a wrong name (that file does not exist).
And what _WIDTH returns?

How to manage this kind of issue