Search in sources :

Example 1 with MCRTiledPictureProps

use of org.mycore.imagetiler.MCRTiledPictureProps in project mycore by MyCoRe-Org.

the class MCRIVIEWIIIFImageImpl method getInformation.

public MCRIIIFImageInformation getInformation(String identifier) throws MCRIIIFImageNotFoundException, MCRIIIFImageProvidingException, MCRAccessException {
    try {
        Path tiledFile = tileFileProvider.getTiledFile(identifier);
        MCRTiledPictureProps tiledPictureProps = getTiledPictureProps(tiledFile);
        MCRIIIFImageInformation imageInformation = new MCRIIIFImageInformation(MCRIIIFBase.API_IMAGE_2, buildURL(identifier), DEFAULT_PROTOCOL, tiledPictureProps.getWidth(), tiledPictureProps.getHeight());
        MCRIIIFImageTileInformation tileInformation = new MCRIIIFImageTileInformation(256, 256);
        for (int i = 0; i < tiledPictureProps.getZoomlevel(); i++) {
            tileInformation.scaleFactors.add((int) Math.pow(2, i));
        }
        imageInformation.tiles.add(tileInformation);
        return imageInformation;
    } catch (FileSystemNotFoundException e) {
        LOGGER.error("Could not find Iview ZIP for {}", identifier, e);
        throw new MCRIIIFImageNotFoundException(identifier);
    }
}
Also used : Path(java.nio.file.Path) MCRIIIFImageNotFoundException(org.mycore.iiif.image.impl.MCRIIIFImageNotFoundException) MCRIIIFImageTileInformation(org.mycore.iiif.image.model.MCRIIIFImageTileInformation) FileSystemNotFoundException(java.nio.file.FileSystemNotFoundException) MCRIIIFImageInformation(org.mycore.iiif.image.model.MCRIIIFImageInformation) MCRTiledPictureProps(org.mycore.imagetiler.MCRTiledPictureProps)

Example 2 with MCRTiledPictureProps

use of org.mycore.imagetiler.MCRTiledPictureProps in project mycore by MyCoRe-Org.

the class MCRIView2Tools method getZoomLevel.

/**
 * combines image tiles of specified zoomLevel to one image.
 *
 * @param iviewFileRoot
 *            root directory of .iview2 file
 * @param imageProperties
 *            imageProperties, if available or null
 * @param zoomLevel
 *            the zoom level where 0 is thumbnail size
 * @return a combined image
 * @throws IOException
 *             any IOException while reading tiles
 * @throws JDOMException
 *             if image properties could not be parsed.
 */
public static BufferedImage getZoomLevel(final Path iviewFileRoot, final MCRTiledPictureProps imageProperties, final ImageReader reader, final int zoomLevel) throws IOException, JDOMException {
    if (zoomLevel == 0) {
        return readTile(iviewFileRoot, reader, 0, 0, 0);
    }
    MCRTiledPictureProps imageProps = imageProperties == null ? MCRTiledPictureProps.getInstanceFromDirectory(iviewFileRoot) : imageProperties;
    double zoomFactor = Math.pow(2, (imageProps.getZoomlevel() - zoomLevel));
    int maxX = (int) Math.ceil((imageProps.getWidth() / zoomFactor) / MCRImage.getTileSize());
    int maxY = (int) Math.ceil((imageProps.getHeight() / zoomFactor) / MCRImage.getTileSize());
    LOGGER.debug(MessageFormat.format("Image size:{0}x{1}, tiles:{2}x{3}", imageProps.getWidth(), imageProps.getHeight(), maxX, maxY));
    int imageType = getImageType(iviewFileRoot, reader, zoomLevel, 0, 0);
    int xDim = ((maxX - 1) * MCRImage.getTileSize() + readTile(iviewFileRoot, reader, zoomLevel, maxX - 1, 0).getWidth());
    int yDim = ((maxY - 1) * MCRImage.getTileSize() + readTile(iviewFileRoot, reader, zoomLevel, 0, maxY - 1).getHeight());
    BufferedImage resultImage = new BufferedImage(xDim, yDim, imageType);
    Graphics graphics = resultImage.getGraphics();
    try {
        for (int x = 0; x < maxX; x++) {
            for (int y = 0; y < maxY; y++) {
                BufferedImage tile = readTile(iviewFileRoot, reader, zoomLevel, x, y);
                graphics.drawImage(tile, x * MCRImage.getTileSize(), y * MCRImage.getTileSize(), null);
            }
        }
        return resultImage;
    } finally {
        graphics.dispose();
    }
}
Also used : Graphics(java.awt.Graphics) MCRTiledPictureProps(org.mycore.imagetiler.MCRTiledPictureProps) BufferedImage(java.awt.image.BufferedImage)

