OSDN Git Service

Merge branch 'git-svn'
[nyartoolkit-and/nyartoolkit-and.git] / tags / 2.5.1 / src / jp / nyatla / nyartoolkit / core / rasterfilter / NyARRasterFilter_GaussianSmooth.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.core.rasterfilter;\r
26 \r
27 import jp.nyatla.nyartoolkit.NyARException;\r
28 import jp.nyatla.nyartoolkit.core.raster.*;\r
29 import jp.nyatla.nyartoolkit.core.types.NyARBufferType;\r
30 import jp.nyatla.nyartoolkit.core.types.NyARIntSize;\r
31 \r
32 \r
33 /**\r
34  * 平滑化フィルタ\r
35  * Gaussianフィルタで画像を平滑化します。\r
36  * カーネルサイズは3x3です。\r
37  */\r
38 public class NyARRasterFilter_GaussianSmooth implements INyARRasterFilter\r
39 {\r
40         private IdoFilterImpl _do_filter_impl; \r
41         public NyARRasterFilter_GaussianSmooth(int i_raster_type) throws NyARException\r
42         {\r
43                 switch (i_raster_type) {\r
44                 case NyARBufferType.INT1D_GRAY_8:\r
45                         this._do_filter_impl=new IdoFilterImpl_GRAY_8();\r
46                         break;\r
47                 default:\r
48                         throw new NyARException();\r
49                 }\r
50         }\r
51         public void doFilter(INyARRaster i_input, INyARRaster i_output) throws NyARException\r
52         {\r
53                 assert (i_input!=i_output);\r
54                 this._do_filter_impl.doFilter(i_input,i_output,i_input.getSize());\r
55         }\r
56         \r
57         interface IdoFilterImpl\r
58         {\r
59                 public void doFilter(INyARRaster i_input, INyARRaster i_output,NyARIntSize i_size) throws NyARException;\r
60         }\r
61         class IdoFilterImpl_GRAY_8 implements IdoFilterImpl\r
62         {\r
63                 public void doFilter(INyARRaster i_input, INyARRaster i_output,NyARIntSize i_size) throws NyARException\r
64                 {\r
65                         assert (i_input.isEqualBufferType(NyARBufferType.INT1D_GRAY_8));\r
66                         assert (i_output.isEqualBufferType(NyARBufferType.INT1D_GRAY_8));\r
67                         int[] in_ptr =(int[])i_input.getBuffer();\r
68                         int[] out_ptr=(int[])i_output.getBuffer();\r
69                         int width=i_size.w;\r
70                         int height=i_size.h;\r
71                         int col0,col1,col2;\r
72                         int bptr=0;\r
73                         //1行目\r
74                         col1=in_ptr[bptr  ]*2+in_ptr[bptr+width  ];\r
75                         col2=in_ptr[bptr+1]*2+in_ptr[bptr+width+1];\r
76                         out_ptr[bptr]=(col1*2+col2)/9;\r
77                         bptr++;\r
78                         for(int x=0;x<width-2;x++){\r
79                                 col0=col1;\r
80                                 col1=col2;\r
81                                 col2=in_ptr[bptr+1]*2+in_ptr[bptr+width+1];\r
82                                 out_ptr[bptr]=(col0+col1*2+col2)/12;\r
83                                 bptr++;\r
84                         }                       \r
85                         out_ptr[bptr]=(col1+col2)/9;\r
86                         bptr++;\r
87                         //2行目-末行-1\r
88 \r
89                         for(int y=0;y<height-2;y++){\r
90                                 //左端\r
91                                 col1=in_ptr[bptr  ]*2+in_ptr[bptr-width  ]+in_ptr[bptr+width  ];\r
92                                 col2=in_ptr[bptr+1]*2+in_ptr[bptr-width+1]+in_ptr[bptr+width+1];\r
93                                 out_ptr[bptr]=(col1+col2)/12;\r
94                                 bptr++;\r
95                                 for(int x=0;x<width-2;x++){\r
96                                         col0=col1;\r
97                                         col1=col2;\r
98                                         col2=in_ptr[bptr+1]*2+in_ptr[bptr-width+1]+in_ptr[bptr+width+1];\r
99                                         out_ptr[bptr]=(col0+col1*2+col2)/16;\r
100                                         bptr++;\r
101                                 }\r
102                                 //右端\r
103                                 out_ptr[bptr]=(col1*2+col2)/12;\r
104                                 bptr++;\r
105                         }\r
106                         //末行目\r
107                         col1=in_ptr[bptr  ]*2+in_ptr[bptr-width  ];\r
108                         col2=in_ptr[bptr+1]*2+in_ptr[bptr-width+1];\r
109                         out_ptr[bptr]=(col1+col2)/9;\r
110                         bptr++;\r
111                         for(int x=0;x<width-2;x++){\r
112                                 col0=col1;\r
113                                 col1=col2;\r
114                                 col2=in_ptr[bptr+1]*2+in_ptr[bptr-width+1];\r
115                                 out_ptr[bptr]=(col0+col1*2+col2)/12;\r
116                                 bptr++;\r
117                         }                       \r
118                         out_ptr[bptr]=(col1*2+col2)/9;\r
119                         bptr++;\r
120                         return;\r
121                 }\r
122         }\r
123 }