Skip to content

Commit

Permalink
Add MCRImage.getInstance() without derivateID required #35
Browse files Browse the repository at this point in the history
  • Loading branch information
yagee-de authored and mcradmin committed Sep 21, 2023
1 parent ab28b30 commit 321f40d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
45 changes: 39 additions & 6 deletions src/main/java/org/mycore/imagetiler/MCRImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.nio.file.StandardOpenOption;
import java.util.Iterator;
import java.util.Locale;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.IntStream;
import java.util.zip.ZipEntry;
Expand Down Expand Up @@ -205,6 +206,33 @@ public static MCRImage getInstance(final Path file, final String derivateID, fin
return new MCRMemSaveImage(file, derivateID, imagePath);
}

/**
* returns an instance of MCRImage (or subclass) based on the provided parameters.
*
* @param baseDir the base directory where the image is located
* @param file the image file
* @param targetDir the target directory where the tiled image should be saved
* @return a new instance of MCRImage representing <code>file</code>
*/
public static MCRImage getInstance(final Path baseDir, final Path file, final Path targetDir) {
Path imageFile = safeResolve(baseDir, file);
String relImagePath = baseDir.toAbsolutePath().relativize(imageFile).toString();
MCRMemSaveImage image = new MCRMemSaveImage(imageFile, null, relImagePath);
image.setTileDir(targetDir);
return image;
}

public static Path safeResolve(Path basePath, Path resolve) {
Path absoluteBasePath = Objects.requireNonNull(basePath).toAbsolutePath();
final Path resolved = absoluteBasePath
.resolve(Objects.requireNonNull(resolve))
.normalize();
if (resolved.startsWith(absoluteBasePath.normalize())) {
return resolved;
}
throw new IllegalArgumentException("Bad path: " + resolve);
}

/**
* calculates the amount of tiles produces by this image dimensions.
*
Expand Down Expand Up @@ -233,9 +261,7 @@ public static int getTileCount(final int imageWidth, final int imageHeight) {
* @return tile directory of derivate if <code>imagePath</code> is null or the tile file (.iview2)
*/
public static Path getTiledFile(final Path tileDir, final String derivateID, final String imagePath) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("tileDir: {}, derivate: {}, imagePath: {}", tileDir, derivateID, imagePath);
}
LOGGER.debug("tileDir: {}, derivate: {}, imagePath: {}", tileDir, derivateID, imagePath);
Path tileFile = getTiledFileBaseDir(tileDir, derivateID);
if (imagePath == null) {
return tileFile;
Expand Down Expand Up @@ -513,6 +539,13 @@ public MCRTiledPictureProps tile() throws IOException {
return tile(null);
}

private String getName() {
if (derivate != null) {
return derivate + ":" + imagePath;
}
return imageFile.toString();
}

/**
* starts the tile process.
*
Expand All @@ -522,7 +555,7 @@ public MCRTiledPictureProps tile() throws IOException {
*/
public MCRTiledPictureProps tile(MCRTileEventHandler eventHandler) throws IOException {
long start = System.nanoTime();
LOGGER.info(String.format(Locale.ENGLISH, "Start tiling of %s:%s", derivate, imagePath));
LOGGER.info(String.format(Locale.ENGLISH, "Start tiling of %s", getName()));
setOrientation();
//waterMarkFile = ImageIO.read(new File(MCRIview2Props.getProperty("Watermark")));
//initialize some basic variables
Expand Down Expand Up @@ -556,8 +589,8 @@ public MCRTiledPictureProps tile(MCRTileEventHandler eventHandler) throws IOExce
final MCRTiledPictureProps imageProperties = getImageProperties();
long pixel = (long) imageProperties.getWidth() * imageProperties.getHeight();
LOGGER.info(() -> String.format(Locale.ENGLISH,
"Finished tiling of %s:%s in %.0f ms (%d MPixel/s). ",
derivate, imagePath, (end - start) / 1e6, 1000 * pixel / (end - start)));
"Finished tiling of %s in %.0f ms (%d MPixel/s). ",
getName(), (end - start) / 1e6, 1000 * pixel / (end - start)));
return imageProperties;
}

Expand Down
14 changes: 13 additions & 1 deletion src/test/java/org/mycore/imagetiler/MCRImageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private static boolean deleteDirectory(final Path path) {
*/
@Before
public void setUp() throws IOException {
Path testFilesDir = Path.of("src").resolve("test").resolve("resources");
Path testFilesDir = Path.of("src", "test", "resources");
final Path landscapeZip = testFilesDir.resolve("landscape.zip");
landscapeZipFS = FileSystems.newFileSystem(landscapeZip, this.getClass().getClassLoader());
pics.put("small", testFilesDir.resolve("Bay_of_Noboto.jpg"));
Expand Down Expand Up @@ -248,4 +248,16 @@ public void testgetTiledFile() {
assertEquals("Path to file is not es axpected.", pExpected, tiledFile);
}

@Test
public void testGetTiledFileWithoutMCR() throws IOException {
Path firstImage = pics.values().stream().findFirst().get().toAbsolutePath();
Path baseDir = firstImage.getParent().getParent().toAbsolutePath();
Path targetDir = Path.of("target");
Path imageFile = baseDir.relativize(firstImage);
MCRImage image = MCRImage.getInstance(baseDir, imageFile, targetDir);
Path tiledFile = MCRImage.getTiledFile(targetDir, null, imageFile.toString());
image.tile();
assertTrue("Tiled file is not present " + tiledFile, Files.exists(tiledFile));
}

}

0 comments on commit 321f40d

Please sign in to comment.