Example 3 with MCRTiledPictureProps

use of org.mycore.imagetiler.MCRTiledPictureProps in project mycore by MyCoRe-Org.

the class MCRIView2Tools method getZoomLevel.

/**
 * combines image tiles of specified zoomLevel to one image.
 *
 * @param iviewFile
 *            .iview2 file
 * @param zoomLevel
 *            the zoom level where 0 is thumbnail size
 * @return a combined image
 * @throws IOException
 *             any IOException while reading tiles
 * @throws JDOMException
 *             if image properties could not be parsed.
 */
public static BufferedImage getZoomLevel(Path iviewFile, int zoomLevel) throws IOException, JDOMException {
    ImageReader reader = getTileImageReader();
    try (FileSystem zipFileSystem = getFileSystem(iviewFile)) {
        Path iviewFileRoot = zipFileSystem.getRootDirectories().iterator().next();
        MCRTiledPictureProps imageProps = MCRTiledPictureProps.getInstanceFromDirectory(iviewFileRoot);
        if (zoomLevel < 0 || zoomLevel > imageProps.getZoomlevel()) {
            throw new IndexOutOfBoundsException("Zoom level " + zoomLevel + " is not in range 0 - " + imageProps.getZoomlevel());
        }
        return getZoomLevel(iviewFileRoot, imageProps, reader, zoomLevel);
    } finally {
        reader.dispose();
    }
}
Also used : Path(java.nio.file.Path) MCRPath(org.mycore.datamodel.niofs.MCRPath) FileSystem(java.nio.file.FileSystem) ImageReader(javax.imageio.ImageReader) MCRTiledPictureProps(org.mycore.imagetiler.MCRTiledPictureProps)

Example 4 with MCRTiledPictureProps

use of org.mycore.imagetiler.MCRTiledPictureProps in project mycore by MyCoRe-Org.

the class MCRIView2Commands method checkImage.

/**
 * checks and repairs tile of this derivate.
 * @param derivate derivate ID
 * @param absoluteImagePath absolute path to image file
 */
@MCRCommand(syntax = CHECK_TILES_OF_IMAGE_COMMAND_SYNTAX, help = "checks if tiles a specific file identified by its derivate {0} and absolute path {1} are valid or generates new one", order = 30)
public static void checkImage(String derivate, String absoluteImagePath) throws IOException {
    Path iviewFile = MCRImage.getTiledFile(MCRIView2Tools.getTileDir(), derivate, absoluteImagePath);
    // file checks
    if (!Files.exists(iviewFile)) {
        LOGGER.warn("IView2 file does not exist: {}", iviewFile);
        tileImage(derivate, absoluteImagePath);
        return;
    }
    MCRTiledPictureProps props;
    try {
        props = MCRTiledPictureProps.getInstanceFromFile(iviewFile);
    } catch (Exception e) {
        LOGGER.warn("Error while reading image metadata. Recreating tiles.", e);
        tileImage(derivate, absoluteImagePath);
        return;
    }
    if (props == null) {
        LOGGER.warn("Could not get tile metadata");
        tileImage(derivate, absoluteImagePath);
        return;
    }
    ZipFile iviewImage;
    try {
        iviewImage = new ZipFile(iviewFile.toFile());
        validateZipFile(iviewImage);
    } catch (Exception e) {
        LOGGER.warn("Error while reading Iview2 file: {}", iviewFile, e);
        tileImage(derivate, absoluteImagePath);
        return;
    }
    try (FileSystem fs = MCRIView2Tools.getFileSystem(iviewFile)) {
        Path iviewFileRoot = fs.getRootDirectories().iterator().next();
        // structure and metadata checks
        // one for metadata
        int tilesCount = iviewImage.size() - 1;
        if (props.getTilesCount() != tilesCount) {
            LOGGER.warn("Metadata tile count does not match stored tile count: {}", iviewFile);
            tileImage(derivate, absoluteImagePath);
            return;
        }
        int x = props.getWidth();
        int y = props.getHeight();
        if (MCRImage.getTileCount(x, y) != tilesCount) {
            LOGGER.warn("Calculated tile count does not match stored tile count: {}", iviewFile);
            tileImage(derivate, absoluteImagePath);
            return;
        }
        try {
            ImageReader imageReader = MCRIView2Tools.getTileImageReader();
            @SuppressWarnings("unused") BufferedImage thumbnail = MCRIView2Tools.getZoomLevel(iviewFileRoot, props, imageReader, 0);
            int maxX = (int) Math.ceil((double) props.getWidth() / MCRImage.getTileSize());
            int maxY = (int) Math.ceil((double) props.getHeight() / MCRImage.getTileSize());
            LOGGER.debug(MessageFormat.format("Image size:{0}x{1}, tiles:{2}x{3}", props.getWidth(), props.getHeight(), maxX, maxY));
            try {
                @SuppressWarnings("unused") BufferedImage sampleTile = MCRIView2Tools.readTile(iviewFileRoot, imageReader, props.getZoomlevel(), maxX - 1, 0);
            } finally {
                imageReader.dispose();
            }
        } catch (IOException | JDOMException e) {
            LOGGER.warn("Could not read thumbnail of {}", iviewFile, e);
            tileImage(derivate, absoluteImagePath);
        }
    }
}
Also used : Path(java.nio.file.Path) MCRPath(org.mycore.datamodel.niofs.MCRPath) ZipFile(java.util.zip.ZipFile) FileSystem(java.nio.file.FileSystem) IOException(java.io.IOException) MCRTiledPictureProps(org.mycore.imagetiler.MCRTiledPictureProps) ImageReader(javax.imageio.ImageReader) JDOMException(org.jdom2.JDOMException) MCRException(org.mycore.common.MCRException) JDOMException(org.jdom2.JDOMException) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 5 with MCRTiledPictureProps

use of org.mycore.imagetiler.MCRTiledPictureProps in project mycore by MyCoRe-Org.

the class MCRThumbnailServlet method getThumbnail.

private BufferedImage getThumbnail(Path iviewFile, boolean centered) throws IOException, JDOMException {
    BufferedImage level1Image;
    try (FileSystem fs = MCRIView2Tools.getFileSystem(iviewFile)) {
        Path iviewFileRoot = fs.getRootDirectories().iterator().next();
        MCRTiledPictureProps props = MCRTiledPictureProps.getInstanceFromDirectory(iviewFileRoot);
        // get next bigger zoomLevel and scale image to THUMBNAIL_SIZE
        ImageReader reader = MCRIView2Tools.getTileImageReader();
        try {
            level1Image = MCRIView2Tools.getZoomLevel(iviewFileRoot, props, reader, Math.min(1, props.getZoomlevel()));
        } finally {
            reader.dispose();
        }
    }
    final double width = level1Image.getWidth();
    final double height = level1Image.getHeight();
    final int newWidth = width < height ? (int) Math.ceil(thumbnailSize * width / height) : thumbnailSize;
    final int newHeight = width < height ? thumbnailSize : (int) Math.ceil(thumbnailSize * height / width);
    // if centered make transparent image
    int imageType = centered ? BufferedImage.TYPE_INT_ARGB : MCRImage.getImageType(level1Image);
    // if centered make thumbnailSize x thumbnailSize image
    final BufferedImage bicubic = new BufferedImage(centered ? thumbnailSize : newWidth, centered ? thumbnailSize : newHeight, imageType);
    final Graphics2D bg = bicubic.createGraphics();
    try {
        bg.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        int x = centered ? (thumbnailSize - newWidth) / 2 : 0;
        int y = centered ? (thumbnailSize - newHeight) / 2 : 0;
        if (x != 0 && y != 0) {
            LOGGER.warn("Writing at position {},{}", x, y);
        }
        bg.drawImage(level1Image, x, y, x + newWidth, y + newHeight, 0, 0, (int) Math.ceil(width), (int) Math.ceil(height), null);
    } finally {
        bg.dispose();
    }
    return bicubic;
}
Also used : Path(java.nio.file.Path) FileSystem(java.nio.file.FileSystem) MCRTiledPictureProps(org.mycore.imagetiler.MCRTiledPictureProps) ImageReader(javax.imageio.ImageReader) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Aggregations

MCRTiledPictureProps (org.mycore.imagetiler.MCRTiledPictureProps)9 Path (java.nio.file.Path)8 BufferedImage (java.awt.image.BufferedImage)6 FileSystem (java.nio.file.FileSystem)6 ImageReader (javax.imageio.ImageReader)6 MCRPath (org.mycore.datamodel.niofs.MCRPath)4 Graphics2D (java.awt.Graphics2D)3 IOException (java.io.IOException)3 Graphics (java.awt.Graphics)1 FileSystemNotFoundException (java.nio.file.FileSystemNotFoundException)1 Date (java.util.Date)1 ZipFile (java.util.zip.ZipFile)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Session (org.hibernate.Session)1 Transaction (org.hibernate.Transaction)1 JDOMException (org.jdom2.JDOMException)1 MCRException (org.mycore.common.MCRException)1 MCRSession (org.mycore.common.MCRSession)1 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)1