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 edition ARToolKit class library.
\r
11 * Copyright (C)2008-2009 Ryo Iizuka
\r
13 * This program is free software; you can redistribute it and/or
\r
14 * modify it under the terms of the GNU Lesser General Public License
\r
15 * as published by the Free Software Foundation; either version 3
\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 Lesser General Public License for more details
\r
23 * You should have received a copy of the GNU Lesser General Public
\r
24 * License along with this program. If not, see <http://www.gnu.org/licenses/>.
\r
26 * For further information please contact.
\r
27 * http://nyatla.jp/nyatoolkit/
\r
28 * <airmail(at)ebony.plala.or.jp> or <nyatla(at)nyatla.jp>
\r
31 package jp.nyatla.nyartoolkit.core.rasterfilter.rgb2bin;
\r
33 import jp.nyatla.nyartoolkit.NyARException;
\r
34 import jp.nyatla.nyartoolkit.core.raster.*;
\r
35 import jp.nyatla.nyartoolkit.core.raster.rgb.INyARRgbRaster;
\r
36 import jp.nyatla.nyartoolkit.core.rasterreader.INyARBufferReader;
\r
37 import jp.nyatla.nyartoolkit.core.types.*;
\r
43 public class NyARRasterFilter_ARToolkitThreshold implements INyARRasterFilter_RgbToBin
\r
45 interface IdoThFilterImpl
\r
47 public void doThFilter(INyARBufferReader i_input, INyARBufferReader i_output,NyARIntSize i_size,int i_threshold);
\r
49 class doThFilterImpl_BUFFERFORMAT_BYTE1D_RGB_24 implements IdoThFilterImpl
\r
51 public void doThFilter(INyARBufferReader i_input, INyARBufferReader i_output,NyARIntSize i_size,int i_threshold)
\r
53 int[] out_buf = (int[]) i_output.getBuffer();
\r
54 byte[] in_buf = (byte[]) i_input.getBuffer();
\r
56 final int th=i_threshold*3;
\r
57 int bp =(i_size.w*i_size.h-1)*3;
\r
60 final int pix_count =i_size.h*i_size.w;
\r
61 final int pix_mod_part=pix_count-(pix_count%8);
\r
62 for(xy=pix_count-1;xy>=pix_mod_part;xy--){
\r
63 w= ((in_buf[bp] & 0xff) + (in_buf[bp + 1] & 0xff) + (in_buf[bp + 2] & 0xff));
\r
64 out_buf[xy]=w<=th?0:1;
\r
69 w= ((in_buf[bp] & 0xff) + (in_buf[bp + 1] & 0xff) + (in_buf[bp + 2] & 0xff));
\r
70 out_buf[xy]=w<=th?0:1;
\r
73 w= ((in_buf[bp] & 0xff) + (in_buf[bp + 1] & 0xff) + (in_buf[bp + 2] & 0xff));
\r
74 out_buf[xy]=w<=th?0:1;
\r
77 w= ((in_buf[bp] & 0xff) + (in_buf[bp + 1] & 0xff) + (in_buf[bp + 2] & 0xff));
\r
78 out_buf[xy]=w<=th?0:1;
\r
81 w= ((in_buf[bp] & 0xff) + (in_buf[bp + 1] & 0xff) + (in_buf[bp + 2] & 0xff));
\r
82 out_buf[xy]=w<=th?0:1;
\r
85 w= ((in_buf[bp] & 0xff) + (in_buf[bp + 1] & 0xff) + (in_buf[bp + 2] & 0xff));
\r
86 out_buf[xy]=w<=th?0:1;
\r
89 w= ((in_buf[bp] & 0xff) + (in_buf[bp + 1] & 0xff) + (in_buf[bp + 2] & 0xff));
\r
90 out_buf[xy]=w<=th?0:1;
\r
93 w= ((in_buf[bp] & 0xff) + (in_buf[bp + 1] & 0xff) + (in_buf[bp + 2] & 0xff));
\r
94 out_buf[xy]=w<=th?0:1;
\r
97 w= ((in_buf[bp] & 0xff) + (in_buf[bp + 1] & 0xff) + (in_buf[bp + 2] & 0xff));
\r
98 out_buf[xy]=w<=th?0:1;
\r
106 class doThFilterImpl_BUFFERFORMAT_BYTE1D_B8G8R8X8_32 implements IdoThFilterImpl
\r
108 public void doThFilter(INyARBufferReader i_input, INyARBufferReader i_output,NyARIntSize i_size,int i_threshold)
\r
110 int[] out_buf = (int[]) i_output.getBuffer();
\r
111 byte[] in_buf = (byte[]) i_input.getBuffer();
\r
113 final int th=i_threshold*3;
\r
114 int bp =(i_size.w*i_size.h-1)*4;
\r
117 final int pix_count =i_size.h*i_size.w;
\r
118 final int pix_mod_part=pix_count-(pix_count%8);
\r
119 for(xy=pix_count-1;xy>=pix_mod_part;xy--){
\r
120 w= ((in_buf[bp] & 0xff) + (in_buf[bp + 1] & 0xff) + (in_buf[bp + 2] & 0xff));
\r
121 out_buf[xy]=w<=th?0:1;
\r
126 w= ((in_buf[bp] & 0xff) + (in_buf[bp + 1] & 0xff) + (in_buf[bp + 2] & 0xff));
\r
127 out_buf[xy]=w<=th?0:1;
\r
130 w= ((in_buf[bp] & 0xff) + (in_buf[bp + 1] & 0xff) + (in_buf[bp + 2] & 0xff));
\r
131 out_buf[xy]=w<=th?0:1;
\r
134 w= ((in_buf[bp] & 0xff) + (in_buf[bp + 1] & 0xff) + (in_buf[bp + 2] & 0xff));
\r
135 out_buf[xy]=w<=th?0:1;
\r
138 w= ((in_buf[bp] & 0xff) + (in_buf[bp + 1] & 0xff) + (in_buf[bp + 2] & 0xff));
\r
139 out_buf[xy]=w<=th?0:1;
\r
142 w= ((in_buf[bp] & 0xff) + (in_buf[bp + 1] & 0xff) + (in_buf[bp + 2] & 0xff));
\r
143 out_buf[xy]=w<=th?0:1;
\r
146 w= ((in_buf[bp] & 0xff) + (in_buf[bp + 1] & 0xff) + (in_buf[bp + 2] & 0xff));
\r
147 out_buf[xy]=w<=th?0:1;
\r
150 w= ((in_buf[bp] & 0xff) + (in_buf[bp + 1] & 0xff) + (in_buf[bp + 2] & 0xff));
\r
151 out_buf[xy]=w<=th?0:1;
\r
154 w= ((in_buf[bp] & 0xff) + (in_buf[bp + 1] & 0xff) + (in_buf[bp + 2] & 0xff));
\r
155 out_buf[xy]=w<=th?0:1;
\r
162 class doThFilterImpl_BUFFERFORMAT_BYTE1D_X8R8G8B8_32 implements IdoThFilterImpl
\r
164 public void doThFilter(INyARBufferReader i_input, INyARBufferReader i_output,NyARIntSize i_size,int i_threshold)
\r
166 int[] out_buf = (int[]) i_output.getBuffer();
\r
167 byte[] in_buf = (byte[]) i_input.getBuffer();
\r
169 final int th=i_threshold*3;
\r
170 int bp =(i_size.w*i_size.h-1)*4;
\r
173 final int pix_count =i_size.h*i_size.w;
\r
174 final int pix_mod_part=pix_count-(pix_count%8);
\r
175 for(xy=pix_count-1;xy>=pix_mod_part;xy--){
\r
176 w= ((in_buf[bp+1] & 0xff) + (in_buf[bp + 2] & 0xff) + (in_buf[bp + 3] & 0xff));
\r
177 out_buf[xy]=w<=th?0:1;
\r
182 w= ((in_buf[bp+1] & 0xff) + (in_buf[bp + 2] & 0xff) + (in_buf[bp + 3] & 0xff));
\r
183 out_buf[xy]=w<=th?0:1;
\r
186 w= ((in_buf[bp+1] & 0xff) + (in_buf[bp + 2] & 0xff) + (in_buf[bp + 3] & 0xff));
\r
187 out_buf[xy]=w<=th?0:1;
\r
190 w= ((in_buf[bp+1] & 0xff) + (in_buf[bp + 2] & 0xff) + (in_buf[bp + 3] & 0xff));
\r
191 out_buf[xy]=w<=th?0:1;
\r
194 w= ((in_buf[bp+1] & 0xff) + (in_buf[bp + 2] & 0xff) + (in_buf[bp + 3] & 0xff));
\r
195 out_buf[xy]=w<=th?0:1;
\r
198 w= ((in_buf[bp+1] & 0xff) + (in_buf[bp + 2] & 0xff) + (in_buf[bp + 3] & 0xff));
\r
199 out_buf[xy]=w<=th?0:1;
\r
202 w= ((in_buf[bp+1] & 0xff) + (in_buf[bp + 2] & 0xff) + (in_buf[bp + 3] & 0xff));
\r
203 out_buf[xy]=w<=th?0:1;
\r
206 w= ((in_buf[bp+1] & 0xff) + (in_buf[bp + 2] & 0xff) + (in_buf[bp + 3] & 0xff));
\r
207 out_buf[xy]=w<=th?0:1;
\r
210 w= ((in_buf[bp+1] & 0xff) + (in_buf[bp + 2] & 0xff) + (in_buf[bp + 3] & 0xff));
\r
211 out_buf[xy]=w<=th?0:1;
\r
220 class doThFilterImpl_BUFFERFORMAT_INT1D_X8R8G8B8_32 implements IdoThFilterImpl
\r
222 public void doThFilter(INyARBufferReader i_input, INyARBufferReader i_output,NyARIntSize i_size,int i_threshold)
\r
224 int[] out_buf = (int[]) i_output.getBuffer();
\r
225 int[] in_buf = (int[]) i_input.getBuffer();
\r
227 final int th=i_threshold*3;
\r
230 final int pix_count =i_size.h*i_size.w;
\r
231 final int pix_mod_part=pix_count-(pix_count%8);
\r
233 for(xy=pix_count-1;xy>=pix_mod_part;xy--){
\r
235 out_buf[xy]=(((w>>16)&0xff)+((w>>8)&0xff)+(w&0xff))<=th?0:1;
\r
240 out_buf[xy]=(((w>>16)&0xff)+((w>>8)&0xff)+(w&0xff))<=th?0:1;
\r
243 out_buf[xy]=(((w>>16)&0xff)+((w>>8)&0xff)+(w&0xff))<=th?0:1;
\r
246 out_buf[xy]=(((w>>16)&0xff)+((w>>8)&0xff)+(w&0xff))<=th?0:1;
\r
249 out_buf[xy]=(((w>>16)&0xff)+((w>>8)&0xff)+(w&0xff))<=th?0:1;
\r
252 out_buf[xy]=(((w>>16)&0xff)+((w>>8)&0xff)+(w&0xff))<=th?0:1;
\r
255 out_buf[xy]=(((w>>16)&0xff)+((w>>8)&0xff)+(w&0xff))<=th?0:1;
\r
258 out_buf[xy]=(((w>>16)&0xff)+((w>>8)&0xff)+(w&0xff))<=th?0:1;
\r
261 out_buf[xy]=(((w>>16)&0xff)+((w>>8)&0xff)+(w&0xff))<=th?0:1;
\r
267 class doThFilterImpl_BUFFERFORMAT_WORD1D_R5G6B5_16LE implements IdoThFilterImpl
\r
269 public void doThFilter(INyARBufferReader i_input, INyARBufferReader i_output,NyARIntSize i_size,int i_threshold)
\r
271 int[] out_buf = (int[]) i_output.getBuffer();
\r
272 short[] in_buf = (short[]) i_input.getBuffer();
\r
274 final int th=i_threshold*3;
\r
277 final int pix_count =i_size.h*i_size.w;
\r
278 final int pix_mod_part=pix_count-(pix_count%8);
\r
280 for(xy=pix_count-1;xy>=pix_mod_part;xy--){
\r
281 w =(int)in_buf[xy];
\r
282 w = ((w & 0xf800) >> 8) + ((w & 0x07e0) >> 3) + ((w & 0x001f) << 3);
\r
283 out_buf[xy] = w <= th ? 0 : 1;
\r
287 w =(int)in_buf[xy];
\r
288 w = ((w & 0xf800) >> 8) + ((w & 0x07e0) >> 3) + ((w & 0x001f) << 3);
\r
289 out_buf[xy] = w <= th ? 0 : 1;
\r
291 w =(int)in_buf[xy];
\r
292 w = ((w & 0xf800) >> 8) + ((w & 0x07e0) >> 3) + ((w & 0x001f) << 3);
\r
293 out_buf[xy] = w <= th ? 0 : 1;
\r
295 w =(int)in_buf[xy];
\r
296 w = ((w & 0xf800) >> 8) + ((w & 0x07e0) >> 3) + ((w & 0x001f) << 3);
\r
297 out_buf[xy] = w <= th ? 0 : 1;
\r
299 w =(int)in_buf[xy];
\r
300 w = ((w & 0xf800) >> 8) + ((w & 0x07e0) >> 3) + ((w & 0x001f) << 3);
\r
301 out_buf[xy] = w <= th ? 0 : 1;
\r
303 w =(int)in_buf[xy];
\r
304 w = ((w & 0xf800) >> 8) + ((w & 0x07e0) >> 3) + ((w & 0x001f) << 3);
\r
305 out_buf[xy] = w <= th ? 0 : 1;
\r
307 w =(int)in_buf[xy];
\r
308 w = ((w & 0xf800) >> 8) + ((w & 0x07e0) >> 3) + ((w & 0x001f) << 3);
\r
309 out_buf[xy] = w <= th ? 0 : 1;
\r
311 w =(int)in_buf[xy];
\r
312 w = ((w & 0xf800) >> 8) + ((w & 0x07e0) >> 3) + ((w & 0x001f) << 3);
\r
313 out_buf[xy] = w <= th ? 0 : 1;
\r
315 w =(int)in_buf[xy];
\r
316 w = ((w & 0xf800) >> 8) + ((w & 0x07e0) >> 3) + ((w & 0x001f) << 3);
\r
317 out_buf[xy] = w <= th ? 0 : 1;
\r
331 private int _threshold;
\r
332 private IdoThFilterImpl _do_threshold_impl;
\r
334 public NyARRasterFilter_ARToolkitThreshold(int i_threshold,int i_input_raster_type) throws NyARException
\r
336 this._threshold = i_threshold;
\r
337 switch (i_input_raster_type) {
\r
338 case INyARBufferReader.BUFFERFORMAT_BYTE1D_B8G8R8_24:
\r
339 case INyARBufferReader.BUFFERFORMAT_BYTE1D_R8G8B8_24:
\r
340 this._do_threshold_impl=new doThFilterImpl_BUFFERFORMAT_BYTE1D_RGB_24();
\r
342 case INyARBufferReader.BUFFERFORMAT_BYTE1D_B8G8R8X8_32:
\r
343 this._do_threshold_impl=new doThFilterImpl_BUFFERFORMAT_BYTE1D_B8G8R8X8_32();
\r
345 case INyARBufferReader.BUFFERFORMAT_BYTE1D_X8R8G8B8_32:
\r
346 this._do_threshold_impl=new doThFilterImpl_BUFFERFORMAT_BYTE1D_X8R8G8B8_32();
\r
348 case INyARBufferReader.BUFFERFORMAT_INT1D_X8R8G8B8_32:
\r
349 this._do_threshold_impl=new doThFilterImpl_BUFFERFORMAT_INT1D_X8R8G8B8_32();
\r
351 case INyARBufferReader.BUFFERFORMAT_WORD1D_R5G6B5_16LE:
\r
352 this._do_threshold_impl=new doThFilterImpl_BUFFERFORMAT_WORD1D_R5G6B5_16LE();
\r
355 throw new NyARException();
\r
360 public void setThreshold(int i_threshold)
\r
362 this._threshold = i_threshold;
\r
365 public void doFilter(INyARRgbRaster i_input, NyARBinRaster i_output) throws NyARException
\r
367 INyARBufferReader in_buffer_reader=i_input.getBufferReader();
\r
368 INyARBufferReader out_buffer_reader=i_output.getBufferReader();
\r
370 assert (out_buffer_reader.isEqualBufferType(INyARBufferReader.BUFFERFORMAT_INT1D_BIN_8));
\r
371 assert (i_input.getSize().isEqualSize(i_output.getSize()) == true);
\r
372 this._do_threshold_impl.doThFilter(in_buffer_reader,out_buffer_reader,i_output.getSize(), this._threshold);
\r