OSDN Git Service

[TAG]NyARToolkit/2.5.0
[nyartoolkit-and/nyartoolkit-and.git] / tags / 2.5.0 / sample / sandbox / jp / nyatla / nyartoolkit / utils / j2se / NyARRGBRaster_BufferedImage.java
1 /* \r
2  * PROJECT: NyARToolkit(Extension)\r
3  * --------------------------------------------------------------------------------\r
4  * The NyARToolkit is Java edition ARToolKit class library.\r
5  * Copyright (C)2008-2010 Ryo Iizuka\r
6  *\r
7  * This program is free software: you can redistribute it and/or modify\r
8  * it under the terms of the GNU General Public License as published by\r
9  * the Free Software Foundation, either version 3 of the License, or\r
10  * (at your option) any later version.\r
11  * \r
12  * This program is distributed in the hope that it will be useful,\r
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
15  * GNU General Public License for more details.\r
16  *\r
17  * You should have received a copy of the GNU General Public License\r
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
19  * \r
20  * For further information please contact.\r
21  *      http://nyatla.jp/nyatoolkit/\r
22  *      <airmail(at)ebony.plala.or.jp> or <nyatla(at)nyatla.jp>\r
23  * \r
24  */\r
25 package jp.nyatla.nyartoolkit.utils.j2se;\r
26 \r
27 import jp.nyatla.nyartoolkit.NyARException;\r
28 import jp.nyatla.nyartoolkit.core.types.NyARBufferType;\r
29 import jp.nyatla.nyartoolkit.core.types.NyARIntSize;\r
30 import jp.nyatla.nyartoolkit.core.raster.rgb.*;\r
31 import jp.nyatla.nyartoolkit.core.raster.*;\r
32 import jp.nyatla.nyartoolkit.core.rasterreader.*;\r
33 \r
34 import java.awt.color.ColorSpace;\r
35 import java.awt.image.BufferedImage;\r
36 \r
37 \r
38 public class NyARRGBRaster_BufferedImage extends NyARRgbRaster_BasicClass\r
39 {\r
40         private INyARRgbPixelReader _reader;\r
41         private BufferedImage _buf; \r
42         \r
43         /**\r
44          * バッファオブジェクトがアタッチされていればtrue\r
45          */\r
46         protected boolean _is_attached_buffer;\r
47         public NyARRGBRaster_BufferedImage(int i_width, int i_height,boolean i_is_alloc) throws NyARException\r
48         {\r
49                 super( new NyARIntSize(i_width,i_height),NyARBufferType.OBJECT_Java_BufferedImage);\r
50                 if(!initInstance(this._size,i_is_alloc)){\r
51                         throw new NyARException();\r
52                 }\r
53         }\r
54         public NyARRGBRaster_BufferedImage(int i_width, int i_height) throws NyARException\r
55         {\r
56                 super( new NyARIntSize(i_width,i_height),NyARBufferType.OBJECT_Java_BufferedImage);\r
57                 if(!initInstance(this._size,true)){\r
58                         throw new NyARException();\r
59                 }\r
60         }\r
61         protected boolean initInstance(NyARIntSize i_size,boolean i_is_alloc)\r
62         {\r
63                 this._buf=i_is_alloc?new BufferedImage(i_size.w,i_size.h, ColorSpace.TYPE_RGB):null;\r
64                 this._reader=new PixelReader((BufferedImage)this._buf);\r
65                 this._is_attached_buffer=i_is_alloc;\r
66                 return true;\r
67         }       \r
68         \r
69 \r
70         public INyARRgbPixelReader getRgbPixelReader() throws NyARException\r
71         {\r
72                 return this._reader;\r
73         }\r
74         public Object getBuffer()\r
75         {\r
76                 return this._buf;\r
77         }\r
78         public boolean hasBuffer()\r
79         {\r
80                 return this._buf!=null;\r
81         }\r
82         public void wrapBuffer(Object i_ref_buf) throws NyARException\r
83         {\r
84                 assert(!this._is_attached_buffer);//バッファがアタッチされていたら機能しない。\r
85                 BufferedImage buf=(BufferedImage)i_ref_buf;\r
86                 assert(buf.getWidth()==this._size.w && buf.getHeight()==this._size.h);\r
87                 this._buf=buf;\r
88                 //ピクセルリーダーの参照バッファを切り替える。\r
89                 this._reader.switchBuffer(i_ref_buf);\r
90         }\r
91         \r
92         \r
93         /**\r
94          * byte[]配列に、パディング無しの8bit画素値が、RGBRGBの順で並んでいる\r
95          * バッファに使用できるピクセルリーダー\r
96          *\r
97          */\r
98         private class PixelReader implements INyARRgbPixelReader\r
99         {\r
100                 protected BufferedImage _ref_buf;\r
101 \r
102                 public PixelReader(BufferedImage i_buf)\r
103                 {\r
104                         this._ref_buf = i_buf;\r
105                 }\r
106 \r
107                 public void getPixel(int i_x, int i_y, int[] o_rgb)\r
108                 {\r
109                         final BufferedImage ref_buf = this._ref_buf;\r
110                         int p=ref_buf.getRGB(i_x, i_y);\r
111                         o_rgb[0] = (p>>16) & 0xff;// R\r
112                         o_rgb[1] = (p>>8) & 0xff;// G\r
113                         o_rgb[2] = (p>>0) & 0xff;// B\r
114                         return;\r
115                 }\r
116 \r
117                 public void getPixelSet(int[] i_x, int[] i_y, int i_num, int[] o_rgb)\r
118                 {\r
119                         final BufferedImage ref_buf = this._ref_buf;\r
120                         for (int i = i_num - 1; i >= 0; i--) {\r
121                                 int p=ref_buf.getRGB(i_x[i], i_y[i]);\r
122                                 o_rgb[i * 3 + 0] = (p>>16) & 0xff;// R\r
123                                 o_rgb[i * 3 + 1] = (p>>8) & 0xff;// G\r
124                                 o_rgb[i * 3 + 2] = (p>>0) & 0xff;// B\r
125                         }\r
126                         return;\r
127                 }\r
128                 public void setPixel(int i_x, int i_y, int[] i_rgb) throws NyARException\r
129                 {\r
130                         NyARException.notImplement();           \r
131                 }\r
132                 public void setPixels(int[] i_x, int[] i_y, int i_num, int[] i_intrgb) throws NyARException\r
133                 {\r
134                         NyARException.notImplement();           \r
135                 }\r
136                 public void switchBuffer(Object i_ref_buffer) throws NyARException\r
137                 {\r
138                         this._ref_buf = (BufferedImage)i_ref_buffer;\r
139                 }\r
140                 \r
141         }       \r
142         \r
143         \r
144         \r
145         \r
146 }\r