No longer insist that we ask the AWT to create optimal images. Instead an

interface is provided that does that in a pluggable manner.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@3720 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2005-10-05 01:39:45 +00:00
parent 9c2fccb8eb
commit 1631093451
4 changed files with 109 additions and 34 deletions
@@ -0,0 +1,70 @@
//
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2005 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/narya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.threerings.media.image;
import java.awt.Component;
import java.awt.GraphicsConfiguration;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
/**
* If the user of the image manager services intends to create and display
* images using the AWT, they can use this image creator which will use the
* AWT to determine the optimal image format.
*/
public class AWTImageCreator
implements ImageManager.OptimalImageCreator
{
/**
* Create an image creator that will rely on the AWT to determine the
* optimal image format.
*
* @param context if non-null, the graphics configuration will be obtained
* therefrom; otherwise {@link GraphicsDevice#getDefaultConfiguration}
* will be used.
*/
public AWTImageCreator (Component context)
{
// obtain our graphics configuration
if (context != null) {
_gc = context.getGraphicsConfiguration();
} else {
_gc = ImageUtil.getDefGC();
}
}
// documentation inherited from interface ImageManager.OptimalImageCreator
public BufferedImage createImage (int width, int height, int trans)
{
// DEBUG: override transparency for the moment on all images
trans = Transparency.TRANSLUCENT;
if (_gc != null) {
return _gc.createCompatibleImage(width, height, trans);
} else {
// if we're running in headless mode, do everything in 24-bit
return new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
}
}
/** The graphics configuration for the default screen device. */
protected GraphicsConfiguration _gc;
}
@@ -23,7 +23,6 @@ package com.threerings.media.image;
import java.awt.Component; import java.awt.Component;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.Rectangle; import java.awt.Rectangle;
import java.awt.Transparency; import java.awt.Transparency;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
@@ -96,16 +95,26 @@ public class ImageManager
} }
/** /**
* Construct an image manager with the specified {@link * This interface allows the image manager to create images that are in a
* ResourceManager} from which it will obtain its data. A non-null * format optimal for rendering to the screen.
* <code>context</code> must be provided if there is any expectation
* that the image manager will not be able to load images via the
* ImageIO services and will have to fallback to Toolkit-style
* loading.
*/ */
public ImageManager (ResourceManager rmgr, Component context) public interface OptimalImageCreator
{
/**
* Requests that a blank image be created that is in a format and of a
* depth that are optimal for rendering to the screen.
*/
public BufferedImage createImage (int width, int height, int trans);
}
/**
* Construct an image manager with the specified {@link ResourceManager}
* from which it will obtain its data.
*/
public ImageManager (ResourceManager rmgr, OptimalImageCreator icreator)
{ {
_rmgr = rmgr; _rmgr = rmgr;
_icreator = icreator;
// create our image cache // create our image cache
int icsize = _cacheSize.getValue(); int icsize = _cacheSize.getValue();
@@ -116,13 +125,15 @@ public class ImageManager
} }
}); });
_ccache.setTracking(true); _ccache.setTracking(true);
}
// obtain our graphics configuration /**
if (context != null) { * A convenience constructor that creates an {@link AWTImageCreator} for
_gc = context.getGraphicsConfiguration(); * use by the image manager.
} else { */
_gc = ImageUtil.getDefGC(); public ImageManager (ResourceManager rmgr, Component context)
} {
this(rmgr, new AWTImageCreator(context));
} }
/** /**
@@ -131,15 +142,7 @@ public class ImageManager
*/ */
public BufferedImage createImage (int width, int height, int transparency) public BufferedImage createImage (int width, int height, int transparency)
{ {
// DEBUG: override transparency for the moment on all images return _icreator.createImage(width, height, transparency);
transparency = Transparency.TRANSLUCENT;
if (_gc != null) {
return _gc.createCompatibleImage(width, height, transparency);
} else {
// if we're running in headless mode, do everything in 24-bit
return new BufferedImage(
width, height, BufferedImage.TYPE_INT_ARGB);
}
} }
/** /**
@@ -224,8 +227,8 @@ public class ImageManager
* they (some day) will automatically use volatile images to increase * they (some day) will automatically use volatile images to increase
* performance. * performance.
*/ */
public BufferedImage getPreparedImage (String rset, String path, public BufferedImage getPreparedImage (
Colorization[] zations) String rset, String path, Colorization[] zations)
{ {
BufferedImage image = getImage(rset, path, zations); BufferedImage image = getImage(rset, path, zations);
BufferedImage prepped = null; BufferedImage prepped = null;
@@ -462,12 +465,12 @@ public class ImageManager
} }
/** /**
* Returns the graphics configuration that should be used to optimize * Returns the image creator that can be used to create buffered images
* images for display. * optimized for rendering to the screen.
*/ */
public GraphicsConfiguration getGraphicsConfiguration () public OptimalImageCreator getImageCreator ()
{ {
return _gc; return _icreator;
} }
/** /**
@@ -564,6 +567,9 @@ public class ImageManager
* by default. */ * by default. */
protected ResourceManager _rmgr; protected ResourceManager _rmgr;
/** We use this to create images optimized for rendering. */
protected OptimalImageCreator _icreator;
/** A cache of loaded images. */ /** A cache of loaded images. */
protected LRUHashMap _ccache; protected LRUHashMap _ccache;
@@ -573,9 +579,6 @@ public class ImageManager
/** Throttle our cache status logging to once every 300 seconds. */ /** Throttle our cache status logging to once every 300 seconds. */
protected Throttle _cacheStatThrottle = new Throttle(1, 300000L); protected Throttle _cacheStatThrottle = new Throttle(1, 300000L);
/** The graphics configuration for the default screen device. */
protected GraphicsConfiguration _gc;
/** Our default data provider. */ /** Our default data provider. */
protected ImageDataProvider _defaultProvider = new ImageDataProvider() { protected ImageDataProvider _defaultProvider = new ImageDataProvider() {
public BufferedImage loadImage (String path) throws IOException { public BufferedImage loadImage (String path) throws IOException {
@@ -21,6 +21,7 @@
package com.threerings.cast.bundle; package com.threerings.cast.bundle;
import java.awt.Component;
import java.util.Iterator; import java.util.Iterator;
import com.threerings.cast.ComponentClass; import com.threerings.cast.ComponentClass;
@@ -43,7 +44,7 @@ public class BundledComponentRepositoryTest extends TestCase
ResourceManager rmgr = new ResourceManager("rsrc"); ResourceManager rmgr = new ResourceManager("rsrc");
rmgr.initBundles( rmgr.initBundles(
null, "config/resource/manager.properties", null); null, "config/resource/manager.properties", null);
ImageManager imgr = new ImageManager(rmgr, null); ImageManager imgr = new ImageManager(rmgr, (Component)null);
BundledComponentRepository repo = BundledComponentRepository repo =
new BundledComponentRepository(rmgr, imgr, "components"); new BundledComponentRepository(rmgr, imgr, "components");
@@ -21,6 +21,7 @@
package com.threerings.media.tile.bundle; package com.threerings.media.tile.bundle;
import java.awt.Component;
import java.util.Iterator; import java.util.Iterator;
import com.threerings.media.image.ImageManager; import com.threerings.media.image.ImageManager;
@@ -43,7 +44,7 @@ public class BundledTileSetRepositoryTest extends TestCase
rmgr.initBundles( rmgr.initBundles(
null, "config/resource/manager.properties", null); null, "config/resource/manager.properties", null);
BundledTileSetRepository repo = new BundledTileSetRepository( BundledTileSetRepository repo = new BundledTileSetRepository(
rmgr, new ImageManager(rmgr, null), "tilesets"); rmgr, new ImageManager(rmgr, (Component)null), "tilesets");
Iterator sets = repo.enumerateTileSets(); Iterator sets = repo.enumerateTileSets();
while (sets.hasNext()) { while (sets.hasNext()) {
sets.next(); sets.next();