OSDN Git Service

[更新]NyARToolkit for Java
[nyartoolkit-and/nyartoolkit-and.git] / 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 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.labeling.artoolkit;\r
33 \r
34 import jp.nyatla.nyartoolkit.NyARException;\r
35 import jp.nyatla.nyartoolkit.core.raster.*;\r
36 import jp.nyatla.nyartoolkit.core.rasterreader.INyARBufferReader;\r
37 import jp.nyatla.nyartoolkit.core.rasterreader.NyARBufferReader;\r
38 import jp.nyatla.nyartoolkit.core.types.*;\r
39 \r
40 /**\r
41  *\r
42  */\r
43 public class NyARLabelingImage extends NyARRaster_BasicClass\r
44 {\r
45         private final static int MAX_LABELS = 1024*32;  \r
46         protected int[] _ref_buf;\r
47         private INyARBufferReader _buffer_reader;\r
48         protected NyARLabelingLabelStack _label_list;\r
49         protected int[] _index_table;\r
50         protected boolean _is_index_table_enable;\r
51         public NyARLabelingImage(int i_width, int i_height)\r
52         {\r
53                 super(new NyARIntSize(i_width,i_height));\r
54                 this._ref_buf =new int[i_height*i_width];\r
55                 this._label_list = new NyARLabelingLabelStack(MAX_LABELS);\r
56                 this._index_table=new int[MAX_LABELS];\r
57                 this._is_index_table_enable=false;\r
58                 this._buffer_reader=new NyARBufferReader(this._ref_buf,INyARBufferReader.BUFFERFORMAT_INT1D);\r
59                 //生成時に枠を書きます。\r
60                 drawFrameEdge();\r
61                 return;\r
62         }\r
63         public INyARBufferReader getBufferReader()\r
64         {\r
65                 return this._buffer_reader;\r
66         }\r
67         /**\r
68          * エッジを書きます。\r
69          */\r
70         public void drawFrameEdge()\r
71         {\r
72                 int w=this._size.w;\r
73                 int h=this._size.h;\r
74                 // NyLabelingImageのイメージ初期化(枠書き)\r
75                 int[] img = (int[]) this._ref_buf;\r
76                 int bottom_ptr = (h - 1) * w;\r
77                 for (int i = 0; i < w; i++) {\r
78                         img[i] = 0;\r
79                         img[bottom_ptr + i] = 0;\r
80                 }\r
81                 for (int i = 0; i < h; i++) {\r
82                         img[i * w] = 0;\r
83                         img[(i + 1) * w - 1] = 0;\r
84                 }\r
85                 return;\r
86         }\r
87 \r
88         /**\r
89          * ラベリング結果がインデックステーブルを持つ場合、その配列を返します。\r
90          * 持たない場合、nullを返します。\r
91          * \r
92          * 値がnullの時はラベル番号そのものがラスタに格納されていますが、\r
93          * null以外の時はラスタに格納されているのはインデクス番号です。\r
94          * \r
95          * インデクス番号とラベル番号の関係は、以下の式で表されます。\r
96          * ラベル番号:=value[インデクス番号]\r
97          * \r
98          */\r
99         public int[] getIndexArray()\r
100         {\r
101                 return this._is_index_table_enable?this._index_table:null;\r
102         }\r
103         \r
104         public NyARLabelingLabelStack getLabelStack()\r
105         {\r
106                 return this._label_list;\r
107         }\r
108         public void reset(boolean i_label_index_enable)\r
109         {\r
110                 assert(i_label_index_enable==true);//非ラベルモードは未実装\r
111                 this._label_list.clear();\r
112                 this._is_index_table_enable=i_label_index_enable;\r
113                 return;\r
114         }\r
115         //巡回参照できるように、テーブルを二重化\r
116         //                                           0  1  2  3  4  5  6  7   0  1  2  3  4  5  6\r
117         protected final static int[] _getContour_xdir = { 0, 1, 1, 1, 0,-1,-1,-1 , 0, 1, 1, 1, 0,-1,-1};\r
118         protected final static int[] _getContour_ydir = {-1,-1, 0, 1, 1, 1, 0,-1 ,-1,-1, 0, 1, 1, 1, 0};\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