Permalink
Browse files

GraphicsUtils.getAlphaImage() returns white image for input without a…

…lpha channel.
  • Loading branch information...
eseifert committed Aug 19, 2017
1 parent 28a15ae commit 59e97f7143d52c5507f77780da8a912d0f023ede
@@ -21,6 +21,7 @@
*/
package de.erichseifert.vectorgraphics2d.util;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
@@ -35,7 +36,15 @@
import java.awt.color.ColorSpace;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.*;
import java.awt.geom.Arc2D;
import java.awt.geom.CubicCurve2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Path2D;
import java.awt.geom.PathIterator;
import java.awt.geom.QuadCurve2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.ComponentColorModel;
@@ -50,7 +59,6 @@
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Set;
import javax.swing.ImageIcon;
/**
@@ -353,6 +361,14 @@ public static BufferedImage getAlphaImage(BufferedImage image) {
int width = image.getWidth();
int height = image.getHeight();
if (alphaRaster == null) {
BufferedImage opaqueAlphaImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
Graphics g = opaqueAlphaImage.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
return opaqueAlphaImage;
}
ColorModel cm;
WritableRaster raster;
// TODO Handle bitmap masks (work on ImageDataStream is necessary)
@@ -51,6 +51,7 @@
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.DataBuffer;
import java.awt.image.FilteredImageSource;
import java.awt.image.RGBImageFilter;
import java.awt.image.Raster;
@@ -66,18 +67,31 @@
private void assertBufferedImageEquals(Image expected, BufferedImage actual) {
assertNotNull(actual);
assertEquals(BufferedImage.class, actual.getClass());
assertEquals(expected.getWidth(null), actual.getWidth());
assertEquals(expected.getHeight(null), actual.getHeight());
}
private void assertBufferedImageEquals(RenderedImage expected, BufferedImage actual) {
assertNotNull(actual);
assertEquals(BufferedImage.class, actual.getClass());
assertEquals(expected.getWidth(), actual.getWidth());
assertEquals(expected.getHeight(), actual.getHeight());
}
private void assertBufferedImageContentEquals(BufferedImage expected, BufferedImage actual) {
DataBuffer expectedData = expected.getData().getDataBuffer();
DataBuffer actualData = actual.getData().getDataBuffer();
int expectedDataSize = expectedData.getSize();
int actualDataSize = actualData.getSize();
assertEquals(expectedDataSize, actualDataSize);
for (int i = 0; i < actualDataSize; i++) {
assertEquals(String.format("Mismatch at pixel %d", i),
actualData.getElem(i), expectedData.getElem(i));
}
}
@Test
public void toBufferedImageCanConvertImageWithAlphaChannel() {
Image image = new BufferedImage(320, 240, BufferedImage.TYPE_INT_ARGB);
@@ -395,4 +409,17 @@ public void usesAlphaReturnsFalseForImageWithoutTransparentPixels() {
assertFalse(result);
}
@Test
public void getAlphaImageReturnsWhiteImageForInputWithoutAlphaChannel() {
BufferedImage image = new BufferedImage(3, 3, BufferedImage.TYPE_INT_RGB);
BufferedImage result = GraphicsUtils.getAlphaImage(image);
BufferedImage expected = new BufferedImage(3, 3, BufferedImage.TYPE_BYTE_GRAY);
Graphics g = expected.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, expected.getWidth(), expected.getHeight());
assertBufferedImageContentEquals(expected, result);
}
}

0 comments on commit 59e97f7

Please sign in to comment.