Search in sources :

Example 1 with ImageTransformationOptions

use of org.alfresco.repo.content.transform.magick.ImageTransformationOptions in project alfresco-repository by Alfresco.

the class ThumbnailRenditionConvertor method convert.

public ThumbnailDefinition convert(RenditionDefinition renditionDefinition) {
    ThumbnailDefinition thDefn = new ThumbnailDefinition();
    Map<String, Serializable> params = renditionDefinition.getParameterValues();
    // parameters common to all the built-in thumbnail definitions
    Serializable mimeTypeParam = params.get(AbstractRenderingEngine.PARAM_MIME_TYPE);
    thDefn.setMimetype((String) mimeTypeParam);
    thDefn.setName(renditionDefinition.getRenditionName().getLocalName());
    Serializable placeHolderResourcePathParam = params.get(AbstractRenderingEngine.PARAM_PLACEHOLDER_RESOURCE_PATH);
    if (placeHolderResourcePathParam != null) {
        thDefn.setPlaceHolderResourcePath((String) placeHolderResourcePathParam);
    }
    TransformationOptions transformationOptions = null;
    Serializable flashVersion = renditionDefinition.getParameterValue(ReformatRenderingEngine.PARAM_FLASH_VERSION);
    if (flashVersion != null) {
        // Thumbnails based on SWFTransformationOptions
        transformationOptions = new SWFTransformationOptions();
        SWFTransformationOptions swfTranOpts = (SWFTransformationOptions) transformationOptions;
        swfTranOpts.setFlashVersion((String) flashVersion);
    } else {
        // Thumbnails based on ImageTransformationOptions
        transformationOptions = new ImageTransformationOptions();
        ImageTransformationOptions imgTrOpts = (ImageTransformationOptions) transformationOptions;
        ImageResizeOptions resizeOptions = new ImageResizeOptions();
        Serializable xsize = renditionDefinition.getParameterValue(ImageRenderingEngine.PARAM_RESIZE_WIDTH);
        if (xsize != null) {
            resizeOptions.setWidth(((Integer) xsize).intValue());
        }
        Serializable ysize = renditionDefinition.getParameterValue(ImageRenderingEngine.PARAM_RESIZE_HEIGHT);
        if (ysize != null) {
            resizeOptions.setHeight(((Integer) ysize).intValue());
        }
        Serializable maintainAspectRatio = renditionDefinition.getParameterValue(ImageRenderingEngine.PARAM_MAINTAIN_ASPECT_RATIO);
        if (maintainAspectRatio != null) {
            resizeOptions.setMaintainAspectRatio((Boolean) maintainAspectRatio);
        }
        Serializable resizeToThumbnail = renditionDefinition.getParameterValue(ImageRenderingEngine.PARAM_RESIZE_TO_THUMBNAIL);
        if (resizeToThumbnail != null) {
            resizeOptions.setResizeToThumbnail((Boolean) resizeToThumbnail);
        }
        Serializable allowEnlargement = renditionDefinition.getParameterValue(ImageRenderingEngine.PARAM_ALLOW_ENLARGEMENT);
        if (allowEnlargement != null) {
            resizeOptions.setAllowEnlargement((Boolean) allowEnlargement);
        }
        imgTrOpts.setResizeOptions(resizeOptions);
    }
    thDefn.setTransformationOptions(transformationOptions);
    TransformationOptionLimits limits = transformationOptions.getLimits();
    Serializable v = params.get(OPT_TIMEOUT_MS);
    if (v != null) {
        limits.setTimeoutMs((Long) v);
    }
    v = params.get(OPT_READ_LIMIT_TIME_MS);
    if (v != null) {
        limits.setReadLimitTimeMs((Long) v);
    }
    v = params.get(OPT_MAX_SOURCE_SIZE_K_BYTES);
    if (v != null) {
        limits.setMaxSourceSizeKBytes((Long) v);
    }
    v = params.get(OPT_READ_LIMIT_K_BYTES);
    if (v != null) {
        limits.setReadLimitKBytes((Long) v);
    }
    v = params.get(OPT_MAX_PAGES);
    if (v != null) {
        limits.setMaxPages((Integer) v);
    }
    v = params.get(OPT_PAGE_LIMIT);
    if (v != null) {
        limits.setPageLimit((Integer) v);
    }
    return thDefn;
}
Also used : Serializable(java.io.Serializable) TransformationOptionLimits(org.alfresco.service.cmr.repository.TransformationOptionLimits) SWFTransformationOptions(org.alfresco.repo.content.transform.swf.SWFTransformationOptions) ImageTransformationOptions(org.alfresco.repo.content.transform.magick.ImageTransformationOptions) ImageResizeOptions(org.alfresco.repo.content.transform.magick.ImageResizeOptions) SWFTransformationOptions(org.alfresco.repo.content.transform.swf.SWFTransformationOptions) ImageTransformationOptions(org.alfresco.repo.content.transform.magick.ImageTransformationOptions) TransformationOptions(org.alfresco.service.cmr.repository.TransformationOptions)

