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 javax.media.*;
\r
31 import java.awt.Dimension;
\r
32 import com.sun.media.codec.video.colorspace.*;
\r
34 import jp.nyatla.nyartoolkit.NyARException;
\r
35 import jp.nyatla.nyartoolkit.core.raster.rgb.*;
\r
36 import jp.nyatla.nyartoolkit.core.rasterreader.*;
\r
37 import jp.nyatla.nyartoolkit.core.types.*;
\r
42 * RGB24フォーマットのデータを扱うピクセルリーダ
\r
44 class NyARBufferReader_Reader_RGB24 extends NyARBufferReader_Reader
\r
46 protected boolean _is_flipped;
\r
47 public NyARBufferReader_Reader_RGB24(RGBFormat i_input_format,NyARIntSize i_ref_size) throws NyARException
\r
50 //ピクセルフォーマットを設定(現状は24bitRGBを受けつける。)
\r
51 RGBFormat fm=(RGBFormat)i_input_format;
\r
52 if(fm.getBitsPerPixel()!=24){
\r
53 throw new NyARException();
\r
55 int r=fm.getRedMask();
\r
56 int b=fm.getBlueMask();
\r
58 this._buf_type=BUFFERFORMAT_BYTE1D_R8G8B8_24;
\r
59 }else if(r==3 && b==1){
\r
60 this._buf_type=BUFFERFORMAT_BYTE1D_B8G8R8_24;
\r
62 throw new NyARException();
\r
65 this._is_flipped=i_input_format.getFlipped()!=0?true:false;
\r
66 this._ref_buf=new byte[i_ref_size.w*i_ref_size.h*3];
\r
71 public void changeBuffer(javax.media.Buffer i_buffer)
\r
73 //vertical反転が必要ならば、反転した画像を作成する。
\r
74 byte[] src=(byte[])i_buffer.getData();
\r
75 if(this._is_flipped){
\r
76 final int length = this._ref_size.w * 3;
\r
78 int dest_idx = (this._ref_size.h - 1) * length;
\r
79 for (int i = 0; i < this._ref_size.h; i++) {
\r
80 System.arraycopy(src,src_idx, this._ref_buf, dest_idx, length);
\r
85 System.arraycopy(src,0,this._ref_buf,0,this._ref_buf.length);
\r
86 this._ref_buf=(byte[])i_buffer.getData();
\r
90 public void getPixel(int i_x, int i_y, int[] o_rgb) throws NyARException
\r
92 int bp = (i_x + i_y * this._ref_size.w) * 3;
\r
93 byte[] ref = this._ref_buf;
\r
94 switch(this._buf_type){
\r
95 case BUFFERFORMAT_BYTE1D_R8G8B8_24:
\r
96 o_rgb[0] = (ref[bp + 0] & 0xff);// R
\r
97 o_rgb[1] = (ref[bp + 1] & 0xff);// G
\r
98 o_rgb[2] = (ref[bp + 2] & 0xff);// B
\r
100 case BUFFERFORMAT_BYTE1D_B8G8R8_24:
\r
101 o_rgb[0] = (ref[bp + 2] & 0xff);// B
\r
102 o_rgb[1] = (ref[bp + 1] & 0xff);// G
\r
103 o_rgb[2] = (ref[bp + 0] & 0xff);// R
\r
106 throw new NyARException();
\r
110 public void getPixelSet(int[] i_x, int i_y[], int i_num, int[] o_rgb) throws NyARException
\r
112 int width = this._ref_size.w;
\r
113 byte[] ref = this._ref_buf;
\r
115 switch(this._buf_type){
\r
116 case BUFFERFORMAT_BYTE1D_R8G8B8_24:
\r
117 for (int i = i_num - 1; i >= 0; i--) {
\r
118 bp = (i_x[i] + i_y[i] * width) * 3;
\r
119 o_rgb[i * 3 + 0] = (ref[bp + 0] & 0xff);// R
\r
120 o_rgb[i * 3 + 1] = (ref[bp + 1] & 0xff);// G
\r
121 o_rgb[i * 3 + 2] = (ref[bp + 2] & 0xff);// B
\r
124 case BUFFERFORMAT_BYTE1D_B8G8R8_24:
\r
125 for (int i = i_num - 1; i >= 0; i--) {
\r
126 bp = (i_x[i] + i_y[i] * width) * 3;
\r
127 o_rgb[i * 3 + 0] = (ref[bp + 2] & 0xff);// B
\r
128 o_rgb[i * 3 + 1] = (ref[bp + 1] & 0xff);// G
\r
129 o_rgb[i * 3 + 2] = (ref[bp + 0] & 0xff);// R
\r
133 throw new NyARException();
\r
144 * ソースがYUVフォーマットのデータをBGR24として扱うピクセルリーダ
\r
145 * ソースデータをセットした時に変換します。
\r
146 * (将来YUVをそのまま素通りさせるように書き換えるかも)
\r
148 class NyARBufferReader_Reader_YUV extends NyARBufferReader_Reader
\r
151 private YUVToRGB _yuv2rgb;
\r
152 private javax.media.Buffer _rgb_buf;
\r
154 * フォーマットアレイから、BGR24フォーマットを探す
\r
158 private Format pickRGB24Format(Format[] i_formats)
\r
160 for(int i=0;i<i_formats.length;i++){
\r
161 RGBFormat f=(RGBFormat)i_formats[i];
\r
162 if(f.getBitsPerPixel()!=24){
\r
165 if(f.getRedMask()!=3 ||f.getGreenMask()!=2 ||f.getBlueMask()!=1 || f.getFlipped()!=0)
\r
173 public NyARBufferReader_Reader_YUV(YUVFormat i_input_format,NyARIntSize i_ref_size) throws NyARException
\r
175 super(i_ref_size,BUFFERFORMAT_BYTE1D_B8G8R8_24);
\r
176 this._yuv2rgb=new YUVToRGB();
\r
177 this._rgb_buf=new javax.media.Buffer();
\r
178 this._ref_buf=null;
\r
179 //24bit-BGRフォーマットのものを探す
\r
180 Format output_format=pickRGB24Format(this._yuv2rgb.getSupportedOutputFormats(i_input_format));
\r
181 if(output_format==null){
\r
182 throw new NyARException();
\r
184 this._yuv2rgb.setInputFormat(i_input_format);
\r
185 this._yuv2rgb.setOutputFormat(output_format);
\r
187 this._yuv2rgb.open();
\r
188 }catch(Exception e){
\r
189 throw new NyARException();
\r
193 public void changeBuffer(javax.media.Buffer i_buffer)
\r
196 if(this._yuv2rgb.process(i_buffer, this._rgb_buf)!=YUVToRGB.BUFFER_PROCESSED_OK){
\r
197 System.err.println("YUVToRGB.process error:");
\r
199 this._ref_buf=(byte[])this._rgb_buf.getData();
\r
202 public void getPixel(int i_x, int i_y, int[] o_rgb) throws NyARException
\r
206 final int bp = (i_x + i_y * this._ref_size.w) * 3;
\r
207 final byte[] ref = this._ref_buf;
\r
208 o_rgb[0] = (ref[bp + 2] & 0xff);// R
\r
209 o_rgb[1] = (ref[bp + 1] & 0xff);// G
\r
210 o_rgb[2] = (ref[bp + 0] & 0xff);// B
\r
213 public void getPixelSet(int[] i_x, int i_y[], int i_num, int[] o_rgb) throws NyARException
\r
216 final int width = this._ref_size.w;
\r
217 final byte[] ref = this._ref_buf;
\r
218 for (int i = i_num - 1; i >= 0; i--) {
\r
219 bp = (i_x[i] + i_y[i] * width) * 3;
\r
220 o_rgb[i * 3 + 0] = (ref[bp + 2] & 0xff);// R
\r
221 o_rgb[i * 3 + 1] = (ref[bp + 1] & 0xff);// G
\r
222 o_rgb[i * 3 + 2] = (ref[bp + 0] & 0xff);// B
\r
231 * RGB形式のJMFバッファをラップするNyARRasterです。
\r
232 * JMFから得たラスタデータのピクセル並び順を考慮します。
\r
235 public class JmfNyARRaster_RGB extends NyARRgbRaster_BasicClass
\r
238 protected NyARBufferReader_Reader _reader;
\r
240 * i_formatに一致する画素フォーマットの
\r
243 * @throws NyARException
\r
247 public JmfNyARRaster_RGB(NyARIntSize i_ref_size,VideoFormat i_format) throws NyARException
\r
249 super(new NyARIntSize(i_ref_size));
\r
250 this._reader = createReader(i_format);
\r
252 public JmfNyARRaster_RGB(int i_width,int i_height,VideoFormat i_format) throws NyARException
\r
254 super(new NyARIntSize(i_width,i_height));
\r
255 this._reader = createReader(i_format);
\r
259 * フォーマットを解析して、マッチするリーダオブジェクトを返します。
\r
264 * @throws NyARException
\r
266 private NyARBufferReader_Reader createReader(VideoFormat i_fmt) throws NyARException
\r
269 final Dimension s = i_fmt.getSize();
\r
270 if (!this._size.isEqualSize(s.width, s.height)) {
\r
271 throw new NyARException();
\r
274 if(i_fmt instanceof YUVFormat){
\r
276 return new NyARBufferReader_Reader_YUV((YUVFormat)i_fmt,this._size);
\r
277 }else if(i_fmt instanceof RGBFormat){
\r
279 return new NyARBufferReader_Reader_RGB24((RGBFormat)i_fmt,this._size);
\r
281 throw new NyARException();
\r
286 * javax.media.Bufferを分析して、その分析結果をNyARRasterに適合する形で保持します。
\r
287 * 関数実行後に外部でi_bufferの内容変更した場合には、再度setBuffer関数を呼び出してください。
\r
288 * この関数を実行すると、getRgbPixelReaderで取得したReaderのプロパティが変化することがあります。
\r
290 * RGB形式のデータを格納したjavax.media.Bufferオブジェクトを指定してください。
\r
291 * @return i_bufferをラップしたオブジェクトを返します。
\r
292 * @throws NyARException
\r
294 public void setBuffer(javax.media.Buffer i_buffer) throws NyARException
\r
296 this._reader.changeBuffer(i_buffer);
\r
303 public boolean hasData()
\r
305 return this._reader._ref_buf != null;
\r
307 public INyARRgbPixelReader getRgbPixelReader()
\r
309 return this._reader;
\r
311 public INyARBufferReader getBufferReader()
\r
313 return this._reader;
\r