Skip to content

Commit

Permalink
Tests for ImageRaster and refactorings for tests
Browse files Browse the repository at this point in the history
Both DefaultImageRaster and MipMapImageRaster were refactored in order to be able to create tests for each class
  • Loading branch information
kristinfjola authored and JoopAue committed Mar 24, 2016
1 parent ec47807 commit cfcf6a1
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public int getHeight() {
@Override
public void setPixel(int x, int y, ColorRGBA color) {
rangeCheck(x, y);
convertLinearCheck(color);
getSRGB(color);
grayscaleCheck(color);
setComponents(color);
writeComponents(x, y);
Expand All @@ -131,7 +131,7 @@ public void setPixel(int x, int y, ColorRGBA color) {
* @param color
*/
@VisibleForTesting
protected void convertLinearCheck(ColorRGBA color) {
protected void getSRGB(ColorRGBA color) {
if (convertToLinear) {
color = color.getAsSrgb();
}
Expand Down Expand Up @@ -178,6 +178,11 @@ protected void setComponents(ColorRGBA color) {
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);
}

private ByteBuffer getBuffer(){
if(buffer == null){
Expand All @@ -189,32 +194,45 @@ private ByteBuffer getBuffer(){
@Override
public ColorRGBA getPixel(int x, int y, ColorRGBA store) {
rangeCheck(x, y);

codec.readComponents(getBuffer(), x, y, width, offset, components, temp);
readComponents(x, y);
if (store == null) {
store = new ColorRGBA();
}
setStoreComponents(store);
setStoreRGBA(store);
setSRGB(store);

return store;
}


@VisibleForTesting
protected void setStoreComponents(ColorRGBA store) {
switch (codec.type) {
case ImageCodec.FLAG_F16:
store.set(FastMath.convertHalfToFloat((short)components[1]),
FastMath.convertHalfToFloat((short)components[2]),
FastMath.convertHalfToFloat((short)components[3]),
FastMath.convertHalfToFloat((short)components[0]));
FastMath.convertHalfToFloat((short)components[2]),
FastMath.convertHalfToFloat((short)components[3]),
FastMath.convertHalfToFloat((short)components[0]));
break;
case ImageCodec.FLAG_F32:
store.set(Float.intBitsToFloat((int)components[1]),
Float.intBitsToFloat((int)components[2]),
Float.intBitsToFloat((int)components[3]),
Float.intBitsToFloat((int)components[0]));
Float.intBitsToFloat((int)components[2]),
Float.intBitsToFloat((int)components[3]),
Float.intBitsToFloat((int)components[0]));
break;
case 0:
// Convert to float and divide by bitsize to get into range 0.0 - 1.0.
store.set((float)components[1] / codec.maxRed,
(float)components[2] / codec.maxGreen,
(float)components[3] / codec.maxBlue,
(float)components[0] / codec.maxAlpha);
(float)components[2] / codec.maxGreen,
(float)components[3] / codec.maxBlue,
(float)components[0] / codec.maxAlpha);
break;
}
}

@VisibleForTesting
protected void setStoreRGBA(ColorRGBA store) {
if (codec.isGray) {
store.g = store.b = store.r;
} else {
Expand All @@ -231,12 +249,13 @@ public ColorRGBA getPixel(int x, int y, ColorRGBA store) {
store.a = 1;
}
}

}

@VisibleForTesting
protected void setSRGB(ColorRGBA store) {
if (convertToLinear) {
// Input image is sRGB, need to convert to linear.
store.setAsSrgb(store.r, store.g, store.b, store.a);
}

return store;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
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 {
Expand All @@ -49,7 +51,8 @@ public class MipMapImageRaster extends ImageRaster {
private int mipLevel;
private int[] offsets;

private void rangeCheck(int x, int y) {
@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 @@ -99,13 +102,26 @@ public void setMipLevel(int mipLevel) {
@Override
public void setPixel(int x, int y, ColorRGBA color) {
rangeCheck(x, y);
grayscaleCheck(color);
setComponents(color);
writeComponents(x, y);
image.setUpdateNeeded();
}

// Check flags for grayscale
/**
* Check flags for grayscale
* @param color
*/
@VisibleForTesting
protected void grayscaleCheck(ColorRGBA color) {
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) {
switch (codec.type) {
case ImageCodec.FLAG_F16:
components[0] = (int) FastMath.convertFloatToHalf(color.a);
Expand All @@ -127,8 +143,16 @@ public void setPixel(int x, int y, ColorRGBA color) {
components[3] = Math.min((int) (color.b * codec.maxBlue + 0.5f), codec.maxBlue);
break;
}
}

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

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

private ByteBuffer getBuffer() {
Expand All @@ -141,11 +165,17 @@ private ByteBuffer getBuffer() {
@Override
public ColorRGBA getPixel(int x, int y, ColorRGBA store) {
rangeCheck(x, y);

codec.readComponents(getBuffer(), x, y, width[mipLevel], offsets[mipLevel], components, temp);
readComponents(x, y);
if (store == null) {
store = new ColorRGBA();
}
setStoreComponents(store);
setStoreRGBA(store);
return store;
}

@VisibleForTesting
protected void setStoreComponents(ColorRGBA store) {
switch (codec.type) {
case ImageCodec.FLAG_F16:
store.set(FastMath.convertHalfToFloat((short) components[1]),
Expand All @@ -167,6 +197,10 @@ public ColorRGBA getPixel(int x, int y, ColorRGBA store) {
(float) components[0] / codec.maxAlpha);
break;
}
}

@VisibleForTesting
protected void setStoreRGBA(ColorRGBA store) {
if (codec.isGray) {
store.g = store.b = store.r;
} else {
Expand All @@ -183,7 +217,6 @@ public ColorRGBA getPixel(int x, int y, ColorRGBA store) {
store.a = 1;
}
}
return store;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import org.junit.Before;
import org.junit.Test;

import java.nio.ByteBuffer;

import static org.mockito.Mockito.*;

/**
Expand All @@ -15,12 +13,14 @@
public class DefaultImageRasterTest {

DefaultImageRaster ir;
Image image;

@Before
public void setUp() {
Image image = createImage();
image = createImage();
ir = spy(new DefaultImageRaster(image, 0 ,0, false));
doNothing().when(ir).writeComponents(any(Integer.class), any(Integer.class));
doNothing().when(ir).readComponents(any(Integer.class), any(Integer.class));
}

public Image createImage() {
Expand All @@ -39,8 +39,24 @@ public void testSetPixel() {
ir.setPixel(x, y, color);

verify(ir).rangeCheck(x, y);
verify(ir).convertLinearCheck(color);
verify(ir).getSRGB(color);
verify(ir).grayscaleCheck(color);
verify(ir).setComponents(color);
verify(ir).writeComponents(x, y);
verify(image).setUpdateNeeded();
}

@Test
public void testGetPixel() {
int x = 0;
int y = 0;
ColorRGBA color = ColorRGBA.Black;
ColorRGBA pixelColor = ir.getPixel(x, y, color);

verify(ir).rangeCheck(x, y);
verify(ir).readComponents(x, y);
verify(ir).setStoreComponents(color);
verify(ir).setStoreRGBA(color);
verify(ir).setSRGB(color);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.jme3.texture.image;

import com.jme3.math.ColorRGBA;
import com.jme3.texture.Image;
import org.junit.Before;
import org.junit.Test;

import static org.mockito.Matchers.any;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.verify;

/**
* Created by kristinfjola on 21/03/16.
*/
public class MipMapImageRasterTest {

MipMapImageRaster ir;
Image image;

@Before
public void setUp() {
image = createImage();
ir = spy(new MipMapImageRaster(image, 0));
doNothing().when(ir).writeComponents(any(Integer.class), any(Integer.class));
doNothing().when(ir).readComponents(any(Integer.class), any(Integer.class));
}

public Image createImage() {
Image image = spy(new Image());
when(image.getFormat()).thenReturn(Image.Format.BGR8);
when(image.getWidth()).thenReturn(1);
when(image.getHeight()).thenReturn(1);
when(image.hasMipmaps()).thenReturn(true);
when(image.getMipMapSizes()).thenReturn(new int[5]);
return image;
}

@Test
public void testSetPixel() {
int x = 0;
int y = 0;
ColorRGBA color = ColorRGBA.Black;
ir.setPixel(x, y, color);

verify(ir).rangeCheck(x, y);
verify(ir).grayscaleCheck(color);
verify(ir).setComponents(color);
verify(ir).writeComponents(x, y);
verify(image).setUpdateNeeded();
}

@Test
public void testGetPixel() {
int x = 0;
int y = 0;
ColorRGBA color = ColorRGBA.Black;
ColorRGBA pixelColor = ir.getPixel(x, y, color);

verify(ir).rangeCheck(x, y);
verify(ir).readComponents(x, y);
verify(ir).setStoreComponents(color);
verify(ir).setStoreRGBA(color);
}
}

0 comments on commit cfcf6a1

Please sign in to comment.