/src/away3d/loaders/parsers/ImageParser.as

http://github.com/away3d/away3d-core-fp11 · ActionScript · 165 lines · 110 code · 27 blank · 28 comment · 38 complexity · b4f61dacc0736bfe92240148dca92270 MD5 · raw file

  1. package away3d.loaders.parsers
  2. {
  3. import away3d.arcane;
  4. import away3d.events.AssetEvent;
  5. import away3d.library.assets.BitmapDataAsset;
  6. import away3d.textures.ATFTexture;
  7. import away3d.textures.BitmapTexture;
  8. import away3d.textures.Texture2DBase;
  9. import away3d.tools.utils.TextureUtils;
  10. import flash.display.Bitmap;
  11. import flash.display.BitmapData;
  12. import flash.display.Loader;
  13. import flash.events.Event;
  14. import flash.utils.ByteArray;
  15. use namespace arcane;
  16. /**
  17. * ImageParser provides a "parser" for natively supported image types (jpg, png). While it simply loads bytes into
  18. * a loader object, it wraps it in a BitmapDataResource so resource management can happen consistently without
  19. * exception cases.
  20. */
  21. public class ImageParser extends ParserBase
  22. {
  23. private var _byteData:ByteArray;
  24. private var _startedParsing:Boolean;
  25. private var _doneParsing:Boolean;
  26. private var _loader:Loader;
  27. /**
  28. * Creates a new ImageParser object.
  29. * @param uri The url or id of the data or file to be parsed.
  30. * @param extra The holder for extra contextual data that the parser might need.
  31. */
  32. public function ImageParser()
  33. {
  34. super(ParserDataFormat.BINARY);
  35. }
  36. /**
  37. * Indicates whether or not a given file extension is supported by the parser.
  38. * @param extension The file extension of a potential file to be parsed.
  39. * @return Whether or not the given file type is supported.
  40. */
  41. public static function supportsType(extension:String):Boolean
  42. {
  43. extension = extension.toLowerCase();
  44. return extension == "jpg" || extension == "jpeg" || extension == "png" || extension == "gif" || extension == "bmp" || extension == "atf";
  45. }
  46. /**
  47. * Tests whether a data block can be parsed by the parser.
  48. * @param data The data block to potentially be parsed.
  49. * @return Whether or not the given data is supported.
  50. */
  51. public static function supportsData(data:*):Boolean
  52. {
  53. //shortcut if asset is IFlexAsset
  54. if (data is Bitmap)
  55. return true;
  56. if (data is BitmapData)
  57. return true;
  58. if (!(data is ByteArray))
  59. return false;
  60. var ba:ByteArray = data as ByteArray;
  61. ba.position = 0;
  62. if (ba.readUnsignedShort() == 0xffd8)
  63. return true; // JPEG, maybe check for "JFIF" as well?
  64. ba.position = 0;
  65. if (ba.readShort() == 0x424D)
  66. return true; // BMP
  67. ba.position = 1;
  68. if (ba.readUTFBytes(3) == 'PNG')
  69. return true;
  70. ba.position = 0;
  71. if (ba.readUTFBytes(3) == 'GIF' && ba.readShort() == 0x3839 && ba.readByte() == 0x61)
  72. return true;
  73. ba.position = 0;
  74. if (ba.readUTFBytes(3) == 'ATF')
  75. return true;
  76. return false;
  77. }
  78. /**
  79. * @inheritDoc
  80. */
  81. protected override function proceedParsing():Boolean
  82. {
  83. var asset:Texture2DBase;
  84. if (_data is Bitmap) {
  85. asset = new BitmapTexture(Bitmap(_data).bitmapData);
  86. finalizeAsset(asset, _fileName);
  87. return PARSING_DONE;
  88. }
  89. if (_data is BitmapData) {
  90. asset = new BitmapTexture(_data as BitmapData);
  91. finalizeAsset(asset, _fileName);
  92. return PARSING_DONE;
  93. }
  94. _byteData = getByteData();
  95. if (!_startedParsing) {
  96. _byteData.position = 0;
  97. if (_byteData.readUTFBytes(3) == 'ATF') {
  98. _byteData.position = 0;
  99. asset = new ATFTexture(_byteData);
  100. finalizeAsset(asset, _fileName);
  101. return PARSING_DONE;
  102. } else {
  103. _loader = new Loader();
  104. _loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
  105. _loader.loadBytes(_byteData);
  106. _startedParsing = true;
  107. }
  108. }
  109. return _doneParsing;
  110. }
  111. /**
  112. * Called when "loading" is complete.
  113. */
  114. private function onLoadComplete(event:Event):void
  115. {
  116. var bmp:BitmapData = Bitmap(_loader.content).bitmapData;
  117. var asset:BitmapTexture;
  118. _loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onLoadComplete);
  119. if (!TextureUtils.isBitmapDataValid(bmp)) {
  120. var bmdAsset:BitmapDataAsset = new BitmapDataAsset(bmp);
  121. bmdAsset.name = _fileName;
  122. dispatchEvent(new AssetEvent(AssetEvent.TEXTURE_SIZE_ERROR, bmdAsset));
  123. bmp = new BitmapData(8, 8, false, 0x0);
  124. //create chekerboard for this texture rather than a new Default Material
  125. var i:uint, j:uint;
  126. for (i = 0; i < 8; i++) {
  127. for (j = 0; j < 8; j++) {
  128. if ((j & 1) ^ (i & 1))
  129. bmp.setPixel(i, j, 0XFFFFFF);
  130. }
  131. }
  132. }
  133. asset = new BitmapTexture(bmp);
  134. finalizeAsset(asset, _fileName);
  135. _doneParsing = true;
  136. }
  137. }
  138. }