2 * PROJECT: NyARToolkit JMF utilities.
\r
3 * --------------------------------------------------------------------------------
\r
5 * Copyright (c) 2008 nyatla
\r
6 * airmail(at)ebony.plala.or.jp
\r
7 * http://nyatla.jp/nyartoolkit/
\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
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
27 package jp.nyatla.nyartoolkit.jmf.utils;
\r
29 import javax.media.format.*;
\r
30 import java.awt.Dimension;
\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
38 * RGB形式のJMFバッファをラップするNyARRasterです。
\r
39 * JMFから得たラスタデータのピクセル並び順を考慮します。
\r
42 public class JmfNyARRaster_RGB extends NyARRgbRaster_BasicClass
\r
44 protected class Reader implements INyARRgbPixelReader,INyARBufferReader
\r
46 private int _buffer_type = INyARBufferReader.BUFFERFORMAT_NULL_ALLZERO;
\r
47 private byte[] _ref_buf;
\r
48 private NyARIntSize _size;
\r
50 public Reader(NyARIntSize i_size)
\r
52 this._size = i_size;
\r
55 //INyARRgbPixelReader
\r
57 public void getPixel(int i_x, int i_y, int[] o_rgb) throws NyARException
\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
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
73 throw new NyARException();
\r
78 public void getPixelSet(int[] i_x, int i_y[], int i_num, int[] o_rgb) throws NyARException
\r
80 int width = this._size.w;
\r
81 byte[] ref = this._ref_buf;
\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
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
101 throw new NyARException();
\r
106 public void changeBuffer(int i_buffer_type, byte[] i_buffer)
\r
108 if(i_buffer_type==1){
\r
109 System.out.println("aaa");
\r
112 this._buffer_type = i_buffer_type;
\r
113 this._ref_buf = i_buffer;
\r
116 //INyARBufferReader
\r
118 public Object getBuffer()
\r
120 return this._ref_buf;
\r
122 public int getBufferType()
\r
124 return _buffer_type;
\r
126 public boolean isEqualBufferType(int i_type_value)
\r
128 return this._buffer_type==i_type_value;
\r
132 protected byte[] _ref_buf;
\r
133 protected Reader _reader;
\r
135 * RGB形式のJMFバッファをラップするオブジェクトをつくります。 生成直後のオブジェクトはデータを持ちません。
\r
136 * メンバ関数はsetBufferを実行後に使用可能になります。
\r
138 public JmfNyARRaster_RGB(NyARIntSize i_size)
\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
146 public JmfNyARRaster_RGB(int i_width,int i_height)
\r
148 super(new NyARIntSize(i_width,i_height));
\r
149 this._ref_buf = null;
\r
150 this._reader = new Reader(this._size);
\r
154 * フォーマットを解析して、ラスタタイプを返します。
\r
157 * @throws NyARException
\r
159 protected int analyzeBufferType(RGBFormat i_fmt) throws NyARException
\r
162 Dimension s = i_fmt.getSize();
\r
163 if (!this._size.isEqualSize(s.width, s.height)) {
\r
164 throw new NyARException();
\r
167 int r = i_fmt.getRedMask() - 1;
\r
168 int b = i_fmt.getBlueMask() - 1;
\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
176 throw new NyARException("Unknown pixel order.");
\r
181 * javax.media.Bufferを分析して、その分析結果をNyARRasterに適合する形で保持します。
\r
182 * 関数実行後に外部でi_bufferの内容変更した場合には、再度setBuffer関数を呼び出してください。
\r
183 * この関数を実行すると、getRgbPixelReaderで取得したReaderのプロパティが変化することがあります。
\r
185 * RGB形式のデータを格納したjavax.media.Bufferオブジェクトを指定してください。
\r
186 * @return i_bufferをラップしたオブジェクトを返します。
\r
187 * @throws NyARException
\r
189 public void setBuffer(javax.media.Buffer i_buffer) throws NyARException
\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
200 public boolean hasData()
\r
202 return this._ref_buf != null;
\r
205 public INyARRgbPixelReader getRgbPixelReader()
\r
207 return this._reader;
\r
209 public INyARBufferReader getBufferReader()
\r
211 return this._reader;
\r