OSDN Git Service

106cc588592f32b8331c9ce8811f4e78b9b501a9
[nyartoolkit-and/nyartoolkit-and.git] / trunk / src.utils / java3d / jp / nyatla / nyartoolkit / java3d / utils / J3dNyARRaster_RGB.java
1 /* \r
2  * PROJECT: NyARToolkit Java3D 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.java3d.utils;\r
28 \r
29 import java.awt.image.*;\r
30 \r
31 import javax.media.format.VideoFormat;\r
32 import javax.media.j3d.ImageComponent;\r
33 import javax.media.j3d.ImageComponent2D;\r
34 \r
35 import jp.nyatla.nyartoolkit.NyARException;\r
36 import jp.nyatla.nyartoolkit.core.param.NyARParam;\r
37 import jp.nyatla.nyartoolkit.jmf.utils.*;\r
38 import jp.nyatla.nyartoolkit.core.rasterreader.*;\r
39 /**\r
40  * \r
41  * このクラスは、Java3Dと互換性のあるNyARToolkitのラスタイメージを保持します。\r
42  *\r
43  */\r
44 public class J3dNyARRaster_RGB extends JmfNyARRaster_RGB\r
45 {\r
46         private ImageComponent2D imc2d;\r
47 \r
48         private byte[] i2d_buf;\r
49 \r
50         private BufferedImage bufferd_image;\r
51 \r
52         /**\r
53          * JMFのキャプチャ画像をこのクラスのBufferedImageにコピーします。\r
54          * @param i_buffer\r
55          * 画像の格納されたバッファを指定して下さい。\r
56          * 画像サイズはコンストラクタで与えたパラメタと同じサイズである必要があります。\r
57          */\r
58         public void setBuffer(javax.media.Buffer i_buffer) throws NyARException\r
59         {\r
60                 this._reader.changeBuffer(i_buffer);\r
61                 synchronized (this){\r
62                         //キャプチャデータをi2dのバッファにコピーする。\r
63                         //現在はJmfNyARRaster_RGBでRGB画像がノーマライズされているので、\r
64                         //ここでもう一度flipする。(これ省略したいなあ…。)\r
65                         byte[] src=(byte[])this._reader.getBuffer();\r
66                         final int length = this._size.w * 3;\r
67                         int src_idx = 0;\r
68                         int dest_idx = (this._size.h - 1) * length;                     \r
69                         for (int i = 0; i < this._size.h; i++) {\r
70                                 System.arraycopy(src,src_idx, this.i2d_buf, dest_idx, length);\r
71                                 src_idx += length;\r
72                                 dest_idx -= length;\r
73                         }\r
74                 }\r
75                 return;\r
76         }\r
77 \r
78         public J3dNyARRaster_RGB(NyARParam i_cparam,VideoFormat i_format) throws NyARException\r
79         {\r
80                 super(i_cparam.getScreenSize(),i_format);\r
81                 //bufferdimageの種類を決める\r
82                 if(this._reader.getBufferType()!=INyARBufferReader.BUFFERFORMAT_BYTE1D_B8G8R8_24){\r
83                         throw new NyARException();\r
84                 }\r
85                 //RGBのラスタを作る。\r
86                 this.bufferd_image = new BufferedImage(this._size.w, this._size.h, BufferedImage.TYPE_3BYTE_BGR);\r
87                 i2d_buf = ((DataBufferByte) bufferd_image.getRaster().getDataBuffer()).getData();\r
88                 this.imc2d = new ImageComponent2D(ImageComponent2D.FORMAT_RGB, this.bufferd_image, true, true);\r
89                 imc2d.setCapability(ImageComponent.ALLOW_IMAGE_WRITE);\r
90         }\r
91 \r
92         /**\r
93          * 自身の格納しているImageComponent2Dオブジェクトを作り直します。\r
94          * Java3D1.5がDirectXで動いた(らしいとき)に、ImageComponent2Dのインスタンス\r
95          * IDが異ならないと、Behavior内でイメージの更新を通知できない事象に対応するために実装してあります。\r
96          * Behavior内でgetImageComponent2()関数を実行する直前に呼び出すことで、この事象を回避することができます。\r
97          * \r
98          */\r
99         public void renewImageComponent2D()\r
100         {\r
101                 this.imc2d = new ImageComponent2D(ImageComponent2D.FORMAT_RGB, this.bufferd_image, true, true);\r
102                 this.imc2d.setCapability(ImageComponent.ALLOW_IMAGE_WRITE);\r
103         }\r
104 \r
105         public ImageComponent2D getImageComponent2D()\r
106         {\r
107                 return this.imc2d;\r
108         }\r
109 }\r