Pre-emptively decode images

RMMZ 1.8.1

Hello.

It's possible to cache images before displaying them, which is very useful for animations (when you don't want to go the video format route). Naturally I can optimize the images as much as possible, as well as use layers so the animation is not changing unnecessary details like backgrounds etc. However, that still leaves the last part: decoding.

Caching the images helps a lot (i.e. ImageManager.loadPicture()), but there is still a delay when you rapidly "show" cached images for an animation due to the decoding required before display, which will become more and more significant the more frames you're trying to display at once—and it'll also slow down the worse the hardware is.

To be clear, this is on the first display of the images. If the animation is repeated after the first display, it is played perfectly because the images are decoded and ready to repeat. But the first display (that the user sees) is what's important, and it is possible it may only be played the one time even if you could live with the initial laggy/image-skipping display.

I would like to mitigate that as much as possible by both caching and decoding the images before display. It is possible to sort of do that if you display the image on the canvas, and hide it under another layer or similar, but that's not always viable. It is also extremely cumbersome. I had no luck getting it to decode an image unless it was actually drawn to the canvas (so no displaying at 0 alpha etc.) although I must admit that testing of mine was done a while ago.

I was wondering if it's possible to tell it to decode a series of images, similar to how I can just give it a little loop of images to cache? And if that's sadly not possible, if there are any code workarounds, is it possible to make it decode the image when displayed despite it being initially shown completely invisible (whereas usually it would not decode presumably because it's not actually being drawn on the canvas)?

I'd appreciate any help!
If you load an image in the scene's create function, it preloads it. You can also add a load listener to the bitmap to only block transfer it when it finishes so that it definitely gets drawn.
If you load an image in the scene's create function, it preloads it. You can also add a load listener to the bitmap to only block transfer it when it finishes so that it definitely gets drawn.
By "preloads", I assume you mean both caches and decodes it? Just to make sure we're on the same page 😅

Unfortunately I'm not super sharp with this stuff, and also got help with what I'm currently doing:
JavaScript:
for (let i = 1; i <= 10; i++) {
const fn = "img" + i;
const bitmap = ImageManager.loadPicture(fn);
}
(My images are set up so the next image in the sequence is just incremented e.g. img_1, img_2, img_3. But this code caches it without decoding, although I forgot the code to check the current cache to double check it's doing it properly... but I digress.)

All that to say that I don't quite understand how to do what you mention, I really don't know much about the code of RPGM itself.
  • So it is possible to cache and decode (preload) the list of images I want?
  • How would you code it using the scene's create function you mention?
  • If you load let's say 100 images for an extreme example, would the load listener be what makes it so it doesn't move on until all 100 images are ready to be used? I.e., the event you put the script in waits until all images are ready.
  • Once the images are ready, using a normal "Show Picture" would have the image show immediately because it's already cached and decoded?
Sorry if any of that was asking similar stuff, just trying to understand as much as I can.

I really appreciate the help as I'm kinda lost here, but I use images so much it'd be a huge boon to make this work 😀
Well one example from my own project:

1737334761466.png


I load all of these images in the create function so that when I draw them in their respective windows/scene children, they're always there in memory from the first opening. But there's also:

1737334824706.png


This is for drawing characters in the social section of the game compendium; basically, if it's not already ready to draw, it adds a listener that won't draw it until it is.
The author DK uses preloading. I don't know the details, but take a look at DKVideoTool. It plays GIFS as well as videos and the pro version has the option to preload video instead of just loading video, so you may want to ask him about how preloading an image or images would work.

(There is a free version of the plugin as well, but only the pro version has preloading.)

If I understand correct, ImageManager.loadPicture already tries to decode the image after it is loaded.
The problem is you have to let it do when you are not using them so the player can't see the lag.
For example, create an arrray of sprites at the beginning of the map (when the fade-in has not begun), and use the sprites directly when you need.
But if the animation is too long, it will cost too much memory.

Also I have to see your actual setup to give you advice if you need. Do you play the animation using javascript, or using show picture command?
Well one example from my own project:

image

I load all of these images in the create function so that when I draw them in their respective windows/scene children, they're always there in memory from the first opening. But there's also:

image

This is for drawing characters in the social section of the game compendium; basically, if it's not already ready to draw, it adds a listener that won't draw it until it is.
It's currently up in the air when I have time to sit and work on this, but I'll take a look whenever I get the time and see if I can figure something out, thanks.

The author DK uses preloading. I don't know the details, but take a look at DKVideoTool. It plays GIFS as well as videos and the pro version has the option to preload video instead of just loading video, so you may want to ask him about how preloading an image or images would work.

(There is a free version of the plugin as well, but only the pro version has preloading.)

I don't play videos nor do I use GIFs, but I'll keep it in mind should I ever pursue that route, thank you.

If I understand correct, ImageManager.loadPicture already tries to decode the image after it is loaded.
The problem is you have to let it do when you are not using them so the player can't see the lag.
For example, create an arrray of sprites at the beginning of the map (when the fade-in has not begun), and use the sprites directly when you need.
But if the animation is too long, it will cost too much memory.

Also I have to see your actual setup to give you advice if you need. Do you play the animation using javascript, or using show picture command?
I don't think ImageManager.loadPicture decodes the image as I've done extensive testing around this. I'll set off some more time to double check later, but I'm fairly certain.
Let's use 10 images as an example. I've loaded those 10 images using loadPicture, both with and without waiting between. I have tested this with various time between loading and displaying them. When actually displaying them with Show Picture (both the event and throught script, although they should be the same as far as I know), upon first drawing it onto the canvas it has to decode them. I know this from checking with developer tools. Besides more concrete info like that, it's also quite obvious with how it lags on its first display but not subsequent ones (where the images have been decoded already).
Even if you use Show Picture, it doesn't actually seem to decode the images unless it's actually drawn onto the canvas. Meaning you can't display them at say, 0 alpha to load them in a hidden state.

What I currently do is a mix. I use ImageManager.loadPicture to quickly (and easily) cache the images, then I try to hide the loading of the images by displaying them on a layer beneath another image on the screen. As you can imagine, that is difficult to do if there is currently no other image to hide them under in some scenarios. I've tried displaying them at alpha 1, however it can be surprisingly visible depending on the images and the background they're displayed on.

I have okay-ish results using a mix of the above, but that's not great, and as a bonus it's extremely cumbersome to do. That is not ideal by any means when I have to do this hundreds and hundreds of times. I also thought it was odd there wasn't a way to make it decode the images without having to actually draw them on the canvas. Granted I am no expert when it comes to working with images in JavaScript, much less in the RMMZ engine.
Well it is in the source code loadBitmap decodes and creates a base texture so it is a fact.
The only missing step is the bitmap needs to be converted into a sprite to display.
Usually it is fast enough but maybe different hadwares have different results.
So I said creating an array of sprites at the beginning using the loaded bitmaps.

Maybe you can post a screenshots of your solution so we can test or maybe try to improve it.
Well it is in the source code loadBitmap decodes and creates a base texture so it is a fact.
The only missing step is the bitmap needs to be converted into a sprite to display.
Usually it is fast enough but maybe different hadwares have different results.
So I said creating an array of sprites at the beginning using the loaded bitmaps.

Maybe you can post a screenshots of your solution so we can test or maybe try to improve it.
I understand that you say it decodes the image, but regardless it doesn't decode the image for me (only caches it—I can see as much in the performance tab) until it is drawn onto the canvas by actually using Show Picture, so I must be doing something wrong.

An example of how I'm doing it now is this:
JavaScript:
for (let i = 1; i <= 10; i++) {
const fn = "img" + i;
const bitmap = ImageManager.loadPicture(fn);
}
This is currently used to just have them cached, this happens at whatever point before the images actually need to be used.

Once I actually need an animation to play I already have some CG up on the screen. This lets me "Show Picture" the animation I want to play underneath the CG which allows it to draw the image to the canvas and has it actually decode it. This is done with normal Show Picture for all images in the animation, with a short wait between each one.

Then I play the actual animation on the layer the "cover CG" was, and it plays smoothly usually. The performance here becomes worse the more images are required for the animation.

Here's a screenshot example as requested (although this is just one of many ways I've tried):
Spoiler: Image
Example_Animation_Display.jpg
However, this has three issues.
  1. It has a delay when you "load" the animation by displaying it in that hidden manner, the bigger the animation the bigger the delay needed to give it time to decode with the wait in-between each image. The wait is also kinda arbitrary, because different devices would be able to do it faster or slower.
    If you load it with a parallel event and continue to have something like dialogue for the player, the loading of all the images will cause lag instead.
    The delay between displaying the images, in this case 5 frames, is arbitrary. It does not, as an example, start loading all images and then wait until they are all decoded before continuing, so it uses as little time on all systems without risk of taking too long for no reason. It also risks the opposite; continuing before the images are decoded and as such have the animation not display completely smoothly.
  2. There's not always CG on the screen to hide the loading of the animation behind before it needs to be displayed, sometimes it cuts from the map directly to the animation.
  3. It's very labor intensive to do this for tens and tens of images for each and every animation. This is less important but still relevant.
To reitrate, the goal is basically to cache and decode the images shortly before they're used in an animation being displayed via repeated Show Picture commands.

As for creating an array of sprites like you suggest using loaded bitmaps, I am unsure how to do that. But I should mention that I cannot do it on map load, as all animations on a map are not always required. They should be limited to be loaded in the event itself that's about to use the images. But if an array of sprites were made, would that make them ready to be displayed through "normal means" like Show Picture? Would you need to change map to clear the array to avoid memory issues? Is there a way to manually clear specific images from memory besides changing map?
Not sure if this would help, but have you tried going the PIXI route?

I added it to the pictureContainer property to test. Average is 3-5ms per photo loaded and each photo is about 190KB each, which isn't that big, but it does need to load into RAM and then show up on the canvas.



I used a 2 wait frame, but it really should be played with 4 wait frames. I allow for at least a 2-3 picture frame overlap to compensate for any glitching (deleting quicker than loading) issues.

Here is the code I used:
s1.png


I'm assuming your computer is up-to-date on everything as well.

I have also tested it quickly with just some show picture script calls and I have the same success with loading and performance without preloading.

If I missed something, then I am not sure what else to do or offer.

For kicks, here is what the other code looks like.

also... ignore the 50 pages in the event... it's... for testing purposes... yeah... lol.

this is an old event with old code so I would do it differently this time around, but it works so no need to change it just yet:
s2.png
Thanks for this, took me some time to experiment with it.

So it is indeed useful for decreasing the labor involved with something like "Show Picture" x60 (if there were 60 frames in the example animation).

The first time it plays it still has poor performance (I use much larger images than the ones you showed in your video, so the ms required went up proportionally. I should point out I do not use bloated images, I optimize where I can). However, that isn't really an issue since the point is to "load" the images before they are actually displayed.

For example:
Use your method to display the animation in a manner the user cannot see. Then repeat where the user can see it (actually "display" the animation to the user), where this time it is "ready" and won't have any delays from caching or decoding.

The issue here is indeed, how would we hide the still "loading" animation before it's actually displayed to the user? As mentioned before what I've been doing is play it on a layer under another image that's already on screen.
An example would be to put the 1 CG you hide the loading under at layer 10, while you use layer 1-5 for playing the animation under. Then you actually play the animation on layer 10 after having deleted layer 1-5.
But I am unsure how to use layers with this method you've shared—and there's still the original issue I mentioned which is I do not always have a scene where I have CG to hide it behind, e.g. it cuts directly to an animation from the map scene.
I believe I once tested displaying images outside the canvas, but IIRC they were never decoded there because they weren't actually "drawn"—same for opacity at 0. Unsure if that would be different with this method.

A second issue is the wait time isn't always consistent. For example an animation may have wait times of 3, 7, and 9 frames for various reasons instead of 4 between every single one.

One more thing would be the time between displaying the frames when it's "loading" the animation. Let's say it's always set to 5 frames between images when it isn't shown to the user, since the point is just to prep the images (as opposed to 3, 7, 9 frames between images when displaying it to the user). On a slower device, like an older phone, 5 frames may not be enough to properly load all images before they're deleted, resulting in choppy/laggy display of the animation for the images that didn't initially load in time and as such have to do that mid-animation.
This is somewhat mitigated by loading several at a time (as opposed to all on layer 9, underneath the "cover picture" on layer 10 like I've been doing, so thanks for that idea), however it's still limited how fast a device can load images. All that to say it doesn't check when each image is done "loading" and then delete it, instead it waits the 5 frames or whatever, then deletes it. Or in your case, if I've read it correctly, it loads 4 images at a time and then the next one deletes the oldest image in the loop. So to reiterate, the issue there would be "What if the image that is deleted in that scenario, wasn't done loading/being displayed?"

Another thing I noticed is, to my understanding, by default when you transfer maps it'll clear cached images. And indeed looking at the network tab, using Show Picture has it GET the images again. That is not the case for the PIXI method you showed, so how would one clear those (manually)?

Original thread: https://forums.rpgmakerweb.com/threads/pre-emptively-decode-images.174814/