2 * PROJECT: NyARToolkit
\r
3 * --------------------------------------------------------------------------------
\r
4 * This work is based on the original ARToolKit developed by
\r
7 * HITLab, University of Washington, Seattle
\r
8 * http://www.hitl.washington.edu/artoolkit/
\r
10 * The NyARToolkit is Java edition ARToolKit class library.
\r
11 * Copyright (C)2008-2009 Ryo Iizuka
\r
13 * This program is free software: you can redistribute it and/or modify
\r
14 * it under the terms of the GNU General Public License as published by
\r
15 * the Free Software Foundation, either version 3 of the License, or
\r
16 * (at your option) any later version.
\r
18 * This program is distributed in the hope that it will be useful,
\r
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
21 * GNU General Public License for more details.
\r
23 * You should have received a copy of the GNU General Public License
\r
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
\r
26 * For further information please contact.
\r
27 * http://nyatla.jp/nyatoolkit/
\r
28 * <airmail(at)ebony.plala.or.jp> or <nyatla(at)nyatla.jp>
\r
31 package jp.nyatla.nyartoolkit.core;
\r
33 import java.io.FileInputStream;
\r
34 import java.io.InputStream;
\r
35 import java.io.InputStreamReader;
\r
36 import java.io.StreamTokenizer;
\r
38 import jp.nyatla.nyartoolkit.*;
\r
39 import jp.nyatla.nyartoolkit.core.match.*;
\r
40 import jp.nyatla.nyartoolkit.core.raster.*;
\r
41 import jp.nyatla.nyartoolkit.core.raster.rgb.NyARRgbRaster;
\r
42 import jp.nyatla.nyartoolkit.core.types.NyARBufferType;
\r
45 * NyARCodeクラスの支援クラスです。このクラスは、NyARCodeのマーカファイル読み取り機能を担当します。
\r
46 * InputStreamからARToolkit形式のマーカデータを読み取って配列に格納する手順を実装します。
\r
48 class NyARCodeFileReader
\r
52 * ImputStreamからARToolKit形式のマーカデータを読み込んでo_raster[4]に格納します。
\r
56 * 出力先のラスタ配列です。バッファ形式は形式はINT1D_X8R8G8B8_32であり、全て同一なサイズである必要があります。
\r
57 * @throws NyARException
\r
59 public static void loadFromARToolKitFormFile(InputStream i_stream,NyARRaster[] o_raster) throws NyARException
\r
61 assert o_raster.length==4;
\r
64 StreamTokenizer st = new StreamTokenizer(new InputStreamReader(i_stream));
\r
66 for (int h = 0; h < 4; h++) {
\r
67 assert o_raster[h].isEqualBufferType(NyARBufferType.INT1D_X8R8G8B8_32);
\r
68 final NyARRaster ra=o_raster[h];
\r
69 readBlock(st,ra.getWidth(),ra.getHeight(),(int[])ra.getBuffer());
\r
71 } catch (Exception e) {
\r
72 throw new NyARException(e);
\r
77 * ImputStreamからARToolKit形式のマーカデータを読み込んでo_codeに格納します。
\r
81 * 出力先のNyARCodeオブジェクトです。
\r
82 * @throws NyARException
\r
84 public static void loadFromARToolKitFormFile(InputStream i_stream,NyARCode o_code) throws NyARException
\r
86 int width=o_code.getWidth();
\r
87 int height=o_code.getHeight();
\r
88 NyARRgbRaster tmp_raster=new NyARRgbRaster(width,height, NyARBufferType.INT1D_X8R8G8B8_32);
\r
91 StreamTokenizer st = new StreamTokenizer(new InputStreamReader(i_stream));
\r
92 int[] buf=(int[])tmp_raster.getBuffer();
\r
94 for (int h = 0; h < 4; h++){
\r
95 readBlock(st,width,height,buf);
\r
97 o_code.getColorData(h).setRaster(tmp_raster);
\r
98 o_code.getBlackWhiteData(h).setRaster(tmp_raster);
\r
100 } catch (Exception e) {
\r
101 throw new NyARException(e);
\r
103 tmp_raster=null;//ポイ
\r
107 * 1ブロック分のXRGBデータをi_stからo_bufへ読みだします。
\r
109 * 入力元のStreamTokenizerを指定します。関数実行後、読み取り位置は更新されます。
\r
116 * @throws NyARException
\r
118 private static void readBlock(StreamTokenizer i_st,int i_width,int i_height,int[] o_buf) throws NyARException
\r
121 final int pixels=i_width*i_height;
\r
122 for (int i3 = 0; i3 < 3; i3++) {
\r
123 for (int i2 = 0; i2 < pixels; i2++){
\r
125 switch (i_st.nextToken()){
\r
126 case StreamTokenizer.TT_NUMBER:
\r
129 throw new NyARException();
\r
131 o_buf[i2]=(o_buf[i2]<<8)|((0x000000ff&(int)i_st.nval));
\r
135 for(int i3=0;i3<pixels;i3++){
\r
136 o_buf[i3]=((o_buf[i3]<<16)&0xff0000)|(o_buf[i3]&0x00ff00)|((o_buf[i3]>>16)&0x0000ff);
\r
138 } catch (Exception e) {
\r
139 throw new NyARException(e);
\r
146 * ARToolKitのマーカーパターン1個のデータに相当するクラスです。
\r
147 * マーカーパターンに対する、ARToolKit相当のプロパティ値を提供します。
\r
149 public class NyARCode
\r
151 private NyARMatchPattDeviationColorData[] _color_pat=new NyARMatchPattDeviationColorData[4];
\r
152 private NyARMatchPattDeviationBlackWhiteData[] _bw_pat=new NyARMatchPattDeviationBlackWhiteData[4];
\r
153 private int _width;
\r
154 private int _height;
\r
157 * directionを指定して、NyARMatchPattDeviationColorDataオブジェクトを取得します。
\r
161 * NyARMatchPattDeviationColorDataオブジェクト
\r
163 public NyARMatchPattDeviationColorData getColorData(int i_index)
\r
165 return this._color_pat[i_index];
\r
167 public NyARMatchPattDeviationBlackWhiteData getBlackWhiteData(int i_index)
\r
169 return this._bw_pat[i_index];
\r
171 public int getWidth()
\r
177 * パターンデータの高さを取得します。
\r
180 public int getHeight()
\r
185 * コンストラクタです。空のNyARCodeオブジェクトを作成します。
\r
190 * @throws NyARException
\r
192 public NyARCode(int i_width, int i_height) throws NyARException
\r
194 this._width = i_width;
\r
195 this._height = i_height;
\r
197 for(int i=0;i<4;i++){
\r
198 this._color_pat[i]=new NyARMatchPattDeviationColorData(i_width,i_height);
\r
199 this._bw_pat[i]=new NyARMatchPattDeviationBlackWhiteData(i_width,i_height);
\r
204 * ファイル名を指定して、パターンデータをロードします。
\r
205 * ロードするパターンデータのサイズは、現在の値と同じである必要があります。
\r
207 * ARToolKit形式のパターンデータファイルの名前
\r
208 * @throws NyARException
\r
210 public void loadARPattFromFile(String filename) throws NyARException
\r
213 loadARPatt(new FileInputStream(filename));
\r
214 } catch (Exception e) {
\r
215 throw new NyARException(e);
\r
220 * 4枚のラスタから、マーカーパターンを生成して格納します。
\r
222 * direction毎のパターンを格納したラスタ配列を指定します。
\r
223 * ラスタは同一なサイズ、かつマーカーパターンと同じサイズである必要があります。
\r
224 * 格納順は、パターンの右上が、1,2,3,4象限になる順番です。
\r
225 * @throws NyARException
\r
227 public void setRaster(NyARRgbRaster[] i_raster) throws NyARException
\r
229 assert(i_raster.length!=4);
\r
231 for(int i=0;i<4;i++){
\r
232 this._color_pat[i].setRaster(i_raster[i]);
\r
237 * 1枚のラスタから、マーカーパターンを生成して格納します。
\r
239 * 基準となるラスタを指定します。ラスタの解像度は、ARマーカコードと同じである必要があります。
\r
240 * @throws NyARException
\r
242 public void setRaster(NyARRgbRaster i_raster) throws NyARException
\r
245 for(int i=0;i<4;i++){
\r
246 this._color_pat[i].setRaster(i_raster,i);
\r
252 * inputStreamから、パターンデータをロードします。
\r
253 * ロードするパターンのサイズは、現在の値と同じである必要があります。
\r
255 * @throws NyARException
\r
257 public void loadARPatt(InputStream i_stream) throws NyARException
\r
260 NyARCodeFileReader.loadFromARToolKitFormFile(i_stream,this);
\r