Search in sources :

Example 1 with MCRIIIFUnsupportedFormatException

use of org.mycore.iiif.image.impl.MCRIIIFUnsupportedFormatException in project mycore by MyCoRe-Org.

the class MCRIVIEWIIIFImageImpl method provide.

@Override
public BufferedImage provide(String identifier, MCRIIIFImageSourceRegion region, MCRIIIFImageTargetSize targetSize, MCRIIIFImageTargetRotation rotation, MCRIIIFImageQuality imageQuality, String format) throws MCRIIIFImageNotFoundException, MCRIIIFImageProvidingException, MCRIIIFUnsupportedFormatException, MCRAccessException {
    long resultingSize = (long) targetSize.getHeight() * targetSize.getWidth() * (imageQuality.equals(MCRIIIFImageQuality.color) ? 3 : 1);
    long maxImageSize = MCRConfiguration.instance().getLong(MAX_BYTES);
    if (resultingSize > maxImageSize) {
        throw new MCRIIIFImageProvidingException("Maximal image size is " + (maxImageSize / 1024 / 1024) + "MB. [" + resultingSize + "/" + maxImageSize + "]");
    }
    if (!SUPPORTED_FORMATS.contains(format.toLowerCase(Locale.ENGLISH))) {
        throw new MCRIIIFUnsupportedFormatException(format);
    }
    Path tiledFile = tileFileProvider.getTiledFile(identifier);
    MCRTiledPictureProps tiledPictureProps = getTiledPictureProps(tiledFile);
    int sourceWidth = region.getX2() - region.getX1();
    int sourceHeight = region.getY2() - region.getY1();
    double targetWidth = targetSize.getWidth();
    double targetHeight = targetSize.getHeight();
    double rotatationRadians = Math.toRadians(rotation.getDegrees());
    double sinRotation = Math.sin(rotatationRadians);
    double cosRotation = Math.cos(rotatationRadians);
    final int height = (int) (Math.abs(targetWidth * sinRotation) + Math.abs(targetHeight * cosRotation));
    final int width = (int) (Math.abs(targetWidth * cosRotation) + Math.abs(targetHeight * sinRotation));
    BufferedImage targetImage;
    switch(imageQuality) {
        case bitonal:
            targetImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY);
            break;
        case gray:
            targetImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
            break;
        case color:
        default:
            if (transparentFormats.contains(format)) {
                targetImage = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
            } else {
                targetImage = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
            }
    }
    // this value determines the zoom level!
    double largestScaling = Math.max(targetWidth / sourceWidth, targetHeight / sourceHeight);
    // We always want to use the the best needed zoom level!
    int sourceZoomLevel = (int) Math.min(Math.max(0, Math.ceil(tiledPictureProps.getZoomlevel() - Math.log(largestScaling) / LOG_HALF)), tiledPictureProps.getZoomlevel());
    // largestScaling is the real scale which is needed! zoomLevelScale is the scale of the nearest zoom level!
    double zoomLevelScale = Math.min(1.0, Math.pow(0.5, tiledPictureProps.getZoomlevel() - sourceZoomLevel));
    // this is the scale which is needed from the nearest zoom level to the required size of image
    double drawScaleX = (targetWidth / (sourceWidth * zoomLevelScale)), drawScaleY = (targetHeight / (sourceHeight * zoomLevelScale));
    // absolute region in zoom level this nearest zoom level
    double x1 = region.getX1() * zoomLevelScale, x2 = region.getX2() * zoomLevelScale, y1 = region.getY1() * zoomLevelScale, y2 = region.getY2() * zoomLevelScale;
    // now we detect the tiles to draw!
    int x1Tile = (int) Math.floor(x1 / 256), y1Tile = (int) Math.floor(y1 / 256), x2Tile = (int) Math.ceil(x2 / 256), y2Tile = (int) Math.ceil(y2 / 256);
    try (FileSystem zipFileSystem = MCRIView2Tools.getFileSystem(tileFileProvider.getTiledFile(identifier))) {
        Path rootPath = zipFileSystem.getPath("/");
        Graphics2D graphics = targetImage.createGraphics();
        if (rotation.isMirrored()) {
            graphics.scale(-1, 1);
            graphics.translate(-width, 0);
        }
        int xt = (int) ((targetWidth - 1) / 2), yt = (int) ((targetHeight - 1) / 2);
        graphics.translate((width - targetWidth) / 2, (height - targetHeight) / 2);
        graphics.rotate(rotatationRadians, xt, yt);
        graphics.scale(drawScaleX, drawScaleY);
        graphics.translate(-x1, -y1);
        graphics.scale(zoomLevelScale, zoomLevelScale);
        graphics.setClip(region.getX1(), region.getY1(), sourceWidth, sourceHeight);
        graphics.scale(1 / zoomLevelScale, 1 / zoomLevelScale);
        LOGGER.info(String.format(Locale.ROOT, "Using zoom-level: %d and scales %s/%s!", sourceZoomLevel, drawScaleX, drawScaleY));
        for (int x = x1Tile; x < x2Tile; x++) {
            for (int y = y1Tile; y < y2Tile; y++) {
                ImageReader imageReader = MCRIView2Tools.getTileImageReader();
                BufferedImage tile = MCRIView2Tools.readTile(rootPath, imageReader, sourceZoomLevel, x, y);
                graphics.drawImage(tile, x * 256, y * 256, null);
            }
        }
    } catch (IOException e) {
        throw new MCRIIIFImageProvidingException("Error while reading tiles!", e);
    }
    return targetImage;
}
Also used : Path(java.nio.file.Path) FileSystem(java.nio.file.FileSystem) MCRIIIFImageProvidingException(org.mycore.iiif.image.impl.MCRIIIFImageProvidingException) IOException(java.io.IOException) MCRIIIFUnsupportedFormatException(org.mycore.iiif.image.impl.MCRIIIFUnsupportedFormatException) MCRTiledPictureProps(org.mycore.imagetiler.MCRTiledPictureProps) ImageReader(javax.imageio.ImageReader) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Example 2 with MCRIIIFUnsupportedFormatException

