#261 Code clean up.

This commit is contained in:
Harald Kuhr 2016-09-28 12:13:35 +02:00
parent 5f12c88609
commit 288ad54c42
2 changed files with 90 additions and 74 deletions

View File

@ -44,7 +44,7 @@ import java.awt.image.*;
*/ */
public class AffineTransformOp implements BufferedImageOp, RasterOp { public class AffineTransformOp implements BufferedImageOp, RasterOp {
java.awt.image.AffineTransformOp delegate; final java.awt.image.AffineTransformOp delegate;
public static final int TYPE_NEAREST_NEIGHBOR = java.awt.image.AffineTransformOp.TYPE_NEAREST_NEIGHBOR; public static final int TYPE_NEAREST_NEIGHBOR = java.awt.image.AffineTransformOp.TYPE_NEAREST_NEIGHBOR;
@ -56,8 +56,7 @@ public class AffineTransformOp implements BufferedImageOp, RasterOp {
* @param xform The {@link AffineTransform} to use for the operation. * @param xform The {@link AffineTransform} to use for the operation.
* @param hints The {@link RenderingHints} object used to specify the interpolation type for the operation. * @param hints The {@link RenderingHints} object used to specify the interpolation type for the operation.
*/ */
public AffineTransformOp(AffineTransform xform, public AffineTransformOp(final AffineTransform xform, final RenderingHints hints) {
RenderingHints hints) {
delegate = new java.awt.image.AffineTransformOp(xform, hints); delegate = new java.awt.image.AffineTransformOp(xform, hints);
} }
@ -65,14 +64,12 @@ public class AffineTransformOp implements BufferedImageOp, RasterOp {
* @param xform The {@link AffineTransform} to use for the operation. * @param xform The {@link AffineTransform} to use for the operation.
* @param interpolationType One of the integer interpolation type constants defined by this class: {@link #TYPE_NEAREST_NEIGHBOR}, {@link #TYPE_BILINEAR}, {@link #TYPE_BICUBIC}. * @param interpolationType One of the integer interpolation type constants defined by this class: {@link #TYPE_NEAREST_NEIGHBOR}, {@link #TYPE_BILINEAR}, {@link #TYPE_BICUBIC}.
*/ */
public AffineTransformOp(AffineTransform xform, public AffineTransformOp(final AffineTransform xform, final int interpolationType) {
int interpolationType) {
delegate = new java.awt.image.AffineTransformOp(xform, interpolationType); delegate = new java.awt.image.AffineTransformOp(xform, interpolationType);
} }
@Override @Override
public BufferedImage filter(BufferedImage src, public BufferedImage filter(final BufferedImage src, BufferedImage dst) {
BufferedImage dst) {
try { try {
return delegate.filter(src, dst); return delegate.filter(src, dst);
} }
@ -80,12 +77,16 @@ public class AffineTransformOp implements BufferedImageOp, RasterOp {
if (dst == null) { if (dst == null) {
dst = createCompatibleDestImage(src, src.getColorModel()); dst = createCompatibleDestImage(src, src.getColorModel());
} }
Graphics2D g2d = null; Graphics2D g2d = null;
try { try {
g2d = dst.createGraphics(); g2d = dst.createGraphics();
int interpolationType = delegate.getInterpolationType(); int interpolationType = delegate.getInterpolationType();
if (interpolationType > 0) { if (interpolationType > 0) {
Object interpolationValue = RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR; Object interpolationValue = RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR;
switch (interpolationType) { switch (interpolationType) {
case java.awt.image.AffineTransformOp.TYPE_BILINEAR: case java.awt.image.AffineTransformOp.TYPE_BILINEAR:
interpolationValue = RenderingHints.VALUE_INTERPOLATION_BILINEAR; interpolationValue = RenderingHints.VALUE_INTERPOLATION_BILINEAR;
@ -94,12 +95,15 @@ public class AffineTransformOp implements BufferedImageOp, RasterOp {
interpolationValue = RenderingHints.VALUE_INTERPOLATION_BICUBIC; interpolationValue = RenderingHints.VALUE_INTERPOLATION_BICUBIC;
break; break;
} }
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, interpolationValue); g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, interpolationValue);
} }
else if (getRenderingHints() != null) { else if (getRenderingHints() != null) {
g2d.setRenderingHints(getRenderingHints()); g2d.setRenderingHints(getRenderingHints());
} }
g2d.drawImage(src, delegate.getTransform(), null); g2d.drawImage(src, delegate.getTransform(), null);
return dst; return dst;
} }
finally { finally {
@ -111,32 +115,32 @@ public class AffineTransformOp implements BufferedImageOp, RasterOp {
} }
@Override @Override
public Rectangle2D getBounds2D(BufferedImage src) { public Rectangle2D getBounds2D(final BufferedImage src) {
return delegate.getBounds2D(src); return delegate.getBounds2D(src);
} }
@Override @Override
public BufferedImage createCompatibleDestImage(BufferedImage src, ColorModel destCM) { public BufferedImage createCompatibleDestImage(final BufferedImage src, final ColorModel destCM) {
return delegate.createCompatibleDestImage(src, destCM); return delegate.createCompatibleDestImage(src, destCM);
} }
@Override @Override
public WritableRaster filter(Raster src, WritableRaster dest) { public WritableRaster filter(final Raster src, final WritableRaster dest) {
return delegate.filter(src, dest); return delegate.filter(src, dest);
} }
@Override @Override
public Rectangle2D getBounds2D(Raster src) { public Rectangle2D getBounds2D(final Raster src) {
return delegate.getBounds2D(src); return delegate.getBounds2D(src);
} }
@Override @Override
public WritableRaster createCompatibleDestRaster(Raster src) { public WritableRaster createCompatibleDestRaster(final Raster src) {
return delegate.createCompatibleDestRaster(src); return delegate.createCompatibleDestRaster(src);
} }
@Override @Override
public Point2D getPoint2D(Point2D srcPt, Point2D dstPt) { public Point2D getPoint2D(final Point2D srcPt, final Point2D dstPt) {
return delegate.getPoint2D(srcPt, dstPt); return delegate.getPoint2D(srcPt, dstPt);
} }

View File

@ -12,6 +12,7 @@ import java.util.List;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
/** /**
* AffineTransformOpTest. * AffineTransformOpTest.
@ -70,8 +71,8 @@ public class AffineTransformOpTest {
ImageTypeSpecifier.createInterleaved(S_RGB, new int[] {0, 1, 2, 3}, DataBuffer.TYPE_DOUBLE, true, false) ImageTypeSpecifier.createInterleaved(S_RGB, new int[] {0, 1, 2, 3}, DataBuffer.TYPE_DOUBLE, true, false)
); );
final int width = 30; private final int width = 30;
final int height = 20; private final int height = 20;
@Test @Test
public void testGetPoint2D() { public void testGetPoint2D() {
@ -99,59 +100,64 @@ public class AffineTransformOpTest {
@Test @Test
public void testFilterRotateBIStandard() { public void testFilterRotateBIStandard() {
BufferedImageOp op_jre = new java.awt.image.AffineTransformOp(AffineTransform.getQuadrantRotateInstance(1, Math.min(width, height) / 2, Math.min(width, height) / 2), null); BufferedImageOp jreOp = new java.awt.image.AffineTransformOp(AffineTransform.getQuadrantRotateInstance(1, Math.min(width, height) / 2, Math.min(width, height) / 2), null);
BufferedImageOp op_tm = new com.twelvemonkeys.image.AffineTransformOp(AffineTransform.getQuadrantRotateInstance(1, Math.min(width, height) / 2, Math.min(width, height) / 2), null); BufferedImageOp tmOp = new com.twelvemonkeys.image.AffineTransformOp(AffineTransform.getQuadrantRotateInstance(1, Math.min(width, height) / 2, Math.min(width, height) / 2), null);
for (Integer type : TYPES) { for (Integer type : TYPES) {
BufferedImage image = new BufferedImage(width, height, type); BufferedImage image = new BufferedImage(width, height, type);
BufferedImage result_jre = op_jre.filter(image, null); BufferedImage jreResult = jreOp.filter(image, null);
BufferedImage result_tm = op_tm.filter(image, null); BufferedImage tmResult = tmOp.filter(image, null);
assertNotNull("No result!", result_tm); assertNotNull("No result!", tmResult);
assertEquals("Bad type", result_jre.getType(), result_tm.getType()); assertEquals("Bad type", jreResult.getType(), tmResult.getType());
assertEquals("Incorrect color model", result_jre.getColorModel(), result_tm.getColorModel()); assertEquals("Incorrect color model", jreResult.getColorModel(), tmResult.getColorModel());
assertEquals("Incorrect width", result_jre.getWidth(), result_tm.getWidth()); assertEquals("Incorrect width", jreResult.getWidth(), tmResult.getWidth());
assertEquals("Incorrect height", result_jre.getHeight(), result_tm.getHeight()); assertEquals("Incorrect height", jreResult.getHeight(), tmResult.getHeight());
} }
} }
@Test @Test
public void testFilterRotateBICustom() { public void testFilterRotateBICustom() {
BufferedImageOp op_jre = new java.awt.image.AffineTransformOp(AffineTransform.getQuadrantRotateInstance(1, Math.min(width, height) / 2, Math.min(width, height) / 2), null); BufferedImageOp jreOp = new java.awt.image.AffineTransformOp(AffineTransform.getQuadrantRotateInstance(1, Math.min(width, height) / 2, Math.min(width, height) / 2), null);
BufferedImageOp op_tm = new com.twelvemonkeys.image.AffineTransformOp(AffineTransform.getQuadrantRotateInstance(1, Math.min(width, height) / 2, Math.min(width, height) / 2), null); BufferedImageOp tmOp = new com.twelvemonkeys.image.AffineTransformOp(AffineTransform.getQuadrantRotateInstance(1, Math.min(width, height) / 2, Math.min(width, height) / 2), null);
for (ImageTypeSpecifier spec : SPECS) { for (ImageTypeSpecifier spec : SPECS) {
BufferedImage image = spec.createBufferedImage(width, height); BufferedImage image = spec.createBufferedImage(width, height);
BufferedImage result_tm = op_tm.filter(image, null); BufferedImage tmResult = tmOp.filter(image, null);
assertNotNull("No result!", result_tm); assertNotNull("No result!", tmResult);
BufferedImage jreResult = null;
BufferedImage result_jre;
try { try {
result_jre = op_jre.filter(image, null); jreResult = jreOp.filter(image, null);
}
assertEquals("Bad type", result_jre.getType(), result_tm.getType()); catch (ImagingOpException ignore) {
assertEquals("Incorrect color model", result_jre.getColorModel(), result_tm.getColorModel()); // We expect this to fail for certain cases, that's why we crated the class in the first place
assertEquals("Incorrect width", result_jre.getWidth(), result_tm.getWidth());
assertEquals("Incorrect height", result_jre.getHeight(), result_tm.getHeight());
} }
catch (ImagingOpException e) {
System.err.println("spec: " + spec);
assertEquals("Bad type", spec.getBufferedImageType(), result_tm.getType());
assertEquals("Incorrect color model", spec.getColorModel(), result_tm.getColorModel());
assertEquals("Incorrect width", height, result_tm.getWidth()); if (jreResult != null) {
assertEquals("Incorrect height", width, result_tm.getHeight()); assertEquals("Bad type", jreResult.getType(), tmResult.getType());
assertEquals("Incorrect color model", jreResult.getColorModel(), tmResult.getColorModel());
assertEquals("Incorrect width", jreResult.getWidth(), tmResult.getWidth());
assertEquals("Incorrect height", jreResult.getHeight(), tmResult.getHeight());
}
else {
assertEquals("Bad type", spec.getBufferedImageType(), tmResult.getType());
assertEquals("Incorrect color model", spec.getColorModel(), tmResult.getColorModel());
assertEquals("Incorrect width", height, tmResult.getWidth());
assertEquals("Incorrect height", width, tmResult.getHeight());
} }
} }
} }
// TODO: Test RasterOp variants of filter too // Test RasterOp variants
@Test @Test
public void testRasterGetBounds2D() { public void testGetBounds2DRaster() {
AffineTransform shearInstance = AffineTransform.getShearInstance(33.77, 77.33); AffineTransform shearInstance = AffineTransform.getShearInstance(33.77, 77.33);
RasterOp original = new java.awt.image.AffineTransformOp(shearInstance, null); RasterOp original = new java.awt.image.AffineTransformOp(shearInstance, null);
RasterOp fallback = new com.twelvemonkeys.image.AffineTransformOp(shearInstance, null); RasterOp fallback = new com.twelvemonkeys.image.AffineTransformOp(shearInstance, null);
@ -162,81 +168,87 @@ public class AffineTransformOpTest {
} }
@Test @Test
public void testRasterRotateBIStandard() { public void testFilterRotateRasterStandard() {
RasterOp op_jre = new java.awt.image.AffineTransformOp(AffineTransform.getQuadrantRotateInstance(1, Math.min(width, height) / 2, Math.min(width, height) / 2), null); RasterOp jreOp = new java.awt.image.AffineTransformOp(AffineTransform.getQuadrantRotateInstance(1, Math.min(width, height) / 2, Math.min(width, height) / 2), null);
RasterOp op_tm = new com.twelvemonkeys.image.AffineTransformOp(AffineTransform.getQuadrantRotateInstance(1, Math.min(width, height) / 2, Math.min(width, height) / 2), null); RasterOp tmOp = new com.twelvemonkeys.image.AffineTransformOp(AffineTransform.getQuadrantRotateInstance(1, Math.min(width, height) / 2, Math.min(width, height) / 2), null);
for (Integer type : TYPES) { for (Integer type : TYPES) {
Raster raster = new BufferedImage(width, height, type).getRaster(); Raster raster = new BufferedImage(width, height, type).getRaster();
Raster result_jre = null, result_tm = null; Raster jreResult = null;
Raster tmResult = null;
try { try {
result_jre = op_jre.filter(raster, null); jreResult = jreOp.filter(raster, null);
} }
catch (ImagingOpException e) { catch (ImagingOpException ignore) {
System.err.println("type: " + type); // We expect this to fail for certain cases, that's why we crated the class in the first place
} }
try { try {
result_tm = op_tm.filter(raster, null); tmResult = tmOp.filter(raster, null);
} }
catch (ImagingOpException e) { catch (ImagingOpException e) {
// Only fail if JRE AffineOp produces a result and our version not // Only fail if JRE AffineOp produces a result and our version not
if (result_jre != null) { if (jreResult != null) {
assertNotNull("No result!", result_tm); fail("No result!");
} }
else { else {
System.err.println("AffineTransformOpTest.testFilterRotateRasterStandard");
System.err.println("type: " + type);
continue; continue;
} }
} }
if (result_jre != null) { if (jreResult != null) {
assertEquals("Incorrect width", result_jre.getWidth(), result_tm.getWidth()); assertEquals("Incorrect width", jreResult.getWidth(), tmResult.getWidth());
assertEquals("Incorrect height", result_jre.getHeight(), result_tm.getHeight()); assertEquals("Incorrect height", jreResult.getHeight(), tmResult.getHeight());
} }
else { else {
assertEquals("Incorrect width", height, result_tm.getWidth()); assertEquals("Incorrect width", height, tmResult.getWidth());
assertEquals("Incorrect height", width, result_tm.getHeight()); assertEquals("Incorrect height", width, tmResult.getHeight());
} }
} }
} }
@Test @Test
public void testRasterRotateBICustom() { public void testFilterRotateRasterCustom() {
RasterOp op_jre = new java.awt.image.AffineTransformOp(AffineTransform.getQuadrantRotateInstance(1, Math.min(width, height) / 2, Math.min(width, height) / 2), null); RasterOp jreOp = new java.awt.image.AffineTransformOp(AffineTransform.getQuadrantRotateInstance(1, Math.min(width, height) / 2, Math.min(width, height) / 2), null);
RasterOp op_tm = new com.twelvemonkeys.image.AffineTransformOp(AffineTransform.getQuadrantRotateInstance(1, Math.min(width, height) / 2, Math.min(width, height) / 2), null); RasterOp tmOp = new com.twelvemonkeys.image.AffineTransformOp(AffineTransform.getQuadrantRotateInstance(1, Math.min(width, height) / 2, Math.min(width, height) / 2), null);
for (ImageTypeSpecifier spec : SPECS) { for (ImageTypeSpecifier spec : SPECS) {
Raster raster = spec.createBufferedImage(width, height).getRaster(); Raster raster = spec.createBufferedImage(width, height).getRaster();
Raster result_jre = null, result_tm = null; Raster jreResult = null;
Raster tmResult = null;
try { try {
result_jre = op_jre.filter(raster, null); jreResult = jreOp.filter(raster, null);
} }
catch (ImagingOpException e) { catch (ImagingOpException ignore) {
System.err.println("spec: " + spec); // We expect this to fail for certain cases, that's why we crated the class in the first place
} }
try { try {
result_tm = op_tm.filter(raster, null); tmResult = tmOp.filter(raster, null);
} }
catch (ImagingOpException e) { catch (ImagingOpException e) {
// Only fail if JRE AffineOp produces a result and our version not // Only fail if JRE AffineOp produces a result and our version not
if (result_jre != null) { if (jreResult != null) {
assertNotNull("No result!", result_tm); fail("No result!");
} }
else { else {
System.err.println("AffineTransformOpTest.testFilterRotateRasterCustom");
System.err.println("spec: " + spec);
continue; continue;
} }
} }
if (result_jre != null) { if (jreResult != null) {
assertEquals("Incorrect width", result_jre.getWidth(), result_tm.getWidth()); assertEquals("Incorrect width", jreResult.getWidth(), tmResult.getWidth());
assertEquals("Incorrect height", result_jre.getHeight(), result_tm.getHeight()); assertEquals("Incorrect height", jreResult.getHeight(), tmResult.getHeight());
} }
else { else {
assertEquals("Incorrect width", height, result_tm.getWidth()); assertEquals("Incorrect width", height, tmResult.getWidth());
assertEquals("Incorrect height", width, result_tm.getHeight()); assertEquals("Incorrect height", width, tmResult.getHeight());
} }
} }
} }