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
13 * この関数は、ARToolKit互換のアルゴリズムでRGBラスタを2値画像へ変換します。
\r
14 * <p>ARToolKitのアルゴリズム -
\r
15 * ARToolKitでは、敷居値thと、RGB成分 R,G,Bから、次の式で2値画素を求めます。
\r
16 * <pre>A=th*3<(R+G+B)?0:1</pre>
\r
19 * 入力可能な画素形式は以下の通りです。
\r
21 * <li>{@link NyARBufferType#BYTE1D_B8G8R8_24}
\r
22 * <li>{@link NyARBufferType#BYTE1D_R8G8B8_24}
\r
23 * <li>{@link NyARBufferType#BYTE1D_B8G8R8X8_32}
\r
24 * <li>{@link NyARBufferType#BYTE1D_X8R8G8B8_32}
\r
25 * <li>{@link NyARBufferType#INT1D_X8R8G8B8_32}
\r
26 * <li>{@link NyARBufferType#WORD1D_R5G6B5_16LE}
\r
32 * <li>{@link NyARBufferType#INT1D_BIN_8}
\r
36 public class NyARRasterFilter_ARToolkitThreshold implements INyARRasterFilter_Rgb2Bin
\r
39 protected int _threshold;
\r
40 private IdoThFilterImpl _do_threshold_impl;
\r
43 * 固定式位置の初期値、入力ラスタの画素形式を指定して、フィルタを作成します。
\r
44 * 出力ラスタの形式は、{@link NyARBufferType#INT1D_BIN_8}を選択します。
\r
45 * @param i_threshold
\r
46 * 敷居値の初期値です。0<n<256の値を指定します。
\r
47 * @param i_in_raster_type
\r
49 * @throws NyARException
\r
51 public NyARRasterFilter_ARToolkitThreshold(int i_threshold,int i_in_raster_type) throws NyARException
\r
53 if(!initInstance(i_threshold,i_in_raster_type,NyARBufferType.INT1D_BIN_8)){
\r
54 throw new NyARException();
\r
59 * 固定式位置の初期値、入力、出力ラスタの画素形式を指定して、フィルタを作成します。
\r
60 * @param i_threshold
\r
61 * 敷居値の初期値です。0<n<256の値を指定します。
\r
62 * @param i_in_raster_type
\r
64 * @param i_out_raster_type
\r
66 * @throws NyARException
\r
68 public NyARRasterFilter_ARToolkitThreshold(int i_threshold,int i_in_raster_type,int i_out_raster_type) throws NyARException
\r
70 if(!initInstance(i_threshold,i_in_raster_type,i_out_raster_type)){
\r
71 throw new NyARException();
\r
77 * @param i_threshold
\r
78 * 敷居値の初期値です。0以上、256未満の数値を指定します。
\r
79 * @param i_in_raster_type
\r
81 * @param i_out_raster_type
\r
84 * 初期化に成功すると、trueを返します。
\r
86 protected boolean initInstance(int i_threshold,int i_in_raster_type,int i_out_raster_type)
\r
88 switch(i_out_raster_type){
\r
89 case NyARBufferType.INT1D_BIN_8:
\r
90 switch (i_in_raster_type){
\r
91 case NyARBufferType.BYTE1D_B8G8R8_24:
\r
92 case NyARBufferType.BYTE1D_R8G8B8_24:
\r
93 this._do_threshold_impl=new doThFilterImpl_BUFFERFORMAT_BYTE1D_RGB_24();
\r
95 case NyARBufferType.BYTE1D_B8G8R8X8_32:
\r
96 this._do_threshold_impl=new doThFilterImpl_BUFFERFORMAT_BYTE1D_B8G8R8X8_32();
\r
98 case NyARBufferType.BYTE1D_X8R8G8B8_32:
\r
99 this._do_threshold_impl=new doThFilterImpl_BUFFERFORMAT_BYTE1D_X8R8G8B8_32();
\r
101 case NyARBufferType.INT1D_X8R8G8B8_32:
\r
102 this._do_threshold_impl=new doThFilterImpl_BUFFERFORMAT_INT1D_X8R8G8B8_32();
\r
104 case NyARBufferType.WORD1D_R5G6B5_16LE:
\r
105 this._do_threshold_impl=new doThFilterImpl_BUFFERFORMAT_WORD1D_R5G6B5_16LE();
\r
108 return false;//サポートしない組み合わせ
\r
112 return false;//サポートしない組み合わせ
\r
114 this._threshold = i_threshold;
\r
120 * 0以上、256未満の数値を指定してください。
\r
121 * @param i_threshold
\r
124 public void setThreshold(int i_threshold)
\r
126 this._threshold = i_threshold;
\r
129 * この関数は、入力画像を2値化した画像を出力画像へ書込みます。
\r
130 * 入力画像と出力画像のサイズは同じである必要があります。
\r
132 public void doFilter(INyARRgbRaster i_input, NyARBinRaster i_output) throws NyARException
\r
134 assert (i_input.getSize().isEqualSize(i_output.getSize()) == true);
\r
135 NyARIntSize s=i_input.getSize();
\r
136 this._do_threshold_impl.doThFilter(i_input,0,0,s.w,s.h,this._threshold,i_output);
\r
140 * この関数は、入力画像の一部分だけを2値化して、出力画像の該当位置へ書込みます。
\r
141 * 入力画像と出力画像のサイズは同じである必要があります。
\r
145 * 2値化する矩形範囲。入力画像の範囲内である必要があります。
\r
148 * @throws NyARException
\r
150 public void doFilter(INyARRgbRaster i_input,NyARIntRect i_area, NyARBinRaster i_output) throws NyARException
\r
152 assert (i_input.getSize().isEqualSize(i_output.getSize()) == true);
\r
153 this._do_threshold_impl.doThFilter(i_input,i_area.x,i_area.y,i_area.w,i_area.h,this._threshold,i_output);
\r
160 protected interface IdoThFilterImpl
\r
162 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
165 private class doThFilterImpl_BUFFERFORMAT_BYTE1D_RGB_24 implements IdoThFilterImpl
\r
167 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
170 i_raster.isEqualBufferType(NyARBufferType.BYTE1D_B8G8R8_24)||
\r
171 i_raster.isEqualBufferType(NyARBufferType.BYTE1D_R8G8B8_24));
\r
172 final byte[] input=(byte[])i_raster.getBuffer();
\r
173 final int[] output=(int[])o_raster.getBuffer();
\r
175 NyARIntSize s=i_raster.getSize();
\r
176 int skip_dst=(s.w-i_w);
\r
177 int skip_src=skip_dst*3;
\r
178 final int pix_count=i_w;
\r
179 final int pix_mod_part=pix_count-(pix_count%8);
\r
181 int pt_dst=(i_t*s.w+i_l);
\r
182 int pt_src=pt_dst*3;
\r
183 for (int y = i_h-1; y >=0 ; y-=1){
\r
185 for (x = pix_count-1; x >=pix_mod_part; x--){
\r
186 output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;
\r
190 output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;
\r
192 output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;
\r
194 output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;
\r
196 output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;
\r
198 output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;
\r
200 output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;
\r
202 output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;
\r
204 output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;
\r
214 private class doThFilterImpl_BUFFERFORMAT_INT1D_X8R8G8B8_32 implements IdoThFilterImpl
\r
216 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
218 assert (i_raster.isEqualBufferType( NyARBufferType.INT1D_X8R8G8B8_32));
\r
219 final int[] input=(int[])i_raster.getBuffer();
\r
220 final int[] output=(int[])o_raster.getBuffer();
\r
223 NyARIntSize s=i_raster.getSize();
\r
224 int skip_src=(s.w-i_w);
\r
225 int skip_dst=skip_src;
\r
226 final int pix_count=i_w;
\r
227 final int pix_mod_part=pix_count-(pix_count%8);
\r
229 int pt_dst=(i_t*s.w+i_l);
\r
231 for (int y = i_h-1; y >=0 ; y-=1){
\r
233 for (x = pix_count-1; x >=pix_mod_part; x--){
\r
234 v=input[pt_src++];output[pt_dst++]=(((v>>16)& 0xff)+((v>>8)& 0xff)+(v& 0xff))<=th?0:1;
\r
237 v=input[pt_src++];output[pt_dst++]=(((v>>16)& 0xff)+((v>>8)& 0xff)+(v& 0xff))<=th?0:1;
\r
238 v=input[pt_src++];output[pt_dst++]=(((v>>16)& 0xff)+((v>>8)& 0xff)+(v& 0xff))<=th?0:1;
\r
239 v=input[pt_src++];output[pt_dst++]=(((v>>16)& 0xff)+((v>>8)& 0xff)+(v& 0xff))<=th?0:1;
\r
240 v=input[pt_src++];output[pt_dst++]=(((v>>16)& 0xff)+((v>>8)& 0xff)+(v& 0xff))<=th?0:1;
\r
241 v=input[pt_src++];output[pt_dst++]=(((v>>16)& 0xff)+((v>>8)& 0xff)+(v& 0xff))<=th?0:1;
\r
242 v=input[pt_src++];output[pt_dst++]=(((v>>16)& 0xff)+((v>>8)& 0xff)+(v& 0xff))<=th?0:1;
\r
243 v=input[pt_src++];output[pt_dst++]=(((v>>16)& 0xff)+((v>>8)& 0xff)+(v& 0xff))<=th?0:1;
\r
244 v=input[pt_src++];output[pt_dst++]=(((v>>16)& 0xff)+((v>>8)& 0xff)+(v& 0xff))<=th?0:1;
\r
257 private class doThFilterImpl_BUFFERFORMAT_BYTE1D_B8G8R8X8_32 implements IdoThFilterImpl
\r
259 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
261 assert(i_raster.isEqualBufferType(NyARBufferType.BYTE1D_B8G8R8X8_32));
\r
262 final byte[] input=(byte[])i_raster.getBuffer();
\r
263 final int[] output=(int[])o_raster.getBuffer();
\r
264 NyARIntSize s=i_raster.getSize();
\r
266 int skip_dst=(s.w-i_w);
\r
267 int skip_src=skip_dst*4;
\r
268 final int pix_count=i_w;
\r
269 final int pix_mod_part=pix_count-(pix_count%8);
\r
271 int pt_dst=(i_t*s.w+i_l);
\r
272 int pt_src=pt_dst*4;
\r
273 for (int y = i_h-1; y >=0 ; y-=1){
\r
275 for (x = pix_count-1; x >=pix_mod_part; x--){
\r
276 output[pt_dst++]=((input[pt_src+ 0]& 0xff)+(input[pt_src+ 1]& 0xff)+(input[pt_src+ 2]& 0xff))<=th?0:1;
\r
280 output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;
\r
282 output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;
\r
284 output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;
\r
286 output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;
\r
288 output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;
\r
290 output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;
\r
292 output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;
\r
294 output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;
\r
305 private class doThFilterImpl_BUFFERFORMAT_BYTE1D_X8R8G8B8_32 implements IdoThFilterImpl
\r
307 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
309 assert(i_raster.isEqualBufferType(NyARBufferType.BYTE1D_X8R8G8B8_32));
\r
310 final byte[] input=(byte[])i_raster.getBuffer();
\r
311 final int[] output=(int[])o_raster.getBuffer();
\r
313 NyARIntSize s=i_raster.getSize();
\r
314 int skip_dst=(s.w-i_w);
\r
315 int skip_src=skip_dst*4;
\r
316 final int pix_count=i_w;
\r
317 final int pix_mod_part=pix_count-(pix_count%8);
\r
319 int pt_dst=(i_t*s.w+i_l);
\r
320 int pt_src=pt_dst*4;
\r
321 for (int y = i_h-1; y >=0 ; y-=1){
\r
323 for (x = pix_count-1; x >=pix_mod_part; x--){
\r
324 output[pt_dst++]=((input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff)+(input[pt_src+3]& 0xff))<=th?0:1;
\r
328 output[pt_dst++]=((input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff)+(input[pt_src+3]& 0xff))<=th?0:1;
\r
330 output[pt_dst++]=((input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff)+(input[pt_src+3]& 0xff))<=th?0:1;
\r
332 output[pt_dst++]=((input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff)+(input[pt_src+3]& 0xff))<=th?0:1;
\r
334 output[pt_dst++]=((input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff)+(input[pt_src+3]& 0xff))<=th?0:1;
\r
336 output[pt_dst++]=((input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff)+(input[pt_src+3]& 0xff))<=th?0:1;
\r
338 output[pt_dst++]=((input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff)+(input[pt_src+3]& 0xff))<=th?0:1;
\r
340 output[pt_dst++]=((input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff)+(input[pt_src+3]& 0xff))<=th?0:1;
\r
342 output[pt_dst++]=((input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff)+(input[pt_src+3]& 0xff))<=th?0:1;
\r
353 private class doThFilterImpl_BUFFERFORMAT_WORD1D_R5G6B5_16LE implements IdoThFilterImpl
\r
355 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
357 assert(i_raster.isEqualBufferType(NyARBufferType.WORD1D_R5G6B5_16LE));
\r
358 final short[] input=(short[])i_raster.getBuffer();
\r
359 final int[] output=(int[])o_raster.getBuffer();
\r
361 NyARIntSize s=i_raster.getSize();
\r
362 int skip_dst=(s.w-i_w);
\r
363 int skip_src=skip_dst;
\r
364 final int pix_count=i_w;
\r
365 final int pix_mod_part=pix_count-(pix_count%8);
\r
367 int pt_dst=(i_t*s.w+i_l);
\r
369 for (int y = i_h-1; y >=0 ; y-=1){
\r
371 for (x = pix_count-1; x >=pix_mod_part; x--){
\r
372 v =(int)input[pt_src++]; output[pt_dst++]=(((v & 0xf800) >> 8) + ((v & 0x07e0) >> 3) + ((v & 0x001f) << 3))<=th?0:1;
\r
375 v =(int)input[pt_src++]; output[pt_dst++]=(((v & 0xf800) >> 8) + ((v & 0x07e0) >> 3) + ((v & 0x001f) << 3))<=th?0:1;
\r
376 v =(int)input[pt_src++]; output[pt_dst++]=(((v & 0xf800) >> 8) + ((v & 0x07e0) >> 3) + ((v & 0x001f) << 3))<=th?0:1;
\r
377 v =(int)input[pt_src++]; output[pt_dst++]=(((v & 0xf800) >> 8) + ((v & 0x07e0) >> 3) + ((v & 0x001f) << 3))<=th?0:1;
\r
378 v =(int)input[pt_src++]; output[pt_dst++]=(((v & 0xf800) >> 8) + ((v & 0x07e0) >> 3) + ((v & 0x001f) << 3))<=th?0:1;
\r
379 v =(int)input[pt_src++]; output[pt_dst++]=(((v & 0xf800) >> 8) + ((v & 0x07e0) >> 3) + ((v & 0x001f) << 3))<=th?0:1;
\r
380 v =(int)input[pt_src++]; output[pt_dst++]=(((v & 0xf800) >> 8) + ((v & 0x07e0) >> 3) + ((v & 0x001f) << 3))<=th?0:1;
\r
381 v =(int)input[pt_src++]; output[pt_dst++]=(((v & 0xf800) >> 8) + ((v & 0x07e0) >> 3) + ((v & 0x001f) << 3))<=th?0:1;
\r
382 v =(int)input[pt_src++]; output[pt_dst++]=(((v & 0xf800) >> 8) + ((v & 0x07e0) >> 3) + ((v & 0x001f) << 3))<=th?0:1;
\r