OSDN Git Service

Merge branch 'git-svn'
[nyartoolkit-and/nyartoolkit-and.git] / trunk / sample / sandbox / jp / nyatla / nyartoolkit / sandbox / quadx2 / NyARRasterFilter_ARTTh_Quad.java
1 /* \r
2  * PROJECT: NyARToolkit\r
3  * --------------------------------------------------------------------------------\r
4  * This work is based on the original ARToolKit developed by\r
5  *   Hirokazu Kato\r
6  *   Mark Billinghurst\r
7  *   HITLab, University of Washington, Seattle\r
8  * http://www.hitl.washington.edu/artoolkit/\r
9  *\r
10  * The NyARToolkit is Java version ARToolkit class library.\r
11  * Copyright (C)2008 R.Iizuka\r
12  *\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
17  * \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
22  * \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
26  * \r
27  * For further information please contact.\r
28  *      http://nyatla.jp/nyatoolkit/\r
29  *      <airmail(at)ebony.plala.or.jp>\r
30  * \r
31  */\r
32 package jp.nyatla.nyartoolkit.sandbox.quadx2;\r
33 \r
34 import jp.nyatla.nyartoolkit.NyARException;\r
35 import jp.nyatla.nyartoolkit.core.raster.*;\r
36 import jp.nyatla.nyartoolkit.core.raster.rgb.*;\r
37 import jp.nyatla.nyartoolkit.core.types.*;\r
38 import jp.nyatla.nyartoolkit.core.rasterfilter.rgb2bin.*;\r
39 \r
40 /**\r
41  * 1/4のサイズの画像に変換しながら閾値判定する関数\r
42  * \r
43  */\r
44 public class NyARRasterFilter_ARTTh_Quad implements INyARRasterFilter_Rgb2Bin\r
45 {\r
46         private int _threshold;\r
47 \r
48         public NyARRasterFilter_ARTTh_Quad(int i_threshold)\r
49         {\r
50                 this._threshold = i_threshold;\r
51         }\r
52         public void setThreshold(int i_threshold)\r
53         {\r
54                 this._threshold = i_threshold;\r
55         }\r
56 \r
57         public void doFilter(INyARRgbRaster i_input, NyARBinRaster i_output) throws NyARException\r
58         {\r
59                 int in_buf_type=i_input.getBufferType();\r
60 \r
61                 NyARIntSize size = i_output.getSize();\r
62                 assert (i_output.isEqualBufferType(NyARBufferType.INT1D_BIN_8));\r
63                 assert (checkInputType(in_buf_type)==true);     \r
64                 assert (i_input.getSize().isEqualSize(size.w*2,size.h*2) == true);\r
65 \r
66                 int[] out_buf = (int[]) i_output.getBuffer();\r
67                 byte[] in_buf = (byte[]) i_input.getBuffer();\r
68 \r
69                 switch (i_input.getBufferType()) {\r
70                 case NyARBufferType.BYTE1D_B8G8R8_24:\r
71                 case NyARBufferType.BYTE1D_R8G8B8_24:\r
72                         convert24BitRgb(in_buf, out_buf, size);\r
73                         break;\r
74 //              case INyARBufferReader.BUFFERFORMAT_BYTE1D_B8G8R8X8_32:\r
75 //                      convert32BitRgbx(in_buf, out_buf, size);\r
76 //                      break;\r
77                 default:\r
78                         throw new NyARException();\r
79                 }\r
80                 return;\r
81         }\r
82 \r
83         private void convert24BitRgb(byte[] i_in, int[] i_out, NyARIntSize i_size)\r
84         {\r
85                 final int size_w=i_size.w*2;\r
86                 final int x_mod_end= size_w-(size_w%8);\r
87                 final int th=this._threshold*3;\r
88                 int bp =(size_w*i_size.h*2-1)*3;        \r
89                 int w;\r
90                 int x;          \r
91                 for (int y =i_size.h-1; y>=0 ; y--){\r
92                         //端数分\r
93                         final int row_ptr=y*i_size.w;\r
94                         for (x = i_size.w-1;x>=x_mod_end;x--) {\r
95                                 w= ((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff));\r
96                                 i_out[row_ptr+x]=w<=th?0:1;\r
97                                 bp -= 6;\r
98                         }\r
99                         //タイリング               \r
100                         for (;x>=0;x-=8) {\r
101                                 w=((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff));\r
102                                 i_out[row_ptr+x]=w<=th?0:1;\r
103                                 bp -= 6;\r
104                                 w=((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff));\r
105                                 i_out[row_ptr+x-1]=w<=th?0:1;\r
106                                 bp -= 6;\r
107                                 w=((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff));\r
108                                 i_out[row_ptr+x-2]=w<=th?0:1;\r
109                                 bp -= 6;\r
110                                 w=((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff));\r
111                                 i_out[row_ptr+x-3]=w<=th?0:1;\r
112                                 bp -= 6;\r
113                                 w=((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff));\r
114                                 i_out[row_ptr+x-4]=w<=th?0:1;\r
115                                 bp -= 6;\r
116                                 w=((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff));\r
117                                 i_out[row_ptr+x-5]=w<=th?0:1;\r
118                                 bp -= 6;\r
119                                 w=((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff));\r
120                                 i_out[row_ptr+x-6]=w<=th?0:1;\r
121                                 bp -= 6;\r
122                                 w=((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff));\r
123                                 i_out[row_ptr+x-7]=w<=th?0:1;\r
124                                 bp -= 6;\r
125                         }\r
126                         //1行飛ばし\r
127                         bp-=size_w*3;\r
128                 }\r
129                 return;\r
130         }\r
131         private void convert32BitRgbx(byte[] i_in, int[] i_out, NyARIntSize i_size)\r
132         {\r
133                 final int size_w=i_size.w;\r
134                 final int x_mod_end= size_w-(size_w%8);\r
135                 final int th=this._threshold*3;\r
136                 int bp =(size_w*i_size.h-1)*4;\r
137                 int w;\r
138                 int x;\r
139                 for (int y =i_size.h-1; y>=0 ; y--){\r
140                         final int row_ptr=y*i_size.w;\r
141 \r
142                         //端数分\r
143                         for (x = size_w-1;x>=x_mod_end;x--) {\r
144                                 w= ((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff));\r
145                                 i_out[row_ptr+x]=w<=th?0:1;\r
146                                 bp -= 4;\r
147                         }\r
148                         //タイリング\r
149                         for (;x>=0;x-=8) {\r
150                                 w= ((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff));\r
151                                 i_out[row_ptr+x]=w<=th?0:1;\r
152                                 bp -= 4;\r
153                                 w= ((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff));\r
154                                 i_out[row_ptr+x-1]=w<=th?0:1;\r
155                                 bp -= 4;\r
156                                 w= ((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff));\r
157                                 i_out[row_ptr+x-2]=w<=th?0:1;\r
158                                 bp -= 4;\r
159                                 w= ((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff));\r
160                                 i_out[row_ptr+x-3]=w<=th?0:1;\r
161                                 bp -= 4;\r
162                                 w= ((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff));\r
163                                 i_out[row_ptr+x-4]=w<=th?0:1;\r
164                                 bp -= 4;\r
165                                 w= ((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff));\r
166                                 i_out[row_ptr+x-5]=w<=th?0:1;\r
167                                 bp -= 4;\r
168                                 w= ((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff));\r
169                                 i_out[row_ptr+x-6]=w<=th?0:1;\r
170                                 bp -= 4;\r
171                                 w= ((i_in[bp] & 0xff) + (i_in[bp + 1] & 0xff) + (i_in[bp + 2] & 0xff));\r
172                                 i_out[row_ptr+x-7]=w<=th?0:1;\r
173                                 bp -= 4;\r
174                         }       \r
175                 }\r
176                 return;\r
177         }\r
178         \r
179         private boolean checkInputType(int i_input_type) throws NyARException\r
180         {\r
181                 switch(i_input_type){\r
182                 case NyARBufferType.BYTE1D_B8G8R8_24:\r
183                 case NyARBufferType.BYTE1D_R8G8B8_24:\r
184 //              case NyARBufferType.BYTE1D_B8G8R8X8_32:\r
185 //              case NyARBufferType.BYTE1D_R5G6B5_16LE:\r
186                         return true;\r
187                 default:\r
188                         return false;\r
189                 }\r
190         }\r
191 }\r