I was trying to further reduce the filesize of a SWF file by optimizing the embedded PNG graphics (using ImageOptim tool). To my surprise, this didn't yield any effect, so I created two test Images:
Original (433883 bytes)
Optimized (273723 bytes)
When embedding either of these assets in a simple ActionScript project, the compiled SWF is ~274kb in size. Which raises the question: Does Flex optimize embedded PNG assets during compile-time? If yes, is there some documentation about the optimization going on? It can't be because of the SWF compression alone, because zipping the images doesn't reduce filesize at all.
Here's the Code for completeness:
package
{
import flash.display.Sprite;
public class SizeTest extends Sprite
{
[Embed("/assets/original.png")]
private var ImageAsset:Class;
public function SizeTest(){
}
}
}
The answer is yes, the flex compiler does automatically compress data using one of the inbult algorithms (probably ZLIB) and transparently decompresses it on the other side (flash player) just before it gives you access to the uncompressed data.
I was embedding a ByteArray into an SWF, and although the bytes are huge outside (200 KB), when embedded into a SWF it turns into 30 KB, producing almost exactly the same results as manually compressing this using ByteArray.compress("zlib")
.
I then tried manually compressing PNG/JPEG bitmaps, and it turned out larger than embedding it plainly using the [Embed]
tag (172 KB as a compressed ByteArray vs. 168 KB as an embedded image). Letting the Flex compiler handle embedded data compression is actually better than trying to do your own tricks on the ByteArray side.
Edit: To answer your question on PNG embedding, its hard to tell what's going on inside the flex compiler/flash player. Although known for proper documentation, Adobe is also known for many "undocumented features". Your specific question is best sent to a flash player architect (eg Tinic Uro) or an adobe evangelist (eg Lee Brimelow), some of whom you can easily reach on facebook (see this list).
The swf format has internally a special format for 32 bit PNGs (those with an alpha channel) where they get split up into a 24bit png and a greyscale alpha mask, whereby the alpha mask gets JPEG compressed. No totally sure if Flex Builder does that kind of optimization since I remember that at least in an older version embedded PNG where not at all optimized.
Nevertheless, if you are looking for a tool that can optimize embedded images in swfs you should check out Joa Ebert's "Reducer": http://blog.joa-ebert.com/2009/08/08/reducer/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With