Example 2 with ImageTransformationOptions

use of org.alfresco.repo.content.transform.magick.ImageTransformationOptions in project alfresco-repository by Alfresco.

the class AlfrescoPdfRendererContentTransformerWorker method getSourcePageRange.

/**
 * Gets the page range from the source to use in the command line.
 *
 * @param options the transformation options
 * @param sourceMimetype the source mimetype
 * @param targetMimetype the target mimetype
 * @return the source page range for the command line
 */
private String getSourcePageRange(TransformationOptions options, String sourceMimetype, String targetMimetype) {
    if (options instanceof ImageTransformationOptions) {
        ImageTransformationOptions imageOptions = (ImageTransformationOptions) options;
        PagedSourceOptions pagedSourceOptions = imageOptions.getSourceOptions(PagedSourceOptions.class);
        if (pagedSourceOptions != null) {
            if (pagedSourceOptions.getStartPageNumber() != null && pagedSourceOptions.getEndPageNumber() != null) {
                if (pagedSourceOptions.getStartPageNumber().equals(pagedSourceOptions.getEndPageNumber())) {
                    return "" + (pagedSourceOptions.getStartPageNumber() - 1);
                } else {
                    throw new AlfrescoRuntimeException("The alfresco-pdf-renderer can only convert single pages, no page ranges.");
                }
            } else {
                throw new AlfrescoRuntimeException("The alfresco-pdf-renderer can only convert single pages, no page ranges.");
            }
        }
    }
    return "0";
}
Also used : ImageTransformationOptions(org.alfresco.repo.content.transform.magick.ImageTransformationOptions) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) PagedSourceOptions(org.alfresco.service.cmr.repository.PagedSourceOptions)

Example 3 with ImageTransformationOptions

use of org.alfresco.repo.content.transform.magick.ImageTransformationOptions in project alfresco-repository by Alfresco.

the class RenditionServiceIntegrationTest method testRenderCropImage.

/**
 * This test method used the RenditionService to render a test image (of
 * type PNG) as a cropped image of the same type.
 */
