| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 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 |
|
|
| 39 |
|
|
| 40 |
|
|
| 41 |
|
public class ImageManager { |
| 42 |
|
|
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
0 |
private static ImageManager _instance = null; |
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
|
|
| 51 |
|
|
| 52 |
|
protected ImageManager() |
| 53 |
|
{ |
| 54 |
0 |
super(); |
| 55 |
0 |
} |
| 56 |
|
|
| 57 |
|
|
| 58 |
|
|
| 59 |
|
|
| 60 |
|
|
| 61 |
|
|
| 62 |
|
|
| 63 |
|
public Image getImage( String name ) |
| 64 |
|
{ |
| 65 |
0 |
return getImage( name, false ); |
| 66 |
|
} |
| 67 |
|
|
| 68 |
|
|
| 69 |
|
|
| 70 |
|
|
| 71 |
|
|
| 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 |
|
|
| 116 |
|
|
| 117 |
|
|
| 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 |
|
|
| 134 |
|
|
| 135 |
|
|
| 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 |
|
|
| 162 |
|
|
| 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 |
|
} |