PDA

View Full Version : AIR What is the fastest way to load a .png



kenshinx
September 19th, 2009, 01:02 AM
Hi,

I'm trying to figure out the fastest possible way to load a .png image into a BitmapData object. I am performing multiple processes that do this (load a .png from filesystem). The progress is shown using a loading bar, and I would like to speed up the loading time. Now this process can be called over n number times (via a timer). Right now the method I am using to this is:


private function loadPNG(e:TimerEvent):void
{
var pngStream:FileStream = new FileStream();
pngStream.open(new File("path/to/png/file.png",FileMode.READ));
var pngBytes:ByteArray = new ByteArray();
pngStream.readBytes(pngBytes,0,pngStream.bytesAvai lable);
pngStream.close();
var pngLoader:Loader = new Loader();
pngLoader.width = pngLoader.height = 172;
pngLoader.contentLoaderInfo.addEventListener(Event .COMPLETE, finishLoading);
pngLoader.loadBytes(pngBytes);
}
private function finishLoading(e:Event):void
{
var png:Bitmap = new Bitmap();
png.bitmapData = new BitmapData(e.target.width, e.target.height, true, 0x00FF00);
png.bitmapData.draw(e.target.content)
}
I think the reason that my loading time is so slow is because it is asynchronous. It has to wait until the content is finished loading and then can move on. This loading speed is acceptable for about 170-200 times, but any higher than that and it seems like it takes forever.

Thanks.

Krilnon
September 19th, 2009, 10:48 PM
Since you're using FileStream#open, the readBytes calls are actually fetching the data synchronously. FileStream#openAsync is the asynchronous version.

If you were to use the asynchronous version, you would be able to do other stuff while loading the images, so it might be 'faster' in the sense that your IO aren't blocking execution. However, the file is probably going to take a similar amount of time to load either way… since it seems like the bottleneck would be the speed of the disk.

kenshinx
September 20th, 2009, 08:29 PM
Thanks for the reply,

Since the loadbar is there for viewing and informative purposes, there is nothing else to be done. One process must happen at a time. After that one is done the next one starts. This is kind like my implementation of multithreading. There has got to be some way to load a .png without adding the onComplete listener to the loader. That is what seems like to be slowing it down. As soon as I comment out that part of the process (there is more, that is not all that important: loading text from a file), the loadbar is done in a matter of seconds, as apposed to 10 seconds.

Krilnon
September 20th, 2009, 10:44 PM
Is there any chance that you could put together a quick demo file of what's going on? (Or perhaps just post up your existing file, if it doesn't have a bunch of irrelevant or sensitive stuff)


There has got to be some way to load a .png without adding the onComplete listener to the loader. That is what seems like to be slowing it down.

Well, you could parse the PNG file yourself and insert the results into a bitmap (all synchronously), but I suspect that Flash Player's native PNG decoder is faster than one that anyone could write in ActionScript.

kenshinx
September 20th, 2009, 11:58 PM
My code lies within multiple classes, including the main timeline, that do way more than the previous mentioned. You're probably right about the "hand-made" PNG Decoder, it would only make my loading slower. The gist of my processing lies within the timer. The timer starts, stops, then does the .png load process, then starts again. The timer must wait until one process is done, before it can start another. Maybe explaining what my program does might help: I am making a Content manager for the fighting game engine: MUGEN. What I need to happen is load in my pre-extracted sprites (.png) and display it.