use of com.day.image.Layer in project aem-core-wcm-components by Adobe-Marketing-Cloud.
the class AdaptiveImageServlet method transformAndStreamAsset.
@SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE", justification = "Scanning generated code of try-with-resources")
protected void transformAndStreamAsset(SlingHttpServletResponse response, ValueMap componentProperties, int resizeWidth, double quality, Asset asset, String imageType, String imageName) throws IOException {
String extension = mimeTypeService.getExtension(imageType);
if ("gif".equalsIgnoreCase(extension) || "svg".equalsIgnoreCase(extension)) {
LOGGER.debug("GIF or SVG asset detected; will render the original rendition.");
metrics.markOriginalRenditionUsed();
try (InputStream is = asset.getOriginal().getStream()) {
if (is != null) {
stream(response, is, imageType, imageName);
}
}
return;
}
int rotationAngle = getRotation(componentProperties);
Rectangle rectangle = getCropRect(componentProperties);
boolean flipHorizontally = componentProperties.get(Image.PN_FLIP_HORIZONTAL, Boolean.FALSE);
boolean flipVertically = componentProperties.get(Image.PN_FLIP_VERTICAL, Boolean.FALSE);
if (rotationAngle != 0 || rectangle != null || resizeWidth > 0 || flipHorizontally || flipVertically) {
int originalWidth = getDimension(asset.getMetadataValue(DamConstants.TIFF_IMAGEWIDTH));
int originalHeight = getDimension(asset.getMetadataValue(DamConstants.TIFF_IMAGELENGTH));
Layer layer = null;
boolean appliedTransformation = false;
if (rectangle != null) {
double scaling;
EnhancedRendition wcmRendition = getWCMRendition(asset);
double renditionWidth;
Dimension renditionDimension = wcmRendition.getDimension();
if (renditionDimension != null) {
renditionWidth = renditionDimension.getWidth();
} else {
renditionWidth = originalWidth;
}
if (originalWidth > renditionWidth) {
scaling = (double) originalWidth / renditionWidth;
} else {
if (originalWidth > 0) {
scaling = renditionWidth / originalWidth;
} else {
scaling = 1.0;
}
}
layer = getLayer(getOriginal(asset));
if (layer != null) {
if (Math.abs(scaling - 1.0D) != 0) {
Rectangle scaledRectangle = new Rectangle((int) (rectangle.x * scaling), (int) (rectangle.y * scaling), (int) (rectangle.getWidth() * scaling), (int) (rectangle.getHeight() * scaling));
layer.crop(scaledRectangle);
} else {
layer.crop(rectangle);
}
appliedTransformation = true;
}
}
if (rotationAngle != 0) {
if (layer == null) {
layer = getLayer(getBestRendition(asset, resizeWidth, imageType));
}
if (layer != null) {
layer.rotate(rotationAngle);
LOGGER.debug("Applied rotation transformation ({} degrees).", rotationAngle);
appliedTransformation = true;
}
}
if (flipHorizontally) {
if (layer == null) {
layer = getLayer(getBestRendition(asset, resizeWidth, imageType));
}
if (layer != null) {
layer.flipHorizontally();
LOGGER.debug("Flipped image horizontally.");
appliedTransformation = true;
}
}
if (flipVertically) {
if (layer == null) {
layer = getLayer(getBestRendition(asset, resizeWidth, imageType));
}
if (layer != null) {
layer.flipVertically();
LOGGER.debug("Flipped image vertically.");
appliedTransformation = true;
}
}
if (!appliedTransformation) {
EnhancedRendition rendition = getBestRendition(asset, resizeWidth, imageType);
Dimension dimension = rendition.getDimension();
if (dimension != null) {
// keeping aspect ratio
originalHeight = Math.round(originalHeight * (dimension.width / (float) originalWidth));
originalWidth = dimension.width;
}
if (originalWidth > resizeWidth) {
int resizeHeight = calculateResizeHeight(originalWidth, originalHeight, resizeWidth);
if (resizeHeight > 0 && resizeHeight != originalHeight) {
layer = getLayer(rendition);
if (layer != null) {
if (layer.getBackground().getTransparency() != Transparency.OPAQUE && ("jpg".equalsIgnoreCase(extension) || "jpeg".equalsIgnoreCase(extension))) {
LOGGER.debug("Adding default (white) background to a transparent PNG: {}/{}", asset.getPath(), rendition.getName());
layer.setBackground(Color.white);
}
layer.resize(resizeWidth, resizeHeight);
response.setContentType(imageType);
LOGGER.debug("Resizing asset {}/{} to requested width of {}px; rendering.", asset.getPath(), rendition.getName(), resizeWidth);
layer.write(imageType, quality, response.getOutputStream());
} else {
streamOrConvert(response, rendition, imageType, imageName, resizeWidth, quality);
}
} else {
streamOrConvert(response, rendition, imageType, imageName, resizeWidth, quality);
}
} else {
streamOrConvert(response, rendition, imageType, imageName, resizeWidth, quality);
}
} else {
resizeAndStreamLayer(response, layer, imageType, resizeWidth, quality);
}
} else {
LOGGER.debug("No need to perform any processing on asset {}; rendering.", asset.getPath());
try (InputStream is = getOriginal(asset).getStream()) {
if (is != null) {
stream(response, is, imageType, imageName);
}
}
}
}
use of com.day.image.Layer in project acs-aem-commons by Adobe-Consulting-Services.
the class RenditionModifyingProcessTest method test_with_rendition_arg_getting_real_rendition.
@Test
public void test_with_rendition_arg_getting_real_rendition() throws Exception {
String path = "/content/dam/some/path.ext";
WorkItem workItem = mock(WorkItem.class);
WorkflowData data = mock(WorkflowData.class);
when(workItem.getWorkflowData()).thenReturn(data);
when(data.getPayloadType()).thenReturn(WorkflowHelper.TYPE_JCR_PATH);
when(data.getPayload()).thenReturn(path);
Resource resource = mock(Resource.class);
Asset asset = mock(Asset.class);
Rendition rendition = mock(Rendition.class);
when(resource.adaptTo(Asset.class)).thenReturn(asset);
when(resource.getResourceType()).thenReturn(DamConstants.NT_DAM_ASSET);
when(resourceResolver.getResource(path)).thenReturn(resource);
when(asset.getRendition(isA(RenditionPicker.class))).thenReturn(rendition);
when(rendition.getStream()).then(new Answer<InputStream>() {
@Override
public InputStream answer(InvocationOnMock invocation) throws Throwable {
return getClass().getResourceAsStream("/img/test.png");
}
});
when(harness.processLayer(any(Layer.class), eq(rendition), eq(workflowSession), any(String[].class))).thenAnswer(new Answer<Layer>() {
@Override
public Layer answer(InvocationOnMock invocation) throws Throwable {
return (Layer) invocation.getArguments()[0];
}
});
MetaDataMap metaData = new SimpleMetaDataMap();
metaData.put("PROCESS_ARGS", "renditionName:test");
process.execute(workItem, workflowSession, metaData, workflowHelper);
verify(harness, times(1)).processLayer(any(Layer.class), eq(rendition), eq(workflowSession), any(String[].class));
verify(harness, times(1)).saveImage(eq(asset), eq(rendition), any(Layer.class), eq("image/png"), eq(0.6));
}
use of com.day.image.Layer in project acs-aem-commons by Adobe-Consulting-Services.
the class LetterPillarBoxImageTransformerImplTest method testTransform_invalidHeightAndWidth.
@Test
public void testTransform_invalidHeightAndWidth() throws Exception {
final int width = -100;
final int height = -200;
map.put("width", width);
map.put("height", height);
ValueMap properties = new ValueMapDecorator(map);
final Layer mockLayer = mock(Layer.class);
final Transformer transformer = new Transformer(mockLayer);
when(layer.getWidth()).thenReturn(START_WIDTH);
when(layer.getHeight()).thenReturn(START_HEIGHT);
doNothing().when(mockLayer).blit(any(Layer.class), anyInt(), anyInt(), anyInt(), anyInt(), anyInt(), anyInt());
transformer.transform(layer, properties);
assertNotNull("Layer constructor was not intercepted.", transformer.layerCreated);
verify(layer, times(3)).getWidth();
verify(layer, times(3)).getHeight();
verify(layer, times(1)).resize(START_WIDTH, START_HEIGHT);
verify(mockLayer, times(1)).blit(layer, 0, 0, START_WIDTH, START_HEIGHT, 0, 0);
verifyNoMoreInteractions(layer);
verifyNoMoreInteractions(mockLayer);
}
use of com.day.image.Layer in project acs-aem-commons by Adobe-Consulting-Services.
the class LetterPillarBoxImageTransformerImplTest method testTransform.
@Test
public void testTransform() throws Exception {
final int width = 400;
final int height = 225;
map.put("width", width);
map.put("height", height);
ValueMap properties = new ValueMapDecorator(map);
final Layer mockLayer = mock(Layer.class);
final Transformer transformer = new Transformer(mockLayer);
when(layer.getWidth()).thenReturn(START_WIDTH, START_WIDTH, width);
when(layer.getHeight()).thenReturn(START_HEIGHT, START_HEIGHT, height);
doNothing().when(mockLayer).blit(any(Layer.class), anyInt(), anyInt(), anyInt(), anyInt(), anyInt(), anyInt());
transformer.transform(layer, properties);
assertNotNull("Layer constructor was not intercepted.", transformer.layerCreated);
verify(layer, times(3)).getWidth();
verify(layer, times(3)).getHeight();
verify(layer, times(1)).resize(width, height);
verify(mockLayer, times(1)).blit(layer, 0, 0, width, height, 0, 0);
verifyNoMoreInteractions(layer);
verifyNoMoreInteractions(mockLayer);
}
use of com.day.image.Layer in project acs-aem-commons by Adobe-Consulting-Services.
the class LetterPillarBoxImageTransformerImplTest method testTransform_withPillarBoxing.
@Test
public void testTransform_withPillarBoxing() throws Exception {
final int width = 600;
final int height = 600;
final int calcWidth = 338;
final int startPos = (width - calcWidth) / 2;
map.put("width", width);
map.put("height", height);
ValueMap properties = new ValueMapDecorator(map);
final Layer mockLayer = mock(Layer.class);
final Transformer transformer = new Transformer(mockLayer);
// Don't get confused, switching the dimensions forces Pillar boxing.
when(layer.getWidth()).thenReturn(START_HEIGHT, START_HEIGHT, calcWidth);
when(layer.getHeight()).thenReturn(START_WIDTH, START_WIDTH, height);
doNothing().when(mockLayer).blit(any(Layer.class), anyInt(), anyInt(), anyInt(), anyInt(), anyInt(), anyInt());
transformer.transform(layer, properties);
assertNotNull("Layer constructor was not intercepted.", transformer.layerCreated);
verify(layer, times(3)).getWidth();
verify(layer, times(3)).getHeight();
verify(layer, times(1)).resize(calcWidth, height);
verify(mockLayer, times(1)).blit(layer, startPos, 0, calcWidth, height, 0, 0);
verifyNoMoreInteractions(layer);
verifyNoMoreInteractions(mockLayer);
}
Aggregations