View Javadoc

1   /*
2    * All Rights Reserved.
3    * Copyright (C) 1999-2005 Tsukuba Bunko.
4    *
5    * Licensed under the BSD License ("the License"); you may not use
6    * this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    *       http://www.tsukuba-bunko.org/licenses/LICENSE.txt
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   *
17   * $Id: ImageManager.java,v 1.3 2005/08/06 15:42:48 ppoi Exp $
18   */
19  package tsukuba_bunko.peko.canvas.stage;
20  
21  import java.awt.Image;
22  
23  import java.awt.image.BufferedImage;
24  
25  import java.io.IOException;
26  
27  import java.net.URL;
28  
29  import javax.imageio.ImageIO;
30  
31  import tsukuba_bunko.peko.Logger;
32  
33  import tsukuba_bunko.peko.resource.ResourceManager;
34  
35  
36  /***
37   * 画像データを管理する機能を提供します。
38   * @author	$Author: ppoi $
39   * @version	$Revision: 1.3 $
40   */
41  public class ImageManager	{
42  
43  	/***
44  	 * 唯一のインスタンス
45  	 */
46  	private static ImageManager	_instance = null;
47  
48  
49  	/***
50  	 * <code>ImageManager</code> のインスタンスを作成します。
51  	 */
52  	protected ImageManager()
53  	{
54  		super();
55  	}
56  
57  
58  	/***
59  	 * 画像をキャッシュから取得します。キャッシュにない場合は新規にロードされます。
60  	 * @param	name	画像名
61  	 * @return	画像データ
62  	 */
63  	public Image getImage( String name )
64  	{
65  		return getImage( name, false );
66  	}
67  
68  	/***
69  	 * 画像をキャッシュから取得します。キャッシュにない場合は新規にロードされます。
70  	 * @param	name	画像名
71  	 * @return	画像データ
72  	 */
73  	public Image getImage( String name, boolean transparentize )
74  	{
75  		BufferedImage	image = null;
76  
77  		BufferedImage	source = null;
78  		try	{
79  			Logger.debug( "[canvas.stage] start to load image \"" + name + "\"." );
80  			source = ImageIO.read( getURL(name) );
81  			Logger.debug( "[canvas.stage] finish to load image \"" + name + "\"." );
82  		}
83  		catch( IOException ioe )	{
84  			Logger.debug( "[canvas.stage] fail to load image :" + name, ioe );
85  			return null;
86  		}
87  
88  		if( transparentize )	{
89  			int	transColor = source.getRGB( 0, 0 );
90  			if( (transColor & 0xFF000000) != 0 )	{
91  				image = filterTransparentColor( source, transColor );
92  				source.flush();
93  			}
94  			else	{
95  				image = source;
96  			}
97  		}
98  		else	{
99  			image = source;
100 		}
101 		return image;
102 	}
103 
104 	/***
105 	 * 画像をキャッシュに戻します。
106 	 */
107 	public void putImage( String name, Image image )
108 	{
109 		if( image != null )	{
110 			image.flush();
111 		}
112 	}
113 
114 	/***
115 	 * 画像名を URL に変換します。
116 	 * @param	name	画像名
117 	 * @return	画像ファイルの URL
118 	 */
119 	protected URL getURL( String name )
120 	{
121 		ResourceManager	resources = ResourceManager.getInstance();
122 		try	{
123 			return new URL( resources.getLocationResources().getImagesDirecotryURL(), name );
124 		}
125 		catch( Exception e )	{
126 			Logger.error( "[scene.stage] fatal error!", e );
127 			return null;
128 		}
129 	}
130 
131 	/***
132 	 * 指定された色を透明色としてフィルター処理した新しい画像を生成します。
133 	 * @param	source	処理元の画像
134 	 * @param	transColor	透過色
135 	 * @return	処理結果の画像
136 	 */
137 	private BufferedImage filterTransparentColor( BufferedImage source, int transColor )
138 	{
139 		Logger.debug( "[canvas.stage] start transparency filtering." );
140 		int	height = source.getHeight();
141 		int	width = source.getWidth();
142 		int	pixel = 0;
143 		BufferedImage	image = new BufferedImage( width, height, BufferedImage.TYPE_INT_ARGB );
144 		for( int y = 0; y < height; ++y )	{
145 			for( int x = 0;x < width; ++x )	{
146 				pixel = source.getRGB( x, y );
147 				if( pixel == transColor )	{
148 					image.setRGB( x, y, 0 );
149 				}
150 				else	{
151 					image.setRGB( x, y , pixel );
152 				}
153 			}
154 		}
155 		Logger.debug( "[canvas.stage] finish transparency filtering." );
156 		return image;
157 	}
158 
159 
160 	/***
161 	 * 唯一の <code>ImageManager</code> インスタンスを取得します。
162 	 * @return	<code>ImageManager</code> インスタンス
163 	 */
164 	public static ImageManager getInstance()
165 	{
166 		if( _instance == null )	{
167 			synchronized( ImageManager.class )	{
168 				if( _instance == null )	{
169 					_instance = new ImageManager();
170 				}
171 			}
172 		}
173 		return _instance;
174 	}
175 }