OSDN Git Service

0596ebfa73aa0939fd119a6fe422ec948ee29bad
[nyartoolkit-and/nyartoolkit-and.git] / src / jp / nyatla / nyartoolkit / core / match / NyARMatchPattDeviationBlackWhiteData.java
1 /* \r
2  * PROJECT: NyARToolkit\r
3  * --------------------------------------------------------------------------------\r
4  * This work is based on the original ARToolKit developed by\r
5  *   Hirokazu Kato\r
6  *   Mark Billinghurst\r
7  *   HITLab, University of Washington, Seattle\r
8  * http://www.hitl.washington.edu/artoolkit/\r
9  *\r
10  * The NyARToolkit is Java edition ARToolKit class library.\r
11  * Copyright (C)2008-2009 Ryo Iizuka\r
12  *\r
13  * This program is free software; you can redistribute it and/or\r
14  * modify it under the terms of the GNU Lesser General Public License\r
15  * as published by the Free Software Foundation; either version 3\r
16  * of the License, or (at your option) any later version.\r
17  * \r
18  * This program is distributed in the hope that it will be useful,\r
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
21  * GNU Lesser General Public License for more details\r
22  * \r
23  * You should have received a copy of the GNU Lesser General Public\r
24  * License along with this program. If not, see <http://www.gnu.org/licenses/>.\r
25  * \r
26  * For further information please contact.\r
27  *      http://nyatla.jp/nyatoolkit/\r
28  *      <airmail(at)ebony.plala.or.jp> or <nyatla(at)nyatla.jp>\r
29  * \r
30  */\r
31 package jp.nyatla.nyartoolkit.core.match;\r
32 \r
33 \r
34 import jp.nyatla.nyartoolkit.core.raster.*;\r
35 \r
36 \r
37 /**\r
38  * INyARMatchPattのColor差分ラスタを格納するクラスです。\r
39  *\r
40  */\r
41 public class NyARMatchPattDeviationBlackWhiteData\r
42 {\r
43         private int _data[];\r
44         private double _pow;\r
45         //\r
46         private int _number_of_pixels;\r
47         public int[] refData()\r
48         {\r
49                 return this._data;\r
50         }\r
51         public double getPow()\r
52         {\r
53                 return this._pow;\r
54         }\r
55                           \r
56         public NyARMatchPattDeviationBlackWhiteData(int i_width,int i_height)\r
57         {\r
58                 this._number_of_pixels=i_height*i_width;\r
59                 this._data=new int[this._number_of_pixels];\r
60                 return;\r
61         }\r
62         /**\r
63          * XRGB[width*height]の配列から、パターンデータを構築。\r
64          * @param i_buffer\r
65          */\r
66         public void setRaster(INyARRaster i_raster)\r
67         {\r
68                 //i_buffer[XRGB]→差分[BW]変換                     \r
69                 int i;\r
70                 int ave;//<PV/>\r
71                 int rgb;//<PV/>\r
72                 final int[] linput=this._data;//<PV/>\r
73                 final int[] buf=(int[])i_raster.getBufferReader().getBuffer();\r
74 \r
75                 // input配列のサイズとwhも更新// input=new int[height][width][3];\r
76                 final int number_of_pixels=this._number_of_pixels;\r
77 \r
78                 //<平均値計算(FORの1/8展開)/>\r
79                 ave = 0;\r
80                 for(i=number_of_pixels-1;i>=0;i--){\r
81                         rgb = buf[i];\r
82                         ave += ((rgb >> 16) & 0xff) + ((rgb >> 8) & 0xff) + (rgb & 0xff);\r
83                 }\r
84                 ave=(number_of_pixels*255*3-ave)/(3*number_of_pixels);\r
85                 //\r
86                 int sum = 0,w_sum;\r
87                 \r
88                 //<差分値計算/>\r
89                 for (i = number_of_pixels-1; i >= 0;i--) {\r
90                         rgb = buf[i];\r
91                         w_sum =((255*3-(rgb & 0xff)-((rgb >> 8) & 0xff)-((rgb >> 16) & 0xff))/3)-ave;\r
92                         linput[i] = w_sum;\r
93                         sum += w_sum * w_sum;\r
94                 }\r
95                 final double p=Math.sqrt((double) sum);\r
96                 this._pow=p!=0.0?p:0.0000001;\r
97                 return;\r
98         }\r
99 }\r