OSDN Git Service

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