OSDN Git Service

[TAG]NyARToolkit-2.0.0
[nyartoolkit-and/nyartoolkit-and.git] / branches / nyatla / src.utils / jmf / jp / nyatla / nyartoolkit / jmf / utils / JmfNyARRaster_RGB.java
1 /* \r
2  * PROJECT: NyARToolkit JMF utilities.\r
3  * --------------------------------------------------------------------------------\r
4  * The MIT License\r
5  * Copyright (c) 2008 nyatla\r
6  * airmail(at)ebony.plala.or.jp\r
7  * http://nyatla.jp/nyartoolkit/\r
8  * \r
9  * Permission is hereby granted, free of charge, to any person obtaining a copy\r
10  * of this software and associated documentation files (the "Software"), to deal\r
11  * in the Software without restriction, including without limitation the rights\r
12  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
13  * copies of the Software, and to permit persons to whom the Software is\r
14  * furnished to do so, subject to the following conditions:\r
15  * The above copyright notice and this permission notice shall be included in\r
16  * all copies or substantial portions of the Software.\r
17  * \r
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r
24  * THE SOFTWARE.\r
25  * \r
26  */\r
27 package jp.nyatla.nyartoolkit.jmf.utils;\r
28 \r
29 import javax.media.format.*;\r
30 import java.awt.Dimension;\r
31 \r
32 import jp.nyatla.nyartoolkit.NyARException;\r
33 import jp.nyatla.nyartoolkit.core.raster.rgb.*;\r
34 import jp.nyatla.nyartoolkit.core.rasterreader.*;\r
35 import jp.nyatla.nyartoolkit.core.types.*;\r
36 /**\r
37  * \r
38  * RGB形式のJMFバッファをラップするNyARRasterです。\r
39  * JMFから得たラスタデータのピクセル並び順を考慮します。\r
40  *\r
41  */\r
42 public class JmfNyARRaster_RGB extends NyARRgbRaster_BasicClass\r
43 {\r
44         protected class Reader implements INyARRgbPixelReader,INyARBufferReader\r
45         {\r
46                 private int _buffer_type = INyARBufferReader.BUFFERFORMAT_NULL_ALLZERO;\r
47                 private byte[] _ref_buf;\r
48                 private NyARIntSize _size;\r
49 \r
50                 public Reader(NyARIntSize i_size)\r
51                 {\r
52                         this._size = i_size;\r
53                 }\r
54                 //\r
55                 //INyARRgbPixelReader\r
56                 //\r
57                 public void getPixel(int i_x, int i_y, int[] o_rgb) throws NyARException\r
58                 {\r
59                         int bp = (i_x + i_y * this._size.w) * 3;\r
60                         byte[] ref = this._ref_buf;\r
61                         switch (this._buffer_type) {\r
62                         case INyARBufferReader.BUFFERFORMAT_BYTE1D_R8G8B8_24:\r
63                                 o_rgb[0] = (ref[bp + 0] & 0xff);// R\r
64                                 o_rgb[1] = (ref[bp + 1] & 0xff);// G\r
65                                 o_rgb[2] = (ref[bp + 2] & 0xff);// B\r
66                                 break;\r
67                         case INyARBufferReader.BUFFERFORMAT_BYTE1D_B8G8R8_24:\r
68                                 o_rgb[0] = (ref[bp + 2] & 0xff);// B\r
69                                 o_rgb[1] = (ref[bp + 1] & 0xff);// G\r
70                                 o_rgb[2] = (ref[bp + 0] & 0xff);// R\r
71                                 break;\r
72                         default:\r
73                                 throw new NyARException();\r
74                         }\r
75                         return;\r
76                 }\r
77 \r
78                 public void getPixelSet(int[] i_x, int i_y[], int i_num, int[] o_rgb) throws NyARException\r
79                 {\r
80                         int width = this._size.w;\r
81                         byte[] ref = this._ref_buf;\r
82                         int bp;\r
83                         switch (this._buffer_type) {\r
84                         case INyARBufferReader.BUFFERFORMAT_BYTE1D_R8G8B8_24:\r
85                                 for (int i = i_num - 1; i >= 0; i--) {\r
86                                         bp = (i_x[i] + i_y[i] * width) * 3;\r
87                                         o_rgb[i * 3 + 0] = (ref[bp + 0] & 0xff);// R\r
88                                         o_rgb[i * 3 + 1] = (ref[bp + 1] & 0xff);// G\r
89                                         o_rgb[i * 3 + 2] = (ref[bp + 2] & 0xff);// B\r
90                                 }\r
91                                 break;\r
92                         case INyARBufferReader.BUFFERFORMAT_BYTE1D_B8G8R8_24:\r
93                                 for (int i = i_num - 1; i >= 0; i--) {\r
94                                         bp = (i_x[i] + i_y[i] * width) * 3;\r
95                                         o_rgb[i * 3 + 0] = (ref[bp + 2] & 0xff);// R\r
96                                         o_rgb[i * 3 + 1] = (ref[bp + 1] & 0xff);// G\r
97                                         o_rgb[i * 3 + 2] = (ref[bp + 0] & 0xff);// B\r
98                                 }\r
99                                 break;\r
100                         default:\r
101                                 throw new NyARException();\r
102                         }\r
103                         return;\r
104                 }\r
105 \r
106                 public void changeBuffer(int i_buffer_type, byte[] i_buffer)\r
107                 {\r
108                         if(i_buffer_type==1){\r
109                                 System.out.println("aaa");\r
110                         }\r
111                         \r
112                         this._buffer_type = i_buffer_type;\r
113                         this._ref_buf = i_buffer;\r
114                 }\r
115                 //\r
116                 //INyARBufferReader\r
117                 //\r
118                 public Object getBuffer()\r
119                 {\r
120                         return this._ref_buf;\r
121                 }\r
122                 public int getBufferType()\r
123                 {\r
124                         return _buffer_type;\r
125                 }\r
126                 public boolean isEqualBufferType(int i_type_value)\r
127                 {\r
128                         return this._buffer_type==i_type_value;\r
129                 }               \r
130         }\r
131 \r
132         protected byte[] _ref_buf;\r
133         protected Reader _reader;\r
134         /**\r
135          * RGB形式のJMFバッファをラップするオブジェクトをつくります。 生成直後のオブジェクトはデータを持ちません。\r
136          * メンバ関数はsetBufferを実行後に使用可能になります。\r
137          */\r
138         public JmfNyARRaster_RGB(NyARIntSize i_size)\r
139         {\r
140                 super(new NyARIntSize(i_size.w,i_size.w));\r
141                 this._size.w = i_size.w;\r
142                 this._size.h = i_size.h;\r
143                 this._ref_buf = null;\r
144                 this._reader = new Reader(this._size);\r
145         }\r
146         public JmfNyARRaster_RGB(int i_width,int i_height)\r
147         {\r
148                 super(new NyARIntSize(i_width,i_height));\r
149                 this._ref_buf = null;\r
150                 this._reader = new Reader(this._size);\r
151         }       \r
152         \r
153         /**\r
154          * フォーマットを解析して、ラスタタイプを返します。\r
155          * \r
156          * @param i_fmt\r
157          * @throws NyARException\r
158          */\r
159         protected int analyzeBufferType(RGBFormat i_fmt) throws NyARException\r
160         {\r
161                 // データサイズの確認\r
162                 Dimension s = i_fmt.getSize();\r
163                 if (!this._size.isEqualSize(s.width, s.height)) {\r
164                         throw new NyARException();\r
165                 }\r
166                 // データ配列の確認\r
167                 int r = i_fmt.getRedMask() - 1;\r
168                 int b = i_fmt.getBlueMask() - 1;\r
169 \r
170                 // 色配列の特定\r
171                 if (r == 0 && b == 2) {\r
172                         return INyARBufferReader.BUFFERFORMAT_BYTE1D_R8G8B8_24;\r
173                 } else if (r == 2 && b == 0) {\r
174                         return INyARBufferReader.BUFFERFORMAT_BYTE1D_B8G8R8_24;\r
175                 } else {\r
176                         throw new NyARException("Unknown pixel order.");\r
177                 }\r
178         }\r
179 \r
180         /**\r
181          * javax.media.Bufferを分析して、その分析結果をNyARRasterに適合する形で保持します。\r
182          * 関数実行後に外部でi_bufferの内容変更した場合には、再度setBuffer関数を呼び出してください。\r
183          * この関数を実行すると、getRgbPixelReaderで取得したReaderのプロパティが変化することがあります。\r
184          * @param i_buffer\r
185          * RGB形式のデータを格納したjavax.media.Bufferオブジェクトを指定してください。\r
186          * @return i_bufferをラップしたオブジェクトを返します。\r
187          * @throws NyARException\r
188          */\r
189         public void setBuffer(javax.media.Buffer i_buffer) throws NyARException\r
190         {\r
191                 int buftype= analyzeBufferType((RGBFormat) i_buffer.getFormat());\r
192                 this._ref_buf = (byte[]) i_buffer.getData();\r
193                 this._reader.changeBuffer(buftype, this._ref_buf);\r
194         }\r
195 \r
196         /**\r
197          * データを持っているかを返します。\r
198          * @return\r
199          */\r
200         public boolean hasData()\r
201         {\r
202                 return this._ref_buf != null;\r
203         }\r
204 \r
205         public INyARRgbPixelReader getRgbPixelReader()\r
206         {\r
207                 return this._reader;\r
208         }\r
209         public INyARBufferReader getBufferReader()\r
210         {\r
211                 return this._reader;\r
212         }\r
213 }\r