Skip to content

Commit

Permalink
Remove cyclic dependencies between ImageRasters
Browse files Browse the repository at this point in the history
Moved constructors in ImageRaster to DefaultImageRaster
  • Loading branch information
kristinfjola committed Apr 3, 2016
1 parent 9873a76 commit 0495b8b
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 84 deletions.
3 changes: 2 additions & 1 deletion common.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ configurations {
dependencies {
// Adding dependencies here will add the dependencies to each subproject.
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile group: 'org.mockito', name: 'mockito-core', version: '2.0.28-beta'
testCompile group: 'org.easytesting', name: 'fest-assert-core', version: '2.0M10'
testCompile group: 'org.mockito', name: 'mockito-core', version: '2.0.28-beta'
compile 'org.apache.commons:commons-lang3:3.0'
deployerJars "org.apache.maven.wagon:wagon-ssh:2.9"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,14 @@
import com.jme3.math.ColorRGBA;
import com.jme3.math.FastMath;
import com.jme3.texture.Image;
import org.fest.util.VisibleForTesting;

import java.nio.ByteBuffer;

public class DefaultImageRaster extends ImageRaster {

@VisibleForTesting
protected final int[] components = new int[4];
private ByteBuffer buffer;
@VisibleForTesting
protected final Image image;
@VisibleForTesting
protected final ImageCodec codec;
private final int width;
private final int height;
Expand All @@ -55,14 +51,45 @@ public class DefaultImageRaster extends ImageRaster {
private int slice;


@VisibleForTesting
protected void rangeCheck(int x, int y) {
if (x < 0 || y < 0 || x >= width || y >= height) {
throw new IllegalArgumentException("x and y must be inside the image dimensions:"
+ x + ", " + y + " in:" + width + ", " + height);
}
}


/**
* Create new image reader / writer.
*
* @param image The image to read / write to.
* @param slice Which slice to use. Only applies to 3D images, 2D image
* arrays or cubemaps.
* @param mipMapLevel The mipmap level to read / write to. To access levels
* other than 0, the image must have
* {@link Image#setMipMapSizes(int[]) mipmap sizes} set.
* @param convertToLinear If true, the application expects read or written
* colors to be in linear color space (<code>ImageRaster</code> will
* automatically perform a conversion as needed). If false, the application expects
* colors to be in the image's native {@link Image#getColorSpace() color space}.
* @return An ImageRaster to read / write to the image.
*/
public static DefaultImageRaster create(Image image, int slice, int mipMapLevel, boolean convertToLinear) {
return new DefaultImageRaster(image, slice, mipMapLevel, convertToLinear);
}

/**
* Create new image reader / writer for 2D images.
*
* @param image The image to read / write to.
* @return An ImageRaster to read / write to the image.
*/
public static DefaultImageRaster create(Image image) {
if (image.getData().size() > 1) {
throw new IllegalStateException("Use constructor that takes slices argument to read from multislice image");
}
return new DefaultImageRaster(image, 0, 0, false);
}

public DefaultImageRaster(Image image, int slice, int mipMapLevel, boolean convertToLinear) {
int[] mipMapSizes = image.getMipMapSizes();
int availableMips = mipMapSizes != null ? mipMapSizes.length : 1;
Expand Down Expand Up @@ -133,19 +160,16 @@ public void setPixel(int x, int y, ColorRGBA color) {
* Input is linear, needs to be converted to sRGB before writing into image.
* @param color
*/
@VisibleForTesting
protected void getSRGB(ColorRGBA color) {
if (convertToLinear) {
color = color.getAsSrgb();
}
}

@VisibleForTesting
protected void writeComponents(int x, int y) {
codec.writeComponents(getBuffer(), x, y, width, offset, components, temp);
}

@VisibleForTesting
protected void readComponents(int x, int y) {
codec.readComponents(getBuffer(), x, y, width, offset, components, temp);
}
Expand All @@ -171,7 +195,6 @@ public ColorRGBA getPixel(int x, int y, ColorRGBA store) {
return store;
}

@VisibleForTesting
protected void setSRGB(ColorRGBA store) {
if (convertToLinear) {
// Input image is sRGB, need to convert to linear.
Expand Down
50 changes: 0 additions & 50 deletions jme3-core/src/main/java/com/jme3/texture/image/ImageRaster.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import com.jme3.math.FastMath;
import com.jme3.system.JmeSystem;
import com.jme3.texture.Image;
import org.fest.util.VisibleForTesting;

/**
* Utility class for reading and writing from jME3 {@link Image images}.
Expand Down Expand Up @@ -67,51 +66,6 @@
*/
public abstract class ImageRaster {



/**
* Create new image reader / writer.
*
* @param image The image to read / write to.
* @param slice Which slice to use. Only applies to 3D images, 2D image
* arrays or cubemaps.
* @param mipMapLevel The mipmap level to read / write to. To access levels
* other than 0, the image must have
* {@link Image#setMipMapSizes(int[]) mipmap sizes} set.
* @param convertToLinear If true, the application expects read or written
* colors to be in linear color space (<code>ImageRaster</code> will
* automatically perform a conversion as needed). If false, the application expects
* colors to be in the image's native {@link Image#getColorSpace() color space}.
* @return An ImageRaster to read / write to the image.
*/
public static ImageRaster create(Image image, int slice, int mipMapLevel, boolean convertToLinear) {
return new DefaultImageRaster(image, slice, mipMapLevel, convertToLinear);
}

/**
* Create new image reader / writer.
*
* @param image The image to read / write to.
* @param slice Which slice to use. Only applies to 3D images, 2D image
* arrays or cubemaps.
* @return An ImageRaster to read / write to the image.
*/
public static ImageRaster create(Image image, int slice) {
return create(image, slice, 0, false);
}

/**
* Create new image reader / writer for 2D images.
*
* @param image The image to read / write to.
* @return An ImageRaster to read / write to the image.
*/
public static ImageRaster create(Image image) {
if (image.getData().size() > 1) {
throw new IllegalStateException("Use constructor that takes slices argument to read from multislice image");
}
return create(image, 0, 0, false);
}

public ImageRaster() {
}
Expand Down Expand Up @@ -210,15 +164,13 @@ public ColorRGBA getPixel(int x, int y) {
* Check flags for grayscale
* @param color
*/
@VisibleForTesting
protected void grayscaleCheck(ColorRGBA color, ImageCodec codec) {
if (codec.isGray) {
float gray = color.r * 0.27f + color.g * 0.67f + color.b * 0.06f;
color = new ColorRGBA(gray, gray, gray, color.a);
}
}

@VisibleForTesting
protected void setComponents(ColorRGBA color, ImageCodec codec, int[] components) {
switch (codec.type) {
case ImageCodec.FLAG_F16:
Expand Down Expand Up @@ -247,7 +199,6 @@ protected void setImageUpdateNeeded(Image image) {
image.setUpdateNeeded();
}

@VisibleForTesting
protected void setStoreComponents(ColorRGBA store, ImageCodec codec, int[] components) {
switch (codec.type) {
case ImageCodec.FLAG_F16:
Expand All @@ -272,7 +223,6 @@ protected void setStoreComponents(ColorRGBA store, ImageCodec codec, int[] compo
}
}

@VisibleForTesting
protected void setStoreRGBA(ColorRGBA store, ImageCodec codec) {
if (codec.isGray) {
store.g = store.b = store.r;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,14 @@
import com.jme3.math.ColorRGBA;
import com.jme3.math.FastMath;
import com.jme3.texture.Image;
import org.fest.util.VisibleForTesting;

import java.nio.ByteBuffer;

public class MipMapImageRaster extends ImageRaster {

@VisibleForTesting
protected final int[] components = new int[4];
private ByteBuffer buffer;
@VisibleForTesting
protected final Image image;
@VisibleForTesting
protected final ImageCodec codec;
private int width[];
private int height[];
Expand All @@ -54,7 +50,6 @@ public class MipMapImageRaster extends ImageRaster {
private int mipLevel;
private int[] offsets;

@VisibleForTesting
protected void rangeCheck(int x, int y) {
if (x < 0 || y < 0 || x >= width[mipLevel] || y >= height[mipLevel]) {
throw new IllegalArgumentException("x and y must be inside the image dimensions");
Expand Down Expand Up @@ -111,12 +106,10 @@ public void setPixel(int x, int y, ColorRGBA color) {
setImageUpdateNeeded(image);
}

@VisibleForTesting
protected void writeComponents(int x, int y) {
codec.writeComponents(getBuffer(), x, y, width[mipLevel], offsets[mipLevel], components, temp);
}

@VisibleForTesting
protected void readComponents(int x, int y) {
codec.readComponents(getBuffer(), x, y, width[mipLevel], offsets[mipLevel], components, temp);
}
Expand Down
7 changes: 4 additions & 3 deletions jme3-core/src/main/java/com/jme3/util/MipMapGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.jme3.math.FastMath;
import com.jme3.texture.Image;
import com.jme3.texture.Image.Format;
import com.jme3.texture.image.DefaultImageRaster;
import com.jme3.texture.image.ImageRaster;
import java.nio.ByteBuffer;
import java.util.ArrayList;
Expand All @@ -52,9 +53,9 @@ public static Image scaleImage(Image inputImage, int outputWidth, int outputHeig
outputHeight,
buffer,
inputImage.getColorSpace());
ImageRaster input = ImageRaster.create(inputImage, 0, 0, false);
ImageRaster output = ImageRaster.create(outputImage, 0, 0, false);

ImageRaster input = DefaultImageRaster.create(inputImage, 0, 0, false);
ImageRaster output = DefaultImageRaster.create(inputImage, 0, 0, false);

float xRatio = ((float)(input.getWidth() - 1)) / output.getWidth();
float yRatio = ((float)(input.getHeight() - 1)) / output.getHeight();
Expand Down
16 changes: 8 additions & 8 deletions jme3-core/src/main/resources/com/jme3/system/version.properties
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# THIS IS AN AUTO-GENERATED FILE..
# DO NOT MODIFY!
build.date=2016-03-21
git.revision=5389
git.branch=test-texture
git.hash=b99b43942ed5cf2b922c2268161efb413cc721d6
git.hash.short=b99b439
git.tag=v3.1-alpha1-231-gb99b439
name.full=jMonkeyEngine 3.1-test-texture-5389
version.full=3.1-test-texture-5389
build.date=2016-03-29
git.revision=5495
git.branch=master
git.hash=9873a76327519d60380864617c1c74f58802b99c
git.hash.short=9873a76
git.tag=null
name.full=jMonkeyEngine 3.1-5495
version.full=3.1-5495
version.number=3.1.0
version.tag=SNAPSHOT
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,21 @@

import com.jme3.math.ColorRGBA;
import com.jme3.texture.Image;
import com.jme3.util.BufferUtils;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

import org.junit.runner.RunWith;
import org.mockito.Matchers;
import org.mockito.Mockito;
import org.mockito.internal.matchers.apachecommons.ReflectionEquals;

import static org.mockito.Mockito.*;

/**
Expand Down Expand Up @@ -64,4 +76,36 @@ public void testGetPixel() {
verify(ir).setStoreRGBA(color, ir.codec);
verify(ir).setSRGB(color);
}

/**
* Tests that the create methods creates an actual instance with the correct arguments
* @throws Exception
*/
@Test
public void testCreate() throws Exception {
DefaultImageRaster expectedRaster1 = DefaultImageRaster.create(image);
DefaultImageRaster expectedRaster2 = DefaultImageRaster.create(image, 0, 0, true);
DefaultImageRaster actualRaster = new DefaultImageRaster(image, 0, 0, true);
DefaultImageRaster wrongRaster = new DefaultImageRaster(image, 1, 0, true);

Assert.assertTrue(EqualsBuilder.reflectionEquals(expectedRaster1, actualRaster));
Assert.assertFalse(EqualsBuilder.reflectionEquals(expectedRaster1, wrongRaster));

Assert.assertTrue(EqualsBuilder.reflectionEquals(expectedRaster2, actualRaster));
Assert.assertFalse(EqualsBuilder.reflectionEquals(expectedRaster2, wrongRaster));
}

/**
* Test that the create method throws an error when a slice should be specified
*/
@Test(expected = IllegalStateException.class)
public void testInvalidCreate() {
ArrayList<ByteBuffer> byteBuffers = new ArrayList<ByteBuffer>(2);
byteBuffers.add(BufferUtils.createByteBuffer(1));
byteBuffers.add(BufferUtils.createByteBuffer(1));
image.setData(byteBuffers);

assert(image.getData().size() == 2);
DefaultImageRaster actualRaster = DefaultImageRaster.create(image);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.jme3.texture.Texture.MinFilter;
import com.jme3.texture.TextureDefault2D;
import com.jme3.texture.image.ColorSpace;
import com.jme3.texture.image.DefaultImageRaster;
import com.jme3.texture.image.ImageRaster;
import com.jme3.util.BufferUtils;
import java.nio.ByteBuffer;
Expand All @@ -30,8 +31,8 @@ private Image convertImage(Image image, Format newFormat) {
ByteBuffer data = BufferUtils.createByteBuffer( (int)Math.ceil(newFormat.getBitsPerPixel() / 8.0) * width * height);
Image convertedImage = new Image(newFormat, width, height, data,null, image.getColorSpace());

ImageRaster sourceReader = ImageRaster.create(image);
ImageRaster targetWriter = ImageRaster.create(convertedImage);
ImageRaster sourceReader = DefaultImageRaster.create(image);
ImageRaster targetWriter = DefaultImageRaster.create(convertedImage);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
ColorRGBA color = sourceReader.getPixel(x, y);
Expand Down Expand Up @@ -69,7 +70,7 @@ private void convertAndPutImage(Image image, float posX, float posY) {
private Image createTestImage() {
Image testImage = new Image(Format.BGR8, 4, 3, BufferUtils.createByteBuffer(4 * 4 * 3), null, ColorSpace.Linear);

ImageRaster io = ImageRaster.create(testImage);
ImageRaster io = DefaultImageRaster.create(testImage);
io.setPixel(0, 0, ColorRGBA.Black);
io.setPixel(1, 0, ColorRGBA.Gray);
io.setPixel(2, 0, ColorRGBA.White);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import com.jme3.math.ColorRGBA;
import com.jme3.texture.Image;
import com.jme3.texture.image.DefaultImageRaster;
import com.jme3.texture.image.ImageRaster;

/**
Expand Down Expand Up @@ -94,9 +95,9 @@ protected float calculateHeight(ColorRGBA color) {
}

protected ImageRaster getImageRaster() {
return ImageRaster.create(colorImage);
return DefaultImageRaster.create(colorImage);
}

public boolean load(boolean flipX, boolean flipY) {

int imageWidth = colorImage.getWidth();
Expand Down
Loading

0 comments on commit 0495b8b

Please sign in to comment.