OSDN Git Service

e71955ea5e0ecd82618c192df4ec6b3066cb117a
[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 edition ARToolKit class library.\r
11  * Copyright (C)2008-2009 Ryo Iizuka\r
12  *\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
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 program.  If not, see <http://www.gnu.org/licenses/>.\r
25  * \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
29  * \r
30  */\r
31 package jp.nyatla.nyartoolkit.core;\r
32 \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
37 \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.types.*;\r
42 import jp.nyatla.nyartoolkit.core.rasterreader.*;\r
43 \r
44 class NyARCodeFileReader\r
45 {\r
46 \r
47         /**\r
48          * ARコードファイルからデータを読み込んでo_raster[4]に格納します。\r
49          * @param i_stream\r
50          * @param o_raster\r
51          * @throws NyARException\r
52          */\r
53         public static void loadFromARToolKitFormFile(InputStream i_stream,NyARRaster[] o_raster) throws NyARException\r
54         {\r
55                 assert o_raster.length==4;\r
56                 //4個の要素をラスタにセットする。\r
57                 try {\r
58                         StreamTokenizer st = new StreamTokenizer(new InputStreamReader(i_stream));\r
59                         //GBRAで一度読みだす。\r
60                         for (int h = 0; h < 4; h++) {\r
61                                 assert o_raster[h].getBufferReader().isEqualBufferType(INyARBufferReader.BUFFERFORMAT_INT1D_X8R8G8B8_32);\r
62                                 final NyARRaster ra=o_raster[h];\r
63                                 readBlock(st,ra.getWidth(),ra.getHeight(),(int[])ra.getBufferReader().getBuffer());\r
64                         }\r
65                 } catch (Exception e) {\r
66                         throw new NyARException(e);\r
67                 }\r
68                 return;\r
69         }\r
70         /**\r
71          * ARコードファイルからデータを読み込んでo_codeに格納します。\r
72          * @param i_stream\r
73          * @param o_code\r
74          * @throws NyARException\r
75          */\r
76         public static void loadFromARToolKitFormFile(InputStream i_stream,NyARCode o_code) throws NyARException\r
77         {\r
78                 int width=o_code.getWidth();\r
79                 int height=o_code.getHeight();\r
80                 NyARRaster tmp_raster=new NyARRaster(new NyARIntSize(width,height),INyARBufferReader.BUFFERFORMAT_INT1D_X8R8G8B8_32);\r
81                 //4個の要素をラスタにセットする。\r
82                 try {\r
83                         StreamTokenizer st = new StreamTokenizer(new InputStreamReader(i_stream));\r
84                         int[] buf=(int[])tmp_raster.getBufferReader().getBuffer();\r
85                         //GBRAで一度読みだす。\r
86                         for (int h = 0; h < 4; h++){\r
87                                 readBlock(st,width,height,buf);\r
88                                 //ARCodeにセット(カラー)\r
89                                 o_code.getColorData(h).setRaster(tmp_raster);\r
90                                 o_code.getBlackWhiteData(h).setRaster(tmp_raster);\r
91                         }\r
92                 } catch (Exception e) {\r
93                         throw new NyARException(e);\r
94                 }\r
95                 tmp_raster=null;//ポイ\r
96                 return;\r
97         }\r
98         /**\r
99          * 1ブロック分のXRGBデータをi_stからo_bufへ読みだします。\r
100          * @param i_st\r
101          * @param o_buf\r
102          */\r
103         private static void readBlock(StreamTokenizer i_st,int i_width,int i_height,int[] o_buf) throws NyARException\r
104         {\r
105                 try {\r
106                         final int pixels=i_width*i_height;\r
107                         for (int i3 = 0; i3 < 3; i3++) {\r
108                                 for (int i2 = 0; i2 < pixels; i2++){\r
109                                         // 数値のみ読み出す\r
110                                         switch (i_st.nextToken()){\r
111                                         case StreamTokenizer.TT_NUMBER:\r
112                                                 break;\r
113                                         default:\r
114                                                 throw new NyARException();\r
115                                         }\r
116                                         o_buf[i2]=(o_buf[i2]<<8)|((0x000000ff&(int)i_st.nval));\r
117                                 }\r
118                         }\r
119                         //GBR→RGB\r
120                         for(int i3=0;i3<pixels;i3++){\r
121                                 o_buf[i3]=((o_buf[i3]<<16)&0xff0000)|(o_buf[i3]&0x00ff00)|((o_buf[i3]>>16)&0x0000ff);\r
122                         }\r
123                 } catch (Exception e) {\r
124                         throw new NyARException(e);\r
125                 }               \r
126                 return;\r
127         }\r
128 }\r
129 \r
130 /**\r
131  * ARToolKitのマーカーコードを1個保持します。\r
132  * \r
133  */\r
134 public class NyARCode\r
135 {\r
136         private NyARMatchPattDeviationColorData[] _color_pat=new NyARMatchPattDeviationColorData[4];\r
137         private NyARMatchPattDeviationBlackWhiteData[] _bw_pat=new NyARMatchPattDeviationBlackWhiteData[4];\r
138         private int _width;\r
139         private int _height;\r
140         \r
141         public NyARMatchPattDeviationColorData getColorData(int i_index)\r
142         {\r
143                 return this._color_pat[i_index];\r
144         }\r
145         public NyARMatchPattDeviationBlackWhiteData getBlackWhiteData(int i_index)\r
146         {\r
147                 return this._bw_pat[i_index];\r
148         }       \r
149         public int getWidth()\r
150         {\r
151                 return _width;\r
152         }\r
153 \r
154         public int getHeight()\r
155         {\r
156                 return _height;\r
157         }\r
158         public NyARCode(int i_width, int i_height) throws NyARException\r
159         {\r
160                 this._width = i_width;\r
161                 this._height = i_height;\r
162                 //空のラスタを4個作成\r
163                 for(int i=0;i<4;i++){\r
164                         this._color_pat[i]=new NyARMatchPattDeviationColorData(i_width,i_height);\r
165                         this._bw_pat[i]=new NyARMatchPattDeviationBlackWhiteData(i_width,i_height);\r
166                 }\r
167                 return;\r
168         }\r
169         public void loadARPattFromFile(String filename) throws NyARException\r
170         {\r
171                 try {\r
172                         loadARPatt(new FileInputStream(filename));\r
173                 } catch (Exception e) {\r
174                         throw new NyARException(e);\r
175                 }\r
176                 return;\r
177         }\r
178         public void setRaster(NyARRaster[] i_raster) throws NyARException\r
179         {\r
180                 assert i_raster.length!=4;\r
181                 //ラスタにパターンをロードする。\r
182                 for(int i=0;i<4;i++){\r
183                         this._color_pat[i].setRaster(i_raster[i]);                              \r
184                 }\r
185                 return;\r
186         }\r
187 \r
188         public void loadARPatt(InputStream i_stream) throws NyARException\r
189         {\r
190                 //ラスタにパターンをロードする。\r
191                 NyARCodeFileReader.loadFromARToolKitFormFile(i_stream,this);\r
192                 return;\r
193         }\r
194 }\r