diff --git a/.gitignore b/.gitignore index 69d4f1d3..f8468377 100644 --- a/.gitignore +++ b/.gitignore @@ -15,4 +15,5 @@ libjpeg-turbo/config.sub libjpeg-turbo/configure libjpeg-turbo/java/Makefile.in libjpeg-turbo/ltmain.sh -/nb-configuration.xml \ No newline at end of file +/nb-configuration.xml +/nbproject/ \ No newline at end of file diff --git a/src/main/java/de/digitalcollections/iiif/hymir/image/business/ImageServiceImpl.java b/src/main/java/de/digitalcollections/iiif/hymir/image/business/ImageServiceImpl.java index 1756648c..5a989a92 100644 --- a/src/main/java/de/digitalcollections/iiif/hymir/image/business/ImageServiceImpl.java +++ b/src/main/java/de/digitalcollections/iiif/hymir/image/business/ImageServiceImpl.java @@ -35,12 +35,15 @@ import javax.imageio.stream.ImageInputStream; import javax.imageio.stream.ImageOutputStream; import org.imgscalr.Scalr; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; @Service public class ImageServiceImpl implements ImageService { + private static final Logger LOGGER = LoggerFactory.getLogger(ImageServiceImpl.class); private final ImageSecurityService imageSecurityService; private final ResolvedFileResourceServiceImpl fileResourceService; @@ -60,6 +63,12 @@ public class ImageServiceImpl implements ImageService { @Value("${custom.iiif.image.maxHeight:65500}") private int maxHeight; + @Value("${custom.iiif.image.checkAlphaChannel:false}") + private boolean checkAlphaChannel; + + @Value("${custom.iiif.image.checkTransparency:false}") + private boolean checkTransparency; + private static class DecodedImage { /** Decoded image * */ @@ -372,12 +381,27 @@ public void processImage( String identifier, ImageApiSelector selector, ImageApiProfile profile, OutputStream os) throws InvalidParametersException, UnsupportedOperationException, UnsupportedFormatException, ResourceNotFoundException, IOException { - DecodedImage img = readImage(identifier, selector, profile); + DecodedImage decodedImage = readImage(identifier, selector, profile); + + boolean containsAlphaChannel = false; + if (checkAlphaChannel) { + containsAlphaChannel = containsAlphaChannel(decodedImage.img); + LOGGER.debug("image contains alpha channel: " + containsAlphaChannel); + // no further consequences in handling image with alpha channel implemented, yet + } + + boolean containsTransparency = false; + if (checkTransparency) { + containsTransparency = containsTransparency(decodedImage.img); + LOGGER.debug("image contains transparency: " + containsTransparency); + // no further consequences in handling image with transparency implemented, yet + } + BufferedImage outImg = transformImage( - img.img, - img.targetSize, - img.rotation, + decodedImage.img, + decodedImage.targetSize, + decodedImage.rotation, selector.getRotation().isMirror(), selector.getQuality()); @@ -407,6 +431,46 @@ public Instant getImageModificationDate(String identifier) throws ResourceNotFou } } + /** + * @param image buffered image to check for alpha channel + * @return true, if image contains alpha channel + * @see Javadoc + * ColorModel + */ + public boolean containsAlphaChannel(BufferedImage image) { + return image.getColorModel().hasAlpha(); + } + + /** + * @param image buffered image to check for transparent pixels + * @return true, if image contains transparent pixels + */ + public boolean containsTransparency(BufferedImage image) { + for (int i = 0; i < image.getHeight(); i++) { + for (int j = 0; j < image.getWidth(); j++) { + if (isTransparent(image, j, i)) { + return true; + } + } + } + return false; + } + + /** + * @param image buffered image to check for transparent pixels + * @param x x coordinate of pixel to check + * @param y y coordinate of pixel to check + * @return + * @see Javadoc + * BufferedImage + */ + public boolean isTransparent(BufferedImage image, int x, int y) { + int pixel = image.getRGB(x, y); + return (pixel >> 24) == 0x00; + } + public String getLogoUrl() { return logoUrl; } diff --git a/src/test/java/de/digitalcollections/iiif/hymir/image/business/ImageServiceImplTest.java b/src/test/java/de/digitalcollections/iiif/hymir/image/business/ImageServiceImplTest.java new file mode 100644 index 00000000..3cd3e28c --- /dev/null +++ b/src/test/java/de/digitalcollections/iiif/hymir/image/business/ImageServiceImplTest.java @@ -0,0 +1,46 @@ +package de.digitalcollections.iiif.hymir.image.business; + +import static org.junit.jupiter.api.Assertions.*; + +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import javax.imageio.ImageIO; +import org.junit.jupiter.api.Test; +import org.springframework.util.ResourceUtils; + +public class ImageServiceImplTest { + + ImageServiceImpl instance = new ImageServiceImpl(null, null); + + @Test + public void testContainsAlphaChannel() throws FileNotFoundException, IOException { + File file = ResourceUtils.getFile("classpath:test-alpha-transparency-yes.png"); + BufferedImage image = ImageIO.read(file); + boolean expResult = true; + boolean result = instance.containsAlphaChannel(image); + assertEquals(expResult, result); + + file = ResourceUtils.getFile("classpath:test-alpha-transparency-no.png"); + image = ImageIO.read(file); + expResult = false; + result = instance.containsAlphaChannel(image); + assertEquals(expResult, result); + } + + @Test + public void testContainsTransparency() throws FileNotFoundException, IOException { + File file = ResourceUtils.getFile("classpath:test-alpha-transparency-yes.png"); + BufferedImage image = ImageIO.read(file); + boolean expResult = true; + boolean result = instance.containsTransparency(image); + assertEquals(expResult, result); + + file = ResourceUtils.getFile("classpath:test-alpha-transparency-no.png"); + image = ImageIO.read(file); + expResult = false; + result = instance.containsTransparency(image); + assertEquals(expResult, result); + } +} diff --git a/src/test/resources/test-alpha-transparency-no.png b/src/test/resources/test-alpha-transparency-no.png new file mode 100644 index 00000000..aa0e1405 Binary files /dev/null and b/src/test/resources/test-alpha-transparency-no.png differ diff --git a/src/test/resources/test-alpha-transparency-yes.png b/src/test/resources/test-alpha-transparency-yes.png new file mode 100644 index 00000000..f6e83575 Binary files /dev/null and b/src/test/resources/test-alpha-transparency-yes.png differ