This is the quick-start guide to my PNGEncoder2 library, which compresses BitmapData objects into PNG files (stored in ByteArray objects). For a more in-depth explanation of how it works check out my blog post about it.
-
Blazing fast performance: Written in haXe and highly tuned for speed, it can outperform the as3corelib PNGEncoder by up to 5x (on the FAST setting)! This is made possible by the custom DEFLATE library I wrote from scratch specifically for this encoder.
-
Compression options: Also made possible by my custom DEFLATE implementation, you can now choose between compression levels: FAST, NORMAL, and GOOD (and UNCOMPRESSED too, but...).
-
True asynchronous encoding: Most other asynchronous PNG encoders do the conversion from bitmap data to PNG data asynchronously, then call ByteArray.compress() at the end -- which is a long, synchronous operation that can take half the total encoding time or more. (An exception is the cool In-Spirit PNG encoder, but its asynchronous mode is amazingly slow, and the SWC weighs in at over 100KB.) Mine does completely smooth encoding, asynchronously (with progress events!) and scales to a target FPS without wasting any cycles idling.
-
Large image support: Can compress ridiculously large images on-the-fly, since it does the work in manageable chunks (when done asynchronously) instead of all at once. There is no single-step bottleneck.
-
Opaque bitmap support: If your BitmapData is not transparent, then it will be automatically encoded using the (generally smaller) 24-bit PNG format, otherwise the 32-bit format is used. (Note that transparency is detected via the
transparent
property of the BitmapData object, and not by scanning every pixel, which would be too slow.) -
Open source: Use the haXe classes if you have a haXe project, or build against an SWC if you have an AS3 project. See the licenses at the top of PNGEncoder2.hx and DeflateStream.hx (both have separate, but similarly permissive, licenses).
PNGEncoder2 supports both synchronous (fast but freezes the UI while it encodes) and asynchronous PNG encoding (a smidgeon slower, but can maintain a target FPS).
var bmp : BitmapData = ...; // The bitmap you want to encode
var png : ByteArray; // Variable to store the result
PNGEncoder2.level = CompressionLevel.FAST; // Optional. Defaults to FAST
png = PNGEncoder2.encode(bmp);
var bmp : BitmapData = ...; // The bitmap you want to encode
PNGEncoder2.level = CompressionLevel.FAST; // Optional. Defaults to FAST
var encoder : PNGEncoder2 = PNGEncoder2.encodeAsync(bmp);
encoder.targetFPS = 12; // Optional. Defaults to 20. Lower FPSs yield faster compression
// Because the encoder is guaranteed to fire the COMPLETE event
// after at least one frame, it's safe to attach the listener after
// starting the encoding (as long as it's done before the next frame)
encoder.addEventListener(Event.COMPLETE, function (e) {
var png : ByteArray = encoder.png;
});
// encoder also dispatches ProgressEvent.PROGRESS events if you
// want to be notified of progress
Note that this library requires Flash 10 or higher. Unfortunately, until Adobe decides what they're doing with the next version of Alchemy, this library won't work in any SWF compiled to target Flash player 11 (SWF version 13) or above (as of Flash Player 11.2). So, you can target Flash 10 (SWF versions 10-12) only for now.
If you're using ActionScript 3 you can simply download the SWC file and link it to your project. There are also "slim" versions of the library that contain only a single compression method each; these are recommended for production since they are smaller (assuming you only use one compression level).
To link an SWC into a project made with the Flash CS5.5 IDE, the steps are as follows:
- In the File menu, select "ActionScript Settings...".
- Under the "Library Path" tab, click the little red icon with the "f" logo on it. The tooltip of the button reads: "Browse to SWC file"
- Browse to wherever it was that you saved the SWC, and select it
- Press OK!
To link an SWC in other versions of the Flash IDE, follow this guide instead.
If you're using haXe, just use the source directly instead of fiddling with SWCs (actually, your project won't compile if you use the SWC). All you need are the PNGEncoder2.hx and DeflateStream.hx files. Note that this project was built for haXe 2; I haven't tested it under the new haXe 3 compiler.
If you want to have only one compression method compiled
(to cut down on generated code size), add -D FAST_ONLY
,
-D NORMAL_ONLY
, or -D GOOD_ONLY
to your compile options
(this is equivalent to using the slim SWC versions).