%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
tsukuba_bunko.peko.canvas.stage.ImageManager |
|
|
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 | 0 | private static ImageManager _instance = null; |
47 | ||
48 | ||
49 | /** |
|
50 | * <code>ImageManager</code> のインスタンスを作成します。 |
|
51 | */ |
|
52 | protected ImageManager() |
|
53 | { |
|
54 | 0 | super(); |
55 | 0 | } |
56 | ||
57 | ||
58 | /** |
|
59 | * 画像をキャッシュから取得します。キャッシュにない場合は新規にロードされます。 |
|
60 | * @param name 画像名 |
|
61 | * @return 画像データ |
|
62 | */ |
|
63 | public Image getImage( String name ) |
|
64 | { |
|
65 | 0 | return getImage( name, false ); |
66 | } |
|
67 | ||
68 | /** |
|
69 | * 画像をキャッシュから取得します。キャッシュにない場合は新規にロードされます。 |
|
70 | * @param name 画像名 |
|
71 | * @return 画像データ |
|
72 | */ |
|
73 | public Image getImage( String name, boolean transparentize ) |
|
74 | { |
|
75 | 0 | BufferedImage image = null; |
76 | ||
77 | 0 | BufferedImage source = null; |
78 | try { |
|
79 | 0 | Logger.debug( "[canvas.stage] start to load image \"" + name + "\"." ); |
80 | 0 | source = ImageIO.read( getURL(name) ); |
81 | 0 | Logger.debug( "[canvas.stage] finish to load image \"" + name + "\"." ); |
82 | } |
|
83 | 0 | catch( IOException ioe ) { |
84 | 0 | Logger.debug( "[canvas.stage] fail to load image :" + name, ioe ); |
85 | 0 | return null; |
86 | 0 | } |
87 | ||
88 | 0 | if( transparentize ) { |
89 | 0 | int transColor = source.getRGB( 0, 0 ); |
90 | 0 | if( (transColor & 0xFF000000) != 0 ) { |
91 | 0 | image = filterTransparentColor( source, transColor ); |
92 | 0 | source.flush(); |
93 | 0 | } |
94 | else { |
|
95 | 0 | image = source; |
96 | } |
|
97 | 0 | } |
98 | else { |
|
99 | 0 | image = source; |
100 | } |
|
101 | 0 | return image; |
102 | } |
|
103 | ||
104 | /** |
|
105 | * 画像をキャッシュに戻します。 |
|
106 | */ |
|
107 | public void putImage( String name, Image image ) |
|
108 | { |
|
109 | 0 | if( image != null ) { |
110 | 0 | image.flush(); |
111 | } |
|
112 | 0 | } |
113 | ||
114 | /** |
|
115 | * 画像名を URL に変換します。 |
|
116 | * @param name 画像名 |
|
117 | * @return 画像ファイルの URL |
|
118 | */ |
|
119 | protected URL getURL( String name ) |
|
120 | { |
|
121 | 0 | ResourceManager resources = ResourceManager.getInstance(); |
122 | try { |
|
123 | 0 | return new URL( resources.getLocationResources().getImagesDirecotryURL(), name ); |
124 | } |
|
125 | 0 | catch( Exception e ) { |
126 | 0 | Logger.error( "[scene.stage] fatal error!", e ); |
127 | 0 | 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 | 0 | Logger.debug( "[canvas.stage] start transparency filtering." ); |
140 | 0 | int height = source.getHeight(); |
141 | 0 | int width = source.getWidth(); |
142 | 0 | int pixel = 0; |
143 | 0 | BufferedImage image = new BufferedImage( width, height, BufferedImage.TYPE_INT_ARGB ); |
144 | 0 | for( int y = 0; y < height; ++y ) { |
145 | 0 | for( int x = 0;x < width; ++x ) { |
146 | 0 | pixel = source.getRGB( x, y ); |
147 | 0 | if( pixel == transColor ) { |
148 | 0 | image.setRGB( x, y, 0 ); |
149 | 0 | } |
150 | else { |
|
151 | 0 | image.setRGB( x, y , pixel ); |
152 | } |
|
153 | } |
|
154 | } |
|
155 | 0 | Logger.debug( "[canvas.stage] finish transparency filtering." ); |
156 | 0 | return image; |
157 | } |
|
158 | ||
159 | ||
160 | /** |
|
161 | * 唯一の <code>ImageManager</code> インスタンスを取得します。 |
|
162 | * @return <code>ImageManager</code> インスタンス |
|
163 | */ |
|
164 | public static ImageManager getInstance() |
|
165 | { |
|
166 | 0 | if( _instance == null ) { |
167 | 0 | synchronized( ImageManager.class ) { |
168 | 0 | if( _instance == null ) { |
169 | 0 | _instance = new ImageManager(); |
170 | } |
|
171 | 0 | } |
172 | } |
|
173 | 0 | return _instance; |
174 | } |
|
175 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |