OSDN Git Service

2144c7062739841e5279c0a297a87004ca32dcf5
[nyartoolkit-and/nyartoolkit-and.git] / trunk / src / jp / nyatla / utils / j2se / NyARRasterImageIO.java
1 /* \r
2  * PROJECT: NyARToolkit\r
3  * --------------------------------------------------------------------------------\r
4  * This work is based on the original ARToolKit developed by\r
5  *   Hirokazu Kato\r
6  *   Mark Billinghurst\r
7  *   HITLab, University of Washington, Seattle\r
8  * http://www.hitl.washington.edu/artoolkit/\r
9  *\r
10  * The NyARToolkit is Java edition ARToolKit class library.\r
11  * Copyright (C)2008-2009 Ryo Iizuka\r
12  *\r
13  * This program is free software: you can redistribute it and/or modify\r
14  * it under the terms of the GNU General Public License as published by\r
15  * the Free Software Foundation, either version 3 of the License, or\r
16  * (at your option) any later version.\r
17  * \r
18  * This program is distributed in the hope that it will be useful,\r
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
21  * GNU General Public License for more details.\r
22  *\r
23  * You should have received a copy of the GNU General Public License\r
24  * along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
25  * \r
26  * For further information please contact.\r
27  *      http://nyatla.jp/nyatoolkit/\r
28  *      <airmail(at)ebony.plala.or.jp> or <nyatla(at)nyatla.jp>\r
29  * \r
30  */\r
31 package jp.nyatla.utils.j2se;\r
32 \r
33 import java.awt.Graphics.*;\r
34 import java.awt.image.*;\r
35 import java.awt.*;\r
36 import jp.nyatla.nyartoolkit.*;\r
37 import jp.nyatla.nyartoolkit.core.raster.rgb.*;\r
38 import jp.nyatla.nyartoolkit.core.raster.*;\r
39 import jp.nyatla.nyartoolkit.core.rasterreader.*;\r
40 import jp.nyatla.nyartoolkit.core.types.*;\r
41 \r
42 /**\r
43  * BufferdImageとRasterイメージ間で、ビットマップをコピーします。\r
44  */\r
45 public class NyARRasterImageIO\r
46 {\r
47         /**\r
48          * i_inの内容を、このイメージにコピーします。\r
49          * @param i_in\r
50          * @throws NyARException\r
51          */\r
52         public static void copy(INyARRgbRaster i_in,BufferedImage o_out) throws NyARException\r
53         {\r
54                 assert i_in.getSize().isEqualSize(o_out.getWidth(), o_out.getHeight());\r
55                 \r
56                 //thisへ転写\r
57                 INyARRgbPixelReader reader=i_in.getRgbPixelReader();\r
58                 int[] rgb=new int[3];\r
59 \r
60                 for(int y=o_out.getHeight()-1;y>=0;y--){\r
61                         for(int x=o_out.getWidth()-1;x>=0;x--){\r
62                                 reader.getPixel(x,y,rgb);\r
63                                 o_out.setRGB(x,y,(rgb[0]<<16)|(rgb[1]<<8)|rgb[2]);\r
64                         }\r
65                 }\r
66                 return;\r
67         }\r
68         /**\r
69          * GrayScale用\r
70          * @param i_in\r
71          * @throws NyARException\r
72          */\r
73         public static void copy(NyARGrayscaleRaster i_in,BufferedImage o_out) throws NyARException\r
74         {\r
75                 assert i_in.getSize().isEqualSize(o_out.getWidth(), o_out.getHeight());\r
76                 if(i_in.getBufferReader().isEqualBufferType(INyARBufferReader.BUFFERFORMAT_INT1D_GRAY_8))\r
77                 {\r
78                         final int[] buf=(int[])i_in.getBufferReader().getBuffer();\r
79                         final int w=o_out.getWidth();\r
80                         final int h=o_out.getHeight();\r
81                         for(int y=h-1;y>=0;y--){\r
82                                 for(int x=w-1;x>=0;x--){\r
83                                         int v=buf[x+y*w];\r
84                                         o_out.setRGB(x, y,v*(1+0x100+0x10000));\r
85                                 }\r
86                         }\r
87                 }\r
88                 return;\r
89         }       \r
90         /**\r
91          * BIN_8用\r
92          * @param i_in\r
93          * @throws NyARException\r
94          */\r
95         public static void copy(INyARRaster i_in,BufferedImage o_out) throws NyARException\r
96         {\r
97                 assert i_in.getSize().isEqualSize(o_out.getWidth(), o_out.getHeight());\r
98                 if(i_in.getBufferReader().isEqualBufferType(INyARBufferReader.BUFFERFORMAT_INT1D_BIN_8))\r
99                 {\r
100                         final int[] buf=(int[])i_in.getBufferReader().getBuffer();\r
101                         final int w=o_out.getWidth();\r
102                         final int h=o_out.getHeight();\r
103                         for(int y=h-1;y>=0;y--){\r
104                                 for(int x=w-1;x>=0;x--){\r
105                                         o_out.setRGB(x, y,buf[x+y*w]==0?0:0xffffff);\r
106                                 }\r
107                         }\r
108                 }\r
109                 return;\r
110         }       \r
111         /**\r
112          * ヒストグラムを書き出します。\r
113          * @param i_in\r
114          * @param o_out\r
115          * @throws NyARException\r
116          */\r
117         public static void copy(NyARHistgram i_in,Graphics i_g,int i_l,int i_t,int i_h) throws NyARException\r
118         {\r
119                 //正規化のための定数を出す。\r
120                 int max=0;\r
121                 for(int i=0;i<i_in.length;i++){\r
122                         max=max<i_in.data[i]?i_in.data[i]:max;\r
123                 }\r
124                 if(max==0){\r
125                         return;\r
126                 }\r
127                 //ヒストグラムを書く\r
128                 for(int i=0;i<i_in.length;i++){\r
129                         i_g.drawLine(i_l+i,i_t,i_l+i,i_h-i_h*i_in.data[i]/max);\r
130                 }\r
131                 return;\r
132         }       \r
133         /**\r
134          * i_outへこのイメージを出力します。\r
135          * \r
136          * @param i_out\r
137          * @throws NyARException\r
138          */\r
139         public static void copy(BufferedImage i_in,INyARRgbRaster o_out) throws NyARException\r
140         {\r
141                 assert o_out.getSize().isEqualSize(i_in.getWidth(), i_in.getHeight());\r
142                 \r
143                 //thisへ転写\r
144                 INyARRgbPixelReader reader=o_out.getRgbPixelReader();\r
145                 int[] rgb=new int[3];\r
146                 for(int y=i_in.getHeight()-1;y>=0;y--){\r
147                         for(int x=i_in.getWidth()-1;x>=0;x--){\r
148                                 int pix=i_in.getRGB(x, y);\r
149                                 rgb[0]=(pix>>16)&0xff;\r
150                                 rgb[1]=(pix>>8)&0xff;\r
151                                 rgb[2]=(pix)&0xff;\r
152                                 reader.setPixel(x,y,rgb);\r
153                         }\r
154                 }\r
155                 return;\r
156         }\r
157         /**\r
158          * BIN_8用\r
159          * @param i_in\r
160          * @throws NyARException\r
161          */\r
162         public static void copy(BufferedImage i_in,INyARRaster o_out) throws NyARException\r
163         {\r
164                 assert o_out.getSize().isEqualSize(i_in.getWidth(), i_in.getHeight());\r
165                 if(o_out.getBufferReader().isEqualBufferType(INyARBufferReader.BUFFERFORMAT_INT1D_BIN_8))\r
166                 {\r
167                         final int[] buf=(int[])o_out.getBufferReader().getBuffer();\r
168                         final int w=i_in.getWidth();\r
169                         final int h=i_in.getHeight();\r
170                         for(int y=h-1;y>=0;y--){\r
171                                 for(int x=w-1;x>=0;x--){\r
172                                         buf[x+y*w]=(i_in.getRGB(x, y)&0xffffff)>0?1:0;\r
173                                 }\r
174                         }\r
175                 }\r
176                 return;\r
177         }\r
178         \r
179 }\r