@SuppressWarnings("unused")
@Test
public void testRenderCropImage() throws Exception {
    final int originalImageWidth = 512;
    final int originalImageHeight = 512;
    // Create a rendition of an existing image with specified absolute x and
    // y scale.
    final int imageNewXSize = 36;
    final int imageNewYSize = 47;
    final Map<String, Serializable> parameterValues = new HashMap<String, Serializable>();
    parameterValues.put(CropSourceOptionsSerializer.PARAM_CROP_WIDTH, imageNewXSize);
    parameterValues.put(CropSourceOptionsSerializer.PARAM_CROP_HEIGHT, imageNewYSize);
    ImageTransformationOptions imageTransformationOptions = new ImageTransformationOptions();
    final NodeRef newRenditionNode = performImageRendition(parameterValues, nodeWithImageContent);
    // Assert that the rendition is of the correct size and has reasonable
    // content.
    transactionHelper.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<Void>() {

        public Void execute() throws Throwable {
            // The rescaled image rendition is a child of the original test
            // node.
            List<ChildAssociationRef> children = nodeService.getChildAssocs(nodeWithImageContent, new RegexQNamePattern(getLongNameWithEscapedBraces(RenditionModel.ASSOC_RENDITION)), new RegexQNamePattern(getLongNameWithEscapedBraces(RESCALE_RENDER_DEFN_NAME)));
            // There should only be one child of the image node: the
            // rendition we've just created.
            assertEquals("Unexpected number of children", 1, children.size());
            NodeRef newImageRendition = children.get(0).getChildRef();
            assertEquals(newRenditionNode, newImageRendition);
            ContentReader reader = contentService.getReader(newImageRendition, ContentModel.PROP_CONTENT);
            assertNotNull("Reader to rendered image was null", reader);
            BufferedImage img = ImageIO.read(reader.getContentInputStream());
            assertEquals("Rendered image had wrong height", imageNewYSize, img.getHeight());
            assertEquals("Rendered image had wrong width", imageNewXSize, img.getWidth());
            ContentReader srcReader = contentService.getReader(nodeWithImageContent, ContentModel.PROP_CONTENT);
            BufferedImage srcImg = ImageIO.read(srcReader.getContentInputStream());
            // The upper left pixel of the image should be pure black.
            int rgbAtTopLeft = img.getRGB(1, 1);
            int expRgbAtTopLeft = img.getRGB(1, 1);
            assertEquals("Incorrect image content.", expRgbAtTopLeft, rgbAtTopLeft);
            // The lower right pixel of the image should be pure white
            int rightIndex = img.getWidth() - 1;
            int bottomIndex = img.getHeight() - 1;
            int rgbAtBottomRight = img.getRGB(rightIndex, bottomIndex);
            int expRgbAtBottomRight = srcImg.getRGB(rightIndex, bottomIndex);
            assertEquals("Incorrect image content.", expRgbAtBottomRight, rgbAtBottomRight);
            return null;
        }
    });
    // Create a rendition of the same image, this time cropping by 50/25%
    parameterValues.clear();
    // 256 picels
    parameterValues.put(CropSourceOptionsSerializer.PARAM_CROP_WIDTH, 50);
    // 128 pixels
    parameterValues.put(CropSourceOptionsSerializer.PARAM_CROP_HEIGHT, 25);
    parameterValues.put(CropSourceOptionsSerializer.PARAM_IS_PERCENT_CROP, true);
    final NodeRef secondRenditionNode = performImageRendition(parameterValues, nodeWithImageContent);
    // Assert that the rendition is of the correct size and has reasonable
    // content.
    transactionHelper.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<Void>() {

        public Void execute() throws Throwable {
            // The rescaled image rendition is a child of the original test
            // node.
            List<ChildAssociationRef> children = nodeService.getChildAssocs(nodeWithImageContent, new RegexQNamePattern(getLongNameWithEscapedBraces(RenditionModel.ASSOC_RENDITION)), new RegexQNamePattern(getLongNameWithEscapedBraces(RESCALE_RENDER_DEFN_NAME)));
            // There should only be one child of the image node: the
            // rendition we've just created.
            assertEquals("Unexpected number of children", 1, children.size());
            NodeRef newImageRendition = children.get(0).getChildRef();
            assertEquals(secondRenditionNode, newImageRendition);
            ContentReader srcReader = contentService.getReader(nodeWithImageContent, ContentModel.PROP_CONTENT);
            BufferedImage srcImg = ImageIO.read(srcReader.getContentInputStream());
            ContentReader reader = contentService.getReader(newImageRendition, ContentModel.PROP_CONTENT);
            assertNotNull("Reader to rendered image was null", reader);
            BufferedImage img = ImageIO.read(reader.getContentInputStream());
            assertEquals("Rendered image had wrong height", 128, img.getHeight());
            assertEquals("Rendered image had wrong width", 256, img.getWidth());
            // The upper left pixel of the image should be pure black.
            int rgbAtTopLeft = img.getRGB(1, 1);
            int expRgbAtTopLeft = srcImg.getRGB(1, 1);
            assertEquals("Incorrect image content.", expRgbAtTopLeft, rgbAtTopLeft);
            // The lower right pixel of the image should be pure white
            int widthIndex = img.getWidth() - 1;
            int heightIndex = img.getHeight() - 1;
            int rgbAtBottomRight = img.getRGB(widthIndex, heightIndex);
            int expRgbAtBottomRight = srcImg.getRGB(widthIndex, heightIndex);
            assertEquals("Incorrect image content.", expRgbAtBottomRight, rgbAtBottomRight);
            return null;
        }
    });
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) RetryingTransactionHelper(org.alfresco.repo.transaction.RetryingTransactionHelper) ContentReader(org.alfresco.service.cmr.repository.ContentReader) ImageTransformationOptions(org.alfresco.repo.content.transform.magick.ImageTransformationOptions) BufferedImage(java.awt.image.BufferedImage) NodeRef(org.alfresco.service.cmr.repository.NodeRef) RegexQNamePattern(org.alfresco.service.namespace.RegexQNamePattern) List(java.util.List) ArrayList(java.util.ArrayList) BaseAlfrescoSpringTest(org.alfresco.util.BaseAlfrescoSpringTest) Test(org.junit.Test) AbstractContentTransformerTest(org.alfresco.repo.content.transform.AbstractContentTransformerTest)

