2
0

PNGEncoder.as 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. Copyright (c) 2008, Adobe Systems Incorporated
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. * Redistributions of source code must retain the above copyright notice,
  8. this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. * Neither the name of Adobe Systems Incorporated nor the names of its
  13. contributors may be used to endorse or promote products derived from
  14. this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  16. IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  17. THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  18. PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  19. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. package com.adobe.images
  28. {
  29. import flash.geom.*;
  30. import flash.display.Bitmap;
  31. import flash.display.BitmapData;
  32. import flash.utils.ByteArray;
  33. /**
  34. * Class that converts BitmapData into a valid PNG
  35. */
  36. public class PNGEncoder
  37. {
  38. /**
  39. * Created a PNG image from the specified BitmapData
  40. *
  41. * @param image The BitmapData that will be converted into the PNG format.
  42. * @return a ByteArray representing the PNG encoded image data.
  43. * @langversion ActionScript 3.0
  44. * @playerversion Flash 9.0
  45. * @tiptext
  46. */
  47. public static function encode(img:BitmapData):ByteArray {
  48. // Create output byte array
  49. var png:ByteArray = new ByteArray();
  50. // Write PNG signature
  51. png.writeUnsignedInt(0x89504e47);
  52. png.writeUnsignedInt(0x0D0A1A0A);
  53. // Build IHDR chunk
  54. var IHDR:ByteArray = new ByteArray();
  55. IHDR.writeInt(img.width);
  56. IHDR.writeInt(img.height);
  57. IHDR.writeUnsignedInt(0x08060000); // 32bit RGBA
  58. IHDR.writeByte(0);
  59. writeChunk(png,0x49484452,IHDR);
  60. // Build IDAT chunk
  61. var IDAT:ByteArray= new ByteArray();
  62. for(var i:int=0;i < img.height;i++) {
  63. // no filter
  64. IDAT.writeByte(0);
  65. var p:uint;
  66. var j:int;
  67. if ( !img.transparent ) {
  68. for(j=0;j < img.width;j++) {
  69. p = img.getPixel(j,i);
  70. IDAT.writeUnsignedInt(
  71. uint(((p&0xFFFFFF) << 8)|0xFF));
  72. }
  73. } else {
  74. for(j=0;j < img.width;j++) {
  75. p = img.getPixel32(j,i);
  76. IDAT.writeUnsignedInt(
  77. uint(((p&0xFFFFFF) << 8)|
  78. (p>>>24)));
  79. }
  80. }
  81. }
  82. IDAT.compress();
  83. writeChunk(png,0x49444154,IDAT);
  84. // Build IEND chunk
  85. writeChunk(png,0x49454E44,null);
  86. // return PNG
  87. return png;
  88. }
  89. private static var crcTable:Array;
  90. private static var crcTableComputed:Boolean = false;
  91. private static function writeChunk(png:ByteArray,
  92. type:uint, data:ByteArray):void {
  93. if (!crcTableComputed) {
  94. crcTableComputed = true;
  95. crcTable = [];
  96. var c:uint;
  97. for (var n:uint = 0; n < 256; n++) {
  98. c = n;
  99. for (var k:uint = 0; k < 8; k++) {
  100. if (c & 1) {
  101. c = uint(uint(0xedb88320) ^
  102. uint(c >>> 1));
  103. } else {
  104. c = uint(c >>> 1);
  105. }
  106. }
  107. crcTable[n] = c;
  108. }
  109. }
  110. var len:uint = 0;
  111. if (data != null) {
  112. len = data.length;
  113. }
  114. png.writeUnsignedInt(len);
  115. var p:uint = png.position;
  116. png.writeUnsignedInt(type);
  117. if ( data != null ) {
  118. png.writeBytes(data);
  119. }
  120. var e:uint = png.position;
  121. png.position = p;
  122. c = 0xffffffff;
  123. for (var i:int = 0; i < (e-p); i++) {
  124. c = uint(crcTable[
  125. (c ^ png.readUnsignedByte()) &
  126. uint(0xff)] ^ uint(c >>> 8));
  127. }
  128. c = uint(c^uint(0xffffffff));
  129. png.position = e;
  130. png.writeUnsignedInt(c);
  131. }
  132. }
  133. }