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 private int _threshold;
\r
48 public NyARRasterFilter_ARToolkitThreshold(int i_threshold)
\r
50 this._threshold = i_threshold;
\r
52 public void setThreshold(int i_threshold)
\r
54 this._threshold = i_threshold;
\r
57 public void doFilter(INyARRgbRaster i_input, NyARBinRaster i_output) throws NyARException
\r
59 INyARBufferReader in_buffer_reader=i_input.getBufferReader();
\r
60 INyARBufferReader out_buffer_reader=i_output.getBufferReader();
\r
61 int in_buf_type=in_buffer_reader.getBufferType();
\r
63 assert (out_buffer_reader.isEqualBufferType(INyARBufferReader.BUFFERFORMAT_INT1D_BIN_8));
\r
64 assert (checkInputType(in_buf_type)==true);
\r
65 assert (i_input.getSize().isEqualSize(i_output.getSize()) == true);
\r
67 int[] out_buf = (int[]) out_buffer_reader.getBuffer();
\r
68 byte[] in_buf = (byte[]) in_buffer_reader.getBuffer();
\r
70 NyARIntSize size = i_output.getSize();
\r
71 switch (in_buffer_reader.getBufferType()) {
\r
72 case INyARBufferReader.BUFFERFORMAT_BYTE1D_B8G8R8_24:
\r
73 case INyARBufferReader.BUFFERFORMAT_BYTE1D_R8G8B8_24:
\r
74 convert24BitRgb(in_buf, out_buf, size);
\r
76 case INyARBufferReader.BUFFERFORMAT_BYTE1D_B8G8R8X8_32:
\r
77 convert32BitRgbx(in_buf, out_buf, size);
\r
80 throw new NyARException();
\r
85 private void convert24BitRgb(byte[] i_in, int[] i_out, NyARIntSize i_size)
\r
87 final int th=this._threshold*3;
\r
88 int bp =(i_size.w*i_size.h-1)*3;
\r
91 final int pix_count =i_size.h*i_size.w;
\r
92 final int pix_mod_part=pix_count-(pix_count%8);
\r
93 for(xy=pix_count-1;xy>=pix_mod_part;xy--){
\r
94 w= ((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff));
\r
95 i_out[xy]=w<=th?0:1;
\r
100 w= ((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff));
\r
101 i_out[xy]=w<=th?0:1;
\r
104 w= ((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff));
\r
105 i_out[xy]=w<=th?0:1;
\r
108 w= ((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff));
\r
109 i_out[xy]=w<=th?0:1;
\r
112 w= ((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff));
\r
113 i_out[xy]=w<=th?0:1;
\r
116 w= ((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff));
\r
117 i_out[xy]=w<=th?0:1;
\r
120 w= ((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff));
\r
121 i_out[xy]=w<=th?0:1;
\r
124 w= ((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff));
\r
125 i_out[xy]=w<=th?0:1;
\r
128 w= ((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff));
\r
129 i_out[xy]=w<=th?0:1;
\r
135 private void convert32BitRgbx(byte[] i_in, int[] i_out, NyARIntSize i_size)
\r
137 final int th=this._threshold*3;
\r
138 int bp =(i_size.w*i_size.h-1)*4;
\r
141 final int pix_count =i_size.h*i_size.w;
\r
142 final int pix_mod_part=pix_count-(pix_count%8);
\r
143 for(xy=pix_count-1;xy>=pix_mod_part;xy--){
\r
144 w= ((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff));
\r
145 i_out[xy]=w<=th?0:1;
\r
150 w= ((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff));
\r
151 i_out[xy]=w<=th?0:1;
\r
154 w= ((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff));
\r
155 i_out[xy]=w<=th?0:1;
\r
158 w= ((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff));
\r
159 i_out[xy]=w<=th?0:1;
\r
162 w= ((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff));
\r
163 i_out[xy]=w<=th?0:1;
\r
166 w= ((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff));
\r
167 i_out[xy]=w<=th?0:1;
\r
170 w= ((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff));
\r
171 i_out[xy]=w<=th?0:1;
\r
174 w= ((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff));
\r
175 i_out[xy]=w<=th?0:1;
\r
178 w= ((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff));
\r
179 i_out[xy]=w<=th?0:1;
\r
186 private boolean checkInputType(int i_input_type) throws NyARException
\r
188 switch(i_input_type){
\r
189 case INyARBufferReader.BUFFERFORMAT_BYTE1D_B8G8R8_24:
\r
190 case INyARBufferReader.BUFFERFORMAT_BYTE1D_R8G8B8_24:
\r
191 case INyARBufferReader.BUFFERFORMAT_BYTE1D_B8G8R8X8_32:
\r
192 case INyARBufferReader.BUFFERFORMAT_BYTE1D_R5G6B5_16LE:
\r