OSDN Git Service

git-svn-id: http://svn.sourceforge.jp/svnroot/nyartoolkit/NyARToolkit/trunk@759 7cac0...
[nyartoolkit-and/nyartoolkit-and.git] / lib / src / jp / nyatla / nyartoolkit / core / rasterfilter / rgb2bin / NyARRasterFilter_ARToolkitThreshold.java
1 package jp.nyatla.nyartoolkit.core.rasterfilter.rgb2bin;\r
2 \r
3 import jp.nyatla.nyartoolkit.NyARException;\r
4 import jp.nyatla.nyartoolkit.core.raster.*;\r
5 import jp.nyatla.nyartoolkit.core.raster.rgb.INyARRgbRaster;\r
6 import jp.nyatla.nyartoolkit.core.types.NyARBufferType;\r
7 import jp.nyatla.nyartoolkit.core.types.NyARIntRect;\r
8 import jp.nyatla.nyartoolkit.core.types.NyARIntSize;\r
9 \r
10 \r
11 \r
12 /**\r
13  * 定数閾値による2値化をする。\r
14  * \r
15  */\r
16 public class NyARRasterFilter_ARToolkitThreshold implements INyARRasterFilter_Rgb2Bin\r
17 {\r
18         protected int _threshold;\r
19         private IdoThFilterImpl _do_threshold_impl;\r
20 \r
21         public NyARRasterFilter_ARToolkitThreshold(int i_threshold,int i_in_raster_type) throws NyARException\r
22         {\r
23                 if(!initInstance(i_threshold,i_in_raster_type,NyARBufferType.INT1D_BIN_8)){\r
24                         throw new NyARException();\r
25                 }\r
26         }\r
27         public NyARRasterFilter_ARToolkitThreshold(int i_threshold,int i_in_raster_type,int i_out_raster_type) throws NyARException\r
28         {\r
29                 if(!initInstance(i_threshold,i_in_raster_type,i_out_raster_type)){\r
30                         throw new NyARException();\r
31                 }\r
32         }\r
33         protected boolean initInstance(int i_threshold,int i_in_raster_type,int i_out_raster_type)\r
34         {\r
35                 switch(i_out_raster_type){\r
36                 case NyARBufferType.INT1D_BIN_8:\r
37                         switch (i_in_raster_type){\r
38                         case NyARBufferType.BYTE1D_B8G8R8_24:\r
39                         case NyARBufferType.BYTE1D_R8G8B8_24:\r
40                                 this._do_threshold_impl=new doThFilterImpl_BUFFERFORMAT_BYTE1D_RGB_24();\r
41                                 break;\r
42                         case NyARBufferType.BYTE1D_B8G8R8X8_32:\r
43                                 this._do_threshold_impl=new doThFilterImpl_BUFFERFORMAT_BYTE1D_B8G8R8X8_32();\r
44                                 break;\r
45                         case NyARBufferType.BYTE1D_X8R8G8B8_32:\r
46                                 this._do_threshold_impl=new doThFilterImpl_BUFFERFORMAT_BYTE1D_X8R8G8B8_32();\r
47                                 break;\r
48                         case NyARBufferType.INT1D_X8R8G8B8_32:\r
49                                 this._do_threshold_impl=new doThFilterImpl_BUFFERFORMAT_INT1D_X8R8G8B8_32();\r
50                                 break;\r
51                         case NyARBufferType.WORD1D_R5G6B5_16LE:\r
52                                 this._do_threshold_impl=new doThFilterImpl_BUFFERFORMAT_WORD1D_R5G6B5_16LE();\r
53                                 break;\r
54                         default:\r
55                                 return false;//サポートしない組み合わせ\r
56                         }\r
57                         break;\r
58                 default:\r
59                         return false;//サポートしない組み合わせ\r
60                 }\r
61                 this._threshold = i_threshold;\r
62                 return true;\r
63         }       \r
64         \r
65         /**\r
66          * 画像を2値化するための閾値。暗点<=th<明点となります。\r
67          * @param i_threshold\r
68          */\r
69         public void setThreshold(int i_threshold)\r
70         {\r
71                 this._threshold = i_threshold;\r
72         }\r
73 \r
74         public void doFilter(INyARRgbRaster i_input, NyARBinRaster i_output) throws NyARException\r
75         {\r
76                 assert (i_input.getSize().isEqualSize(i_output.getSize()) == true);\r
77                 NyARIntSize s=i_input.getSize();\r
78                 this._do_threshold_impl.doThFilter(i_input,0,0,s.w,s.h,this._threshold,i_output);\r
79                 return;\r
80         }\r
81         public void doFilter(INyARRgbRaster i_input,NyARIntRect i_area, NyARBinRaster i_output) throws NyARException\r
82         {\r
83                 assert (i_input.getSize().isEqualSize(i_output.getSize()) == true);\r
84                 this._do_threshold_impl.doThFilter(i_input,i_area.x,i_area.y,i_area.w,i_area.h,this._threshold,i_output);\r
85                 return;\r
86                 \r
87         }\r
88         \r
89 \r
90 \r
91         protected interface IdoThFilterImpl\r
92         {\r
93                 public void doThFilter(INyARRaster i_raster,int i_l,int i_t,int i_w,int i_h,int i_th,INyARRaster o_raster);\r
94         }\r
95         \r
96         class doThFilterImpl_BUFFERFORMAT_BYTE1D_RGB_24 implements IdoThFilterImpl\r
97         {\r
98                 public void doThFilter(INyARRaster i_raster,int i_l,int i_t,int i_w,int i_h,int i_th,INyARRaster o_raster)\r
99                 {\r
100                         assert (\r
101                                         i_raster.isEqualBufferType(NyARBufferType.BYTE1D_B8G8R8_24)||\r
102                                         i_raster.isEqualBufferType(NyARBufferType.BYTE1D_R8G8B8_24));\r
103                         final byte[] input=(byte[])i_raster.getBuffer();\r
104                         final int[] output=(int[])o_raster.getBuffer();\r
105                         int th=i_th*3;\r
106                         NyARIntSize s=i_raster.getSize();\r
107                         int skip_dst=(s.w-i_w);\r
108                         int skip_src=skip_dst*3;\r
109                         final int pix_count=i_w;\r
110                         final int pix_mod_part=pix_count-(pix_count%8);                 \r
111                         //左上から1行づつ走査していく\r
112                         int pt_dst=(i_t*s.w+i_l);\r
113                         int pt_src=pt_dst*3;\r
114                         for (int y = i_h-1; y >=0 ; y-=1){\r
115                                 int x;\r
116                                 for (x = pix_count-1; x >=pix_mod_part; x--){\r
117                                         output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;\r
118                                         pt_src+=3;\r
119                                 }\r
120                                 for (;x>=0;x-=8){\r
121                                         output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;\r
122                                         pt_src+=3;\r
123                                         output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;\r
124                                         pt_src+=3;\r
125                                         output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;\r
126                                         pt_src+=3;\r
127                                         output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;\r
128                                         pt_src+=3;\r
129                                         output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;\r
130                                         pt_src+=3;\r
131                                         output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;\r
132                                         pt_src+=3;\r
133                                         output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;\r
134                                         pt_src+=3;\r
135                                         output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;\r
136                                         pt_src+=3;\r
137                                 }\r
138                                 //スキップ\r
139                                 pt_src+=skip_src;\r
140                                 pt_dst+=skip_dst;\r
141                         }\r
142                         return; \r
143                 }\r
144         }\r
145         class doThFilterImpl_BUFFERFORMAT_INT1D_X8R8G8B8_32 implements IdoThFilterImpl\r
146         {\r
147                 public void doThFilter(INyARRaster i_raster,int i_l,int i_t,int i_w,int i_h,int i_th,INyARRaster o_raster)\r
148                 {\r
149                         assert (i_raster.isEqualBufferType( NyARBufferType.INT1D_X8R8G8B8_32));\r
150                         final int[] input=(int[])i_raster.getBuffer();\r
151                         final int[] output=(int[])o_raster.getBuffer();\r
152                         int th=i_th*3;\r
153 \r
154                         NyARIntSize s=i_raster.getSize();\r
155                         int skip_src=(s.w-i_w);\r
156                         int skip_dst=skip_src;\r
157                         final int pix_count=i_w;\r
158                         final int pix_mod_part=pix_count-(pix_count%8);                 \r
159                         //左上から1行づつ走査していく\r
160                         int pt_dst=(i_t*s.w+i_l);\r
161                         int pt_src=pt_dst;\r
162                         for (int y = i_h-1; y >=0 ; y-=1){\r
163                                 int x,v;\r
164                                 for (x = pix_count-1; x >=pix_mod_part; x--){\r
165                                         v=input[pt_src++];output[pt_dst++]=(((v>>16)& 0xff)+((v>>8)& 0xff)+(v& 0xff))<=th?0:1;\r
166                                 }\r
167                                 for (;x>=0;x-=8){\r
168                                         v=input[pt_src++];output[pt_dst++]=(((v>>16)& 0xff)+((v>>8)& 0xff)+(v& 0xff))<=th?0:1;\r
169                                         v=input[pt_src++];output[pt_dst++]=(((v>>16)& 0xff)+((v>>8)& 0xff)+(v& 0xff))<=th?0:1;\r
170                                         v=input[pt_src++];output[pt_dst++]=(((v>>16)& 0xff)+((v>>8)& 0xff)+(v& 0xff))<=th?0:1;\r
171                                         v=input[pt_src++];output[pt_dst++]=(((v>>16)& 0xff)+((v>>8)& 0xff)+(v& 0xff))<=th?0:1;\r
172                                         v=input[pt_src++];output[pt_dst++]=(((v>>16)& 0xff)+((v>>8)& 0xff)+(v& 0xff))<=th?0:1;\r
173                                         v=input[pt_src++];output[pt_dst++]=(((v>>16)& 0xff)+((v>>8)& 0xff)+(v& 0xff))<=th?0:1;\r
174                                         v=input[pt_src++];output[pt_dst++]=(((v>>16)& 0xff)+((v>>8)& 0xff)+(v& 0xff))<=th?0:1;\r
175                                         v=input[pt_src++];output[pt_dst++]=(((v>>16)& 0xff)+((v>>8)& 0xff)+(v& 0xff))<=th?0:1;\r
176                                 }\r
177                                 //スキップ\r
178                                 pt_src+=skip_src;\r
179                                 pt_dst+=skip_dst;                               \r
180                         }\r
181                         return;                 \r
182                 }       \r
183         }\r
184 \r
185         \r
186 \r
187 \r
188         class doThFilterImpl_BUFFERFORMAT_BYTE1D_B8G8R8X8_32 implements IdoThFilterImpl\r
189         {\r
190                 public void doThFilter(INyARRaster i_raster,int i_l,int i_t,int i_w,int i_h,int i_th,INyARRaster o_raster)\r
191                 {\r
192                 assert(i_raster.isEqualBufferType(NyARBufferType.BYTE1D_B8G8R8X8_32));\r
193                         final byte[] input=(byte[])i_raster.getBuffer();\r
194                         final int[] output=(int[])o_raster.getBuffer();\r
195                         NyARIntSize s=i_raster.getSize();\r
196                         int th=i_th*3;\r
197                         int skip_dst=(s.w-i_w);\r
198                         int skip_src=skip_dst*4;\r
199                         final int pix_count=i_w;\r
200                         final int pix_mod_part=pix_count-(pix_count%8);                 \r
201                         //左上から1行づつ走査していく\r
202                         int pt_dst=(i_t*s.w+i_l);\r
203                         int pt_src=pt_dst*4;\r
204                         for (int y = i_h-1; y >=0 ; y-=1){\r
205                                 int x;\r
206                                 for (x = pix_count-1; x >=pix_mod_part; x--){\r
207                                         output[pt_dst++]=((input[pt_src+ 0]& 0xff)+(input[pt_src+ 1]& 0xff)+(input[pt_src+ 2]& 0xff))<=th?0:1;\r
208                                         pt_src+=4;\r
209                                 }\r
210                                 for (;x>=0;x-=8){\r
211                                         output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;\r
212                                         pt_src+=4;\r
213                                         output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;\r
214                                         pt_src+=4;\r
215                                         output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;\r
216                                         pt_src+=4;\r
217                                         output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;\r
218                                         pt_src+=4;\r
219                                         output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;\r
220                                         pt_src+=4;\r
221                                         output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;\r
222                                         pt_src+=4;\r
223                                         output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;\r
224                                         pt_src+=4;\r
225                                         output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;\r
226                                         pt_src+=4;\r
227                                 }\r
228                                 //スキップ\r
229                                 pt_src+=skip_src;\r
230                                 pt_dst+=skip_dst;                               \r
231                         }\r
232                         return; \r
233             }\r
234         }\r
235 \r
236         class doThFilterImpl_BUFFERFORMAT_BYTE1D_X8R8G8B8_32 implements IdoThFilterImpl\r
237         {\r
238                 public void doThFilter(INyARRaster i_raster,int i_l,int i_t,int i_w,int i_h,int i_th,INyARRaster o_raster)\r
239                 {\r
240                 assert(i_raster.isEqualBufferType(NyARBufferType.BYTE1D_X8R8G8B8_32));\r
241                         final byte[] input=(byte[])i_raster.getBuffer();\r
242                         final int[] output=(int[])o_raster.getBuffer();\r
243                         int th=i_th*3;\r
244                         NyARIntSize s=i_raster.getSize();\r
245                         int skip_dst=(s.w-i_w);\r
246                         int skip_src=skip_dst*4;\r
247                         final int pix_count=i_w;\r
248                         final int pix_mod_part=pix_count-(pix_count%8);                 \r
249                         //左上から1行づつ走査していく\r
250                         int pt_dst=(i_t*s.w+i_l);\r
251                         int pt_src=pt_dst*4;\r
252                         for (int y = i_h-1; y >=0 ; y-=1){\r
253                                 int x;\r
254                                 for (x = pix_count-1; x >=pix_mod_part; x--){\r
255                                         output[pt_dst++]=((input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff)+(input[pt_src+3]& 0xff))<=th?0:1;\r
256                                         pt_src+=4;\r
257                                 }\r
258                                 for (;x>=0;x-=8){\r
259                                         output[pt_dst++]=((input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff)+(input[pt_src+3]& 0xff))<=th?0:1;\r
260                                         pt_src+=4;\r
261                                         output[pt_dst++]=((input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff)+(input[pt_src+3]& 0xff))<=th?0:1;\r
262                                         pt_src+=4;\r
263                                         output[pt_dst++]=((input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff)+(input[pt_src+3]& 0xff))<=th?0:1;\r
264                                         pt_src+=4;\r
265                                         output[pt_dst++]=((input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff)+(input[pt_src+3]& 0xff))<=th?0:1;\r
266                                         pt_src+=4;\r
267                                         output[pt_dst++]=((input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff)+(input[pt_src+3]& 0xff))<=th?0:1;\r
268                                         pt_src+=4;\r
269                                         output[pt_dst++]=((input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff)+(input[pt_src+3]& 0xff))<=th?0:1;\r
270                                         pt_src+=4;\r
271                                         output[pt_dst++]=((input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff)+(input[pt_src+3]& 0xff))<=th?0:1;\r
272                                         pt_src+=4;\r
273                                         output[pt_dst++]=((input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff)+(input[pt_src+3]& 0xff))<=th?0:1;\r
274                                         pt_src+=4;\r
275                                 }\r
276                                 //スキップ\r
277                                 pt_src+=skip_src;\r
278                                 pt_dst+=skip_dst;                               \r
279                         }\r
280                         return; \r
281             }\r
282         }\r
283 \r
284         class doThFilterImpl_BUFFERFORMAT_WORD1D_R5G6B5_16LE implements IdoThFilterImpl\r
285         {\r
286                 public void doThFilter(INyARRaster i_raster,int i_l,int i_t,int i_w,int i_h,int i_th,INyARRaster o_raster)\r
287                 {\r
288                 assert(i_raster.isEqualBufferType(NyARBufferType.WORD1D_R5G6B5_16LE));\r
289                         final short[] input=(short[])i_raster.getBuffer();\r
290                         final int[] output=(int[])o_raster.getBuffer();\r
291                         int th=i_th*3;\r
292                         NyARIntSize s=i_raster.getSize();\r
293                         int skip_dst=(s.w-i_w);\r
294                         int skip_src=skip_dst;\r
295                         final int pix_count=i_w;\r
296                         final int pix_mod_part=pix_count-(pix_count%8);                 \r
297                         //左上から1行づつ走査していく\r
298                         int pt_dst=(i_t*s.w+i_l);\r
299                         int pt_src=pt_dst;\r
300                         for (int y = i_h-1; y >=0 ; y-=1){\r
301                                 int x,v;\r
302                                 for (x = pix_count-1; x >=pix_mod_part; x--){\r
303                                         v =(int)input[pt_src++]; output[pt_dst++]=(((v & 0xf800) >> 8) + ((v & 0x07e0) >> 3) + ((v & 0x001f) << 3))<=th?0:1;\r
304                                 }\r
305                                 for (;x>=0;x-=8){\r
306                                         v =(int)input[pt_src++]; output[pt_dst++]=(((v & 0xf800) >> 8) + ((v & 0x07e0) >> 3) + ((v & 0x001f) << 3))<=th?0:1;\r
307                                         v =(int)input[pt_src++]; output[pt_dst++]=(((v & 0xf800) >> 8) + ((v & 0x07e0) >> 3) + ((v & 0x001f) << 3))<=th?0:1;\r
308                                         v =(int)input[pt_src++]; output[pt_dst++]=(((v & 0xf800) >> 8) + ((v & 0x07e0) >> 3) + ((v & 0x001f) << 3))<=th?0:1;\r
309                                         v =(int)input[pt_src++]; output[pt_dst++]=(((v & 0xf800) >> 8) + ((v & 0x07e0) >> 3) + ((v & 0x001f) << 3))<=th?0:1;\r
310                                         v =(int)input[pt_src++]; output[pt_dst++]=(((v & 0xf800) >> 8) + ((v & 0x07e0) >> 3) + ((v & 0x001f) << 3))<=th?0:1;\r
311                                         v =(int)input[pt_src++]; output[pt_dst++]=(((v & 0xf800) >> 8) + ((v & 0x07e0) >> 3) + ((v & 0x001f) << 3))<=th?0:1;\r
312                                         v =(int)input[pt_src++]; output[pt_dst++]=(((v & 0xf800) >> 8) + ((v & 0x07e0) >> 3) + ((v & 0x001f) << 3))<=th?0:1;\r
313                                         v =(int)input[pt_src++]; output[pt_dst++]=(((v & 0xf800) >> 8) + ((v & 0x07e0) >> 3) + ((v & 0x001f) << 3))<=th?0:1;\r
314                                 }\r
315                                 //スキップ\r
316                                 pt_src+=skip_src;\r
317                                 pt_dst+=skip_dst;\r
318                         }\r
319                         return; \r
320             }\r
321         }\r
322         \r
323 }\r