Show incomplete please move to author's official blog: http://www.sineysoft.com/blog/post/124.html
Webp is a new Internet image format released by Google. Compared with traditional jpg and PNG images, webp has a larger image compression ratio without significant loss of image quality, at the same time, it supports alpha-channel images and has a higher compression ratio than the transparent image compression method (Channel Separation + jpg image compression) provided by flash, therefore, if webp is used in Flash games, the resource size will be significantly reduced and the download will be accelerated. Unfortunately, flash player does not directly provide webp support, we can use alchemy to port webp code.
I started searching for a blog on Google and did the same thing. But after downloading it, I found that SWC does not support image formats with Alpha channels, isn't this the Code provided by Google? Does he still have deletions? No worries, So I downloaded the webp source code and transplanted it to alchemy to find various problems,
No wonder this is not supported. First, alchemy does not support inline assembly of _ ASM _. After being changed to C code, it finds that its own library does not support alpha channel images, various logs are added to the Code to track the problem. It is found that alchemy does not support the uint64 displacement operation. All <,> operations return 0, this is terrible. There is no way to replace N multi-displacement operations with the handwritten C code. It can finally be compiled and decoded with an alpha channel.
The final generated webpswc. SWC is here
All webp formats are supported, including the webp format with alpha channel.
The test code is as follows:
Package
{
Import flash. display .*;
Import flash. display. Bitmap;
Import flash. display. bitmapdata;
Import flash. display. Sprite;
Import flash. Geom .*;
Import flash. utils .*;
Import flash. utils. bytearray;
Import flash.net .*;
Import flash. Events .*;
Import sineysoft. webpswc;
Public class webptest extends Sprite
{
[Embed (Source = "IMG. webp", mimetype = "application/octet-stream")]
Private const webpimageclass: class;
[Embed (Source = "Monster. webp", mimetype = "application/octet-stream")]
Private const monterclass: class;
Public Function webptest ()
{
Stage. scalemode = stagescalemode. no_scale;
Stage. align = stagealign. top_left;
Init ();
}
Private function Init (): void
{
VaR now: Int = gettimer ();
VaR BA: bytearray = new webpimageclass () as bytearray;
VaR bitmapdata: bitmapdata = webpswc. Decode (BA );
Trace (gettimer ()-Now );
Addchild (New Bitmap (bitmapdata as bitmapdata ));
Now = gettimer ();
BA = new monterclass () as bytearray;
Bitmapdata = webpswc. Decode (BA );
Trace (gettimer ()-Now );
Addchild (New Bitmap (bitmapdata as bitmapdata ));
}
}
}
Data comparison
Format |
---- |
Corresponding to webp size, standard webp output quality 80 |
Monster.png (png8 format, with alpha channel) 44452 bytes
|
|
23814 bytes
Chrome browser is required for preview |
Img.jpg (Photoshop output quality 8) 232407 bytes Click to view the source Image |
|
91238 bytes Chrome browser is required for preview |
Monster.swf (with SwF compression, JPG compression quality 80) 28492 bytes |
|
23814 bytes Chrome browser is required for preview |
The final test SWF is as follows:
Click to view
SWC Library:
The final generated webpswc. SWC is here
Weaknesses
The webp decoding speed transplanted with alchemy cannot be compared with that of the native C code, or even can be described as unbearable. The native C code decoding webp only takes about 1-4 ms, the Alchemy version requires-Ms. The decoding speed cannot be directly used for game resource download. Otherwise, the client gets stuck after one decoding, but there is no optimization solution.
Optimization Method 1: use frame splitting decoding. If you have time, you can modify the existing SWC library and support frame splitting decoding.
Optimization Method 2: The worker provided by flash 11.5 is used for full background decoding and multi-core computing. However, this optimization method should be effective, but it is a pity that there are requirements for the flash version.
Decode time:
Compress compare:
If webp is supported by flash, it should be a good message.