159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta/*
259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Copyright (c) 2009-2010 jMonkeyEngine
359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * All rights reserved.
459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Redistribution and use in source and binary forms, with or without
659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * modification, are permitted provided that the following conditions are
759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * met:
859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Redistributions of source code must retain the above copyright
1059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   notice, this list of conditions and the following disclaimer.
1159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
1259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Redistributions in binary form must reproduce the above copyright
1359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   notice, this list of conditions and the following disclaimer in the
1459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   documentation and/or other materials provided with the distribution.
1559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
1659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
1759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   may be used to endorse or promote products derived from this software
1859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   without specific prior written permission.
1959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
2059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
2459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
2559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
2759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
2859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
2959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */
3259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
3359b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartapackage com.jme3.system.lwjgl;
3459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
3559b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.system.AppSettings;
3659b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.system.JmeContext.Type;
3759b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.awt.Graphics2D;
3859b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.awt.image.BufferedImage;
3959b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.nio.ByteBuffer;
4059b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.util.concurrent.atomic.AtomicBoolean;
4159b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.util.logging.Level;
4259b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.util.logging.Logger;
4359b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport org.lwjgl.LWJGLException;
4459b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport org.lwjgl.opengl.*;
4559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
4659b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartapublic class LwjglDisplay extends LwjglAbstractDisplay {
4759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
4859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private static final Logger logger = Logger.getLogger(LwjglDisplay.class.getName());
4959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
5059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private final AtomicBoolean needRestart = new AtomicBoolean(false);
5159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private PixelFormat pixelFormat;
5259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
5359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    protected DisplayMode getFullscreenDisplayMode(int width, int height, int bpp, int freq){
5459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        try {
5559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            DisplayMode[] modes = Display.getAvailableDisplayModes();
5659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            for (DisplayMode mode : modes){
5759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                if (mode.getWidth() == width
5859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                 && mode.getHeight() == height
5959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                 && (mode.getBitsPerPixel() == bpp || (bpp==24&&mode.getBitsPerPixel()==32))
6059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                 && mode.getFrequency() == freq){
6159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                    return mode;
6259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                }
6359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            }
6459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        } catch (LWJGLException ex) {
6559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            listener.handleError("Failed to acquire fullscreen display mode!", ex);
6659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
6759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return null;
6859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
6959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
7059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    protected void createContext(AppSettings settings) throws LWJGLException{
7159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        DisplayMode displayMode = null;
7259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (settings.getWidth() <= 0 || settings.getHeight() <= 0){
7359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            displayMode = Display.getDesktopDisplayMode();
7459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            settings.setResolution(displayMode.getWidth(), displayMode.getHeight());
7559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }else if (settings.isFullscreen()){
7659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            displayMode = getFullscreenDisplayMode(settings.getWidth(), settings.getHeight(),
7759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                                                   settings.getBitsPerPixel(), settings.getFrequency());
7859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            if (displayMode == null)
7959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                throw new RuntimeException("Unable to find fullscreen display mode matching settings");
8059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }else{
8159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            displayMode = new DisplayMode(settings.getWidth(), settings.getHeight());
8259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
8359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
8459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	   int samples = 0;
8559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (settings.getSamples() > 1){
8659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            samples = settings.getSamples();
8759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
8859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        PixelFormat pf = new PixelFormat(settings.getBitsPerPixel(),
8959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                                         0,
9059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                                         settings.getDepthBits(),
9159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                                         settings.getStencilBits(),
9259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                                         samples);
9359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
9459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        frameRate = settings.getFrameRate();
9559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        logger.log(Level.INFO, "Selected display mode: {0}", displayMode);
9659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
9759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        boolean pixelFormatChanged = false;
9859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (created.get() && (pixelFormat.getBitsPerPixel() != pf.getBitsPerPixel()
9959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                            ||pixelFormat.getDepthBits() != pf.getDepthBits()
10059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                            ||pixelFormat.getStencilBits() != pf.getStencilBits()
10159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                            ||pixelFormat.getSamples() != pf.getSamples())){
10259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            renderer.resetGLObjects();
10359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            Display.destroy();
10459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            pixelFormatChanged = true;
10559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
10659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        pixelFormat = pf;
10759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
10859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Display.setTitle(settings.getTitle());
10959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (displayMode != null){
11059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            if (settings.isFullscreen()){
11159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                Display.setDisplayModeAndFullscreen(displayMode);
11259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            }else{
11359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                Display.setFullscreen(false);
11459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                Display.setDisplayMode(displayMode);
11559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            }
11659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }else{
11759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            Display.setFullscreen(settings.isFullscreen());
11859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
11959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
12059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (settings.getIcons() != null) {
12159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            Display.setIcon(imagesToByteBuffers(settings.getIcons()));
12259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
12359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
12459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Display.setVSyncEnabled(settings.isVSync());
12559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
12659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (created.get() && !pixelFormatChanged){
12759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            Display.releaseContext();
12859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            Display.makeCurrent();
12959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            Display.update();
13059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
13159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
13259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (!created.get() || pixelFormatChanged){
13359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            ContextAttribs attr = createContextAttribs();
13459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            if (attr != null){
13559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                Display.create(pixelFormat, attr);
13659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            }else{
13759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                Display.create(pixelFormat);
13859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            }
13959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            renderable.set(true);
14059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
14159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            if (pixelFormatChanged && pixelFormat.getSamples() > 1
14259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta             && GLContext.getCapabilities().GL_ARB_multisample){
14359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                GL11.glEnable(ARBMultisample.GL_MULTISAMPLE_ARB);
14459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            }
14559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
14659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
14759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
14859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    protected void destroyContext(){
14959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        try {
15059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            renderer.cleanup();
15159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            Display.releaseContext();
15259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            Display.destroy();
15359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        } catch (LWJGLException ex) {
15459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            listener.handleError("Failed to destroy context", ex);
15559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
15659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
15759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
15859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public void create(boolean waitFor){
15959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (created.get()){
16059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            logger.warning("create() called when display is already created!");
16159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            return;
16259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
16359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
16459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        new Thread(this, "LWJGL Renderer Thread").start();
16559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (waitFor)
16659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            waitFor(true);
16759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
16859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
16959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    @Override
17059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public void runLoop(){
17159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        // This method is overriden to do restart
17259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (needRestart.getAndSet(false)){
17359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            try{
17459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                createContext(settings);
17559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            }catch (LWJGLException ex){
17659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                logger.log(Level.SEVERE, "Failed to set display settings!", ex);
17759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            }
17859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            listener.reshape(settings.getWidth(), settings.getHeight());
17959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            logger.info("Display restarted.");
18059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
18159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
18259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        super.runLoop();
18359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
18459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
18559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    @Override
18659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public void restart() {
18759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (created.get()){
18859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            needRestart.set(true);
18959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }else{
19059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            logger.warning("Display is not created, cannot restart window.");
19159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
19259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
19359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
19459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public Type getType() {
19559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return Type.Display;
19659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
19759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
19859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public void setTitle(String title){
19959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (created.get())
20059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            Display.setTitle(title);
20159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
20259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
20359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private ByteBuffer[] imagesToByteBuffers(Object[] images) {
20459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        ByteBuffer[] out = new ByteBuffer[images.length];
20559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        for (int i = 0; i < images.length; i++) {
20659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            BufferedImage image = (BufferedImage) images[i];
20759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            out[i] = imageToByteBuffer(image);
20859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
20959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return out;
21059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
21159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
21259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private ByteBuffer imageToByteBuffer(BufferedImage image) {
21359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (image.getType() != BufferedImage.TYPE_INT_ARGB_PRE) {
21459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            BufferedImage convertedImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB_PRE);
21559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            Graphics2D g = convertedImage.createGraphics();
21659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            double width = image.getWidth() * (double) 1;
21759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            double height = image.getHeight() * (double) 1;
21859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            g.drawImage(image, (int) ((convertedImage.getWidth() - width) / 2),
21959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                    (int) ((convertedImage.getHeight() - height) / 2),
22059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                    (int) (width), (int) (height), null);
22159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            g.dispose();
22259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            image = convertedImage;
22359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
22459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
22559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        byte[] imageBuffer = new byte[image.getWidth() * image.getHeight() * 4];
22659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        int counter = 0;
22759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        for (int i = 0; i < image.getHeight(); i++) {
22859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            for (int j = 0; j < image.getWidth(); j++) {
22959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                int colorSpace = image.getRGB(j, i);
23059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                imageBuffer[counter + 0] = (byte) ((colorSpace << 8) >> 24);
23159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                imageBuffer[counter + 1] = (byte) ((colorSpace << 16) >> 24);
23259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                imageBuffer[counter + 2] = (byte) ((colorSpace << 24) >> 24);
23359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                imageBuffer[counter + 3] = (byte) (colorSpace >> 24);
23459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                counter += 4;
23559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            }
23659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
23759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return ByteBuffer.wrap(imageBuffer);
23859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
23959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
24059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta}
241