use of org.mycore.iiif.image.model.MCRIIIFImageTargetSize in project mycore by MyCoRe-Org.
the class MCRIIIFScaleParserTest method testParseTargetScale.
@org.junit.Test
public void testParseTargetScale() throws Exception {
Map<String, MCRIIIFImageTargetSize> testValues = new Hashtable<>();
testValues.put("!1100,800", new MCRIIIFImageTargetSize(1000, 800));
testValues.put("!1000,900", new MCRIIIFImageTargetSize(1000, 800));
testValues.put("200,200", new MCRIIIFImageTargetSize(200, 200));
testValues.put(",200", new MCRIIIFImageTargetSize(250, 200));
testValues.put(",800", new MCRIIIFImageTargetSize(1000, 800));
testValues.put("1000,", new MCRIIIFImageTargetSize(1000, 800));
testValues.put("pct:200", new MCRIIIFImageTargetSize(1000, 800));
testValues.put("pct:50", new MCRIIIFImageTargetSize(250, 200));
testValues.forEach((scale, expectedResult) -> Assert.assertEquals(expectedResult, new MCRIIIFScaleParser(scale, IMAGE_WIDTH, IMAGE_HEIGHT).parseTargetScale()));
}
use of org.mycore.iiif.image.model.MCRIIIFImageTargetSize in project mycore by MyCoRe-Org.
the class MCRIIIFScaleParser method parseTargetScale.
public MCRIIIFImageTargetSize parseTargetScale() {
if (isFull())
return new MCRIIIFImageTargetSize(sourceRegionWidth, sourceRegionHeight);
if (isPercent())
return parsePercentValue();
StringBuilder wBuilder = new StringBuilder(), hBuilder = new StringBuilder();
getWidthAndHeightStrings(wBuilder, hBuilder);
String w = wBuilder.toString(), h = hBuilder.toString();
try {
if (w.length() == 0) {
return scaleToHeight(Integer.parseInt(h));
} else if (h.length() == 0) {
return scaleToWidth(Integer.parseInt(w));
} else if (isBestFit()) {
return bestFit(Integer.parseInt(w), Integer.parseInt(h));
} else {
return scale(Integer.parseInt(w), Integer.parseInt(h));
}
} catch (NumberFormatException e) {
throw new IllegalArgumentException(h + " or " + w + " ist not a valid scale value!", e);
}
}
use of org.mycore.iiif.image.model.MCRIIIFImageTargetSize 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();
}
}
Aggregations