OSDN Git Service

git-svn-id: http://svn.sourceforge.jp/svnroot/nyartoolkit/NyARToolkit/trunk@748 7cac0...
[nyartoolkit-and/nyartoolkit-and.git] / lib / src.rpf / jp / nyatla / nyartoolkit / rpf / tracker / nyartk / NegativeSqRoberts.java
1 /* \r
2  * PROJECT: NyARToolkit(Extension)\r
3  * --------------------------------------------------------------------------------\r
4  * The NyARToolkit is Java edition ARToolKit class library.\r
5  * Copyright (C)2008-2009 Ryo Iizuka\r
6  *\r
7  * This program is free software: you can redistribute it and/or modify\r
8  * it under the terms of the GNU General Public License as published by\r
9  * the Free Software Foundation, either version 3 of the License, or\r
10  * (at your option) any later version.\r
11  * \r
12  * This program is distributed in the hope that it will be useful,\r
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
15  * GNU General Public License for more details.\r
16  *\r
17  * You should have received a copy of the GNU General Public License\r
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
19  * \r
20  * For further information please contact.\r
21  *      http://nyatla.jp/nyatoolkit/\r
22  *      <airmail(at)ebony.plala.or.jp> or <nyatla(at)nyatla.jp>\r
23  * \r
24  */\r
25 package jp.nyatla.nyartoolkit.rpf.tracker.nyartk;\r
26 \r
27 import jp.nyatla.nyartoolkit.NyARException;\r
28 import jp.nyatla.nyartoolkit.core.raster.*;\r
29 import jp.nyatla.nyartoolkit.core.rasterfilter.INyARRasterFilter;\r
30 import jp.nyatla.nyartoolkit.core.types.NyARBufferType;\r
31 import jp.nyatla.nyartoolkit.core.types.NyARIntSize;\r
32 \r
33 /**\r
34  * NyARReality用のエッジ検出フィルタ。\r
35  * Roberts勾配の2乗値16倍に最大値制限をかけ、反転した値です。\r
36  * 右端と左端の1ピクセルは、常に0が入ります。\r
37  * X=|-1, 0|  Y=|0,-1|\r
38  *   | 0, 1|    |1, 0|\r
39  * V=sqrt(X^2+Y+2)/2\r
40  */\r
41 public class NegativeSqRoberts implements INyARRasterFilter\r
42 {\r
43         private IdoFilterImpl _do_filter_impl; \r
44         public NegativeSqRoberts(int i_raster_type) throws NyARException\r
45         {\r
46                 this.initInstance(i_raster_type);\r
47         }\r
48         public void initInstance(int i_raster_type)throws NyARException\r
49         {\r
50                 switch (i_raster_type) {\r
51                 case NyARBufferType.INT1D_GRAY_8:\r
52                         this._do_filter_impl=new IdoFilterImpl_GRAY_8();\r
53                         break;\r
54                 default:\r
55                         throw new NyARException();\r
56                 }               \r
57         }\r
58         public void doFilter(INyARRaster i_input, INyARRaster i_output) throws NyARException\r
59         {\r
60                 this._do_filter_impl.doFilter(i_input,i_output,i_input.getSize());\r
61         }\r
62         \r
63         interface IdoFilterImpl\r
64         {\r
65                 public void doFilter(INyARRaster i_input, INyARRaster i_output,NyARIntSize i_size) throws NyARException;\r
66         }\r
67         private final static int SH=4;\r
68         class IdoFilterImpl_GRAY_8 implements IdoFilterImpl\r
69         {\r
70                 public void doFilter(INyARRaster i_input, INyARRaster i_output,NyARIntSize i_size) throws NyARException\r
71                 {\r
72                         assert (i_input.isEqualBufferType(NyARBufferType.INT1D_GRAY_8));\r
73                         assert (i_output.isEqualBufferType(NyARBufferType.INT1D_GRAY_8));\r
74                         int[] in_ptr =(int[])i_input.getBuffer();\r
75                         int[] out_ptr=(int[])i_output.getBuffer();\r
76                         int width=i_size.w;\r
77                         int idx=0;\r
78                         int idx2=width;\r
79                         int fx,fy;\r
80                         int mod_p=(width-2)-(width-2)%4;\r
81                         for(int y=i_size.h-2;y>=0;y--){\r
82                                 int p00=in_ptr[idx++];\r
83                                 int p10=in_ptr[idx2++];\r
84                                 int p01,p11;\r
85                                 int x=width-2;\r
86                                 for(;x>=mod_p;x--){\r
87                                         p01=in_ptr[idx++];p11=in_ptr[idx2++];\r
88                                         fx=p11-p00;fy=p10-p01;\r
89 //                                      out_ptr[idx-2]=255-(((fx<0?-fx:fx)+(fy<0?-fy:fy))>>1);\r
90                                         fx=(fx*fx+fy*fy)>>SH;out_ptr[idx-2]=(fx>255?0:255-fx);\r
91                                         p00=p01;\r
92                                         p10=p11;\r
93                                 }\r
94                                 for(;x>=0;x-=4){\r
95                                         p01=in_ptr[idx++];p11=in_ptr[idx2++];\r
96                                         fx=p11-p00;\r
97                                         fy=p10-p01;\r
98 //                                      out_ptr[idx-2]=255-(((fx<0?-fx:fx)+(fy<0?-fy:fy))>>1);\r
99                                         fx=(fx*fx+fy*fy)>>SH;out_ptr[idx-2]=(fx>255?0:255-fx);\r
100                                         p00=p01;p10=p11;\r
101                                         p01=in_ptr[idx++];p11=in_ptr[idx2++];\r
102                                         fx=p11-p00;\r
103                                         fy=p10-p01;\r
104 //                                      out_ptr[idx-2]=255-(((fx<0?-fx:fx)+(fy<0?-fy:fy))>>1);\r
105                                         fx=(fx*fx+fy*fy)>>SH;out_ptr[idx-2]=(fx>255?0:255-fx);\r
106                                         p00=p01;p10=p11;\r
107                                         p01=in_ptr[idx++];p11=in_ptr[idx2++];\r
108                                         \r
109                                         fx=p11-p00;\r
110                                         fy=p10-p01;\r
111 //                                      out_ptr[idx-2]=255-(((fx<0?-fx:fx)+(fy<0?-fy:fy))>>1);\r
112                                         fx=(fx*fx+fy*fy)>>SH;out_ptr[idx-2]=(fx>255?0:255-fx);\r
113                                         p00=p01;p10=p11;\r
114 \r
115                                         p01=in_ptr[idx++];p11=in_ptr[idx2++];\r
116                                         fx=p11-p00;\r
117                                         fy=p10-p01;\r
118 //                                      out_ptr[idx-2]=255-(((fx<0?-fx:fx)+(fy<0?-fy:fy))>>1);\r
119                                         fx=(fx*fx+fy*fy)>>SH;out_ptr[idx-2]=(fx>255?0:255-fx);\r
120                                         p00=p01;p10=p11;\r
121 \r
122                                 }\r
123                                 out_ptr[idx-1]=255;\r
124                         }\r
125                         for(int x=width-1;x>=0;x--){\r
126                                 out_ptr[idx++]=255;\r
127                         }\r
128                         return;\r
129                 }\r
130         }\r
131 }\r
132 \r