731032406
Flash/Flex构架师 / 广东
积分 0

actionscript3.0中PNGEncoder里是如何处理PNG图片透明


2010-07-05 点击:


 

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

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

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

 

    731032406  版权所有
    禁止任何用途(禁止转载、商用和个人使用)


所属分类:技术经验分享

本文标签:处理 PNGEncoder

各位正在潜水的同学请注意,有0位无聊人士 在EBIBI附近出没!







    点击我更换图片 看不清
    评论内容 (*必填):

    (Ctrl + Enter 快速提交)