3

I'm optimizing a PNG encoder for use within Flash SWFs (but I'm writing it in Haxe).

I've so far managed to speed things up a fair bit, mostly by using speedy memory read/writes.

While profiling my code, I noticed that about 75% of the time is spent in a single call to Flash's built-in compress() method, which uses the zlib algorithm (which uses DEFLATE in turn).

I'm not too fussy about compression ratio, but faster compression would be awesome. So, I was wondering if it's worthwhile to try to implement zlib/DEFLATE myself. A little searching has turned up the zlib C-implementation source, but it's not exactly light reading (or easy to port!).

Are there any simple implementations of zlib/DEFLATE that I could use as a starting point? I'd rather not have to wade through the two specs, especially since I don't know whether an implementation running in AVM2 could even perform as fast as the native Flash one in the first place.

3 Answers 3

3

I don't think you will be able to achieve better performances with a non-native deflate algorithm. You might be able to run it asynchronously, though, which could make a difference. If you want to port it, I think it would be best to find an AS3 or JavaScript implementation, like https://github.com/dankogai/js-deflate . Good Luck!

6
  • 1
    it seems there is an haXe written Inflate algorithm here code.google.com/p/hxformat/source/browse/trunk/format/tools/…
    – Waneck
    Jun 13, 2011 at 17:40
  • Thanks Waneck, I'll take a look at those. The HaXe inflate is interesting, but I only need deflating ;-)
    – Cameron
    Jun 13, 2011 at 18:39
  • @Cameron Oh, got it : ) .There seems to have a port of zlib for as3 here code.google.com/p/as3zlib . I haven't looked too deeply into it, but it seems it implements the whole thing from scratch. It's probably going to perform worst, but then you could calculate a part of it each ENTER_FRAME so it doesn't hang the app. Cheers!
    – Waneck
    Jun 14, 2011 at 10:42
  • @Waneck: Ooh, that should come in handy. After reading the author's comment, I realized that Flash's compress() isn't using deflate, it's using zlib, which in turn uses deflate (and the PNG spec says that the compression should be zlib format). So, I still need to implement deflate, but I've been neglecting zlib ;-)
    – Cameron
    Jun 14, 2011 at 14:44
  • The zlib format is only a simple wrapper around the deflate format. Thus if you can deflate and are able to calculate the Adler32 checksum (which is quite easy), you have the zlib format. Jun 15, 2011 at 23:57
3

I ended up implementing deflate/zlib from scratch, and have just released it, and the PNG encoder that's built on top of it, on GitHub (README). I also wrote a blog post about the PNG encoder, with a benchmark comparing it to other encoders.

The compression implementation is contained in DeflateStream.hx, and does manage to speed up the encoding process significantly (and allows it to be made completely asynchronous).

1

try to use fzip or ascompress

1
  • Thanks, but those both use Flash's built-in deflate()/compress() methods for the actual compression
    – Cameron
    Jun 13, 2011 at 11:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.