OSDN Git Service

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