·您当前的位置:首页 > 技术教程 > AS2与AS3技术 >

[AS3]基于PNGEncoder实现PNG图片的透明

时间:2015-06-03 08:29酷播
[AS3]基于PNGEncoder实现PNG图片的透明

AS3]基于PNGEncoder实现PNG图片的透明

  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.                 ppng.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. } 

 

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器