2 * PROJECT: NyARToolkit
\r
3 * --------------------------------------------------------------------------------
\r
4 * This work is based on the original ARToolKit developed by
\r
7 * HITLab, University of Washington, Seattle
\r
8 * http://www.hitl.washington.edu/artoolkit/
\r
10 * The NyARToolkit is Java version ARToolkit class library.
\r
11 * Copyright (C)2008 R.Iizuka
\r
13 * This program is free software; you can redistribute it and/or
\r
14 * modify it under the terms of the GNU General Public License
\r
15 * as published by the Free Software Foundation; either version 2
\r
16 * of the License, or (at your option) any later version.
\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
23 * You should have received a copy of the GNU General Public License
\r
24 * along with this framework; if not, write to the Free Software
\r
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
27 * For further information please contact.
\r
28 * http://nyatla.jp/nyatoolkit/
\r
29 * <airmail(at)ebony.plala.or.jp>
\r
32 package jp.nyatla.nyartoolkit.core.rasterfilter.rgb2bin;
\r
34 import jp.nyatla.nyartoolkit.NyARException;
\r
35 import jp.nyatla.nyartoolkit.core.raster.*;
\r
36 import jp.nyatla.nyartoolkit.core.raster.rgb.INyARRgbRaster;
\r
37 import jp.nyatla.nyartoolkit.core.rasterreader.INyARBufferReader;
\r
38 import jp.nyatla.nyartoolkit.core.types.*;
\r
44 public class NyARRasterFilter_ARToolkitThreshold implements INyARRasterFilter_RgbToBin
\r
46 interface IdoThFilterImpl
\r
48 public void doThFilter(INyARBufferReader i_input, INyARBufferReader i_output,NyARIntSize i_size,int i_threshold);
\r
50 class doThFilterImpl_BUFFERFORMAT_BYTE1D_RGB_24 implements IdoThFilterImpl
\r
52 public void doThFilter(INyARBufferReader i_input, INyARBufferReader i_output,NyARIntSize i_size,int i_threshold)
\r
54 int[] out_buf = (int[]) i_output.getBuffer();
\r
55 byte[] in_buf = (byte[]) i_input.getBuffer();
\r
57 final int th=i_threshold*3;
\r
58 int bp =(i_size.w*i_size.h-1)*3;
\r
61 final int pix_count =i_size.h*i_size.w;
\r
62 final int pix_mod_part=pix_count-(pix_count%8);
\r
63 for(xy=pix_count-1;xy>=pix_mod_part;xy--){
\r
64 w= ((in_buf[bp] & 0xff) + (in_buf[bp + 1] & 0xff) + (in_buf[bp + 2] & 0xff));
\r
65 out_buf[xy]=w<=th?0:1;
\r
70 w= ((in_buf[bp] & 0xff) + (in_buf[bp + 1] & 0xff) + (in_buf[bp + 2] & 0xff));
\r
71 out_buf[xy]=w<=th?0:1;
\r
74 w= ((in_buf[bp] & 0xff) + (in_buf[bp + 1] & 0xff) + (in_buf[bp + 2] & 0xff));
\r
75 out_buf[xy]=w<=th?0:1;
\r
78 w= ((in_buf[bp] & 0xff) + (in_buf[bp + 1] & 0xff) + (in_buf[bp + 2] & 0xff));
\r
79 out_buf[xy]=w<=th?0:1;
\r
82 w= ((in_buf[bp] & 0xff) + (in_buf[bp + 1] & 0xff) + (in_buf[bp + 2] & 0xff));
\r
83 out_buf[xy]=w<=th?0:1;
\r
86 w= ((in_buf[bp] & 0xff) + (in_buf[bp + 1] & 0xff) + (in_buf[bp + 2] & 0xff));
\r
87 out_buf[xy]=w<=th?0:1;
\r
90 w= ((in_buf[bp] & 0xff) + (in_buf[bp + 1] & 0xff) + (in_buf[bp + 2] & 0xff));
\r
91 out_buf[xy]=w<=th?0:1;
\r
94 w= ((in_buf[bp] & 0xff) + (in_buf[bp + 1] & 0xff) + (in_buf[bp + 2] & 0xff));
\r
95 out_buf[xy]=w<=th?0:1;
\r
98 w= ((in_buf[bp] & 0xff) + (in_buf[bp + 1] & 0xff) + (in_buf[bp + 2] & 0xff));
\r
99 out_buf[xy]=w<=th?0:1;
\r
107 class doThFilterImpl_BUFFERFORMAT_BYTE1D_B8G8R8X8_32 implements IdoThFilterImpl
\r
109 public void doThFilter(INyARBufferReader i_input, INyARBufferReader i_output,NyARIntSize i_size,int i_threshold)
\r
111 int[] out_buf = (int[]) i_output.getBuffer();
\r
112 byte[] in_buf = (byte[]) i_input.getBuffer();
\r
114 final int th=i_threshold*3;
\r
115 int bp =(i_size.w*i_size.h-1)*4;
\r
118 final int pix_count =i_size.h*i_size.w;
\r
119 final int pix_mod_part=pix_count-(pix_count%8);
\r
120 for(xy=pix_count-1;xy>=pix_mod_part;xy--){
\r
121 w= ((in_buf[bp] & 0xff) + (in_buf[bp + 1] & 0xff) + (in_buf[bp + 2] & 0xff));
\r
122 out_buf[xy]=w<=th?0:1;
\r
127 w= ((in_buf[bp] & 0xff) + (in_buf[bp + 1] & 0xff) + (in_buf[bp + 2] & 0xff));
\r
128 out_buf[xy]=w<=th?0:1;
\r
131 w= ((in_buf[bp] & 0xff) + (in_buf[bp + 1] & 0xff) + (in_buf[bp + 2] & 0xff));
\r
132 out_buf[xy]=w<=th?0:1;
\r
135 w= ((in_buf[bp] & 0xff) + (in_buf[bp + 1] & 0xff) + (in_buf[bp + 2] & 0xff));
\r
136 out_buf[xy]=w<=th?0:1;
\r
139 w= ((in_buf[bp] & 0xff) + (in_buf[bp + 1] & 0xff) + (in_buf[bp + 2] & 0xff));
\r
140 out_buf[xy]=w<=th?0:1;
\r
143 w= ((in_buf[bp] & 0xff) + (in_buf[bp + 1] & 0xff) + (in_buf[bp + 2] & 0xff));
\r
144 out_buf[xy]=w<=th?0:1;
\r
147 w= ((in_buf[bp] & 0xff) + (in_buf[bp + 1] & 0xff) + (in_buf[bp + 2] & 0xff));
\r
148 out_buf[xy]=w<=th?0:1;
\r
151 w= ((in_buf[bp] & 0xff) + (in_buf[bp + 1] & 0xff) + (in_buf[bp + 2] & 0xff));
\r
152 out_buf[xy]=w<=th?0:1;
\r
155 w= ((in_buf[bp] & 0xff) + (in_buf[bp + 1] & 0xff) + (in_buf[bp + 2] & 0xff));
\r
156 out_buf[xy]=w<=th?0:1;
\r
163 class doThFilterImpl_BUFFERFORMAT_BYTE1D_X8R8G8B8_32 implements IdoThFilterImpl
\r
165 public void doThFilter(INyARBufferReader i_input, INyARBufferReader i_output,NyARIntSize i_size,int i_threshold)
\r
167 int[] out_buf = (int[]) i_output.getBuffer();
\r
168 byte[] in_buf = (byte[]) i_input.getBuffer();
\r
170 final int th=i_threshold*3;
\r
171 int bp =(i_size.w*i_size.h-1)*4;
\r
174 final int pix_count =i_size.h*i_size.w;
\r
175 final int pix_mod_part=pix_count-(pix_count%8);
\r
176 for(xy=pix_count-1;xy>=pix_mod_part;xy--){
\r
177 w= ((in_buf[bp+1] & 0xff) + (in_buf[bp + 2] & 0xff) + (in_buf[bp + 3] & 0xff));
\r
178 out_buf[xy]=w<=th?0:1;
\r
183 w= ((in_buf[bp+1] & 0xff) + (in_buf[bp + 2] & 0xff) + (in_buf[bp + 3] & 0xff));
\r
184 out_buf[xy]=w<=th?0:1;
\r
187 w= ((in_buf[bp+1] & 0xff) + (in_buf[bp + 2] & 0xff) + (in_buf[bp + 3] & 0xff));
\r
188 out_buf[xy]=w<=th?0:1;
\r
191 w= ((in_buf[bp+1] & 0xff) + (in_buf[bp + 2] & 0xff) + (in_buf[bp + 3] & 0xff));
\r
192 out_buf[xy]=w<=th?0:1;
\r
195 w= ((in_buf[bp+1] & 0xff) + (in_buf[bp + 2] & 0xff) + (in_buf[bp + 3] & 0xff));
\r
196 out_buf[xy]=w<=th?0:1;
\r
199 w= ((in_buf[bp+1] & 0xff) + (in_buf[bp + 2] & 0xff) + (in_buf[bp + 3] & 0xff));
\r
200 out_buf[xy]=w<=th?0:1;
\r
203 w= ((in_buf[bp+1] & 0xff) + (in_buf[bp + 2] & 0xff) + (in_buf[bp + 3] & 0xff));
\r
204 out_buf[xy]=w<=th?0:1;
\r
207 w= ((in_buf[bp+1] & 0xff) + (in_buf[bp + 2] & 0xff) + (in_buf[bp + 3] & 0xff));
\r
208 out_buf[xy]=w<=th?0:1;
\r
211 w= ((in_buf[bp+1] & 0xff) + (in_buf[bp + 2] & 0xff) + (in_buf[bp + 3] & 0xff));
\r
212 out_buf[xy]=w<=th?0:1;
\r
221 class doThFilterImpl_BUFFERFORMAT_INT1D_X8R8G8B8_32 implements IdoThFilterImpl
\r
223 public void doThFilter(INyARBufferReader i_input, INyARBufferReader i_output,NyARIntSize i_size,int i_threshold)
\r
225 int[] out_buf = (int[]) i_output.getBuffer();
\r
226 int[] in_buf = (int[]) i_input.getBuffer();
\r
228 final int th=i_threshold*3;
\r
231 final int pix_count =i_size.h*i_size.w;
\r
232 final int pix_mod_part=pix_count-(pix_count%8);
\r
234 for(xy=pix_count-1;xy>=pix_mod_part;xy--){
\r
236 out_buf[xy]=(((w>>16)&0xff)+((w>>8)&0xff)+(w&0xff))<=th?0:1;
\r
241 out_buf[xy]=(((w>>16)&0xff)+((w>>8)&0xff)+(w&0xff))<=th?0:1;
\r
244 out_buf[xy]=(((w>>16)&0xff)+((w>>8)&0xff)+(w&0xff))<=th?0:1;
\r
247 out_buf[xy]=(((w>>16)&0xff)+((w>>8)&0xff)+(w&0xff))<=th?0:1;
\r
250 out_buf[xy]=(((w>>16)&0xff)+((w>>8)&0xff)+(w&0xff))<=th?0:1;
\r
253 out_buf[xy]=(((w>>16)&0xff)+((w>>8)&0xff)+(w&0xff))<=th?0:1;
\r
256 out_buf[xy]=(((w>>16)&0xff)+((w>>8)&0xff)+(w&0xff))<=th?0:1;
\r
259 out_buf[xy]=(((w>>16)&0xff)+((w>>8)&0xff)+(w&0xff))<=th?0:1;
\r
262 out_buf[xy]=(((w>>16)&0xff)+((w>>8)&0xff)+(w&0xff))<=th?0:1;
\r
268 class doThFilterImpl_BUFFERFORMAT_WORD1D_R5G6B5_16LE implements IdoThFilterImpl
\r
270 public void doThFilter(INyARBufferReader i_input, INyARBufferReader i_output,NyARIntSize i_size,int i_threshold)
\r
272 int[] out_buf = (int[]) i_output.getBuffer();
\r
273 short[] in_buf = (short[]) i_input.getBuffer();
\r
275 final int th=i_threshold*3;
\r
278 final int pix_count =i_size.h*i_size.w;
\r
279 final int pix_mod_part=pix_count-(pix_count%8);
\r
281 for(xy=pix_count-1;xy>=pix_mod_part;xy--){
\r
282 w =(int)in_buf[xy];
\r
283 w = ((w & 0xf800) >> 8) + ((w & 0x07e0) >> 3) + ((w & 0x001f) << 3);
\r
284 out_buf[xy] = w <= th ? 0 : 1;
\r
288 w =(int)in_buf[xy];
\r
289 w = ((w & 0xf800) >> 8) + ((w & 0x07e0) >> 3) + ((w & 0x001f) << 3);
\r
290 out_buf[xy] = w <= th ? 0 : 1;
\r
292 w =(int)in_buf[xy];
\r
293 w = ((w & 0xf800) >> 8) + ((w & 0x07e0) >> 3) + ((w & 0x001f) << 3);
\r
294 out_buf[xy] = w <= th ? 0 : 1;
\r
296 w =(int)in_buf[xy];
\r
297 w = ((w & 0xf800) >> 8) + ((w & 0x07e0) >> 3) + ((w & 0x001f) << 3);
\r
298 out_buf[xy] = w <= th ? 0 : 1;
\r
300 w =(int)in_buf[xy];
\r
301 w = ((w & 0xf800) >> 8) + ((w & 0x07e0) >> 3) + ((w & 0x001f) << 3);
\r
302 out_buf[xy] = w <= th ? 0 : 1;
\r
304 w =(int)in_buf[xy];
\r
305 w = ((w & 0xf800) >> 8) + ((w & 0x07e0) >> 3) + ((w & 0x001f) << 3);
\r
306 out_buf[xy] = w <= th ? 0 : 1;
\r
308 w =(int)in_buf[xy];
\r
309 w = ((w & 0xf800) >> 8) + ((w & 0x07e0) >> 3) + ((w & 0x001f) << 3);
\r
310 out_buf[xy] = w <= th ? 0 : 1;
\r
312 w =(int)in_buf[xy];
\r
313 w = ((w & 0xf800) >> 8) + ((w & 0x07e0) >> 3) + ((w & 0x001f) << 3);
\r
314 out_buf[xy] = w <= th ? 0 : 1;
\r
316 w =(int)in_buf[xy];
\r
317 w = ((w & 0xf800) >> 8) + ((w & 0x07e0) >> 3) + ((w & 0x001f) << 3);
\r
318 out_buf[xy] = w <= th ? 0 : 1;
\r
332 private int _threshold;
\r
333 private IdoThFilterImpl _do_threshold_impl;
\r
335 public NyARRasterFilter_ARToolkitThreshold(int i_threshold,int i_input_raster_type) throws NyARException
\r
337 this._threshold = i_threshold;
\r
338 switch (i_input_raster_type) {
\r
339 case INyARBufferReader.BUFFERFORMAT_BYTE1D_B8G8R8_24:
\r
340 case INyARBufferReader.BUFFERFORMAT_BYTE1D_R8G8B8_24:
\r
341 this._do_threshold_impl=new doThFilterImpl_BUFFERFORMAT_BYTE1D_RGB_24();
\r
343 case INyARBufferReader.BUFFERFORMAT_BYTE1D_B8G8R8X8_32:
\r
344 this._do_threshold_impl=new doThFilterImpl_BUFFERFORMAT_BYTE1D_B8G8R8X8_32();
\r
346 case INyARBufferReader.BUFFERFORMAT_BYTE1D_X8R8G8B8_32:
\r
347 this._do_threshold_impl=new doThFilterImpl_BUFFERFORMAT_BYTE1D_X8R8G8B8_32();
\r
349 case INyARBufferReader.BUFFERFORMAT_INT1D_X8R8G8B8_32:
\r
350 this._do_threshold_impl=new doThFilterImpl_BUFFERFORMAT_INT1D_X8R8G8B8_32();
\r
352 case INyARBufferReader.BUFFERFORMAT_WORD1D_R5G6B5_16LE:
\r
353 this._do_threshold_impl=new doThFilterImpl_BUFFERFORMAT_WORD1D_R5G6B5_16LE();
\r
356 throw new NyARException();
\r
361 public void setThreshold(int i_threshold)
\r
363 this._threshold = i_threshold;
\r
366 public void doFilter(INyARRgbRaster i_input, NyARBinRaster i_output) throws NyARException
\r
368 INyARBufferReader in_buffer_reader=i_input.getBufferReader();
\r
369 INyARBufferReader out_buffer_reader=i_output.getBufferReader();
\r
371 assert (out_buffer_reader.isEqualBufferType(INyARBufferReader.BUFFERFORMAT_INT1D_BIN_8));
\r
372 assert (i_input.getSize().isEqualSize(i_output.getSize()) == true);
\r
373 this._do_threshold_impl.doThFilter(in_buffer_reader,out_buffer_reader,i_output.getSize(), this._threshold);
\r