Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

Managing the Image Cache

 [1/3] from: sanghabum::aol::com at: 18-Jun-2001 12:06


Is there any way of getting finer-grained control over the cahse used by Load-Image? Problem. I have a program that lets the family browse a photo album (JPG images.) Each click of forward/back gets them another 12 photos. The images are loaded into a VID box with the Load-Image function. But each image is cached automatically. The machine's memory usage starts to soar and at somewhere around the 200 image mark my system/stats is over 600MB and the machine (a 128Meg win98 thing) starts to impersonate molasses. I know load-image has a /clear refinement to empty the cache. But that's overkill. I'd like the last 100 images or so to remain cached. So does anyone know of a way to set an upper limit on the image cache? Or to partially purge it? --Thanks! --Colin.

 [2/3] from: arolls::bigpond::net::au at: 19-Jun-2001 4:52

Re: Managing the Image Cache - fixed number solution


Hi, see SOLUTION below.
> I know load-image has a /clear refinement to empty the cache. But that's > overkill. I'd like the last 100 images or so to remain cached. > > So does anyone know of a way to set an upper limit on the image > cache? Or to > partially purge it?
Start by looking at the source of 'load-image, below.
>> ?? load-image
load-image: func [ "Load an image through an in-memory image cache." image-file [file! url!] "Local file or remote URL" /update "Force update from source site" /clear "Purge the entire cache" /local image image-cache ][ image-cache: [] if clear [system/words/clear image-cache recycle] if any [update not image: select image-cache image-file] [ if all [update image] [remove/part find image-cache image-file 2] repend image-cache [ image-file image: either file? image-file [load image-file] [ either update [load-thru/binary/update image-file] [load-thru/binary image-file] ] ] ] image ] Now, see where it is written repend image-cache [...] We should patch 'load-image. Insert before that line, the following code: if 100 < length? image-cache [ remove/part head image-cache (2 * (length? image-cache - 100)) recycle ] We can see that 'image-cache is defined in the source of 'load-image. So on successive calls to 'load-image, examining the source will show you 'image-cache growing. Try typing these in at the console: save/png %tiny.png make image! 1x1 save/png %tiny2.png make image! 2x2 ?? load-image ; before any images load-image %tiny.png ?? load-image ; after one has been added load-image %tiny2.png ?? load-image ; after two have been added SOLUTION - the patched 'load-image (watch line wrap): load-image: func [ "Load an image through an in-memory image cache." image-file [file! url!] "Local file or remote URL" /update "Force update from source site" /clear "Purge the entire cache" /local image image-cache ][ image-cache: [] if clear [system/words/clear image-cache recycle] if any [update not image: select image-cache image-file] [ if all [update image] [remove/part find image-cache image-file 2] if 100 <= / length? image-cache 2 [ remove/part head image-cache 2 recycle ] repend image-cache [ image-file image: either file? image-file [load image-file] [ either update [load-thru/binary/update image-file] [load-thru/binary image-file] ] ] ] image ] Where the 100 is your desired maximum number of images to keep in the cache. Since new images are generally appended (with repend), we 'remove from the head. Here are some pieces to help you test the function patched above: This is the function body: probe (second get 'load-image) To look at the contents of the image-cache, as found in the function body: select (second get 'load-image) (to-set-word 'image-cache) To test for 100 images: use [file][ ; show length of image-cache probe length? select (second get 'load-image) (to-set-word 'image-cache) repeat n 104 [ save/png file: join %tiny [n ".png"] make image! to-pair reduce [n 1] load-image file probe length? select (second get 'load-image) (to-set-word 'image-cache) delete file ; clean up ] ] Note: You can see in the source that 'load-image indexes images based on filename alone. So if you 'load-image two images with the same filename, but from different locations, then the first one is returned twice. Be aware, it has this simple logic. Regards, Anton.

 [3/3] from: sanghabum:aol at: 18-Jun-2001 18:47


Wow! Thanks Anton, And you did all that before breakfast, if I read your address and time zone right! --Colin.