OSDN Git Service

[更新]NyARToolkit for Java
[nyartoolkit-and/nyartoolkit-and.git] / trunk / src / jp / nyatla / nyartoolkit / core / NyARCode.java
1 /* \r
2  * PROJECT: NyARToolkit\r
3  * --------------------------------------------------------------------------------\r
4  * This work is based on the original ARToolKit developed by\r
5  *   Hirokazu Kato\r
6  *   Mark Billinghurst\r
7  *   HITLab, University of Washington, Seattle\r
8  * http://www.hitl.washington.edu/artoolkit/\r
9  *\r
10  * The NyARToolkit is Java version ARToolkit class library.\r
11  * Copyright (C)2008 R.Iizuka\r
12  *\r
13  * This program is free software; you can redistribute it and/or\r
14  * modify it under the terms of the GNU General Public License\r
15  * as published by the Free Software Foundation; either version 2\r
16  * of the License, or (at your option) any later version.\r
17  * \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
22  * \r
23  * You should have received a copy of the GNU General Public License\r
24  * along with this framework; if not, write to the Free Software\r
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
26  * \r
27  * For further information please contact.\r
28  *      http://nyatla.jp/nyatoolkit/\r
29  *      <airmail(at)ebony.plala.or.jp>\r
30  * \r
31  */\r
32 package jp.nyatla.nyartoolkit.core;\r
33 \r
34 import java.io.FileInputStream;\r
35 import java.io.InputStream;\r
36 import java.io.InputStreamReader;\r
37 import java.io.StreamTokenizer;\r
38 \r
39 import jp.nyatla.nyartoolkit.*;\r
40 import jp.nyatla.nyartoolkit.core.match.*;\r
41 import jp.nyatla.nyartoolkit.core.raster.*;\r
42 import jp.nyatla.nyartoolkit.core.types.*;\r
43 import jp.nyatla.nyartoolkit.core.rasterreader.*;\r
44 \r
45 class NyARCodeFileReader\r
46 {\r
47 \r
48         /**\r
49          * ARコードファイルからデータを読み込んでo_raster[4]に格納します。\r
50          * @param i_stream\r
51          * @param o_raster\r
52          * @throws NyARException\r
53          */\r
54         public static void loadFromARToolKitFormFile(InputStream i_stream,NyARRaster[] o_raster) throws NyARException\r
55         {\r
56                 assert o_raster.length==4;\r
57                 //4個の要素をラスタにセットする。\r
58                 try {\r
59                         StreamTokenizer st = new StreamTokenizer(new InputStreamReader(i_stream));\r
60                         //GBRAで一度読みだす。\r
61                         for (int h = 0; h < 4; h++) {\r
62                                 assert o_raster[h].getBufferReader().isEqualBufferType(INyARBufferReader.BUFFERFORMAT_INT1D_X8R8G8B8_32);\r
63                                 final NyARRaster ra=o_raster[h];\r
64                                 readBlock(st,ra.getWidth(),ra.getHeight(),(int[])ra.getBufferReader().getBuffer());\r
65                         }\r
66                 } catch (Exception e) {\r
67                         throw new NyARException(e);\r
68                 }\r
69                 return;\r
70         }\r
71         /**\r
72          * ARコードファイルからデータを読み込んでo_codeに格納します。\r
73          * @param i_stream\r
74          * @param o_code\r
75          * @throws NyARException\r
76          */\r
77         public static void loadFromARToolKitFormFile(InputStream i_stream,NyARCode o_code) throws NyARException\r
78         {\r
79                 int width=o_code.getWidth();\r
80                 int height=o_code.getHeight();\r
81                 NyARRaster tmp_raster=new NyARRaster(new NyARIntSize(width,height),new int[width*height],INyARBufferReader.BUFFERFORMAT_INT1D_X8R8G8B8_32);\r
82                 //4個の要素をラスタにセットする。\r
83                 try {\r
84                         StreamTokenizer st = new StreamTokenizer(new InputStreamReader(i_stream));\r
85                         int[] buf=(int[])tmp_raster.getBufferReader().getBuffer();\r
86                         //GBRAで一度読みだす。\r
87                         for (int h = 0; h < 4; h++){\r
88                                 readBlock(st,width,height,buf);\r
89                                 //ARCodeにセット(カラー)\r
90                                 o_code.getColorData(h).setRaster(tmp_raster);\r
91                                 o_code.getBlackWhiteData(h).setRaster(tmp_raster);\r
92                         }\r
93                 } catch (Exception e) {\r
94                         throw new NyARException(e);\r
95                 }\r
96                 tmp_raster=null;//ポイ\r
97                 return;\r
98         }\r
99         /**\r
100          * 1ブロック分のXRGBデータをi_stからo_bufへ読みだします。\r
101          * @param i_st\r
102          * @param o_buf\r
103          */\r
104         private static void readBlock(StreamTokenizer i_st,int i_width,int i_height,int[] o_buf) throws NyARException\r
105         {\r
106                 try {\r
107                         final int pixels=i_width*i_height;\r
108                         for (int i3 = 0; i3 < 3; i3++) {\r
109                                 for (int i2 = 0; i2 < pixels; i2++){\r
110                                         // 数値のみ読み出す\r
111                                         switch (i_st.nextToken()){\r
112                                         case StreamTokenizer.TT_NUMBER:\r
113                                                 break;\r
114                                         default:\r
115                                                 throw new NyARException();\r
116                                         }\r
117                                         o_buf[i2]=(o_buf[i2]<<8)|((0x000000ff&(int)i_st.nval));\r
118                                 }\r
119                         }\r
120                         //GBR→RGB\r
121                         for(int i3=0;i3<pixels;i3++){\r
122                                 o_buf[i3]=((o_buf[i3]<<16)&0xff0000)|(o_buf[i3]&0x00ff00)|((o_buf[i3]>>16)&0x0000ff);\r
123                         }\r
124                 } catch (Exception e) {\r
125                         throw new NyARException(e);\r
126                 }               \r
127                 return;\r
128         }\r
129 }\r
130 \r
131 /**\r
132  * ARToolKitのマーカーコードを1個保持します。\r
133  * \r
134  */\r
135 public class NyARCode\r
136 {\r
137         private NyARMatchPattDeviationColorData[] _color_pat=new NyARMatchPattDeviationColorData[4];\r
138         private NyARMatchPattDeviationBlackWhiteData[] _bw_pat=new NyARMatchPattDeviationBlackWhiteData[4];\r
139         private int _width;\r
140         private int _height;\r
141         \r
142         public NyARMatchPattDeviationColorData getColorData(int i_index)\r
143         {\r
144                 return this._color_pat[i_index];\r
145         }\r
146         public NyARMatchPattDeviationBlackWhiteData getBlackWhiteData(int i_index)\r
147         {\r
148                 return this._bw_pat[i_index];\r
149         }       \r
150         public int getWidth()\r
151         {\r
152                 return _width;\r
153         }\r
154 \r
155         public int getHeight()\r
156         {\r
157                 return _height;\r
158         }\r
159         public NyARCode(int i_width, int i_height) throws NyARException\r
160         {\r
161                 this._width = i_width;\r
162                 this._height = i_height;\r
163                 //空のラスタを4個作成\r
164                 for(int i=0;i<4;i++){\r
165                         this._color_pat[i]=new NyARMatchPattDeviationColorData(i_width,i_height);\r
166                         this._bw_pat[i]=new NyARMatchPattDeviationBlackWhiteData(i_width,i_height);\r
167                 }\r
168                 return;\r
169         }\r
170         public void loadARPattFromFile(String filename) throws NyARException\r
171         {\r
172                 try {\r
173                         loadARPatt(new FileInputStream(filename));\r
174                 } catch (Exception e) {\r
175                         throw new NyARException(e);\r
176                 }\r
177                 return;\r
178         }\r
179         public void setRaster(NyARRaster[] i_raster) throws NyARException\r
180         {\r
181                 assert i_raster.length!=4;\r
182                 //ラスタにパターンをロードする。\r
183                 for(int i=0;i<4;i++){\r
184                         this._color_pat[i].setRaster(i_raster[i]);                              \r
185                 }\r
186                 return;\r
187         }\r
188 \r
189         public void loadARPatt(InputStream i_stream) throws NyARException\r
190         {\r
191                 //ラスタにパターンをロードする。\r
192                 NyARCodeFileReader.loadFromARToolKitFormFile(i_stream,this);\r
193                 return;\r
194         }\r
195 }\r