OSDN Git Service

[NyARToolKit for java]update document
[nyartoolkit-and/nyartoolkit-and.git] / lib / src / jp / nyatla / nyartoolkit / core / rasterfilter / rgb2gs / NyARRasterFilter_Rgb2Gs_YCbCr.java
1 /* \r
2  * PROJECT: NyARToolkit(Extension)\r
3  * --------------------------------------------------------------------------------\r
4  * The NyARToolkit is Java edition ARToolKit class library.\r
5  * Copyright (C)2008-2009 Ryo Iizuka\r
6  *\r
7  * This program is free software: you can redistribute it and/or modify\r
8  * it under the terms of the GNU General Public License as published by\r
9  * the Free Software Foundation, either version 3 of the License, or\r
10  * (at your option) any later version.\r
11  * \r
12  * This program is distributed in the hope that it will be useful,\r
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
15  * GNU General Public License for more details.\r
16  *\r
17  * You should have received a copy of the GNU General Public License\r
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
19  * \r
20  * For further information please contact.\r
21  *      http://nyatla.jp/nyatoolkit/\r
22  *      <airmail(at)ebony.plala.or.jp> or <nyatla(at)nyatla.jp>\r
23  * \r
24  */\r
25 package jp.nyatla.nyartoolkit.core.rasterfilter.rgb2gs;\r
26 \r
27 import jp.nyatla.nyartoolkit.NyARException;\r
28 import jp.nyatla.nyartoolkit.core.raster.*;\r
29 import jp.nyatla.nyartoolkit.core.raster.rgb.INyARRgbRaster;\r
30 import jp.nyatla.nyartoolkit.core.rasterfilter.rgb2gs.INyARRasterFilter_Rgb2Gs;\r
31 import jp.nyatla.nyartoolkit.core.types.NyARBufferType;\r
32 import jp.nyatla.nyartoolkit.core.types.NyARIntSize;\r
33 \r
34 /**\r
35  * このクラスは、RGBラスタをGrayScaleに変換するフィルタを作成します。\r
36  * <p>アルゴリズム\r
37  * このフィルタは、YCbCr変換して、Y成分のグレースケールの値を計算します。(スケールは、255>=n>=0になります。)\r
38  * 変換式は、http://www.tyre.gotdns.org/を参考にしました。\r
39  * </p>\r
40  * <p>入力可能な画素形式\r
41  * 入力可能な画素形式は以下の通りです。\r
42  * <ul>\r
43  * <li>{@link NyARBufferType#BYTE1D_B8G8R8_24}\r
44  * </ul>\r
45  * </p>\r
46  * <p>出力可能な画素形式\r
47  * 出力可能な画素形式は1種類です。\r
48  * <ul>\r
49  * <li>{@link NyARBufferType#INT1D_GRAY_8}\r
50  * </ul>\r
51  * </p>\r
52  */\r
53 public class NyARRasterFilter_Rgb2Gs_YCbCr implements INyARRasterFilter_Rgb2Gs\r
54 {\r
55         private IdoFilterImpl _dofilterimpl;\r
56         /**\r
57          * コンストラクタです。\r
58          * 入力ラスタの画素形式を指定して、フィルタを作成します。\r
59          * 出力ラスタの形式は、{@link NyARBufferType#INT1D_GRAY_8}を選択します。\r
60          * @param i_in_raster_type\r
61          * 入力ラスタの形式です。\r
62          * @throws NyARException\r
63          */     \r
64         public NyARRasterFilter_Rgb2Gs_YCbCr(int i_in_raster_type) throws NyARException\r
65         {\r
66                 if(!initInstance(i_in_raster_type,NyARBufferType.INT1D_GRAY_8))\r
67                 {\r
68                         throw new NyARException();\r
69                 }\r
70         }\r
71         /**\r
72          * コンストラクタです。\r
73          * 入力、出力ラスタの画素形式を指定して、フィルタを作成します。\r
74          * @param i_in_raster_type\r
75          * 入力ラスタの形式です。\r
76          * @param i_out_raster_type\r
77          * 出力ラスタの形式です。\r
78          * @throws NyARException\r
79          */     \r
80         public NyARRasterFilter_Rgb2Gs_YCbCr(int i_in_raster_type,int i_out_raster_type) throws NyARException\r
81         {\r
82                 if(!initInstance(i_in_raster_type,i_out_raster_type))\r
83                 {\r
84                         throw new NyARException();\r
85                 }\r
86         }\r
87         /**\r
88          * この関数は、クラスを初期化します。\r
89          * コンストラクタから呼び出します。\r
90          * @param i_in_raster_type\r
91          * 入力ラスタの画素形式を指定します。\r
92          * @param i_out_raster_type\r
93          * 出力ラスタの画素形式を指定します。\r
94          * @return\r
95          * 初期化に成功すると、trueを返します。\r
96          */             \r
97         protected boolean initInstance(int i_in_raster_type,int i_out_raster_type)\r
98         {\r
99                 switch(i_out_raster_type){\r
100                 case NyARBufferType.INT1D_GRAY_8:\r
101                         switch (i_in_raster_type) {\r
102                         case NyARBufferType.BYTE1D_B8G8R8_24:\r
103                                 this._dofilterimpl=new IdoFilterImpl_BYTE1D_B8G8R8_24();\r
104                                 break;\r
105                         case NyARBufferType.BYTE1D_R8G8B8_24:\r
106                         default:\r
107                                 return false;\r
108                         }\r
109                         break;\r
110                 default:\r
111                         return false;\r
112                 }\r
113                 return true;\r
114         }       \r
115         /**\r
116          * この関数は、入力したRGBラスタを2値ラスタへ変換します。\r
117          */     \r
118         public void doFilter(INyARRgbRaster i_input, NyARGrayscaleRaster i_output) throws NyARException\r
119         {\r
120                 assert (i_input.getSize().isEqualSize(i_output.getSize()) == true);\r
121                 this._dofilterimpl.doFilter(i_input,i_output,i_input.getSize());\r
122         }\r
123         /** 変換関数のインタフェイス*/      \r
124         protected interface IdoFilterImpl\r
125         {\r
126                 public void doFilter(INyARRaster i_input, INyARRaster i_output,NyARIntSize i_size) throws NyARException;\r
127         }\r
128         private class IdoFilterImpl_BYTE1D_B8G8R8_24 implements IdoFilterImpl\r
129         {\r
130                 /**\r
131                  * This function is not optimized.\r
132                  */\r
133                 public void doFilter(INyARRaster i_input, INyARRaster i_output,NyARIntSize i_size) throws NyARException\r
134                 {\r
135                         assert( i_input.isEqualBufferType(NyARBufferType.BYTE1D_B8G8R8_24));\r
136                         assert(i_output.isEqualBufferType(NyARBufferType.INT1D_GRAY_8));\r
137                         \r
138                         int[] out_buf = (int[]) i_output.getBuffer();\r
139                         byte[] in_buf = (byte[]) i_input.getBuffer();\r
140 \r
141                         int bp = 0;\r
142                         for (int y = 0; y < i_size.h; y++){\r
143                                 for (int x = 0; x < i_size.w; x++){\r
144                                         out_buf[y*i_size.w+x]=(306*(in_buf[bp+2] & 0xff)+601*(in_buf[bp + 1] & 0xff)+117 * (in_buf[bp + 0] & 0xff))>>10;\r
145                                         bp += 3;\r
146                                 }\r
147                         }\r
148                         return;\r
149                 }\r
150         }       \r
151 }