OSDN Git Service

ecf4a7057ccbac6158ecb6d1d0a09ae91ce543a3
[nyartoolkit-and/nyartoolkit-and.git] / trunk / src / jp / nyatla / nyartoolkit / core / rasterfilter / gs2bin / NyARRasterFilter_ConstantThrshold.java
1 package jp.nyatla.nyartoolkit.core.rasterfilter.gs2bin;\r
2 \r
3 import jp.nyatla.nyartoolkit.NyARException;\r
4 import jp.nyatla.nyartoolkit.core.raster.NyARBinRaster;\r
5 import jp.nyatla.nyartoolkit.core.raster.NyARGrayscaleRaster;\r
6 import jp.nyatla.nyartoolkit.core.rasterreader.INyARBufferReader;\r
7 import jp.nyatla.nyartoolkit.core.types.NyARIntSize;\r
8 \r
9 public class NyARRasterFilter_ConstantThrshold implements INyARRasterFilter_Gs2Bin\r
10 {\r
11         public int _threshold;\r
12         public NyARRasterFilter_ConstantThrshold(int i_initial_threshold,int i_in_raster_type,int i_out_raster_type) throws NyARException\r
13         {\r
14                 assert(i_in_raster_type==INyARBufferReader.BUFFERFORMAT_INT1D_GRAY_8);\r
15                 assert(i_out_raster_type==INyARBufferReader.BUFFERFORMAT_INT1D_BIN_8);\r
16                 //初期化\r
17                 this._threshold=i_initial_threshold;\r
18                 \r
19         }\r
20         /**\r
21          * 2値化の閾値を設定する。\r
22          * 暗点<=th<明点となります。\r
23          * @throws NyARException\r
24          */\r
25         public NyARRasterFilter_ConstantThrshold() throws NyARException\r
26         {\r
27                 this._threshold=0;\r
28         }\r
29 \r
30         \r
31         public void setThreshold(int i_threshold)\r
32         {\r
33                 this._threshold = i_threshold;\r
34         }\r
35         public void doFilter(NyARGrayscaleRaster i_input, NyARBinRaster i_output) throws NyARException\r
36         {\r
37                 assert(i_input.getBufferReader().getBufferType()==INyARBufferReader.BUFFERFORMAT_INT1D_GRAY_8);\r
38                 assert(i_output.getBufferReader().getBufferType()==INyARBufferReader.BUFFERFORMAT_INT1D_BIN_8);\r
39                 int[] out_buf = (int[]) i_output.getBufferReader().getBuffer();\r
40                 int[] in_buf = (int[]) i_input.getBufferReader().getBuffer();\r
41                 NyARIntSize s=i_input.getSize();\r
42                 \r
43                 final int th=this._threshold;\r
44                 int bp =s.w*s.h-1;\r
45                 final int pix_count   =s.h*s.w;\r
46                 final int pix_mod_part=pix_count-(pix_count%8);\r
47                 for(bp=pix_count-1;bp>=pix_mod_part;bp--){\r
48                         out_buf[bp]=(in_buf[bp] & 0xff)<=th?0:1;\r
49                 }\r
50                 //タイリング\r
51                 for (;bp>=0;) {\r
52                         out_buf[bp]=(in_buf[bp] & 0xff)<=th?0:1;\r
53                         bp--;\r
54                         out_buf[bp]=(in_buf[bp] & 0xff)<=th?0:1;\r
55                         bp--;\r
56                         out_buf[bp]=(in_buf[bp] & 0xff)<=th?0:1;\r
57                         bp--;\r
58                         out_buf[bp]=(in_buf[bp] & 0xff)<=th?0:1;\r
59                         bp--;\r
60                         out_buf[bp]=(in_buf[bp] & 0xff)<=th?0:1;\r
61                         bp--;\r
62                         out_buf[bp]=(in_buf[bp] & 0xff)<=th?0:1;\r
63                         bp--;\r
64                         out_buf[bp]=(in_buf[bp] & 0xff)<=th?0:1;\r
65                         bp--;\r
66                         out_buf[bp]=(in_buf[bp] & 0xff)<=th?0:1;\r
67                         bp--;\r
68                 }\r
69                 return;                 \r
70         }\r
71 }\r