OSDN Git Service

[tag]NyARToolkit/2.5.1
[nyartoolkit-and/nyartoolkit-and.git] / tags / 2.5.1 / src / jp / nyatla / nyartoolkit / core / labeling / artoolkit / NyARLabelingImage.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.labeling.artoolkit;\r
32 \r
33 import jp.nyatla.nyartoolkit.NyARException;\r
34 import jp.nyatla.nyartoolkit.core.raster.*;\r
35 import jp.nyatla.nyartoolkit.core.types.*;\r
36 \r
37 /**\r
38  *\r
39  */\r
40 public class NyARLabelingImage extends NyARRaster_BasicClass\r
41 {\r
42         private final static int MAX_LABELS = 1024*32;\r
43 \r
44         protected int[] _ref_buf;\r
45         protected NyARLabelingLabelStack _label_list;\r
46         protected int[] _index_table;\r
47         protected boolean _is_index_table_enable;\r
48         public NyARLabelingImage(int i_width, int i_height) throws NyARException\r
49         {\r
50                 super(new NyARIntSize(i_width,i_height),NyARBufferType.INT1D);\r
51                 this._ref_buf =new int[i_height*i_width];\r
52                 this._label_list = new NyARLabelingLabelStack(MAX_LABELS);\r
53                 this._index_table=new int[MAX_LABELS];\r
54                 this._is_index_table_enable=false;\r
55                 //生成時に枠を書きます。\r
56                 drawFrameEdge();\r
57                 return;\r
58         }\r
59         public Object getBuffer()\r
60         {\r
61                 return this._ref_buf;\r
62         }\r
63         public boolean hasBuffer()\r
64         {\r
65                 return this._ref_buf!=null;\r
66         }\r
67         public void wrapBuffer(Object i_ref_buf) throws NyARException\r
68         {\r
69                 NyARException.notImplement();\r
70         }       \r
71         /**\r
72          * エッジを書きます。\r
73          */\r
74         public void drawFrameEdge()\r
75         {\r
76                 int w=this._size.w;\r
77                 int h=this._size.h;\r
78                 // NyLabelingImageのイメージ初期化(枠書き)\r
79                 int[] img = (int[]) this._ref_buf;\r
80                 int bottom_ptr = (h - 1) * w;\r
81                 for (int i = 0; i < w; i++) {\r
82                         img[i] = 0;\r
83                         img[bottom_ptr + i] = 0;\r
84                 }\r
85                 for (int i = 0; i < h; i++) {\r
86                         img[i * w] = 0;\r
87                         img[(i + 1) * w - 1] = 0;\r
88                 }\r
89                 return;\r
90         }\r
91 \r
92         /**\r
93          * ラベリング結果がインデックステーブルを持つ場合、その配列を返します。\r
94          * 持たない場合、nullを返します。\r
95          * \r
96          * 値がnullの時はラベル番号そのものがラスタに格納されていますが、\r
97          * null以外の時はラスタに格納されているのはインデクス番号です。\r
98          * \r
99          * インデクス番号とラベル番号の関係は、以下の式で表されます。\r
100          * ラベル番号:=value[インデクス番号]\r
101          * \r
102          */\r
103         public int[] getIndexArray()\r
104         {\r
105                 return this._is_index_table_enable?this._index_table:null;\r
106         }\r
107         \r
108         public NyARLabelingLabelStack getLabelStack()\r
109         {\r
110                 return this._label_list;\r
111         }\r
112         public void reset(boolean i_label_index_enable)\r
113         {\r
114                 assert(i_label_index_enable==true);//非ラベルモードは未実装\r
115                 this._label_list.clear();\r
116                 this._is_index_table_enable=i_label_index_enable;\r
117                 return;\r
118         }\r
119         /**\r
120          * i_labelのラベルの、クリップ領域が上辺に接しているx座標を返します。\r
121          * @param i_index\r
122          * @return\r
123          */\r
124         public int getTopClipTangentX(NyARLabelingLabel i_label) throws NyARException\r
125         {\r
126                 int pix;\r
127                 int i_label_id=i_label.id;\r
128                 int[] index_table=this._index_table;\r
129                 int[] limage=this._ref_buf;\r
130                 int limage_ptr=i_label.clip_t*this._size.w;\r
131                 final int clip1 = i_label.clip_r;\r
132                 // p1=ShortPointer.wrap(limage,j*xsize+clip.get());//p1 =&(limage[j*xsize+clip[0]]);\r
133                 for (int i = i_label.clip_l; i <= clip1; i++) {// for( i = clip[0]; i <=clip[1]; i++, p1++ ) {\r
134                         pix = limage[limage_ptr+i];\r
135                         if (pix > 0 && index_table[pix-1] == i_label_id){\r
136                                 return i;\r
137                         }\r
138                 }\r
139                 //あれ?見つからないよ?\r
140                 throw new NyARException();\r
141         }       \r
142 }\r