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.INyARRgbRaster;
\r
42 import jp.nyatla.nyartoolkit.core.raster.rgb.NyARRgbRaster;
\r
43 import jp.nyatla.nyartoolkit.core.types.NyARBufferType;
\r
46 * {@link NyARCode}クラスの支援クラスです。
\r
47 * このクラスは、{@link NyARCode}のマーカファイル読み取り機能のうち、InputStreamからARToolkit形式のマーカデータを読み取って配列に格納する手順を実装します。
\r
48 * {@link NyARCode}以外から使用することはありません。
\r
50 class NyARCodeFileReader
\r
54 * ImputStreamからARToolKit形式のマーカデータを読み、o_raster[4]に格納します。
\r
59 * バッファ形式は形式はINT1D_X8R8G8B8_32であり、4要素、かつ全て同一なサイズである必要があります。
\r
60 * @throws NyARException
\r
62 public static void loadFromARToolKitFormFile(InputStream i_stream,NyARRaster[] o_raster) throws NyARException
\r
64 assert(o_raster.length==4);
\r
67 StreamTokenizer st = new StreamTokenizer(new InputStreamReader(i_stream));
\r
69 for (int h = 0; h < 4; h++) {
\r
70 assert o_raster[h].isEqualBufferType(NyARBufferType.INT1D_X8R8G8B8_32);
\r
71 final NyARRaster ra=o_raster[h];
\r
72 readBlock(st,ra.getWidth(),ra.getHeight(),(int[])ra.getBuffer());
\r
74 } catch (Exception e) {
\r
75 throw new NyARException(e);
\r
80 * ImputStreamからARToolKit形式のマーカデータを読み込み、o_codeオブジェクトへ格納します。
\r
84 * 出力先の{@link NyARCode}オブジェクトです。
\r
85 * @throws NyARException
\r
87 public static void loadFromARToolKitFormFile(InputStream i_stream,NyARCode o_code) throws NyARException
\r
89 int width=o_code.getWidth();
\r
90 int height=o_code.getHeight();
\r
91 NyARRgbRaster tmp_raster=new NyARRgbRaster(width,height, NyARBufferType.INT1D_X8R8G8B8_32);
\r
94 StreamTokenizer st = new StreamTokenizer(new InputStreamReader(i_stream));
\r
95 int[] buf=(int[])tmp_raster.getBuffer();
\r
97 for (int h = 0; h < 4; h++){
\r
98 readBlock(st,width,height,buf);
\r
100 o_code.getColorData(h).setRaster(tmp_raster);
\r
101 o_code.getBlackWhiteData(h).setRaster(tmp_raster);
\r
103 } catch (Exception e) {
\r
104 throw new NyARException(e);
\r
106 tmp_raster=null;//ポイ
\r
110 * ストリームi_stから、1ブロック(1方位分)のXRGBデータをからo_bufへ読みだします。
\r
112 * 入力元のStreamTokenizerを指定します。
\r
113 * i_stの読み取り位置は更新されます。
\r
115 * パターンの横解像度(pixel)です。
\r
117 * パターンの縦解像度(pixel)です。
\r
120 * @throws NyARException
\r
122 private static void readBlock(StreamTokenizer i_st,int i_width,int i_height,int[] o_buf) throws NyARException
\r
125 final int pixels=i_width*i_height;
\r
126 for (int i3 = 0; i3 < 3; i3++) {
\r
127 for (int i2 = 0; i2 < pixels; i2++){
\r
129 switch (i_st.nextToken()){
\r
130 case StreamTokenizer.TT_NUMBER:
\r
133 throw new NyARException();
\r
135 o_buf[i2]=(o_buf[i2]<<8)|((0x000000ff&(int)i_st.nval));
\r
139 for(int i3=0;i3<pixels;i3++){
\r
140 o_buf[i3]=((o_buf[i3]<<16)&0xff0000)|(o_buf[i3]&0x00ff00)|((o_buf[i3]>>16)&0x0000ff);
\r
142 } catch (Exception e) {
\r
143 throw new NyARException(e);
\r
150 * ARToolKitのマーカーパターン1個のデータを格納するクラスです。
\r
151 * マーカーパターンのプロパティと、データのロード機能を提供します。
\r
153 public class NyARCode
\r
155 private NyARMatchPattDeviationColorData[] _color_pat=new NyARMatchPattDeviationColorData[4];
\r
156 private NyARMatchPattDeviationBlackWhiteData[] _bw_pat=new NyARMatchPattDeviationBlackWhiteData[4];
\r
157 private int _width;
\r
158 private int _height;
\r
161 * 指定したdirection(方位)の{@link NyARMatchPattDeviationColorData}オブジェクトの参照値を返します。
\r
164 * 範囲は、0<=n<=3の数値です。
\r
166 * 指定した方位の{@link NyARMatchPattDeviationColorData}オブジェクトを返します。
\r
168 public NyARMatchPattDeviationColorData getColorData(int i_index)
\r
170 return this._color_pat[i_index];
\r
173 * 指定したdirection(方位)の{@link NyARMatchPattDeviationBlackWhiteData}オブジェクトの参照値を返します。
\r
176 * 範囲は、0<=n<=3の数値です。
\r
178 * 指定した方位の{@link NyARMatchPattDeviationBlackWhiteData}オブジェクトを返します。
\r
180 public NyARMatchPattDeviationBlackWhiteData getBlackWhiteData(int i_index)
\r
182 return this._bw_pat[i_index];
\r
189 public int getWidth()
\r
199 public int getHeight()
\r
205 * 空のNyARCodeオブジェクトを作成します。
\r
210 * @throws NyARException
\r
212 public NyARCode(int i_width, int i_height) throws NyARException
\r
214 this._width = i_width;
\r
215 this._height = i_height;
\r
217 for(int i=0;i<4;i++){
\r
218 this._color_pat[i]=new NyARMatchPattDeviationColorData(i_width,i_height);
\r
219 this._bw_pat[i]=new NyARMatchPattDeviationBlackWhiteData(i_width,i_height);
\r
224 * ARToolKit形式のパターンデータをファイルからロードします。
\r
225 * ロードするパターンデータの縦横解像度は、このインスタンスの値と同じである必要があります。
\r
227 * ARToolKit形式のパターンデータファイルのパス名
\r
228 * @throws NyARException
\r
230 public void loadARPattFromFile(String filename) throws NyARException
\r
233 loadARPatt(new FileInputStream(filename));
\r
234 } catch (Exception e) {
\r
235 throw new NyARException(e);
\r
240 * inputStreamから、ARToolKit形式のパターンデータをロードします。
\r
241 * ロードするパターンデータの縦横解像度は、このインスタンスの値と同じである必要があります。
\r
243 * 読出し元のStreamオブジェクト
\r
244 * @throws NyARException
\r
246 public void loadARPatt(InputStream i_stream) throws NyARException
\r
249 NyARCodeFileReader.loadFromARToolKitFormFile(i_stream,this);
\r
253 * 4枚のラスタオブジェクトから、マーカーパターンを生成して格納します。
\r
255 * パターンデータを格納したラスタオブジェクト配列を指定します。
\r
256 * ラスタは同一な解像度であり、かつこのインスタンスと同じ解像度である必要があります。
\r
257 * 格納順は、パターンの右上が、1,2,3,4象限になる順番です。
\r
258 * @throws NyARException
\r
260 public void setRaster(INyARRgbRaster[] i_raster) throws NyARException
\r
262 assert(i_raster.length!=4);
\r
264 for(int i=0;i<4;i++){
\r
265 this._color_pat[i].setRaster(i_raster[i]);
\r
270 * 1枚のラスタオブジェクトから、マーカーパターンを生成して格納します。
\r
271 * 残りの3枚のデータは、関数がi_rasterを回転させて求めます。
\r
273 * パターンデータを格納したラスタオブジェクトを指定します。
\r
274 * ラスタは、このインスタンスと同じ解像度である必要があります。
\r
275 * @throws NyARException
\r
277 public void setRaster(INyARRgbRaster i_raster) throws NyARException
\r
280 for(int i=0;i<4;i++){
\r
281 this._color_pat[i].setRaster(i_raster,i);
\r