Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Actionscript PNGEncoder performance and UI blocking

I'm trying to use PNGEncoder to encode a bitmapData object into a png ByteArray so I can send the data to the server. Everything would be peachy except the bitmapData is 4000x4000px and when I run the PNGEncoder.encode function on it the whole app stops (UI is blocked) for 5-8 seconds while it runs. Does anybody have any suggestions on how to not make it block so bad, I read about chunking up the process (since you can't multithread in AS3) but can't find any sample code on chunking up the process.

Thanks, Sam

like image 421
Shizam Avatar asked Mar 01 '23 06:03

Shizam


2 Answers

In addition to Arthur's comment, you could also write it in C/C++ for Alchemy, since alchemy supports green threads. Like PixelBender, Alchemy also requires Flash 10.

like image 155
Richard Szalay Avatar answered Mar 07 '23 23:03

Richard Szalay


There are mainly two ways to do this.

a) Use pixel bender: You can off load the work to pixel bender (a shade like language in as3). This has the advantage of using the gpu on some cases, but it also is assynchronous and non blocking (runs on another thread). But it does require player 10+. I haven't seen a pixel bender png encoder, and to be honest, it may not be possible (I am not familiar enough with png encoding to tell), but it might be an option. This is, performance wise, the best you can get. More info here

b) Use chuncking. Basically, you rewrite the encoder to encode blocks (lines, columns or a smaller area), and hook that to an enter frame event, each frame you'd call next on your encoder, until there is no more encoding to do. Zeh has a neat LWZ chunked encoder with source code that might give you insights into the details.

Cheers Arthur

like image 32
Arthur Debert Avatar answered Mar 08 '23 00:03

Arthur Debert