1 package jp.nyatla.nyartoolkit.core.rasterfilter.rgb2bin;
\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
16 public class NyARRasterFilter_ARToolkitThreshold implements INyARRasterFilter_Rgb2Bin
\r
18 protected int _threshold;
\r
19 private IdoThFilterImpl _do_threshold_impl;
\r
21 public NyARRasterFilter_ARToolkitThreshold(int i_threshold,int i_in_raster_type) throws NyARException
\r
23 if(!initInstance(i_threshold,i_in_raster_type,NyARBufferType.INT1D_BIN_8)){
\r
24 throw new NyARException();
\r
27 public NyARRasterFilter_ARToolkitThreshold(int i_threshold,int i_in_raster_type,int i_out_raster_type) throws NyARException
\r
29 if(!initInstance(i_threshold,i_in_raster_type,i_out_raster_type)){
\r
30 throw new NyARException();
\r
33 protected boolean initInstance(int i_threshold,int i_in_raster_type,int i_out_raster_type)
\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
42 case NyARBufferType.BYTE1D_B8G8R8X8_32:
\r
43 this._do_threshold_impl=new doThFilterImpl_BUFFERFORMAT_BYTE1D_B8G8R8X8_32();
\r
45 case NyARBufferType.BYTE1D_X8R8G8B8_32:
\r
46 this._do_threshold_impl=new doThFilterImpl_BUFFERFORMAT_BYTE1D_X8R8G8B8_32();
\r
48 case NyARBufferType.INT1D_X8R8G8B8_32:
\r
49 this._do_threshold_impl=new doThFilterImpl_BUFFERFORMAT_INT1D_X8R8G8B8_32();
\r
51 case NyARBufferType.WORD1D_R5G6B5_16LE:
\r
52 this._do_threshold_impl=new doThFilterImpl_BUFFERFORMAT_WORD1D_R5G6B5_16LE();
\r
55 return false;//サポートしない組み合わせ
\r
59 return false;//サポートしない組み合わせ
\r
61 this._threshold = i_threshold;
\r
66 * 画像を2値化するための閾値。暗点<=th<明点となります。
\r
67 * @param i_threshold
\r
69 public void setThreshold(int i_threshold)
\r
71 this._threshold = i_threshold;
\r
74 public void doFilter(INyARRgbRaster i_input, NyARBinRaster i_output) throws NyARException
\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
81 public void doFilter(INyARRgbRaster i_input,NyARIntRect i_area, NyARBinRaster i_output) throws NyARException
\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
91 protected interface IdoThFilterImpl
\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
96 class doThFilterImpl_BUFFERFORMAT_BYTE1D_RGB_24 implements IdoThFilterImpl
\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
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
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
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
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
121 output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;
\r
123 output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;
\r
125 output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;
\r
127 output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;
\r
129 output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;
\r
131 output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;
\r
133 output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;
\r
135 output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;
\r
145 class doThFilterImpl_BUFFERFORMAT_INT1D_X8R8G8B8_32 implements IdoThFilterImpl
\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
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
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
160 int pt_dst=(i_t*s.w+i_l);
\r
162 for (int y = i_h-1; y >=0 ; y-=1){
\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
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
188 class doThFilterImpl_BUFFERFORMAT_BYTE1D_B8G8R8X8_32 implements IdoThFilterImpl
\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
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
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
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
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
211 output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;
\r
213 output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;
\r
215 output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;
\r
217 output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;
\r
219 output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;
\r
221 output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;
\r
223 output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;
\r
225 output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;
\r
236 class doThFilterImpl_BUFFERFORMAT_BYTE1D_X8R8G8B8_32 implements IdoThFilterImpl
\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
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
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
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
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
259 output[pt_dst++]=((input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff)+(input[pt_src+3]& 0xff))<=th?0:1;
\r
261 output[pt_dst++]=((input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff)+(input[pt_src+3]& 0xff))<=th?0:1;
\r
263 output[pt_dst++]=((input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff)+(input[pt_src+3]& 0xff))<=th?0:1;
\r
265 output[pt_dst++]=((input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff)+(input[pt_src+3]& 0xff))<=th?0:1;
\r
267 output[pt_dst++]=((input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff)+(input[pt_src+3]& 0xff))<=th?0:1;
\r
269 output[pt_dst++]=((input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff)+(input[pt_src+3]& 0xff))<=th?0:1;
\r
271 output[pt_dst++]=((input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff)+(input[pt_src+3]& 0xff))<=th?0:1;
\r
273 output[pt_dst++]=((input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff)+(input[pt_src+3]& 0xff))<=th?0:1;
\r
284 class doThFilterImpl_BUFFERFORMAT_WORD1D_R5G6B5_16LE implements IdoThFilterImpl
\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
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
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
298 int pt_dst=(i_t*s.w+i_l);
\r
300 for (int y = i_h-1; y >=0 ; y-=1){
\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
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