diff options
author | Renanse <[email protected]> | 2013-03-14 21:52:41 -0500 |
---|---|---|
committer | Renanse <[email protected]> | 2013-03-14 21:52:41 -0500 |
commit | 19bd8d8e1e5de5d31fab60bf2a3dedc9778782d0 (patch) | |
tree | f14c233f6181afaf91d00f83d5e078af27c3cafd /ardor3d-awt | |
parent | aedbc04cc0c605496fc5aa12ba074643ef16d6d9 (diff) | |
parent | c81013c8f8bec5588be7e762a254c509286cbb46 (diff) |
Merge remote-tracking branch 'origin/master'
Conflicts:
ardor3d-collada/src/main/java/com/ardor3d/extension/model/collada/jdom/data/DataCache.java
Diffstat (limited to 'ardor3d-awt')
-rw-r--r-- | ardor3d-awt/.classpath | 8 | ||||
-rw-r--r-- | ardor3d-awt/.project | 17 | ||||
-rw-r--r-- | ardor3d-awt/.settings/org.eclipse.core.resources.prefs | 3 | ||||
-rw-r--r-- | ardor3d-awt/.settings/org.eclipse.jdt.core.prefs | 2 | ||||
-rw-r--r-- | ardor3d-awt/.settings/org.eclipse.jdt.ui.prefs | 1 | ||||
-rw-r--r-- | ardor3d-awt/pom.xml | 80 | ||||
-rw-r--r-- | ardor3d-awt/src/main/java/com/ardor3d/image/util/AWTImageLoader.java | 534 | ||||
-rw-r--r-- | ardor3d-awt/src/test/java/com/ardor3d/input/awt/TestAwtKeyboardWrapper.java | 212 |
8 files changed, 414 insertions, 443 deletions
diff --git a/ardor3d-awt/.classpath b/ardor3d-awt/.classpath deleted file mode 100644 index c84bc2d..0000000 --- a/ardor3d-awt/.classpath +++ /dev/null @@ -1,8 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<classpath> - <classpathentry kind="src" path="src/test/java"/> - <classpathentry kind="src" path="src/main/java"/> - <classpathentry combineaccessrules="false" kind="src" path="/ardor3d-core"/> - <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/> - <classpathentry kind="output" path="bin"/> -</classpath> diff --git a/ardor3d-awt/.project b/ardor3d-awt/.project deleted file mode 100644 index 05130fc..0000000 --- a/ardor3d-awt/.project +++ /dev/null @@ -1,17 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<projectDescription> - <name>ardor3d-awt</name> - <comment></comment> - <projects> - </projects> - <buildSpec> - <buildCommand> - <name>org.eclipse.jdt.core.javabuilder</name> - <arguments> - </arguments> - </buildCommand> - </buildSpec> - <natures> - <nature>org.eclipse.jdt.core.javanature</nature> - </natures> -</projectDescription> diff --git a/ardor3d-awt/.settings/org.eclipse.core.resources.prefs b/ardor3d-awt/.settings/org.eclipse.core.resources.prefs deleted file mode 100644 index 5456664..0000000 --- a/ardor3d-awt/.settings/org.eclipse.core.resources.prefs +++ /dev/null @@ -1,3 +0,0 @@ -#Wed Jan 07 11:32:27 PST 2009 -eclipse.preferences.version=1 -encoding/<project>=UTF-8 diff --git a/ardor3d-awt/.settings/org.eclipse.jdt.core.prefs b/ardor3d-awt/.settings/org.eclipse.jdt.core.prefs index 9446d01..19eadb6 100644 --- a/ardor3d-awt/.settings/org.eclipse.jdt.core.prefs +++ b/ardor3d-awt/.settings/org.eclipse.jdt.core.prefs @@ -1,4 +1,3 @@ -#Tue Apr 06 11:25:13 CDT 2010 eclipse.preferences.version=1 org.eclipse.jdt.core.codeComplete.argumentPrefixes= org.eclipse.jdt.core.codeComplete.argumentSuffixes= @@ -17,6 +16,7 @@ org.eclipse.jdt.core.compiler.debug.localVariable=generate org.eclipse.jdt.core.compiler.debug.sourceFile=generate org.eclipse.jdt.core.compiler.problem.assertIdentifier=error org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning org.eclipse.jdt.core.compiler.source=1.6 org.eclipse.jdt.core.formatter.align_type_members_on_columns=false org.eclipse.jdt.core.formatter.alignment_for_arguments_in_allocation_expression=16 diff --git a/ardor3d-awt/.settings/org.eclipse.jdt.ui.prefs b/ardor3d-awt/.settings/org.eclipse.jdt.ui.prefs index f479490..7588391 100644 --- a/ardor3d-awt/.settings/org.eclipse.jdt.ui.prefs +++ b/ardor3d-awt/.settings/org.eclipse.jdt.ui.prefs @@ -1,4 +1,3 @@ -#Sun Jan 04 11:43:23 CST 2009
cleanup.add_default_serial_version_id=true
cleanup.add_generated_serial_version_id=false
cleanup.add_missing_annotations=true
diff --git a/ardor3d-awt/pom.xml b/ardor3d-awt/pom.xml index 789692b..176dfbd 100644 --- a/ardor3d-awt/pom.xml +++ b/ardor3d-awt/pom.xml @@ -1,40 +1,40 @@ -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>com.ardor3d</groupId>
- <artifactId>ardor3d</artifactId>
- <version>0.9-SNAPSHOT</version>
- <relativePath>../pom.xml</relativePath>
- </parent>
-
- <artifactId>ardor3d-awt</artifactId>
- <packaging>bundle</packaging>
- <name>Ardor 3D AWT</name>
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <configuration>
- <source>1.6</source>
- <target>1.6</target>
- <encoding>${project.build.sourceEncoding}</encoding>
- </configuration>
- </plugin>
- </plugins>
- </build>
- <dependencies>
- <dependency>
- <groupId>${project.groupId}</groupId>
- <artifactId>ardor3d-core</artifactId>
- <version>${project.version}</version>
- </dependency>
- <dependency>
- <groupId>org.easymock</groupId>
- <artifactId>easymockclassextension</artifactId>
- </dependency>
- </dependencies>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- </properties>
-</project>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>com.ardor3d</groupId> + <artifactId>ardor3d</artifactId> + <version>0.9-SNAPSHOT</version> + <relativePath>../pom.xml</relativePath> + </parent> + + <artifactId>ardor3d-awt</artifactId> + <packaging>bundle</packaging> + <name>Ardor 3D AWT</name> + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <configuration> + <source>1.6</source> + <target>1.6</target> + <encoding>${project.build.sourceEncoding}</encoding> + </configuration> + </plugin> + </plugins> + </build> + <dependencies> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>ardor3d-core</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.easymock</groupId> + <artifactId>easymockclassextension</artifactId> + </dependency> + </dependencies> + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + </properties> +</project> diff --git a/ardor3d-awt/src/main/java/com/ardor3d/image/util/AWTImageLoader.java b/ardor3d-awt/src/main/java/com/ardor3d/image/util/AWTImageLoader.java index 86a93d3..a1f965e 100644 --- a/ardor3d-awt/src/main/java/com/ardor3d/image/util/AWTImageLoader.java +++ b/ardor3d-awt/src/main/java/com/ardor3d/image/util/AWTImageLoader.java @@ -1,267 +1,267 @@ -/**
- * Copyright (c) 2008-2012 Ardor Labs, Inc.
- *
- * This file is part of Ardor3D.
- *
- * Ardor3D is free software: you can redistribute it and/or modify it
- * under the terms of its license which may be found in the accompanying
- * LICENSE file or at <http://www.ardor3d.com/LICENSE>.
- */
-
-package com.ardor3d.image.util;
-
-import java.awt.image.BufferedImage;
-import java.awt.image.ColorModel;
-import java.awt.image.DataBuffer;
-import java.awt.image.Raster;
-import java.awt.image.RenderedImage;
-import java.awt.image.renderable.RenderableImage;
-import java.io.IOException;
-import java.io.InputStream;
-import java.nio.ByteBuffer;
-import java.util.List;
-import java.util.logging.Logger;
-
-import javax.imageio.ImageIO;
-
-import com.ardor3d.image.Image;
-import com.ardor3d.image.ImageDataFormat;
-import com.ardor3d.image.PixelDataType;
-import com.ardor3d.renderer.state.TextureState;
-import com.ardor3d.util.geom.BufferUtils;
-import com.google.common.collect.Lists;
-
-/**
- * Image loader that makes use of AWT's ImageIO to load image file data.
- */
-public class AWTImageLoader implements ImageLoader {
- private static final Logger logger = Logger.getLogger(AWTImageLoader.class.getName());
-
- private static boolean createOnHeap = false;
-
- private static String[] supportedFormats;
-
- public static String[] getSupportedFormats() {
- return supportedFormats;
- }
-
- public static void registerLoader() {
- if (supportedFormats == null) {
- final List<String> formats = Lists.newArrayList();
- for (String format : ImageIO.getReaderFormatNames()) {
- format = "." + format.toUpperCase();
- if (!formats.contains(format)) {
- formats.add(format);
- }
- }
- supportedFormats = formats.toArray(new String[formats.size()]);
- }
- ImageLoaderUtil.registerHandler(new AWTImageLoader(), supportedFormats);
- }
-
- public Image load(final InputStream is, final boolean flipImage) throws IOException {
- final BufferedImage image = ImageIO.read(is);
- if (image == null) {
- return null;
- }
-
- return makeArdor3dImage(image, flipImage);
- }
-
- public static Image makeArdor3dImage(final BufferedImage image, final boolean flipImage) {
- if (image == null) {
- return null;
- }
-
- final boolean hasAlpha = image.getColorModel().hasAlpha();
- final boolean grayscale = image.getColorModel().getNumComponents() == 1;
- BufferedImage tex;
-
- if (flipImage
- || ((image).getType() != BufferedImage.TYPE_BYTE_GRAY && (hasAlpha ? (image).getType() != BufferedImage.TYPE_4BYTE_ABGR
- : (image).getType() != BufferedImage.TYPE_3BYTE_BGR))) {
- // Obtain the image data.
- try {
- tex = new BufferedImage(image.getWidth(null), image.getHeight(null),
- grayscale ? BufferedImage.TYPE_BYTE_GRAY : hasAlpha ? BufferedImage.TYPE_4BYTE_ABGR
- : BufferedImage.TYPE_3BYTE_BGR);
- } catch (final IllegalArgumentException e) {
- logger.warning("Problem creating buffered Image: " + e.getMessage());
- return TextureState.getDefaultTextureImage();
- }
-
- final int imageWidth = image.getWidth(null);
- final int imageHeight = image.getHeight(null);
- final int[] tmpData = new int[imageWidth];
- int row = 0;
- for (int y = imageHeight - 1; y >= 0; y--) {
- image.getRGB(0, (flipImage ? row++ : y), imageWidth, 1, tmpData, 0, imageWidth);
- tex.setRGB(0, y, imageWidth, 1, tmpData, 0, imageWidth);
- }
-
- } else {
- tex = image;
- }
-
- // Get a pointer to the image memory
- final byte data[] = asByteArray(tex);
- final ByteBuffer scratch = createOnHeap ? BufferUtils.createByteBufferOnHeap(data.length) : BufferUtils
- .createByteBuffer(data.length);
- scratch.clear();
- scratch.put(data);
- scratch.flip();
- final Image ardorImage = new Image();
- ardorImage.setDataFormat(grayscale ? ImageDataFormat.Luminance : hasAlpha ? ImageDataFormat.RGBA
- : ImageDataFormat.RGB);
- ardorImage.setDataType(PixelDataType.UnsignedByte);
- ardorImage.setWidth(tex.getWidth());
- ardorImage.setHeight(tex.getHeight());
- ardorImage.setData(scratch);
- return ardorImage;
- }
-
- public static Image makeArdor3dImage(final RenderableImage image, final boolean flipImage) {
- return makeArdor3dImage(image.createDefaultRendering(), flipImage);
- }
-
- public static Image makeArdor3dImage(final RenderedImage image, final boolean flipImage) {
- if (image == null) {
- return null;
- }
-
- final ColorModel colorModel = image.getColorModel();
- final boolean hasAlpha = colorModel.hasAlpha();
- final boolean grayscale = colorModel.getNumComponents() == 1;
-
- // Get a pointer to the image memory
- final byte data[] = asByteArray(image, grayscale, hasAlpha);
- final ByteBuffer scratch = createOnHeap ? BufferUtils.createByteBufferOnHeap(data.length) : BufferUtils
- .createByteBuffer(data.length);
- scratch.clear();
- scratch.put(data);
- scratch.flip();
- final Image ardorImage = new Image();
- ardorImage.setDataFormat(grayscale ? ImageDataFormat.Luminance : hasAlpha ? ImageDataFormat.RGBA
- : ImageDataFormat.RGB);
- ardorImage.setDataType(PixelDataType.UnsignedByte);
- ardorImage.setWidth(image.getWidth());
- ardorImage.setHeight(image.getHeight());
- ardorImage.setData(scratch);
- return ardorImage;
- }
-
- public static byte[] asByteArray(final BufferedImage image) {
- final int imageWidth = image.getWidth(null);
- final int imageHeight = image.getHeight(null);
- final boolean hasAlpha = image.getColorModel().hasAlpha();
- final boolean grayscale = image.getColorModel().getNumComponents() == 1;
-
- if (image.getRaster().getTransferType() == DataBuffer.TYPE_BYTE) {
- return (byte[]) image.getRaster().getDataElements(0, 0, imageWidth, imageHeight, null);
- }
-
- final byte[] rVal = new byte[imageWidth * imageHeight * (grayscale ? 1 : (hasAlpha ? 4 : 3))];
- final int[] tmpData = new int[imageWidth];
- int index = 0;
- for (int y = 0; y < imageHeight; y++) {
- image.getRGB(0, y, imageWidth, 1, tmpData, 0, imageWidth);
- for (int i = 0; i < imageWidth; i++) {
- final int argb = tmpData[i];
- if (grayscale) {
- rVal[index++] = (byte) (argb & 0xFF);
- } else {
- rVal[index++] = (byte) ((argb >> 16) & 0xFF);
- rVal[index++] = (byte) ((argb >> 8) & 0xFF);
- rVal[index++] = (byte) (argb & 0xFF);
- if (hasAlpha) {
- rVal[index++] = (byte) ((argb >> 24) & 0xFF);
- }
- }
- }
- }
- return rVal;
- }
-
- public static byte[] asByteArray(final RenderedImage image, final boolean isGreyscale, final boolean hasAlpha) {
- final int imageWidth = image.getWidth();
- final int imageHeight = image.getHeight();
- final Raster raster = image.getData();
-
- if (raster.getTransferType() == DataBuffer.TYPE_BYTE) {
- return (byte[]) image.getData().getDataElements(0, 0, imageWidth, imageHeight, null);
- }
-
- final byte[] rVal = new byte[imageWidth * imageHeight * (isGreyscale ? 1 : (hasAlpha ? 4 : 3))];
- final int[] tmpData = new int[imageWidth];
- int index = 0;
- for (int y = 0; y < imageHeight; y++) {
- getRGB(raster, image.getColorModel(), 0, y, imageWidth, 1, tmpData, 0, imageWidth);
- for (int i = 0; i < imageWidth; i++) {
- final int argb = tmpData[i];
- if (isGreyscale) {
- rVal[index++] = (byte) (argb & 0xFF);
- } else {
- rVal[index++] = (byte) ((argb >> 16) & 0xFF);
- rVal[index++] = (byte) ((argb >> 8) & 0xFF);
- rVal[index++] = (byte) (argb & 0xFF);
- if (hasAlpha) {
- rVal[index++] = (byte) ((argb >> 24) & 0xFF);
- }
- }
- }
- }
- return rVal;
- }
-
- /**
- * Extract rgb values from raster using the colormodel.
- */
- private static int[] getRGB(final Raster raster, final ColorModel colorModel, final int startX, final int startY,
- final int w, final int h, int[] rgbArray, final int offset, final int scansize) {
- Object data;
- final int nbands = raster.getNumBands();
- final int dataType = raster.getDataBuffer().getDataType();
- switch (dataType) {
- case DataBuffer.TYPE_BYTE:
- data = new byte[nbands];
- break;
- case DataBuffer.TYPE_USHORT:
- data = new short[nbands];
- break;
- case DataBuffer.TYPE_INT:
- data = new int[nbands];
- break;
- case DataBuffer.TYPE_FLOAT:
- data = new float[nbands];
- break;
- case DataBuffer.TYPE_DOUBLE:
- data = new double[nbands];
- break;
- default:
- throw new IllegalArgumentException("Unknown data buffer type: " + dataType);
- }
-
- if (rgbArray == null) {
- rgbArray = new int[offset + h * scansize];
- }
-
- int yoff = offset;
- int off;
- for (int y = startY; y < startY + h; y++, yoff += scansize) {
- off = yoff;
- for (int x = startX; x < startX + w; x++) {
- rgbArray[off++] = colorModel.getRGB(raster.getDataElements(x, y, data));
- }
- }
-
- return rgbArray;
- }
-
- public static void setCreateOnHeap(final boolean createOnHeap) {
- AWTImageLoader.createOnHeap = createOnHeap;
- }
-
- public static boolean isCreateOnHeap() {
- return createOnHeap;
- }
-}
+/** + * Copyright (c) 2008-2012 Ardor Labs, Inc. + * + * This file is part of Ardor3D. + * + * Ardor3D is free software: you can redistribute it and/or modify it + * under the terms of its license which may be found in the accompanying + * LICENSE file or at <http://www.ardor3d.com/LICENSE>. + */ + +package com.ardor3d.image.util; + +import java.awt.image.BufferedImage; +import java.awt.image.ColorModel; +import java.awt.image.DataBuffer; +import java.awt.image.Raster; +import java.awt.image.RenderedImage; +import java.awt.image.renderable.RenderableImage; +import java.io.IOException; +import java.io.InputStream; +import java.nio.ByteBuffer; +import java.util.List; +import java.util.logging.Logger; + +import javax.imageio.ImageIO; + +import com.ardor3d.image.Image; +import com.ardor3d.image.ImageDataFormat; +import com.ardor3d.image.PixelDataType; +import com.ardor3d.renderer.state.TextureState; +import com.ardor3d.util.geom.BufferUtils; +import com.google.common.collect.Lists; + +/** + * Image loader that makes use of AWT's ImageIO to load image file data. + */ +public class AWTImageLoader implements ImageLoader { + private static final Logger logger = Logger.getLogger(AWTImageLoader.class.getName()); + + private static boolean createOnHeap = false; + + private static String[] supportedFormats; + + public static String[] getSupportedFormats() { + return supportedFormats; + } + + public static void registerLoader() { + if (supportedFormats == null) { + final List<String> formats = Lists.newArrayList(); + for (String format : ImageIO.getReaderFormatNames()) { + format = "." + format.toUpperCase(); + if (!formats.contains(format)) { + formats.add(format); + } + } + supportedFormats = formats.toArray(new String[formats.size()]); + } + ImageLoaderUtil.registerHandler(new AWTImageLoader(), supportedFormats); + } + + public Image load(final InputStream is, final boolean flipImage) throws IOException { + final BufferedImage image = ImageIO.read(is); + if (image == null) { + return null; + } + + return makeArdor3dImage(image, flipImage); + } + + public static Image makeArdor3dImage(final BufferedImage image, final boolean flipImage) { + if (image == null) { + return null; + } + + final boolean hasAlpha = image.getColorModel().hasAlpha(); + final boolean grayscale = image.getColorModel().getNumComponents() == 1; + BufferedImage tex; + + if (flipImage + || ((image).getType() != BufferedImage.TYPE_BYTE_GRAY && (hasAlpha ? (image).getType() != BufferedImage.TYPE_4BYTE_ABGR + : (image).getType() != BufferedImage.TYPE_3BYTE_BGR))) { + // Obtain the image data. + try { + tex = new BufferedImage(image.getWidth(null), image.getHeight(null), + grayscale ? BufferedImage.TYPE_BYTE_GRAY : hasAlpha ? BufferedImage.TYPE_4BYTE_ABGR + : BufferedImage.TYPE_3BYTE_BGR); + } catch (final IllegalArgumentException e) { + logger.warning("Problem creating buffered Image: " + e.getMessage()); + return TextureState.getDefaultTextureImage(); + } + + final int imageWidth = image.getWidth(null); + final int imageHeight = image.getHeight(null); + final int[] tmpData = new int[imageWidth]; + int row = 0; + for (int y = imageHeight - 1; y >= 0; y--) { + image.getRGB(0, (flipImage ? row++ : y), imageWidth, 1, tmpData, 0, imageWidth); + tex.setRGB(0, y, imageWidth, 1, tmpData, 0, imageWidth); + } + + } else { + tex = image; + } + + // Get a pointer to the image memory + final byte data[] = asByteArray(tex); + final ByteBuffer scratch = createOnHeap ? BufferUtils.createByteBufferOnHeap(data.length) : BufferUtils + .createByteBuffer(data.length); + scratch.clear(); + scratch.put(data); + scratch.flip(); + final Image ardorImage = new Image(); + ardorImage.setDataFormat(grayscale ? ImageDataFormat.Luminance : hasAlpha ? ImageDataFormat.RGBA + : ImageDataFormat.RGB); + ardorImage.setDataType(PixelDataType.UnsignedByte); + ardorImage.setWidth(tex.getWidth()); + ardorImage.setHeight(tex.getHeight()); + ardorImage.setData(scratch); + return ardorImage; + } + + public static Image makeArdor3dImage(final RenderableImage image, final boolean flipImage) { + return makeArdor3dImage(image.createDefaultRendering(), flipImage); + } + + public static Image makeArdor3dImage(final RenderedImage image, final boolean flipImage) { + if (image == null) { + return null; + } + + final ColorModel colorModel = image.getColorModel(); + final boolean hasAlpha = colorModel.hasAlpha(); + final boolean grayscale = colorModel.getNumComponents() == 1; + + // Get a pointer to the image memory + final byte data[] = asByteArray(image, grayscale, hasAlpha); + final ByteBuffer scratch = createOnHeap ? BufferUtils.createByteBufferOnHeap(data.length) : BufferUtils + .createByteBuffer(data.length); + scratch.clear(); + scratch.put(data); + scratch.flip(); + final Image ardorImage = new Image(); + ardorImage.setDataFormat(grayscale ? ImageDataFormat.Luminance : hasAlpha ? ImageDataFormat.RGBA + : ImageDataFormat.RGB); + ardorImage.setDataType(PixelDataType.UnsignedByte); + ardorImage.setWidth(image.getWidth()); + ardorImage.setHeight(image.getHeight()); + ardorImage.setData(scratch); + return ardorImage; + } + + public static byte[] asByteArray(final BufferedImage image) { + final int imageWidth = image.getWidth(null); + final int imageHeight = image.getHeight(null); + final boolean hasAlpha = image.getColorModel().hasAlpha(); + final boolean grayscale = image.getColorModel().getNumComponents() == 1; + + if (image.getRaster().getTransferType() == DataBuffer.TYPE_BYTE) { + return (byte[]) image.getRaster().getDataElements(0, 0, imageWidth, imageHeight, null); + } + + final byte[] rVal = new byte[imageWidth * imageHeight * (grayscale ? 1 : (hasAlpha ? 4 : 3))]; + final int[] tmpData = new int[imageWidth]; + int index = 0; + for (int y = 0; y < imageHeight; y++) { + image.getRGB(0, y, imageWidth, 1, tmpData, 0, imageWidth); + for (int i = 0; i < imageWidth; i++) { + final int argb = tmpData[i]; + if (grayscale) { + rVal[index++] = (byte) (argb & 0xFF); + } else { + rVal[index++] = (byte) ((argb >> 16) & 0xFF); + rVal[index++] = (byte) ((argb >> 8) & 0xFF); + rVal[index++] = (byte) (argb & 0xFF); + if (hasAlpha) { + rVal[index++] = (byte) ((argb >> 24) & 0xFF); + } + } + } + } + return rVal; + } + + public static byte[] asByteArray(final RenderedImage image, final boolean isGreyscale, final boolean hasAlpha) { + final int imageWidth = image.getWidth(); + final int imageHeight = image.getHeight(); + final Raster raster = image.getData(); + + if (raster.getTransferType() == DataBuffer.TYPE_BYTE) { + return (byte[]) image.getData().getDataElements(0, 0, imageWidth, imageHeight, null); + } + + final byte[] rVal = new byte[imageWidth * imageHeight * (isGreyscale ? 1 : (hasAlpha ? 4 : 3))]; + final int[] tmpData = new int[imageWidth]; + int index = 0; + for (int y = 0; y < imageHeight; y++) { + getRGB(raster, image.getColorModel(), 0, y, imageWidth, 1, tmpData, 0, imageWidth); + for (int i = 0; i < imageWidth; i++) { + final int argb = tmpData[i]; + if (isGreyscale) { + rVal[index++] = (byte) (argb & 0xFF); + } else { + rVal[index++] = (byte) ((argb >> 16) & 0xFF); + rVal[index++] = (byte) ((argb >> 8) & 0xFF); + rVal[index++] = (byte) (argb & 0xFF); + if (hasAlpha) { + rVal[index++] = (byte) ((argb >> 24) & 0xFF); + } + } + } + } + return rVal; + } + + /** + * Extract rgb values from raster using the colormodel. + */ + private static int[] getRGB(final Raster raster, final ColorModel colorModel, final int startX, final int startY, + final int w, final int h, int[] rgbArray, final int offset, final int scansize) { + Object data; + final int nbands = raster.getNumBands(); + final int dataType = raster.getDataBuffer().getDataType(); + switch (dataType) { + case DataBuffer.TYPE_BYTE: + data = new byte[nbands]; + break; + case DataBuffer.TYPE_USHORT: + data = new short[nbands]; + break; + case DataBuffer.TYPE_INT: + data = new int[nbands]; + break; + case DataBuffer.TYPE_FLOAT: + data = new float[nbands]; + break; + case DataBuffer.TYPE_DOUBLE: + data = new double[nbands]; + break; + default: + throw new IllegalArgumentException("Unknown data buffer type: " + dataType); + } + + if (rgbArray == null) { + rgbArray = new int[offset + h * scansize]; + } + + int yoff = offset; + int off; + for (int y = startY; y < startY + h; y++, yoff += scansize) { + off = yoff; + for (int x = startX; x < startX + w; x++) { + rgbArray[off++] = colorModel.getRGB(raster.getDataElements(x, y, data)); + } + } + + return rgbArray; + } + + public static void setCreateOnHeap(final boolean createOnHeap) { + AWTImageLoader.createOnHeap = createOnHeap; + } + + public static boolean isCreateOnHeap() { + return createOnHeap; + } +} diff --git a/ardor3d-awt/src/test/java/com/ardor3d/input/awt/TestAwtKeyboardWrapper.java b/ardor3d-awt/src/test/java/com/ardor3d/input/awt/TestAwtKeyboardWrapper.java index a5d688d..c03137a 100644 --- a/ardor3d-awt/src/test/java/com/ardor3d/input/awt/TestAwtKeyboardWrapper.java +++ b/ardor3d-awt/src/test/java/com/ardor3d/input/awt/TestAwtKeyboardWrapper.java @@ -1,107 +1,107 @@ -/**
- * Copyright (c) 2008-2012 Ardor Labs, Inc.
- *
- * This file is part of Ardor3D.
- *
- * Ardor3D is free software: you can redistribute it and/or modify it
- * under the terms of its license which may be found in the accompanying
- * LICENSE file or at <http://www.ardor3d.com/LICENSE>.
- */
-
-package com.ardor3d.input.awt;
-
-import static org.easymock.classextension.EasyMock.createMock;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-
-import java.awt.Component;
-import java.util.Iterator;
-
-import org.junit.Before;
-import org.junit.Test;
-
-import com.ardor3d.input.Key;
-import com.ardor3d.input.KeyEvent;
-import com.ardor3d.input.KeyState;
-
-public class TestAwtKeyboardWrapper {
- AwtKeyboardWrapper kw;
-
- Component control;
-
- java.awt.event.KeyEvent e1, e2;
-
- @Before
- public void setup() throws Exception {
- control = createMock("component", Component.class);
-
- kw = new AwtKeyboardWrapper(control);
- }
-
- @Test
- public void testKeys1() throws Exception {
-
- e1 = new java.awt.event.KeyEvent(control, 0, 0, 0, AwtKey.A.getAwtCode(), 'a');
-
- kw.keyPressed(e1);
- kw.keyReleased(e1);
-
- final Iterator<KeyEvent> events = kw.getEvents();
-
- final KeyEvent event1 = events.next();
- final KeyEvent event2 = events.next();
-
- assertFalse("no more", events.hasNext());
- assertFalse("no more", kw.getEvents().hasNext());
-
- assertEquals("key a", Key.A, event1.getKey());
- assertEquals("down", KeyState.DOWN, event1.getState());
-
- assertEquals("key a", Key.A, event2.getKey());
- assertEquals("up", KeyState.UP, event2.getState());
- }
-
- @Test
- public void testKeys2() throws Exception {
- e1 = new java.awt.event.KeyEvent(control, 0, 0, 0, AwtKey.A.getAwtCode(), 'a');
- e2 = new java.awt.event.KeyEvent(control, 0, 0, 0, AwtKey.B.getAwtCode(), 'b');
-
- kw.keyPressed(e1);
- kw.keyPressed(e2);
-
- final Iterator<KeyEvent> events = kw.getEvents();
-
- final KeyEvent event1 = events.next();
- final KeyEvent event2 = events.next();
-
- assertFalse("no more", events.hasNext());
- assertFalse("no more", kw.getEvents().hasNext());
-
- assertEquals("key a", Key.A, event1.getKey());
- assertEquals("down", KeyState.DOWN, event1.getState());
-
- assertEquals("key b", Key.B, event2.getKey());
- assertEquals("down", KeyState.DOWN, event2.getState());
- }
-
- // @Test
- // public void testLostFocusKeyDown() throws Exception {
- //
- // e1 = new java.awt.event.KeyEvent(control, 0, 0, 0, AwtKey.AWT_KEY_A.getAwtCode(), 'a');
- //
- // kw.keyPressed(e1);
- //
- //
- // Iterator<KeyEvent> events = kw.getEvents();
- //
- // KeyEvent event1 = events.next();
- //
- // assertFalse("no more", events.hasNext());
- // assertFalse("no more", kw.getEvents().hasNext());
- //
- // assertEquals("key a", Key.A, event1.getKey());
- // assertEquals("down", KeyState.DOWN, event1.getState());
- // }
- //
- //
+/** + * Copyright (c) 2008-2012 Ardor Labs, Inc. + * + * This file is part of Ardor3D. + * + * Ardor3D is free software: you can redistribute it and/or modify it + * under the terms of its license which may be found in the accompanying + * LICENSE file or at <http://www.ardor3d.com/LICENSE>. + */ + +package com.ardor3d.input.awt; + +import static org.easymock.classextension.EasyMock.createMock; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; + +import java.awt.Component; +import java.util.Iterator; + +import org.junit.Before; +import org.junit.Test; + +import com.ardor3d.input.Key; +import com.ardor3d.input.KeyEvent; +import com.ardor3d.input.KeyState; + +public class TestAwtKeyboardWrapper { + AwtKeyboardWrapper kw; + + Component control; + + java.awt.event.KeyEvent e1, e2; + + @Before + public void setup() throws Exception { + control = createMock("component", Component.class); + + kw = new AwtKeyboardWrapper(control); + } + + @Test + public void testKeys1() throws Exception { + + e1 = new java.awt.event.KeyEvent(control, 0, 0, 0, AwtKey.A.getAwtCode(), 'a'); + + kw.keyPressed(e1); + kw.keyReleased(e1); + + final Iterator<KeyEvent> events = kw.getEvents(); + + final KeyEvent event1 = events.next(); + final KeyEvent event2 = events.next(); + + assertFalse("no more", events.hasNext()); + assertFalse("no more", kw.getEvents().hasNext()); + + assertEquals("key a", Key.A, event1.getKey()); + assertEquals("down", KeyState.DOWN, event1.getState()); + + assertEquals("key a", Key.A, event2.getKey()); + assertEquals("up", KeyState.UP, event2.getState()); + } + + @Test + public void testKeys2() throws Exception { + e1 = new java.awt.event.KeyEvent(control, 0, 0, 0, AwtKey.A.getAwtCode(), 'a'); + e2 = new java.awt.event.KeyEvent(control, 0, 0, 0, AwtKey.B.getAwtCode(), 'b'); + + kw.keyPressed(e1); + kw.keyPressed(e2); + + final Iterator<KeyEvent> events = kw.getEvents(); + + final KeyEvent event1 = events.next(); + final KeyEvent event2 = events.next(); + + assertFalse("no more", events.hasNext()); + assertFalse("no more", kw.getEvents().hasNext()); + + assertEquals("key a", Key.A, event1.getKey()); + assertEquals("down", KeyState.DOWN, event1.getState()); + + assertEquals("key b", Key.B, event2.getKey()); + assertEquals("down", KeyState.DOWN, event2.getState()); + } + + // @Test + // public void testLostFocusKeyDown() throws Exception { + // + // e1 = new java.awt.event.KeyEvent(control, 0, 0, 0, AwtKey.AWT_KEY_A.getAwtCode(), 'a'); + // + // kw.keyPressed(e1); + // + // + // Iterator<KeyEvent> events = kw.getEvents(); + // + // KeyEvent event1 = events.next(); + // + // assertFalse("no more", events.hasNext()); + // assertFalse("no more", kw.getEvents().hasNext()); + // + // assertEquals("key a", Key.A, event1.getKey()); + // assertEquals("down", KeyState.DOWN, event1.getState()); + // } + // + // }
\ No newline at end of file |