use of org.mycore.iiif.image.impl.MCRIIIFUnsupportedFormatException in project mycore by MyCoRe-Org.

the class MCRIIIFImageResource method getImage.

@GET
@Path("{" + IDENTIFIER_PARAM + "}/{region}/{size}/{rotation}/{quality}.{format}")
public Response getImage(@PathParam(IMPL_PARAM) String implStr, @PathParam(IDENTIFIER_PARAM) String identifier, @PathParam("region") String region, @PathParam("size") String size, @PathParam("rotation") String rotation, @PathParam("quality") String quality, @PathParam("format") String format) {
    try {
        MCRIIIFImageImpl impl = getImpl(implStr);
        MCRIIIFImageInformation information = impl.getInformation(identifier);
        MCRIIIFRegionParser rp = new MCRIIIFRegionParser(region, information.width, information.height);
        MCRIIIFImageSourceRegion sourceRegion = rp.parseImageRegion();
        MCRIIIFScaleParser sp = new MCRIIIFScaleParser(size, sourceRegion.getX2() - sourceRegion.getX1(), sourceRegion.getY2() - sourceRegion.getY1());
        MCRIIIFImageTargetSize targetSize = sp.parseTargetScale();
        MCRIIIFRotationParser rotationParser = new MCRIIIFRotationParser(rotation);
        MCRIIIFImageTargetRotation parsedRotation = rotationParser.parse();
        MCRIIIFImageQuality imageQuality = MCRIIIFImageQuality.fromString(quality);
        BufferedImage provide = impl.provide(identifier, sourceRegion, targetSize, parsedRotation, imageQuality, format);
        Response.Status status = rp.isCompleteValid() ? Response.Status.OK : Response.Status.BAD_REQUEST;
        Response.ResponseBuilder responseBuilder = Response.status(status);
        return responseBuilder.header("Link", buildCanonicalURL(impl, identifier)).header("Profile", buildProfileURL()).type("image/" + format).entity((StreamingOutput) outputStream -> ImageIO.write(provide, format, outputStream)).build();
    } catch (MCRIIIFImageNotFoundException e) {
        return Response.status(Response.Status.NOT_FOUND).entity(e.getMessage()).build();
    } catch (IllegalArgumentException | MCRIIIFUnsupportedFormatException e) {
        return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).build();
    } catch (MCRAccessException e) {
        return Response.status(Response.Status.FORBIDDEN).entity(e.getMessage()).build();
    } catch (Exception e) {
        LOGGER.error(() -> "Error while getting Image " + identifier + " from " + implStr + " with region: " + region + ", size: " + size + ", rotation: " + rotation + ", quality: " + quality + ", format: " + format, e);
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
    }
}
Also used : MCRIIIFImageTargetRotation(org.mycore.iiif.image.model.MCRIIIFImageTargetRotation) MCRIIIFImageInformation(org.mycore.iiif.image.model.MCRIIIFImageInformation) MCRAccessException(org.mycore.access.MCRAccessException) MCRIIIFImageQuality(org.mycore.iiif.image.model.MCRIIIFImageQuality) StreamingOutput(javax.ws.rs.core.StreamingOutput) MCRIIIFUnsupportedFormatException(org.mycore.iiif.image.impl.MCRIIIFUnsupportedFormatException) BufferedImage(java.awt.image.BufferedImage) URISyntaxException(java.net.URISyntaxException) MCRIIIFImageProvidingException(org.mycore.iiif.image.impl.MCRIIIFImageProvidingException) MCRAccessException(org.mycore.access.MCRAccessException) MCRIIIFUnsupportedFormatException(org.mycore.iiif.image.impl.MCRIIIFUnsupportedFormatException) MCRIIIFImageNotFoundException(org.mycore.iiif.image.impl.MCRIIIFImageNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Response(javax.ws.rs.core.Response) MCRIIIFImageNotFoundException(org.mycore.iiif.image.impl.MCRIIIFImageNotFoundException) MCRIIIFImageTargetSize(org.mycore.iiif.image.model.MCRIIIFImageTargetSize) MCRIIIFImageSourceRegion(org.mycore.iiif.image.model.MCRIIIFImageSourceRegion) MCRIIIFRotationParser(org.mycore.iiif.image.parser.MCRIIIFRotationParser) MCRIIIFRegionParser(org.mycore.iiif.image.parser.MCRIIIFRegionParser) MCRIIIFScaleParser(org.mycore.iiif.image.parser.MCRIIIFScaleParser) MCRIIIFImageImpl(org.mycore.iiif.image.impl.MCRIIIFImageImpl) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Aggregations

BufferedImage (java.awt.image.BufferedImage)2 MCRIIIFImageProvidingException (org.mycore.iiif.image.impl.MCRIIIFImageProvidingException)2 MCRIIIFUnsupportedFormatException (org.mycore.iiif.image.impl.MCRIIIFUnsupportedFormatException)2 Graphics2D (java.awt.Graphics2D)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URISyntaxException (java.net.URISyntaxException)1 FileSystem (java.nio.file.FileSystem)1 Path (java.nio.file.Path)1 ImageReader (javax.imageio.ImageReader)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Response (javax.ws.rs.core.Response)1 StreamingOutput (javax.ws.rs.core.StreamingOutput)1 MCRAccessException (org.mycore.access.MCRAccessException)1 MCRIIIFImageImpl (org.mycore.iiif.image.impl.MCRIIIFImageImpl)1 MCRIIIFImageNotFoundException (org.mycore.iiif.image.impl.MCRIIIFImageNotFoundException)1 MCRIIIFImageInformation (org.mycore.iiif.image.model.MCRIIIFImageInformation)1 MCRIIIFImageQuality (org.mycore.iiif.image.model.MCRIIIFImageQuality)1 MCRIIIFImageSourceRegion (org.mycore.iiif.image.model.MCRIIIFImageSourceRegion)1