From e883c1e6780a81c8501418c0db450793581a4dbd Mon Sep 17 00:00:00 2001 From: bgamard Date: Thu, 23 Nov 2017 15:40:53 +0100 Subject: [PATCH] Closes #171: strip alpha channel from png --- .../src/main/java/com/sismics/util/ImageUtil.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/docs-core/src/main/java/com/sismics/util/ImageUtil.java b/docs-core/src/main/java/com/sismics/util/ImageUtil.java index 21c2a56a..edc4efa2 100644 --- a/docs-core/src/main/java/com/sismics/util/ImageUtil.java +++ b/docs-core/src/main/java/com/sismics/util/ImageUtil.java @@ -9,6 +9,7 @@ import javax.imageio.ImageIO; import javax.imageio.ImageWriteParam; import javax.imageio.ImageWriter; import javax.imageio.stream.ImageOutputStream; +import java.awt.*; import java.awt.image.BufferedImage; import java.awt.image.WritableRaster; import java.io.IOException; @@ -40,6 +41,17 @@ public class ImageUtil { iwp.setCompressionQuality(1.f); imageOutputStream = ImageIO.createImageOutputStream(outputStream); writer.setOutput(imageOutputStream); + + if (image.getColorModel().hasAlpha()) { + // Strip alpha channel + BufferedImage noAlphaImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB); + Graphics graphics = noAlphaImage.getGraphics(); + graphics.setColor(Color.WHITE); + graphics.fillRect(0, 0, image.getWidth(), image.getHeight()); + graphics.drawImage(image, 0, 0, null); + image = noAlphaImage; + } + IIOImage iioImage = new IIOImage(image, null, null); writer.write(null, iioImage, iwp); } finally {