diff --git a/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFPictureShape.java b/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFPictureShape.java index 80b5d4a6ce..aec5faccf6 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFPictureShape.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFPictureShape.java @@ -225,6 +225,14 @@ public class XSLFPictureShape extends XSLFSimpleShape POIXMLUnits.parsePercent(r.xgetR())); } + public int getAlpha() { + CTBlip blip = getBlip(); + if (blip == null) { + return FULLY_OPAQUE_ALPHA_VALUE; + } + return blip.sizeOfAlphaModFixArray() > 0 ? POIXMLUnits.parsePercent(blip.getAlphaModFixArray(0).xgetAmt()) : FULLY_OPAQUE_ALPHA_VALUE; + } + /** * Add a SVG image reference * @param svgPic a previously imported svg image diff --git a/poi-ooxml/src/test/java/org/apache/poi/sl/tests/draw/TestDrawPictureShape.java b/poi-ooxml/src/test/java/org/apache/poi/sl/tests/draw/TestDrawPictureShape.java index 9f5568c4c4..0bd9414d8b 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/sl/tests/draw/TestDrawPictureShape.java +++ b/poi-ooxml/src/test/java/org/apache/poi/sl/tests/draw/TestDrawPictureShape.java @@ -22,8 +22,9 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assumptions.assumeFalse; -import java.awt.Dimension; +import java.awt.*; import java.awt.geom.Rectangle2D; +import java.awt.image.BufferedImage; import java.io.IOException; import java.io.InputStream; @@ -117,4 +118,33 @@ class TestDrawPictureShape { return val; } } + + @Test + void testAlphaXSLFPictureShape() throws IOException { + SlideShow ss = openSampleDocument("picture-transparency.pptx"); + + // First slide contains a fully opaque bitmap + verifySlideFirstPixelColor(ss.getSlides().get(0), new Color(0, 0, 0, 255)); + // Second slide contains a 20% transparency bitmap (255*0.8=204) + verifySlideFirstPixelColor(ss.getSlides().get(1), new Color(0, 0, 0, 204)); + // Third slide contains a 60% transparency bitmap (255*0.4=102) + verifySlideFirstPixelColor(ss.getSlides().get(2), new Color(0, 0, 0, 102)); + // Fourth slide contains a fully transparent bitmap + verifySlideFirstPixelColor(ss.getSlides().get(3), new Color(0, 0, 0, 0)); + } + + private void verifySlideFirstPixelColor(Slide slide, Color color) { + PictureShape picShape = null; + for (Shape shape : slide.getShapes()) { + if (shape instanceof PictureShape) { + picShape = (PictureShape)shape; + break; + } + } + assertNotNull(picShape); + BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB); + new DrawPictureShape(picShape).draw(img.createGraphics()); + assertEquals(Transparency.TRANSLUCENT, img.getTransparency()); + assertEquals(color, new Color(img.getRGB(0, 0), true)); + } } diff --git a/poi-scratchpad/src/main/java/org/apache/poi/hslf/usermodel/HSLFPictureShape.java b/poi-scratchpad/src/main/java/org/apache/poi/hslf/usermodel/HSLFPictureShape.java index e10a54cb55..ac2f12bd2f 100644 --- a/poi-scratchpad/src/main/java/org/apache/poi/hslf/usermodel/HSLFPictureShape.java +++ b/poi-scratchpad/src/main/java/org/apache/poi/hslf/usermodel/HSLFPictureShape.java @@ -228,4 +228,8 @@ public class HSLFPictureShape extends HSLFSimpleShape implements PictureShape, P extends TextParagraph > extends SimpleShape { + public static final int FULLY_TRANSPARENT_ALPHA_VALUE = 0; + public static final int FULLY_OPAQUE_ALPHA_VALUE = 100000; + /** * Returns the picture data for this picture. * @@ -47,4 +50,12 @@ public interface PictureShape< * @return the clipping rectangle, which is given in percent in relation to the image width/height */ Insets getClipping(); + + /** + * Returns alpha value in a range between 0 and 100000. + * Value 0 represents complete transparency and 100000 represents complete opacity. + * @return alpha value in the range 0..100000. + * @since 6.0.0 + */ + int getAlpha(); } diff --git a/test-data/slideshow/picture-transparency.pptx b/test-data/slideshow/picture-transparency.pptx new file mode 100644 index 0000000000..41121f7dde Binary files /dev/null and b/test-data/slideshow/picture-transparency.pptx differ