Skip to content

Commit

Permalink
Add check methods for alpha channel and transparency (inactive by def…
Browse files Browse the repository at this point in the history
…ault and without further transformation consequences, yet)
  • Loading branch information
datazuul authored and jbaiter committed May 15, 2020
1 parent 04699f8 commit 75014b9
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ libjpeg-turbo/config.sub
libjpeg-turbo/configure
libjpeg-turbo/java/Makefile.in
libjpeg-turbo/ltmain.sh
/nb-configuration.xml
/nb-configuration.xml
/nbproject/
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 * */
Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -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 <a
* href="https://docs.oracle.com/javase/8/docs/api/java/awt/image/ColorModel.html#hasAlpha--">Javadoc
* ColorModel</a>
*/
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 <a
* href="https://docs.oracle.com/javase/8/docs/api/java/awt/image/BufferedImage.html#getRGB-int-int-">Javadoc
* BufferedImage</a>
*/
public boolean isTransparent(BufferedImage image, int x, int y) {
int pixel = image.getRGB(x, y);
return (pixel >> 24) == 0x00;
}

public String getLogoUrl() {
return logoUrl;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Binary file added src/test/resources/test-alpha-transparency-no.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 75014b9

Please sign in to comment.