智慧 + 毅力 = 无所不能

正确性、健壮性、可靠性、效率、易用性、可读性、可复用性、兼容性、可移植性...

导航

PNGEncoder里是如何处理PNG图片透明

Posted on 2009-11-20 09:49  Bill Yuan  阅读(2092)  评论(0编辑  收藏  举报

 1 package
 2 {
 3         import flash.geom.*;
 4         import flash.display.*;
 5         import flash.utils.*;
 6         public class PNGEncoder
 7         {
 8 
 9             public static function encode(img:BitmapData):ByteArray {
10                 // Create output byte array
11                 var png:ByteArray = new ByteArray();
12                 // Write PNG signature
13                 png.writeUnsignedInt(0x89504e47);
14                 png.writeUnsignedInt(0x0D0A1A0A);
15                 // Build IHDR chunk
16                 var IHDR:ByteArray = new ByteArray();
17                 IHDR.writeInt(img.width);
18                 IHDR.writeInt(img.height);
19                 IHDR.writeUnsignedInt(0x08060000); // 32bit RGBA
20                 IHDR.writeByte(0);
21                 writeChunk(png,0x49484452,IHDR);
22                 // Build IDAT chunk
23                 var IDAT:ByteArray= new ByteArray();
24                 for(var i:int=0;i < img.height;i++) {
25                     // no filter
26                     IDAT.writeByte(0);
27                     var p:uint;
28                     if ( !img.transparent ) {
29                         for(var j:int=0;j < img.width;j++) {
30                             p = img.getPixel(j,i);
31                             IDAT.writeUnsignedInt(
32                                 uint(((p&0xFFFFFF<< 8)|0xFF));
33                         }
34                     } else {
35                         for(var k:int=0;k < img.width;k++) {
36                             p = img.getPixel32(k,i);
37                             IDAT.writeUnsignedInt(
38                                 uint(((p&0xFFFFFF<< 8)|(p >>> 24)));
39                         }
40                     }
41                 }
42                 IDAT.compress();
43                 writeChunk(png,0x49444154,IDAT);
44                 // Build IEND chunk
45                 writeChunk(png,0x49454E44,null);
46                 // return PNG
47                 return png;
48             }
49 
50             private static var crcTable:Array;
51             private static var crcTableComputed:Boolean = false;
52 
53             private static function writeChunk(png:ByteArray,
54                     type:uint, data:ByteArray) {
55                 if (!crcTableComputed) {
56                     crcTableComputed = true;
57                     crcTable = [];
58                     for (var n:uint = 0; n < 256; n++) {
59                         var c_1:uint = n;
60                         for (var k:uint = 0; k < 8; k++) {
61                             if (c_1 & 1) {
62                                 c_1 = uint(uint(0xedb88320^
63                                     uint(c_1 >>> 1));
64                             } else {
65                                 c_1 = uint(c_1 >>> 1);
66                             }
67                         }
68                         crcTable[n] = c_1;
69                     }
70                 }
71                 var len:uint = 0;
72                 if (data != null) {
73                     len = data.length;
74                 }
75                 png.writeUnsignedInt(len);
76                 var p:uint = png.position;
77                 png.writeUnsignedInt(type);
78                 if ( data != null ) {
79                     png.writeBytes(data);
80                 }
81                 var e:uint = png.position;
82                 png.position = p;
83                 var c:uint = 0xffffffff;
84                 for (var i:int = 0; i < (e-p); i++) {
85                     c = uint(crcTable[
86                         (c ^ png.readUnsignedByte()) &
87                         uint(0xff)] ^ uint(c >>> 8));
88                 }
89                 c = uint(c^uint(0xffffffff));
90                 png.position = e;
91                 png.writeUnsignedInt(c);
92             }
93         }
94 }
95 
96 
97 

问题已经解决

只用在 new BitmapData()里加上最后一个参数赋值为0x00就可以了.经试验正确无误.

var myBitmapData:BitmapData = new BitmapData(480, 360,true,0x00);
myBitmapData.draw(_base);