| 1 |
|
|---|
| 2 |
/** |
|---|
| 3 |
* - SirdsAS3 - |
|---|
| 4 |
* RDS/SISステレオグラム生成ライブラリ |
|---|
| 5 |
* |
|---|
| 6 |
* RDSImageCreatorクラス |
|---|
| 7 |
* グレースケール画像(深度マップ画像)からRDSイメージを生成 |
|---|
| 8 |
* |
|---|
| 9 |
* @author dsler |
|---|
| 10 |
* @version 1.0.0 |
|---|
| 11 |
* @update 2009/07/02 |
|---|
| 12 |
* |
|---|
| 13 |
* Licensed under The MIT License |
|---|
| 14 |
* Copyright (c) 2009 dsler |
|---|
| 15 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|---|
| 16 |
* of this software and associated documentation files (the "Software"), to deal |
|---|
| 17 |
* in the Software without restriction, including without limitation the rights |
|---|
| 18 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|---|
| 19 |
* copies of the Software, and to permit persons to whom the Software is |
|---|
| 20 |
* furnished to do so, subject to the following conditions: |
|---|
| 21 |
* The above copyright notice and this permission notice shall be included in |
|---|
| 22 |
* all copies or substantial portions of the Software. |
|---|
| 23 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|---|
| 24 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|---|
| 25 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|---|
| 26 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|---|
| 27 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|---|
| 28 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|---|
| 29 |
* THE SOFTWARE. |
|---|
| 30 |
*/ |
|---|
| 31 |
|
|---|
| 32 |
package org.libspark.dsler.SirdsAS3 |
|---|
| 33 |
{ |
|---|
| 34 |
import flash.display.Bitmap; |
|---|
| 35 |
import flash.display.BitmapData; |
|---|
| 36 |
import flash.display.Sprite; |
|---|
| 37 |
import flash.geom.Point; |
|---|
| 38 |
import flash.geom.Rectangle; |
|---|
| 39 |
|
|---|
| 40 |
public class RDSImageCreator implements IRDSImageCreatable |
|---|
| 41 |
{ |
|---|
| 42 |
protected var _pattern:BitmapData; |
|---|
| 43 |
protected var _canvasDefinition:Rectangle; |
|---|
| 44 |
protected var _density:Number; |
|---|
| 45 |
protected var _originalPatternWidth:int; |
|---|
| 46 |
protected var _depthRange:int; |
|---|
| 47 |
|
|---|
| 48 |
public function RDSImageCreator(canvasWidth:int, canvasHeight:int, rdsPatternWidth:int = 100, depthRange:int = 20, density:Number = 1){ |
|---|
| 49 |
_canvasDefinition = new Rectangle(0, 0, canvasWidth, canvasHeight); |
|---|
| 50 |
_originalPatternWidth = rdsPatternWidth; _depthRange = depthRange; _density = density; |
|---|
| 51 |
} |
|---|
| 52 |
|
|---|
| 53 |
public function create(dep:BitmapData):RDSImage{ |
|---|
| 54 |
var rdsImage:Sprite = new Sprite, depthMap:BitmapData = dep.clone(); |
|---|
| 55 |
var canvasHeight:int = _canvasDefinition.height, canvasWidth:int = _canvasDefinition.width; |
|---|
| 56 |
var rdsSurface:BitmapData = new BitmapData(canvasWidth, canvasHeight); |
|---|
| 57 |
|
|---|
| 58 |
var t:Number = (new Date).getTime(); |
|---|
| 59 |
for(var i:int = 0; i < canvasHeight; i++){ |
|---|
| 60 |
var iterator:int = 0, oldDepth:uint = 0; |
|---|
| 61 |
//新規オリジナルランダムドットパターンを設定 |
|---|
| 62 |
_pattern = createNewRandomPattern(_originalPatternWidth); |
|---|
| 63 |
for(var j:int = 0; j < canvasWidth; j++){ |
|---|
| 64 |
var newDepth:int = ( ( depthMap.getPixel(j, i) / 0xffffff ) * _depthRange + 0.5 ) >> 0, depthSub:int = newDepth - oldDepth; |
|---|
| 65 |
if(depthSub != 0){ |
|---|
| 66 |
//上昇、下降処理(ランダムパターンの更新) |
|---|
| 67 |
if(depthSub > 0) _pattern = createDeletedPattern(_pattern, iterator % _pattern.width, depthSub); |
|---|
| 68 |
else if(depthSub < 0) _pattern = createInsertedPattern(_pattern, iterator % _pattern.width, - depthSub); |
|---|
| 69 |
iterator = 0; |
|---|
| 70 |
} |
|---|
| 71 |
rdsSurface.setPixel(j, i, _pattern.getPixel(iterator++ % _pattern.width, 0)); |
|---|
| 72 |
oldDepth = newDepth; |
|---|
| 73 |
} |
|---|
| 74 |
} |
|---|
| 75 |
trace("convert time: ", ((new Date).getTime()-t)*0.001+"sec"); |
|---|
| 76 |
rdsImage.addChild(new Bitmap(rdsSurface)); |
|---|
| 77 |
return(new RDSImage(rdsImage, _originalPatternWidth, depthMap)); |
|---|
| 78 |
} |
|---|
| 79 |
|
|---|
| 80 |
//新しいランダムドットパターンを生成 |
|---|
| 81 |
protected function createNewRandomPattern(width:int):BitmapData{ |
|---|
| 82 |
var randomPattern:BitmapData = new BitmapData(width, 1); |
|---|
| 83 |
for(var xi:int = 0; xi < width; xi++){ |
|---|
| 84 |
var random:Number = Math.random()*256, seed:uint = random >> 0; |
|---|
| 85 |
if(( random - seed ) < _density) randomPattern.setPixel(xi, 0, seed << 16 | seed << 8 | seed); |
|---|
| 86 |
} |
|---|
| 87 |
return(randomPattern); |
|---|
| 88 |
} |
|---|
| 89 |
|
|---|
| 90 |
//ある区間を削除したパターンを生成(上昇処理) |
|---|
| 91 |
protected function createDeletedPattern(prevPattern:BitmapData, delPoint:uint, delWidth:uint):BitmapData{ |
|---|
| 92 |
var returnPattern:BitmapData = new BitmapData(prevPattern.width - delWidth, 1), bWidth:int = 0, prevPatternWidth:int = prevPattern.width; |
|---|
| 93 |
if(prevPatternWidth - delPoint - delWidth > 0){ |
|---|
| 94 |
//通常の場合 |
|---|
| 95 |
returnPattern.copyPixels(prevPattern, new Rectangle(delPoint + delWidth, 0, prevPatternWidth - delPoint - delWidth, 1), new Point(0, 0)); |
|---|
| 96 |
bWidth = prevPatternWidth - delPoint - delWidth; |
|---|
| 97 |
}else{ |
|---|
| 98 |
//削除範囲がパターン幅外に及ぶ場合 |
|---|
| 99 |
returnPattern.copyPixels(prevPattern, new Rectangle(delPoint + delWidth - prevPatternWidth, 0, prevPatternWidth - delWidth, 1), new Point(0, 0)); |
|---|
| 100 |
} |
|---|
| 101 |
if(bWidth && delPoint > 0) returnPattern.copyPixels(prevPattern, new Rectangle(0, 0, delPoint, 1), new Point(bWidth, 0)); |
|---|
| 102 |
return(returnPattern); |
|---|
| 103 |
} |
|---|
| 104 |
|
|---|
| 105 |
//ある幅のランダムドットを追加したパターンを生成(下降処理) |
|---|
| 106 |
protected function createInsertedPattern(prevPattern:BitmapData, edgeAt:int, insertRandWidth:int):BitmapData{ |
|---|
| 107 |
var prevPatternWidth:int = prevPattern.width, returnPattern:BitmapData = new BitmapData(prevPatternWidth + insertRandWidth, 1); |
|---|
| 108 |
returnPattern.copyPixels(prevPattern, new Rectangle(edgeAt, 0, 1, 1), new Point(0, 0)); |
|---|
| 109 |
returnPattern.copyPixels(createNewRandomPattern(insertRandWidth), new Rectangle(0, 0, insertRandWidth, 1), new Point(1, 0)); |
|---|
| 110 |
if(prevPatternWidth - edgeAt - 1 > 0) returnPattern.copyPixels(prevPattern, new Rectangle(edgeAt + 1, 0, prevPatternWidth - edgeAt - 1, 1), new Point(insertRandWidth + 1, 0)); |
|---|
| 111 |
if(edgeAt > 0) returnPattern.copyPixels(prevPattern, new Rectangle(0, 0, edgeAt, 1), new Point(prevPatternWidth - edgeAt + insertRandWidth, 0)); |
|---|
| 112 |
return(returnPattern); |
|---|
| 113 |
} |
|---|
| 114 |
} |
|---|
| 115 |
} |
|---|