Example 4 with ImageTransformationOptions

use of org.alfresco.repo.content.transform.magick.ImageTransformationOptions in project alfresco-repository by Alfresco.

the class TransformationOptionsConverterTest method testImageTransformationOptionsCropGravity.

@Test
public // Checks we do what was in the legacy ImageMagickContentTransformerWorker
void testImageTransformationOptionsCropGravity() {
    ImageTransformationOptions oldOptions = new ImageTransformationOptions();
    oldOptions.setAutoOrient(false);
    CropSourceOptions cropOptions = new CropSourceOptions();
    cropOptions.setGravity("North");
    oldOptions.addSourceOptions(cropOptions);
    assertConverterToMapAndBack(oldOptions, MIMETYPE_IMAGE_JPEG, MIMETYPE_IMAGE_PNG, "ImageTransformationOptions [commandOptions=, resizeOptions=null, autoOrient=false], " + "sourceOptions={ CropSourceOptionsCropSourceOptions " + "[height=-1, width=-1, xOffset=0, yOffset=0, isPercentageCrop=false, gravity=North]} ]", "cropGravity=North " + "cropXOffset=0 " + "cropYOffset=0 " + "timeout=-1 ", true);
}
Also used : ImageTransformationOptions(org.alfresco.repo.content.transform.magick.ImageTransformationOptions) CropSourceOptions(org.alfresco.service.cmr.repository.CropSourceOptions) Test(org.junit.Test)

Example 5 with ImageTransformationOptions

use of org.alfresco.repo.content.transform.magick.ImageTransformationOptions in project alfresco-repository by Alfresco.

the class TransformationOptionsConverterTest method testImageTransformationOptionsResizeNoEnlargementOrAspectRatio.

@Test
public // Checks we do what was in the legacy ImageMagickContentTransformerWorker
void testImageTransformationOptionsResizeNoEnlargementOrAspectRatio() {
    ImageTransformationOptions oldOptions = new ImageTransformationOptions();
    oldOptions.setAutoOrient(false);
    ImageResizeOptions imageResizeOptions = new ImageResizeOptions();
    imageResizeOptions.setAllowEnlargement(false);
    imageResizeOptions.setMaintainAspectRatio(false);
    oldOptions.setResizeOptions(imageResizeOptions);
    assertConverterToMapAndBack(oldOptions, MIMETYPE_IMAGE_JPEG, MIMETYPE_IMAGE_PNG, "ImageTransformationOptions [commandOptions=, " + "resizeOptions=ImageResizeOptions [width=-1, height=-1, maintainAspectRatio=false, " + "percentResize=false, resizeToThumbnail=false, allowEnlargement=false], autoOrient=false]]", "allowEnlargement=false " + "maintainAspectRatio=false " + "timeout=-1 ", true);
}
Also used : ImageTransformationOptions(org.alfresco.repo.content.transform.magick.ImageTransformationOptions) ImageResizeOptions(org.alfresco.repo.content.transform.magick.ImageResizeOptions) Test(org.junit.Test)

Aggregations

ImageTransformationOptions (org.alfresco.repo.content.transform.magick.ImageTransformationOptions)35 Test (org.junit.Test)26 ImageResizeOptions (org.alfresco.repo.content.transform.magick.ImageResizeOptions)22 NodeRef (org.alfresco.service.cmr.repository.NodeRef)8 PagedSourceOptions (org.alfresco.service.cmr.repository.PagedSourceOptions)8 HashMap (java.util.HashMap)7 AbstractContentTransformerTest (org.alfresco.repo.content.transform.AbstractContentTransformerTest)7 CropSourceOptions (org.alfresco.service.cmr.repository.CropSourceOptions)7 BaseAlfrescoSpringTest (org.alfresco.util.BaseAlfrescoSpringTest)7 Serializable (java.io.Serializable)5 SWFTransformationOptions (org.alfresco.repo.content.transform.swf.SWFTransformationOptions)4 TemporalSourceOptions (org.alfresco.service.cmr.repository.TemporalSourceOptions)3 TransformationSourceOptions (org.alfresco.service.cmr.repository.TransformationSourceOptions)3 File (java.io.File)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 RuntimeExecutableContentTransformerOptions (org.alfresco.repo.content.transform.RuntimeExecutableContentTransformerOptions)2 ThumbnailParentAssociationDetails (org.alfresco.service.cmr.thumbnail.ThumbnailParentAssociationDetails)2 BufferedImage (java.awt.image.BufferedImage)1 Arrays (java.util.Arrays)1