--- /dev/null
+package jp.nyatla.nyartoolkit.core.rasterfilter.rgb2bin;\r
+\r
+import jp.nyatla.nyartoolkit.NyARException;\r
+import jp.nyatla.nyartoolkit.core.raster.*;\r
+import jp.nyatla.nyartoolkit.core.raster.rgb.INyARRgbRaster;\r
+import jp.nyatla.nyartoolkit.core.types.NyARBufferType;\r
+import jp.nyatla.nyartoolkit.core.types.NyARIntRect;\r
+import jp.nyatla.nyartoolkit.core.types.NyARIntSize;\r
+\r
+\r
+\r
+/**\r
+ * 定数閾値による2値化をする。\r
+ * \r
+ */\r
+public class NyARRasterFilter_ARToolkitThreshold implements INyARRasterFilter_Rgb2Bin\r
+{\r
+ protected int _threshold;\r
+ private IdoThFilterImpl _do_threshold_impl;\r
+\r
+ public NyARRasterFilter_ARToolkitThreshold(int i_threshold,int i_in_raster_type) throws NyARException\r
+ {\r
+ if(!initInstance(i_threshold,i_in_raster_type,NyARBufferType.INT1D_BIN_8)){\r
+ throw new NyARException();\r
+ }\r
+ }\r
+ public NyARRasterFilter_ARToolkitThreshold(int i_threshold,int i_in_raster_type,int i_out_raster_type) throws NyARException\r
+ {\r
+ if(!initInstance(i_threshold,i_in_raster_type,i_out_raster_type)){\r
+ throw new NyARException();\r
+ }\r
+ }\r
+ protected boolean initInstance(int i_threshold,int i_in_raster_type,int i_out_raster_type)\r
+ {\r
+ switch(i_out_raster_type){\r
+ case NyARBufferType.INT1D_BIN_8:\r
+ switch (i_in_raster_type){\r
+ case NyARBufferType.BYTE1D_B8G8R8_24:\r
+ case NyARBufferType.BYTE1D_R8G8B8_24:\r
+ this._do_threshold_impl=new doThFilterImpl_BUFFERFORMAT_BYTE1D_RGB_24();\r
+ break;\r
+ case NyARBufferType.BYTE1D_B8G8R8X8_32:\r
+ this._do_threshold_impl=new doThFilterImpl_BUFFERFORMAT_BYTE1D_B8G8R8X8_32();\r
+ break;\r
+ case NyARBufferType.BYTE1D_X8R8G8B8_32:\r
+ this._do_threshold_impl=new doThFilterImpl_BUFFERFORMAT_BYTE1D_X8R8G8B8_32();\r
+ break;\r
+ case NyARBufferType.INT1D_X8R8G8B8_32:\r
+ this._do_threshold_impl=new doThFilterImpl_BUFFERFORMAT_INT1D_X8R8G8B8_32();\r
+ break;\r
+ case NyARBufferType.WORD1D_R5G6B5_16LE:\r
+ this._do_threshold_impl=new doThFilterImpl_BUFFERFORMAT_WORD1D_R5G6B5_16LE();\r
+ break;\r
+ default:\r
+ return false;//サポートしない組み合わせ\r
+ }\r
+ break;\r
+ default:\r
+ return false;//サポートしない組み合わせ\r
+ }\r
+ this._threshold = i_threshold;\r
+ return true;\r
+ } \r
+ \r
+ /**\r
+ * 画像を2値化するための閾値。暗点<=th<明点となります。\r
+ * @param i_threshold\r
+ */\r
+ public void setThreshold(int i_threshold)\r
+ {\r
+ this._threshold = i_threshold;\r
+ }\r
+\r
+ public void doFilter(INyARRgbRaster i_input, NyARBinRaster i_output) throws NyARException\r
+ {\r
+ assert (i_input.getSize().isEqualSize(i_output.getSize()) == true);\r
+ NyARIntSize s=i_input.getSize();\r
+ this._do_threshold_impl.doThFilter(i_input,0,0,s.w,s.h,this._threshold,i_output);\r
+ return;\r
+ }\r
+ public void doFilter(INyARRgbRaster i_input,NyARIntRect i_area, NyARBinRaster i_output) throws NyARException\r
+ {\r
+ assert (i_input.getSize().isEqualSize(i_output.getSize()) == true);\r
+ this._do_threshold_impl.doThFilter(i_input,i_area.x,i_area.y,i_area.w,i_area.h,this._threshold,i_output);\r
+ return;\r
+ \r
+ }\r
+ \r
+\r
+\r
+ protected interface IdoThFilterImpl\r
+ {\r
+ 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
+ }\r
+ \r
+ class doThFilterImpl_BUFFERFORMAT_BYTE1D_RGB_24 implements IdoThFilterImpl\r
+ {\r
+ 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
+ {\r
+ assert (\r
+ i_raster.isEqualBufferType(NyARBufferType.BYTE1D_B8G8R8_24)||\r
+ i_raster.isEqualBufferType(NyARBufferType.BYTE1D_R8G8B8_24));\r
+ final byte[] input=(byte[])i_raster.getBuffer();\r
+ final int[] output=(int[])o_raster.getBuffer();\r
+ int th=i_th*3;\r
+ NyARIntSize s=i_raster.getSize();\r
+ int skip_dst=(s.w-i_w);\r
+ int skip_src=skip_dst*3;\r
+ final int pix_count=i_w;\r
+ final int pix_mod_part=pix_count-(pix_count%8); \r
+ //左上から1行づつ走査していく\r
+ int pt_dst=(i_t*s.w+i_l);\r
+ int pt_src=pt_dst*3;\r
+ for (int y = i_h-1; y >=0 ; y-=1){\r
+ int x;\r
+ for (x = pix_count-1; x >=pix_mod_part; x--){\r
+ output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;\r
+ pt_src+=3;\r
+ }\r
+ for (;x>=0;x-=8){\r
+ output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;\r
+ pt_src+=3;\r
+ output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;\r
+ pt_src+=3;\r
+ output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;\r
+ pt_src+=3;\r
+ output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;\r
+ pt_src+=3;\r
+ output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;\r
+ pt_src+=3;\r
+ output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;\r
+ pt_src+=3;\r
+ output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;\r
+ pt_src+=3;\r
+ output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;\r
+ pt_src+=3;\r
+ }\r
+ //スキップ\r
+ pt_src+=skip_src;\r
+ pt_dst+=skip_dst;\r
+ }\r
+ return; \r
+ }\r
+ }\r
+ class doThFilterImpl_BUFFERFORMAT_INT1D_X8R8G8B8_32 implements IdoThFilterImpl\r
+ {\r
+ 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
+ {\r
+ assert (i_raster.isEqualBufferType( NyARBufferType.INT1D_X8R8G8B8_32));\r
+ final int[] input=(int[])i_raster.getBuffer();\r
+ final int[] output=(int[])o_raster.getBuffer();\r
+ int th=i_th*3;\r
+\r
+ NyARIntSize s=i_raster.getSize();\r
+ int skip_src=(s.w-i_w);\r
+ int skip_dst=skip_src;\r
+ final int pix_count=i_w;\r
+ final int pix_mod_part=pix_count-(pix_count%8); \r
+ //左上から1行づつ走査していく\r
+ int pt_dst=(i_t*s.w+i_l);\r
+ int pt_src=pt_dst;\r
+ for (int y = i_h-1; y >=0 ; y-=1){\r
+ int x,v;\r
+ for (x = pix_count-1; x >=pix_mod_part; x--){\r
+ v=input[pt_src++];output[pt_dst++]=(((v>>16)& 0xff)+((v>>8)& 0xff)+(v& 0xff))<=th?0:1;\r
+ }\r
+ for (;x>=0;x-=8){\r
+ v=input[pt_src++];output[pt_dst++]=(((v>>16)& 0xff)+((v>>8)& 0xff)+(v& 0xff))<=th?0:1;\r
+ v=input[pt_src++];output[pt_dst++]=(((v>>16)& 0xff)+((v>>8)& 0xff)+(v& 0xff))<=th?0:1;\r
+ v=input[pt_src++];output[pt_dst++]=(((v>>16)& 0xff)+((v>>8)& 0xff)+(v& 0xff))<=th?0:1;\r
+ v=input[pt_src++];output[pt_dst++]=(((v>>16)& 0xff)+((v>>8)& 0xff)+(v& 0xff))<=th?0:1;\r
+ v=input[pt_src++];output[pt_dst++]=(((v>>16)& 0xff)+((v>>8)& 0xff)+(v& 0xff))<=th?0:1;\r
+ v=input[pt_src++];output[pt_dst++]=(((v>>16)& 0xff)+((v>>8)& 0xff)+(v& 0xff))<=th?0:1;\r
+ v=input[pt_src++];output[pt_dst++]=(((v>>16)& 0xff)+((v>>8)& 0xff)+(v& 0xff))<=th?0:1;\r
+ v=input[pt_src++];output[pt_dst++]=(((v>>16)& 0xff)+((v>>8)& 0xff)+(v& 0xff))<=th?0:1;\r
+ }\r
+ //スキップ\r
+ pt_src+=skip_src;\r
+ pt_dst+=skip_dst; \r
+ }\r
+ return; \r
+ } \r
+ }\r
+\r
+ \r
+\r
+\r
+ class doThFilterImpl_BUFFERFORMAT_BYTE1D_B8G8R8X8_32 implements IdoThFilterImpl\r
+ {\r
+ 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
+ {\r
+ assert(i_raster.isEqualBufferType(NyARBufferType.BYTE1D_B8G8R8X8_32));\r
+ final byte[] input=(byte[])i_raster.getBuffer();\r
+ final int[] output=(int[])o_raster.getBuffer();\r
+ NyARIntSize s=i_raster.getSize();\r
+ int th=i_th*3;\r
+ int skip_dst=(s.w-i_w);\r
+ int skip_src=skip_dst*4;\r
+ final int pix_count=i_w;\r
+ final int pix_mod_part=pix_count-(pix_count%8); \r
+ //左上から1行づつ走査していく\r
+ int pt_dst=(i_t*s.w+i_l);\r
+ int pt_src=pt_dst*4;\r
+ for (int y = i_h-1; y >=0 ; y-=1){\r
+ int x;\r
+ for (x = pix_count-1; x >=pix_mod_part; x--){\r
+ output[pt_dst++]=((input[pt_src+ 0]& 0xff)+(input[pt_src+ 1]& 0xff)+(input[pt_src+ 2]& 0xff))<=th?0:1;\r
+ pt_src+=4;\r
+ }\r
+ for (;x>=0;x-=8){\r
+ output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;\r
+ pt_src+=4;\r
+ output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;\r
+ pt_src+=4;\r
+ output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;\r
+ pt_src+=4;\r
+ output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;\r
+ pt_src+=4;\r
+ output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;\r
+ pt_src+=4;\r
+ output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;\r
+ pt_src+=4;\r
+ output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;\r
+ pt_src+=4;\r
+ output[pt_dst++]=((input[pt_src+0]& 0xff)+(input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff))<=th?0:1;\r
+ pt_src+=4;\r
+ }\r
+ //スキップ\r
+ pt_src+=skip_src;\r
+ pt_dst+=skip_dst; \r
+ }\r
+ return; \r
+ }\r
+ }\r
+\r
+ class doThFilterImpl_BUFFERFORMAT_BYTE1D_X8R8G8B8_32 implements IdoThFilterImpl\r
+ {\r
+ 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
+ {\r
+ assert(i_raster.isEqualBufferType(NyARBufferType.BYTE1D_X8R8G8B8_32));\r
+ final byte[] input=(byte[])i_raster.getBuffer();\r
+ final int[] output=(int[])o_raster.getBuffer();\r
+ int th=i_th*3;\r
+ NyARIntSize s=i_raster.getSize();\r
+ int skip_dst=(s.w-i_w);\r
+ int skip_src=skip_dst*4;\r
+ final int pix_count=i_w;\r
+ final int pix_mod_part=pix_count-(pix_count%8); \r
+ //左上から1行づつ走査していく\r
+ int pt_dst=(i_t*s.w+i_l);\r
+ int pt_src=pt_dst*4;\r
+ for (int y = i_h-1; y >=0 ; y-=1){\r
+ int x;\r
+ for (x = pix_count-1; x >=pix_mod_part; x--){\r
+ output[pt_dst++]=((input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff)+(input[pt_src+3]& 0xff))<=th?0:1;\r
+ pt_src+=4;\r
+ }\r
+ for (;x>=0;x-=8){\r
+ output[pt_dst++]=((input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff)+(input[pt_src+3]& 0xff))<=th?0:1;\r
+ pt_src+=4;\r
+ output[pt_dst++]=((input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff)+(input[pt_src+3]& 0xff))<=th?0:1;\r
+ pt_src+=4;\r
+ output[pt_dst++]=((input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff)+(input[pt_src+3]& 0xff))<=th?0:1;\r
+ pt_src+=4;\r
+ output[pt_dst++]=((input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff)+(input[pt_src+3]& 0xff))<=th?0:1;\r
+ pt_src+=4;\r
+ output[pt_dst++]=((input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff)+(input[pt_src+3]& 0xff))<=th?0:1;\r
+ pt_src+=4;\r
+ output[pt_dst++]=((input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff)+(input[pt_src+3]& 0xff))<=th?0:1;\r
+ pt_src+=4;\r
+ output[pt_dst++]=((input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff)+(input[pt_src+3]& 0xff))<=th?0:1;\r
+ pt_src+=4;\r
+ output[pt_dst++]=((input[pt_src+1]& 0xff)+(input[pt_src+2]& 0xff)+(input[pt_src+3]& 0xff))<=th?0:1;\r
+ pt_src+=4;\r
+ }\r
+ //スキップ\r
+ pt_src+=skip_src;\r
+ pt_dst+=skip_dst; \r
+ }\r
+ return; \r
+ }\r
+ }\r
+\r
+ class doThFilterImpl_BUFFERFORMAT_WORD1D_R5G6B5_16LE implements IdoThFilterImpl\r
+ {\r
+ 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
+ {\r
+ assert(i_raster.isEqualBufferType(NyARBufferType.WORD1D_R5G6B5_16LE));\r
+ final short[] input=(short[])i_raster.getBuffer();\r
+ final int[] output=(int[])o_raster.getBuffer();\r
+ int th=i_th*3;\r
+ NyARIntSize s=i_raster.getSize();\r
+ int skip_dst=(s.w-i_w);\r
+ int skip_src=skip_dst;\r
+ final int pix_count=i_w;\r
+ final int pix_mod_part=pix_count-(pix_count%8); \r
+ //左上から1行づつ走査していく\r
+ int pt_dst=(i_t*s.w+i_l);\r
+ int pt_src=pt_dst;\r
+ for (int y = i_h-1; y >=0 ; y-=1){\r
+ int x,v;\r
+ for (x = pix_count-1; x >=pix_mod_part; x--){\r
+ v =(int)input[pt_src++]; output[pt_dst++]=(((v & 0xf800) >> 8) + ((v & 0x07e0) >> 3) + ((v & 0x001f) << 3))<=th?0:1;\r
+ }\r
+ for (;x>=0;x-=8){\r
+ v =(int)input[pt_src++]; output[pt_dst++]=(((v & 0xf800) >> 8) + ((v & 0x07e0) >> 3) + ((v & 0x001f) << 3))<=th?0:1;\r
+ v =(int)input[pt_src++]; output[pt_dst++]=(((v & 0xf800) >> 8) + ((v & 0x07e0) >> 3) + ((v & 0x001f) << 3))<=th?0:1;\r
+ v =(int)input[pt_src++]; output[pt_dst++]=(((v & 0xf800) >> 8) + ((v & 0x07e0) >> 3) + ((v & 0x001f) << 3))<=th?0:1;\r
+ v =(int)input[pt_src++]; output[pt_dst++]=(((v & 0xf800) >> 8) + ((v & 0x07e0) >> 3) + ((v & 0x001f) << 3))<=th?0:1;\r
+ v =(int)input[pt_src++]; output[pt_dst++]=(((v & 0xf800) >> 8) + ((v & 0x07e0) >> 3) + ((v & 0x001f) << 3))<=th?0:1;\r
+ v =(int)input[pt_src++]; output[pt_dst++]=(((v & 0xf800) >> 8) + ((v & 0x07e0) >> 3) + ((v & 0x001f) << 3))<=th?0:1;\r
+ v =(int)input[pt_src++]; output[pt_dst++]=(((v & 0xf800) >> 8) + ((v & 0x07e0) >> 3) + ((v & 0x001f) << 3))<=th?0:1;\r
+ v =(int)input[pt_src++]; output[pt_dst++]=(((v & 0xf800) >> 8) + ((v & 0x07e0) >> 3) + ((v & 0x001f) << 3))<=th?0:1;\r
+ }\r
+ //スキップ\r
+ pt_src+=skip_src;\r
+ pt_dst+=skip_dst;\r
+ }\r
+ return; \r
+ }\r
+ }\r
+ \r
+}\r