OSDN Git Service

[backup]NyARToolkit
[nyartoolkit-and/nyartoolkit-and.git] / trunk / 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.NyARIntSize;\r
32 \r
33 /**\r
34  * YCbCr変換して、Y成分のグレースケールの値を計算します。\r
35  * 変換式は、http://www.tyre.gotdns.org/を参考にしました。\r
36  */\r
37 public class NyARRasterFilter_Rgb2Gs_YCbCr implements INyARRasterFilter_Rgb2Gs\r
38 {\r
39         private IdoFilterImpl _dofilterimpl;\r
40         public NyARRasterFilter_Rgb2Gs_YCbCr(int i_in_raster_type) throws NyARException\r
41         {\r
42                 if(!initInstance(i_in_raster_type,NyARBufferType.INT1D_GRAY_8))\r
43                 {\r
44                         throw new NyARException();\r
45                 }\r
46         }\r
47         public NyARRasterFilter_Rgb2Gs_YCbCr(int i_in_raster_type,int i_out_raster) throws NyARException\r
48         {\r
49                 if(!initInstance(i_in_raster_type,i_out_raster))\r
50                 {\r
51                         throw new NyARException();\r
52                 }\r
53         }       \r
54         protected boolean initInstance(int i_in_raster_type,int i_out_raster_type)\r
55         {\r
56                 switch(i_out_raster_type){\r
57                 case NyARBufferType.INT1D_GRAY_8:\r
58                         switch (i_in_raster_type) {\r
59                         case NyARBufferType.BYTE1D_B8G8R8_24:\r
60                                 this._dofilterimpl=new IdoFilterImpl_BYTE1D_B8G8R8_24();\r
61                                 break;\r
62                         case NyARBufferType.BYTE1D_R8G8B8_24:\r
63                         default:\r
64                                 return false;\r
65                         }\r
66                         break;\r
67                 default:\r
68                         return false;\r
69                 }\r
70                 return true;\r
71         }       \r
72         \r
73         public void doFilter(INyARRgbRaster i_input, NyARGrayscaleRaster i_output) throws NyARException\r
74         {\r
75                 assert (i_input.getSize().isEqualSize(i_output.getSize()) == true);\r
76                 this._dofilterimpl.doFilter(i_input,i_output,i_input.getSize());\r
77         }\r
78         \r
79         interface IdoFilterImpl\r
80         {\r
81                 public void doFilter(INyARRaster i_input, INyARRaster i_output,NyARIntSize i_size) throws NyARException;\r
82         }\r
83         class IdoFilterImpl_BYTE1D_B8G8R8_24 implements IdoFilterImpl\r
84         {\r
85                 /**\r
86                  * This function is not optimized.\r
87                  */\r
88                 public void doFilter(INyARRaster i_input, INyARRaster i_output,NyARIntSize i_size) throws NyARException\r
89                 {\r
90                         assert( i_input.isEqualBufferType(NyARBufferType.BYTE1D_B8G8R8_24));\r
91                         assert(i_output.isEqualBufferType(NyARBufferType.INT1D_GRAY_8));\r
92                         \r
93                         int[] out_buf = (int[]) i_output.getBuffer();\r
94                         byte[] in_buf = (byte[]) i_input.getBuffer();\r
95 \r
96                         int bp = 0;\r
97                         for (int y = 0; y < i_size.h; y++){\r
98                                 for (int x = 0; x < i_size.w; x++){\r
99                                         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
100                                         bp += 3;\r
101                                 }\r
102                         }\r
103                         return;\r
104                 }\r
105         }       \r
106 }