PageRenderTime 4506ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/src/com/transmote/flar/source/FLARLoaderSource.as

http://github.com/tcha-tcho/EZFLAR
ActionScript | 239 lines | 115 code | 35 blank | 89 comment | 6 complexity | 7ec9e24e3d312846de14532a075673f1 MD5 | raw file
  1. /*
  2. * PROJECT: FLARManager
  3. * http://transmote.com/flar
  4. * Copyright 2009, Eric Socolofsky
  5. * --------------------------------------------------------------------------------
  6. * This work complements FLARToolkit, developed by Saqoosha as part of the Libspark project.
  7. * http://www.libspark.org/wiki/saqoosha/FLARToolKit
  8. * FLARToolkit is Copyright (C)2008 Saqoosha,
  9. * and is ported from NYARToolkit, which is ported from ARToolkit.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version 2
  14. * of the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this framework; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. *
  25. * For further information please contact:
  26. * <eric(at)transmote.com>
  27. * http://transmote.com/flar
  28. *
  29. */
  30. package com.transmote.flar.source {
  31. import flash.display.Bitmap;
  32. import flash.display.BitmapData;
  33. import flash.display.Loader;
  34. import flash.display.LoaderInfo;
  35. import flash.display.Sprite;
  36. import flash.events.Event;
  37. import flash.events.IOErrorEvent;
  38. import flash.events.SecurityErrorEvent;
  39. import flash.geom.Matrix;
  40. import flash.geom.Rectangle;
  41. import flash.net.URLRequest;
  42. /**
  43. * use the contents of a Loader as a source image for FLARToolkit marker detection.
  44. * FLARLoaderSource samples the contents of a Loader against a white background,
  45. * to provide maximum contrast for marker detection.
  46. * this class can be used for testing marker detection without a camera,
  47. * for example with a swf or jpeg with valid patterns within.
  48. *
  49. * @author Eric Socolofsky
  50. * @url http://transmote.com/flar
  51. */
  52. public class FLARLoaderSource extends Sprite implements IFLARSource {
  53. private var _resultsToDisplayRatio:Number;
  54. private var loader:Loader;
  55. private var downsampleRatio:Number;
  56. private var displayWidth:Number;
  57. private var displayHeight:Number;
  58. private var displayBmpData:BitmapData;
  59. private var displayBitmap:Bitmap;
  60. private var displayMatrix:Matrix;
  61. private var sampleWidth:Number;
  62. private var sampleHeight:Number;
  63. private var sampleBmpData:BitmapData;
  64. private var sampleBitmap:Bitmap;
  65. private var sampleMatrix:Matrix;
  66. private var sampleMatRect:Rectangle; // area of sampleBmpData to fill with a white background before sending to FLARToolkit
  67. /**
  68. * constructor.
  69. * @param contentPath filename to load.
  70. * @param displayWidth width of source file.
  71. * @param displayHeight height of source file.
  72. * @param downsampleRatio amount to downsample camera input.
  73. * adjust to balance between image quality and marker tracking performance.
  74. * a value of 1.0 results in no downsampling;
  75. * a value of 0.5 (the default) downsamples the camera input by half.
  76. */
  77. public function FLARLoaderSource (contentPath:String, displayWidth:Number, displayHeight:Number, downsampleRatio:Number=0.5) {
  78. this.downsampleRatio = downsampleRatio;
  79. this.displayWidth = displayWidth;
  80. this.displayHeight = displayHeight;
  81. this.sampleWidth = this.displayWidth * this.downsampleRatio;
  82. this.sampleHeight = this.displayHeight * this.downsampleRatio;
  83. this._resultsToDisplayRatio = this.sampleWidth / this.displayWidth;
  84. this.sampleMatRect = new Rectangle(0, 0, this.sampleWidth, this.sampleHeight);
  85. this.loadContent(contentPath);
  86. }
  87. /**
  88. * update the BitmapData source used for analysis.
  89. */
  90. public function update () :void {
  91. this.displayBmpData.draw(this.loader, this.displayMatrix);
  92. if (this.sampleBmpData) {
  93. this.sampleBmpData.fillRect(this.sampleMatRect, 0xFFFFFFFF);
  94. this.sampleBmpData.draw(this.loader, this.sampleMatrix);
  95. }
  96. }
  97. /**
  98. * the BitmapData source used for analysis, constructed by FLARToolkit.
  99. * NOTE: returns the actual BitmapData object, not a clone.
  100. */
  101. public function get source () :BitmapData {
  102. return this.sampleBmpData;
  103. }
  104. public function set source (val:BitmapData) :void {
  105. // BitmapData downsampled from source video, sent to FLARToolkit every frame
  106. this.sampleBmpData = val;
  107. this.sampleBitmap = new Bitmap(this.sampleBmpData);
  108. this.sampleBitmap.width = this.displayWidth;
  109. this.sampleBitmap.height = this.displayHeight;
  110. // uncomment to view downsampled video sent to FLARToolkit
  111. // this.addChild(this.sampleBitmap);
  112. }
  113. /**
  114. * size of BitmapData source used for analysis.
  115. */
  116. public function get sourceSize () :Rectangle {
  117. return new Rectangle(0, 0, this.sampleWidth, this.sampleHeight);
  118. }
  119. /**
  120. * ratio of area of reported results to display size.
  121. * used to scale results of FLARToolkit analysis to correctly fit display area.
  122. */
  123. public function get resultsToDisplayRatio () :Number {
  124. return this._resultsToDisplayRatio;
  125. }
  126. /**
  127. * FLARLoaderSource cannot be mirrored;
  128. * method is here only for compliance with IFLARSource.
  129. */
  130. public function get mirrored () :Boolean {
  131. return false;
  132. }
  133. public function set mirrored (val:Boolean) :void {}
  134. /**
  135. * returns true if initialization is complete.
  136. * FLARLoaderSource is inited automatically in constructor.
  137. */
  138. public function get inited () :Boolean {
  139. return true;
  140. }
  141. /**
  142. * halts all processes and frees this instance for garbage collection.
  143. */
  144. public function dispose () :void {
  145. this.loader.unloadAndStop();
  146. this.displayBmpData.dispose();
  147. this.displayBmpData = null;
  148. this.displayBitmap = null;
  149. this.displayMatrix = null;
  150. if (this.sampleBmpData) {
  151. this.sampleBmpData.dispose();
  152. this.sampleBmpData = null;
  153. }
  154. this.sampleBitmap = null;
  155. this.sampleMatrix = null;
  156. }
  157. private function loadContent (path:String) :void {
  158. this.loader = new Loader();
  159. this.loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, this.onLoadError, false, 0, true);
  160. this.loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, this.onLoadError, false, 0, true);
  161. this.loader.contentLoaderInfo.addEventListener(Event.COMPLETE, this.onLoaded, false, 0, true);
  162. this.loader.load(new URLRequest(path));
  163. }
  164. private function onLoadError (evt:Event) :void {
  165. var errorText:String = "FLARLoaderSource load error.";
  166. if (evt is IOErrorEvent) {
  167. errorText = IOErrorEvent(evt).text;
  168. } else if (evt is SecurityErrorEvent) {
  169. errorText = SecurityErrorEvent(evt).text;
  170. }
  171. this.onLoaded(evt, new Error(errorText));
  172. }
  173. private function onLoaded (evt:Event, error:Error=null) :void {
  174. var loaderInfo:LoaderInfo = evt.target as LoaderInfo;
  175. loaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, this.onLoadError);
  176. loaderInfo.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, this.onLoadError);
  177. loaderInfo.removeEventListener(Event.COMPLETE, this.onLoaded);
  178. if (error) { throw error; }
  179. /*
  180. // FTK 2.5.1 -- now have to retrieve BitmapData from FTK.
  181. // BitmapData downsampled from source video, sent to FLARToolkit every frame
  182. this.sampleBmpData = new BitmapData(this.sampleWidth, this.sampleHeight, false, 0);
  183. this.sampleBitmap = new Bitmap(this.sampleBmpData);
  184. this.sampleBitmap.width = displayWidth;
  185. this.sampleBitmap.height = displayHeight;
  186. */
  187. // cropped, full-res video displayed on-screen
  188. this.displayBmpData = new BitmapData(this.displayWidth, this.displayHeight, false, 0);
  189. this.displayBitmap = new Bitmap(this.displayBmpData);
  190. // full-res Bitmap for display
  191. this.addChild(this.displayBitmap);
  192. this.buildSampleMatrices();
  193. this.dispatchEvent(new Event(Event.INIT));
  194. }
  195. private function buildSampleMatrices () :void {
  196. // construct transformation matrix used when updating displayed video
  197. // and when updating BitmapData source for FLARToolkit
  198. this.displayMatrix = new Matrix(1, 0, 0, 1);
  199. this.sampleMatrix = new Matrix(
  200. this._resultsToDisplayRatio, 0,
  201. 0, this._resultsToDisplayRatio);
  202. }
  203. }
  204. }