2 * PROJECT: NyARToolkit
\r
3 * --------------------------------------------------------------------------------
\r
4 * The NyARToolkit is Java version ARToolkit class library.
\r
5 * Copyright (C)2008 R.Iizuka
\r
7 * This program is free software; you can redistribute it and/or
\r
8 * modify it under the terms of the GNU General Public License
\r
9 * as published by the Free Software Foundation; either version 2
\r
10 * of the License, or (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 framework; if not, write to the Free Software
\r
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
21 * For further information please contact.
\r
22 * http://nyatla.jp/nyatoolkit/
\r
23 * <airmail(at)ebony.plala.or.jp>
\r
26 package jp.nyatla.nyartoolkit.core.pickup;
\r
29 import jp.nyatla.nyartoolkit.NyARException;
\r
30 import jp.nyatla.nyartoolkit.core.NyARSquare;
\r
31 import jp.nyatla.nyartoolkit.core.raster.rgb.*;
\r
32 import jp.nyatla.nyartoolkit.core.rasterreader.*;
\r
33 import jp.nyatla.nyartoolkit.core.types.*;
\r
34 import jp.nyatla.nyartoolkit.core.types.matrix.*;
\r
35 import jp.nyatla.nyartoolkit.core.utils.NyARDoubleMatrixProcessor;
\r
39 * 疑似アフィン変換を使用して、ラスタ上の四角形から任意解像度
\r
43 public class NyARColorPatt_PseudoAffine implements INyARColorPatt
\r
45 private int[] _patdata;
\r
46 private NyARBufferReader _buf_reader;
\r
47 private NyARRgbPixelReader_INT1D_X8R8G8B8_32 _pixelreader;
\r
48 private NyARIntSize _size;
\r
50 public final int getWidth()
\r
52 return this._size.w;
\r
55 public final int getHeight()
\r
57 return this._size.h;
\r
60 public final NyARIntSize getSize()
\r
65 public final INyARBufferReader getBufferReader()
\r
67 return this._buf_reader;
\r
70 public final INyARRgbPixelReader getRgbPixelReader()
\r
72 return this._pixelreader;
\r
74 NyARDoubleMatrix44 _invmat=new NyARDoubleMatrix44();
\r
79 public NyARColorPatt_PseudoAffine(int i_width, int i_height)
\r
81 this._size=new NyARIntSize(i_width,i_height);
\r
82 this._patdata = new int[i_height*i_width];
\r
83 this._buf_reader=new NyARBufferReader(this._patdata,NyARBufferReader.BUFFERFORMAT_INT1D_X8R8G8B8_32);
\r
84 this._pixelreader=new NyARRgbPixelReader_INT1D_X8R8G8B8_32(this._patdata,this._size);
\r
85 //疑似アフィン変換のパラメタマトリクスを計算します。
\r
86 //長方形から計算すると、有効要素がm00,m01,m02,m03,m10,m11,m20,m23,m30になります。
\r
87 final NyARDoubleMatrix44 mat=this._invmat;
\r
96 mat.m20=(i_width-1)*(i_height-1);
\r
102 mat.m32=i_height-1;
\r
104 NyARDoubleMatrixProcessor.inverse(mat,mat);
\r
109 * 変換行列と頂点座標から、パラメータを計算
\r
110 * o_paramの[0..3]にはXのパラメタ、[4..7]にはYのパラメタを格納する。
\r
115 private void calcPara(NyARIntPoint2d[] i_vertex,double[] o_cparam)
\r
117 final NyARDoubleMatrix44 invmat=this._invmat;
\r
119 //変換行列とベクトルの積から、変換パラメタを計算する。
\r
124 o_cparam[0]=invmat.m00*v1+invmat.m01*v2+invmat.m02*i_vertex[2].x+invmat.m03*v4;
\r
125 o_cparam[1]=invmat.m10*v1+invmat.m11*v2;//m12,m13は0;
\r
126 o_cparam[2]=invmat.m20*v1+invmat.m23*v4;//m21,m22は0;
\r
127 o_cparam[3]=v1;//m30は1.0で、m31,m32,m33は0
\r
133 o_cparam[4]=invmat.m00*v1+invmat.m01*v2+invmat.m02*i_vertex[2].y+invmat.m03*v4;
\r
134 o_cparam[5]=invmat.m10*v1+invmat.m11*v2;//m12,m13は0;
\r
135 o_cparam[6]=invmat.m20*v1+invmat.m23*v4;//m21,m22は0;
\r
136 o_cparam[7]=v1;//m30は1.0で、m31,m32,m33は0
\r
143 private double[] _convparam=new double[8];
\r
145 public boolean pickFromRaster(INyARRgbRaster image, NyARSquare i_square)throws NyARException
\r
147 final double[] conv_param=this._convparam;
\r
151 int[] rgb_tmp=new int[3];
\r
153 INyARRgbPixelReader reader=image.getRgbPixelReader();
\r
156 //変換行列から現在の座標系への変換パラメタを作成
\r
157 calcPara(i_square.imvertex,conv_param);// 変換パラメータを求める
\r
158 for(int y=0;y<ry2;y++){
\r
159 for(int x=0;x<rx2;x++){
\r
160 final int ttx=(int)((conv_param[0]*x*y+conv_param[1]*x+conv_param[2]*y+conv_param[3])+0.5);
\r
161 final int tty=(int)((conv_param[4]*x*y+conv_param[5]*x+conv_param[6]*y+conv_param[7])+0.5);
\r
162 reader.getPixel((int)ttx,(int)tty,rgb_tmp);
\r
163 this._patdata[x+y*rx2]=(rgb_tmp[0]<<16)|(rgb_tmp[1]<<8)|rgb_tmp[2];
\r