OSDN Git Service

Merge branch 'git-svn'
[nyartoolkit-and/nyartoolkit-and.git] / tags / 2.4.2 / 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.NyARGrayscaleRaster;\r
29 import jp.nyatla.nyartoolkit.core.raster.rgb.INyARRgbRaster;\r
30 import jp.nyatla.nyartoolkit.core.rasterfilter.rgb2gs.INyARRasterFilter_RgbToGs;\r
31 import jp.nyatla.nyartoolkit.core.rasterreader.INyARBufferReader;\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_RgbToGs\r
39 {\r
40         private IdoFilterImpl _dofilterimpl;\r
41         public NyARRasterFilter_Rgb2Gs_YCbCr(int i_raster_type) throws NyARException\r
42         {\r
43                 switch (i_raster_type) {\r
44                 case INyARBufferReader.BUFFERFORMAT_BYTE1D_B8G8R8_24:\r
45                         this._dofilterimpl=new IdoFilterImpl_BYTE1D_B8G8R8_24();\r
46                         break;\r
47                 case INyARBufferReader.BUFFERFORMAT_BYTE1D_R8G8B8_24:\r
48                 default:\r
49                         throw new NyARException();\r
50                 }\r
51         }\r
52         public void doFilter(INyARRgbRaster i_input, NyARGrayscaleRaster i_output) throws NyARException\r
53         {\r
54                 assert (i_input.getSize().isEqualSize(i_output.getSize()) == true);\r
55                 this._dofilterimpl.doFilter(i_input.getBufferReader(),i_output.getBufferReader(),i_input.getSize());\r
56         }\r
57         \r
58         interface IdoFilterImpl\r
59         {\r
60                 public void doFilter(INyARBufferReader i_input, INyARBufferReader i_output,NyARIntSize i_size) throws NyARException;\r
61         }\r
62         class IdoFilterImpl_BYTE1D_B8G8R8_24 implements IdoFilterImpl\r
63         {\r
64                 /**\r
65                  * This function is not optimized.\r
66                  */\r
67                 public void doFilter(INyARBufferReader i_input, INyARBufferReader i_output,NyARIntSize i_size) throws NyARException\r
68                 {\r
69                         assert(         i_input.isEqualBufferType(INyARBufferReader.BUFFERFORMAT_BYTE1D_B8G8R8_24));\r
70                         \r
71                         int[] out_buf = (int[]) i_output.getBuffer();\r
72                         byte[] in_buf = (byte[]) i_input.getBuffer();\r
73 \r
74                         int bp = 0;\r
75                         for (int y = 0; y < i_size.h; y++){\r
76                                 for (int x = 0; x < i_size.w; x++){\r
77                                         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
78                                         bp += 3;\r
79                                 }\r
80                         }\r
81                         return;\r
82                 }\r
83         }       \r
84 }