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
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
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
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
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
25 package jp.nyatla.nyartoolkit.core.rasterfilter.rgb2gs;
\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
35 * このクラスは、RGBラスタをGrayScaleに変換するフィルタを作成します。
\r
37 * このフィルタは、YCbCr変換して、Y成分のグレースケールの値を計算します。(スケールは、255>=n>=0になります。)
\r
38 * 変換式は、http://www.tyre.gotdns.org/を参考にしました。
\r
41 * 入力可能な画素形式は以下の通りです。
\r
43 * <li>{@link NyARBufferType#BYTE1D_B8G8R8_24}
\r
49 * <li>{@link NyARBufferType#INT1D_GRAY_8}
\r
53 public class NyARRasterFilter_Rgb2Gs_YCbCr implements INyARRasterFilter_Rgb2Gs
\r
55 private IdoFilterImpl _dofilterimpl;
\r
58 * 入力ラスタの画素形式を指定して、フィルタを作成します。
\r
59 * 出力ラスタの形式は、{@link NyARBufferType#INT1D_GRAY_8}を選択します。
\r
60 * @param i_in_raster_type
\r
62 * @throws NyARException
\r
64 public NyARRasterFilter_Rgb2Gs_YCbCr(int i_in_raster_type) throws NyARException
\r
66 if(!initInstance(i_in_raster_type,NyARBufferType.INT1D_GRAY_8))
\r
68 throw new NyARException();
\r
73 * 入力、出力ラスタの画素形式を指定して、フィルタを作成します。
\r
74 * @param i_in_raster_type
\r
76 * @param i_out_raster_type
\r
78 * @throws NyARException
\r
80 public NyARRasterFilter_Rgb2Gs_YCbCr(int i_in_raster_type,int i_out_raster_type) throws NyARException
\r
82 if(!initInstance(i_in_raster_type,i_out_raster_type))
\r
84 throw new NyARException();
\r
90 * @param i_in_raster_type
\r
92 * @param i_out_raster_type
\r
95 * 初期化に成功すると、trueを返します。
\r
97 protected boolean initInstance(int i_in_raster_type,int i_out_raster_type)
\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
105 case NyARBufferType.BYTE1D_R8G8B8_24:
\r
116 * この関数は、入力したRGBラスタを2値ラスタへ変換します。
\r
118 public void doFilter(INyARRgbRaster i_input, NyARGrayscaleRaster i_output) throws NyARException
\r
120 assert (i_input.getSize().isEqualSize(i_output.getSize()) == true);
\r
121 this._dofilterimpl.doFilter(i_input,i_output,i_input.getSize());
\r
123 /** 変換関数のインタフェイス*/
\r
124 protected interface IdoFilterImpl
\r
126 public void doFilter(INyARRaster i_input, INyARRaster i_output,NyARIntSize i_size) throws NyARException;
\r
128 private class IdoFilterImpl_BYTE1D_B8G8R8_24 implements IdoFilterImpl
\r
131 * This function is not optimized.
\r
133 public void doFilter(INyARRaster i_input, INyARRaster i_output,NyARIntSize i_size) throws NyARException
\r
135 assert( i_input.isEqualBufferType(NyARBufferType.BYTE1D_B8G8R8_24));
\r
136 assert(i_output.isEqualBufferType(NyARBufferType.INT1D_GRAY_8));
\r
138 int[] out_buf = (int[]) i_output.getBuffer();
\r
139 byte[] in_buf = (byte[]) i_input.getBuffer();
\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