Skip to content

Commit

Permalink
fix issue with image type in MCRImage.scaleBufferedImage()
Browse files Browse the repository at this point in the history
  • Loading branch information
yagee-de committed Oct 19, 2020
1 parent d09f972 commit 8994a49
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 13 deletions.
16 changes: 4 additions & 12 deletions src/main/java/org/mycore/imagetiler/MCRImage.java
Expand Up @@ -375,7 +375,7 @@ protected static BufferedImage scaleBufferedImage(final BufferedImage image) {
final int height = image.getHeight();
final int newWidth = (int) Math.ceil(width / 2d);
final int newHeight = (int) Math.ceil(height / 2d);
final BufferedImage bicubic = new BufferedImage(newWidth, newHeight, image.getType());
final BufferedImage bicubic = new BufferedImage(newWidth, newHeight, getImageType(image));
final Graphics2D bg = bicubic.createGraphics();
bg.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
bg.scale(ZOOM_FACTOR, ZOOM_FACTOR);
Expand Down Expand Up @@ -439,15 +439,7 @@ private static int getImageType(ColorModel colorModel) {
* @return a {@link BufferedImage#getType()} response, where BufferedImage.TYPE_CUSTOM is translated to compatible image type.
*/
public static int getImageType(BufferedImage image) {
int imageType;
if (image.getType() != BufferedImage.TYPE_CUSTOM) {
//best fit
LOGGER.debug("Pretty sure we should use {}", image.getType());
imageType = image.getType();
} else {
imageType = getImageType(image.getColorModel());
}
return imageType;
return getImageType(image.getColorModel());
}

/**
Expand Down Expand Up @@ -550,8 +542,8 @@ protected void doTile(final ImageReader imageReader, final ZipOutputStream zout)

final int getMaxTileY = (int) Math.ceil((double) image.getHeight() / TILE_SIZE);
final int getMaxTileX = (int) Math.ceil((double) image.getWidth() / TILE_SIZE);
for (int y = 0; y <= getMaxTileY; y++) {
for (int x = 0; x <= getMaxTileX; x++) {
for (int y = 0; y < getMaxTileY; y++) {
for (int x = 0; x < getMaxTileX; x++) {
final BufferedImage tile = getTile(image, x, y);
writeTile(zout, tile, x, y, z);
}
Expand Down
72 changes: 71 additions & 1 deletion src/test/java/org/mycore/imagetiler/MCRImageTest.java
Expand Up @@ -20,11 +20,16 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.BitSet;
import java.util.Comparator;
import java.util.HashMap;
Expand All @@ -33,6 +38,11 @@
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

Expand Down Expand Up @@ -74,6 +84,7 @@ private static boolean deleteDirectory(final Path path) {
@Before
public void setUp() {
pics.put("small", "src/test/resources/Bay_of_Noboto.jpg");
pics.put("stripes", "src/test/resources/stripes.png");
pics.put("wide", "src/test/resources/labirynth_panorama_010.jpg");
pics.put("1 pixel mega tile rest", "src/test/resources/BE_0681_0397.jpg");
pics.put("extra small", "src/test/resources/5x5.jpg");
Expand Down Expand Up @@ -136,7 +147,15 @@ public void postImageReaderCreated() {
assertTrue("zoomLevel must be zero or positive: " + zAttr, Integer.parseInt(zAttr) >= 0);
int iTiles = Integer.parseInt(tAttr);
assertEquals(tilesCount, iTiles);

if ("stripes".equals(entry.getKey())) {
ZipEntry tileEntry = new ZipEntry("0/0/0.jpg");
try (InputStream is = iviewImage.getInputStream(tileEntry)) {
System.out.println("Reading tile " + tileEntry.getName());
final Path targetDir = tileDir.getParent();
Files.copy(iviewImage.getInputStream(tileEntry), targetDir.resolve("stripes-thumb.jpg"),
StandardCopyOption.REPLACE_EXISTING);
}
}
}
assertEquals(entry.getKey() + ": Metadata tile count does not match stored tile count.",
props.getTilesCount(), tilesCount);
Expand All @@ -147,6 +166,57 @@ public void postImageReaderCreated() {
}
}

@Test
public void testStripes() throws IOException {
BufferedImage stripes = new BufferedImage(3000, 3000, BufferedImage.TYPE_INT_RGB);
Color top = new Color(134, 49, 68);
Color middle = new Color(255, 255, 255);
Color bottom = new Color(36, 52, 83);
for (int y = 0; y < 2366; y++) {
for (int x = 0; x < stripes.getWidth(); x++) {
stripes.setRGB(x, y, top.getRGB());
}
}
for (int y = 2366; y < 2376; y++) {
for (int x = 0; x < stripes.getWidth(); x++) {
stripes.setRGB(x, y, middle.getRGB());
}
}
for (int y = 2376; y < stripes.getHeight(); y++) {
for (int x = 0; x < stripes.getWidth(); x++) {
stripes.setRGB(x, y, bottom.getRGB());
}
}
final ImageWriter pngWriter = ImageIO.getImageWritersByMIMEType("image/png").next();
final String stripesImagePath = "target/simple-stripes.png";
try (FileOutputStream fout = new FileOutputStream(stripesImagePath);
ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(fout)) {
pngWriter.setOutput(imageOutputStream);
final IIOImage iioImage = new IIOImage(stripes, null, null);
ImageWriteParam imageWriteParam = pngWriter.getDefaultWriteParam();
imageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
imageWriteParam.setCompressionQuality(0f);
pngWriter.write(null, iioImage, imageWriteParam);
}
final File file = new File(stripesImagePath);
final String derivateID = "derivateID";
final String imagePath = "imagePath/" + FilenameUtils.getName(stripesImagePath);
final MCRImage image = MCRImage.getInstance(file.toPath(), derivateID, imagePath);
image.setTileDir(tileDir);
image.tile();
assertTrue("Tile directory is not created.", Files.exists(tileDir));
final Path iviewFile = MCRImage.getTiledFile(tileDir, derivateID, imagePath);
try (final ZipFile iviewImage = new ZipFile(iviewFile.toFile())) {
ZipEntry tileEntry = new ZipEntry("0/0/0.jpg");
try (InputStream is = iviewImage.getInputStream(tileEntry)) {
final Path targetDir = tileDir.getParent();
final Path target = targetDir.resolve("simple-stripes-thumb.jpg");
System.out.println("Copy tile " + tileEntry.getName() + " to " + target);
Files.copy(is, target, StandardCopyOption.REPLACE_EXISTING);
}
}
}

@Test
public void testgetTiledFile() {
String final1 = "00";
Expand Down
Binary file added src/test/resources/stripes.png
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 8994a49

Please sign in to comment.