diff --git a/src/java/com/threerings/media/image/AWTImageCreator.java b/src/java/com/threerings/media/image/AWTImageCreator.java new file mode 100644 index 000000000..da0978ee6 --- /dev/null +++ b/src/java/com/threerings/media/image/AWTImageCreator.java @@ -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; +} diff --git a/src/java/com/threerings/media/image/ImageManager.java b/src/java/com/threerings/media/image/ImageManager.java index 2b347664b..880b6293b 100644 --- a/src/java/com/threerings/media/image/ImageManager.java +++ b/src/java/com/threerings/media/image/ImageManager.java @@ -23,7 +23,6 @@ package com.threerings.media.image; import java.awt.Component; import java.awt.Graphics2D; -import java.awt.GraphicsConfiguration; import java.awt.Rectangle; import java.awt.Transparency; import java.awt.image.BufferedImage; @@ -96,16 +95,26 @@ public class ImageManager } /** - * Construct an image manager with the specified {@link - * ResourceManager} from which it will obtain its data. A non-null - * context 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. + * This interface allows the image manager to create images that are in a + * format optimal for rendering to the screen. */ - 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; + _icreator = icreator; // create our image cache int icsize = _cacheSize.getValue(); @@ -116,13 +125,15 @@ public class ImageManager } }); _ccache.setTracking(true); + } - // obtain our graphics configuration - if (context != null) { - _gc = context.getGraphicsConfiguration(); - } else { - _gc = ImageUtil.getDefGC(); - } + /** + * A convenience constructor that creates an {@link AWTImageCreator} for + * use by the image manager. + */ + 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) { - // DEBUG: override transparency for the moment on all images - 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); - } + return _icreator.createImage(width, height, transparency); } /** @@ -224,8 +227,8 @@ public class ImageManager * they (some day) will automatically use volatile images to increase * performance. */ - public BufferedImage getPreparedImage (String rset, String path, - Colorization[] zations) + public BufferedImage getPreparedImage ( + String rset, String path, Colorization[] zations) { BufferedImage image = getImage(rset, path, zations); BufferedImage prepped = null; @@ -462,12 +465,12 @@ public class ImageManager } /** - * Returns the graphics configuration that should be used to optimize - * images for display. + * Returns the image creator that can be used to create buffered images + * 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. */ protected ResourceManager _rmgr; + /** We use this to create images optimized for rendering. */ + protected OptimalImageCreator _icreator; + /** A cache of loaded images. */ protected LRUHashMap _ccache; @@ -573,9 +579,6 @@ public class ImageManager /** Throttle our cache status logging to once every 300 seconds. */ protected Throttle _cacheStatThrottle = new Throttle(1, 300000L); - /** The graphics configuration for the default screen device. */ - protected GraphicsConfiguration _gc; - /** Our default data provider. */ protected ImageDataProvider _defaultProvider = new ImageDataProvider() { public BufferedImage loadImage (String path) throws IOException { diff --git a/tests/src/java/com/threerings/cast/bundle/BundledComponentRepositoryTest.java b/tests/src/java/com/threerings/cast/bundle/BundledComponentRepositoryTest.java index ba1477c42..5b3270901 100644 --- a/tests/src/java/com/threerings/cast/bundle/BundledComponentRepositoryTest.java +++ b/tests/src/java/com/threerings/cast/bundle/BundledComponentRepositoryTest.java @@ -21,6 +21,7 @@ package com.threerings.cast.bundle; +import java.awt.Component; import java.util.Iterator; import com.threerings.cast.ComponentClass; @@ -43,7 +44,7 @@ public class BundledComponentRepositoryTest extends TestCase ResourceManager rmgr = new ResourceManager("rsrc"); rmgr.initBundles( null, "config/resource/manager.properties", null); - ImageManager imgr = new ImageManager(rmgr, null); + ImageManager imgr = new ImageManager(rmgr, (Component)null); BundledComponentRepository repo = new BundledComponentRepository(rmgr, imgr, "components"); diff --git a/tests/src/java/com/threerings/media/tile/bundle/BundledTileSetRepositoryTest.java b/tests/src/java/com/threerings/media/tile/bundle/BundledTileSetRepositoryTest.java index 1f2502ee7..150677cfc 100644 --- a/tests/src/java/com/threerings/media/tile/bundle/BundledTileSetRepositoryTest.java +++ b/tests/src/java/com/threerings/media/tile/bundle/BundledTileSetRepositoryTest.java @@ -21,6 +21,7 @@ package com.threerings.media.tile.bundle; +import java.awt.Component; import java.util.Iterator; import com.threerings.media.image.ImageManager; @@ -43,7 +44,7 @@ public class BundledTileSetRepositoryTest extends TestCase rmgr.initBundles( null, "config/resource/manager.properties", null); BundledTileSetRepository repo = new BundledTileSetRepository( - rmgr, new ImageManager(rmgr, null), "tilesets"); + rmgr, new ImageManager(rmgr, (Component)null), "tilesets"); Iterator sets = repo.enumerateTileSets(); while (sets.hasNext()) { sets.next();