OSDN Git Service

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