OSDN Git Service

[tag]NyARToolkit/2.5.1
[nyartoolkit-and/nyartoolkit-and.git] / tags / 2.5.1 / src / jp / nyatla / nyartoolkit / core / NyARCode.java
diff --git a/tags/2.5.1/src/jp/nyatla/nyartoolkit/core/NyARCode.java b/tags/2.5.1/src/jp/nyatla/nyartoolkit/core/NyARCode.java
new file mode 100644 (file)
index 0000000..7cd7028
--- /dev/null
@@ -0,0 +1,193 @@
+/* \r
+ * PROJECT: NyARToolkit\r
+ * --------------------------------------------------------------------------------\r
+ * This work is based on the original ARToolKit developed by\r
+ *   Hirokazu Kato\r
+ *   Mark Billinghurst\r
+ *   HITLab, University of Washington, Seattle\r
+ * http://www.hitl.washington.edu/artoolkit/\r
+ *\r
+ * The NyARToolkit is Java edition ARToolKit class library.\r
+ * Copyright (C)2008-2009 Ryo Iizuka\r
+ *\r
+ * This program is free software: you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License as published by\r
+ * the Free Software Foundation, either version 3 of the License, or\r
+ * (at your option) any later version.\r
+ * \r
+ * This program is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License\r
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
+ * \r
+ * For further information please contact.\r
+ *     http://nyatla.jp/nyatoolkit/\r
+ *     <airmail(at)ebony.plala.or.jp> or <nyatla(at)nyatla.jp>\r
+ * \r
+ */\r
+package jp.nyatla.nyartoolkit.core;\r
+\r
+import java.io.FileInputStream;\r
+import java.io.InputStream;\r
+import java.io.InputStreamReader;\r
+import java.io.StreamTokenizer;\r
+\r
+import jp.nyatla.nyartoolkit.*;\r
+import jp.nyatla.nyartoolkit.core.match.*;\r
+import jp.nyatla.nyartoolkit.core.raster.*;\r
+import jp.nyatla.nyartoolkit.core.types.NyARBufferType;\r
+\r
+class NyARCodeFileReader\r
+{\r
+\r
+       /**\r
+        * ARコードファイルからデータを読み込んでo_raster[4]に格納します。\r
+        * @param i_stream\r
+        * @param o_raster\r
+        * @throws NyARException\r
+        */\r
+       public static void loadFromARToolKitFormFile(InputStream i_stream,NyARRaster[] o_raster) throws NyARException\r
+       {\r
+               assert o_raster.length==4;\r
+               //4個の要素をラスタにセットする。\r
+               try {\r
+                       StreamTokenizer st = new StreamTokenizer(new InputStreamReader(i_stream));\r
+                       //GBRAで一度読みだす。\r
+                       for (int h = 0; h < 4; h++) {\r
+                               assert o_raster[h].isEqualBufferType(NyARBufferType.INT1D_X8R8G8B8_32);\r
+                               final NyARRaster ra=o_raster[h];\r
+                               readBlock(st,ra.getWidth(),ra.getHeight(),(int[])ra.getBuffer());\r
+                       }\r
+               } catch (Exception e) {\r
+                       throw new NyARException(e);\r
+               }\r
+               return;\r
+       }\r
+       /**\r
+        * ARコードファイルからデータを読み込んでo_codeに格納します。\r
+        * @param i_stream\r
+        * @param o_code\r
+        * @throws NyARException\r
+        */\r
+       public static void loadFromARToolKitFormFile(InputStream i_stream,NyARCode o_code) throws NyARException\r
+       {\r
+               int width=o_code.getWidth();\r
+               int height=o_code.getHeight();\r
+               NyARRaster tmp_raster=new NyARRaster(width,height, NyARBufferType.INT1D_X8R8G8B8_32);\r
+               //4個の要素をラスタにセットする。\r
+               try {\r
+                       StreamTokenizer st = new StreamTokenizer(new InputStreamReader(i_stream));\r
+                       int[] buf=(int[])tmp_raster.getBuffer();\r
+                       //GBRAで一度読みだす。\r
+                       for (int h = 0; h < 4; h++){\r
+                               readBlock(st,width,height,buf);\r
+                               //ARCodeにセット(カラー)\r
+                               o_code.getColorData(h).setRaster(tmp_raster);\r
+                               o_code.getBlackWhiteData(h).setRaster(tmp_raster);\r
+                       }\r
+               } catch (Exception e) {\r
+                       throw new NyARException(e);\r
+               }\r
+               tmp_raster=null;//ポイ\r
+               return;\r
+       }\r
+       /**\r
+        * 1ブロック分のXRGBデータをi_stからo_bufへ読みだします。\r
+        * @param i_st\r
+        * @param o_buf\r
+        */\r
+       private static void readBlock(StreamTokenizer i_st,int i_width,int i_height,int[] o_buf) throws NyARException\r
+       {\r
+               try {\r
+                       final int pixels=i_width*i_height;\r
+                       for (int i3 = 0; i3 < 3; i3++) {\r
+                               for (int i2 = 0; i2 < pixels; i2++){\r
+                                       // 数値のみ読み出す\r
+                                       switch (i_st.nextToken()){\r
+                                       case StreamTokenizer.TT_NUMBER:\r
+                                               break;\r
+                                       default:\r
+                                               throw new NyARException();\r
+                                       }\r
+                                       o_buf[i2]=(o_buf[i2]<<8)|((0x000000ff&(int)i_st.nval));\r
+                               }\r
+                       }\r
+                       //GBR→RGB\r
+                       for(int i3=0;i3<pixels;i3++){\r
+                               o_buf[i3]=((o_buf[i3]<<16)&0xff0000)|(o_buf[i3]&0x00ff00)|((o_buf[i3]>>16)&0x0000ff);\r
+                       }\r
+               } catch (Exception e) {\r
+                       throw new NyARException(e);\r
+               }               \r
+               return;\r
+       }\r
+}\r
+\r
+/**\r
+ * ARToolKitのマーカーコードを1個保持します。\r
+ * \r
+ */\r
+public class NyARCode\r
+{\r
+       private NyARMatchPattDeviationColorData[] _color_pat=new NyARMatchPattDeviationColorData[4];\r
+       private NyARMatchPattDeviationBlackWhiteData[] _bw_pat=new NyARMatchPattDeviationBlackWhiteData[4];\r
+       private int _width;\r
+       private int _height;\r
+       \r
+       public NyARMatchPattDeviationColorData getColorData(int i_index)\r
+       {\r
+               return this._color_pat[i_index];\r
+       }\r
+       public NyARMatchPattDeviationBlackWhiteData getBlackWhiteData(int i_index)\r
+       {\r
+               return this._bw_pat[i_index];\r
+       }       \r
+       public int getWidth()\r
+       {\r
+               return _width;\r
+       }\r
+\r
+       public int getHeight()\r
+       {\r
+               return _height;\r
+       }\r
+       public NyARCode(int i_width, int i_height) throws NyARException\r
+       {\r
+               this._width = i_width;\r
+               this._height = i_height;\r
+               //空のラスタを4個作成\r
+               for(int i=0;i<4;i++){\r
+                       this._color_pat[i]=new NyARMatchPattDeviationColorData(i_width,i_height);\r
+                       this._bw_pat[i]=new NyARMatchPattDeviationBlackWhiteData(i_width,i_height);\r
+               }\r
+               return;\r
+       }\r
+       public void loadARPattFromFile(String filename) throws NyARException\r
+       {\r
+               try {\r
+                       loadARPatt(new FileInputStream(filename));\r
+               } catch (Exception e) {\r
+                       throw new NyARException(e);\r
+               }\r
+               return;\r
+       }\r
+       public void setRaster(NyARRaster[] i_raster) throws NyARException\r
+       {\r
+               assert i_raster.length!=4;\r
+               //ラスタにパターンをロードする。\r
+               for(int i=0;i<4;i++){\r
+                       this._color_pat[i].setRaster(i_raster[i]);                              \r
+               }\r
+               return;\r
+       }\r
+\r
+       public void loadARPatt(InputStream i_stream) throws NyARException\r
+       {\r
+               //ラスタにパターンをロードする。\r
+               NyARCodeFileReader.loadFromARToolKitFormFile(i_stream,this);\r
+               return;\r
+       }\r
+}\r