home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 2010 Software/Programs / PCGuia_programas.iso / Software / Utils / Livebrush / Install-LivebrushLite.air / livebrush.swf / scripts / com / adobe / images / PNGEncoder.as
Encoding:
Text File  |  2009-10-26  |  3.0 KB  |  110 lines

  1. package com.adobe.images
  2. {
  3.    import flash.display.BitmapData;
  4.    import flash.geom.*;
  5.    import flash.utils.ByteArray;
  6.    
  7.    public class PNGEncoder
  8.    {
  9.       private static var crcTable:Array;
  10.       
  11.       private static var crcTableComputed:Boolean = false;
  12.       
  13.       public function PNGEncoder()
  14.       {
  15.          super();
  16.       }
  17.       
  18.       private static function writeChunk(png:ByteArray, type:uint, data:ByteArray) : void
  19.       {
  20.          var c:uint = 0;
  21.          var n:uint = 0;
  22.          var k:uint = 0;
  23.          if(!crcTableComputed)
  24.          {
  25.             crcTableComputed = true;
  26.             crcTable = [];
  27.             for(n = 0; n < 256; n++)
  28.             {
  29.                c = n;
  30.                for(k = 0; k < 8; k++)
  31.                {
  32.                   if(c & 1)
  33.                   {
  34.                      c = uint(uint(3988292384) ^ uint(c >>> 1));
  35.                   }
  36.                   else
  37.                   {
  38.                      c = uint(c >>> 1);
  39.                   }
  40.                }
  41.                crcTable[n] = c;
  42.             }
  43.          }
  44.          var len:uint = 0;
  45.          if(data != null)
  46.          {
  47.             len = data.length;
  48.          }
  49.          png.writeUnsignedInt(len);
  50.          var p:uint = png.position;
  51.          png.writeUnsignedInt(type);
  52.          if(data != null)
  53.          {
  54.             png.writeBytes(data);
  55.          }
  56.          var e:uint = png.position;
  57.          png.position = p;
  58.          c = 4294967295;
  59.          for(var i:int = 0; i < e - p; i++)
  60.          {
  61.             c = uint(crcTable[(c ^ png.readUnsignedByte()) & uint(255)] ^ uint(c >>> 8));
  62.          }
  63.          c = uint(c ^ uint(4294967295));
  64.          png.position = e;
  65.          png.writeUnsignedInt(c);
  66.       }
  67.       
  68.       public static function encode(img:BitmapData) : ByteArray
  69.       {
  70.          var p:uint = 0;
  71.          var j:int = 0;
  72.          var png:ByteArray = new ByteArray();
  73.          png.writeUnsignedInt(2303741511);
  74.          png.writeUnsignedInt(218765834);
  75.          var IHDR:ByteArray = new ByteArray();
  76.          IHDR.writeInt(img.width);
  77.          IHDR.writeInt(img.height);
  78.          IHDR.writeUnsignedInt(134610944);
  79.          IHDR.writeByte(0);
  80.          writeChunk(png,1229472850,IHDR);
  81.          var IDAT:ByteArray = new ByteArray();
  82.          for(var i:int = 0; i < img.height; i++)
  83.          {
  84.             IDAT.writeByte(0);
  85.             if(!img.transparent)
  86.             {
  87.                for(j = 0; j < img.width; j++)
  88.                {
  89.                   p = img.getPixel(j,i);
  90.                   IDAT.writeUnsignedInt(uint((p & 0xFFFFFF) << 8 | 0xFF));
  91.                }
  92.             }
  93.             else
  94.             {
  95.                for(j = 0; j < img.width; j++)
  96.                {
  97.                   p = img.getPixel32(j,i);
  98.                   IDAT.writeUnsignedInt(uint((p & 0xFFFFFF) << 8 | p >>> 24));
  99.                }
  100.             }
  101.          }
  102.          IDAT.compress();
  103.          writeChunk(png,1229209940,IDAT);
  104.          writeChunk(png,1229278788,null);
  105.          return png;
  106.       }
  107.    }
  108. }
  109.  
  110.