001/*
002 * (C) Copyright 2006-2015 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 */
018package org.nuxeo.ecm.platform.picture.magick.utils;
019
020import org.nuxeo.ecm.platform.commandline.executor.api.CmdParameters;
021import org.nuxeo.ecm.platform.commandline.executor.api.CommandException;
022import org.nuxeo.ecm.platform.commandline.executor.api.CommandLineExecutorService;
023import org.nuxeo.ecm.platform.commandline.executor.api.CommandNotAvailable;
024import org.nuxeo.ecm.platform.commandline.executor.api.ExecResult;
025import org.nuxeo.ecm.platform.picture.magick.MagickExecutor;
026import org.nuxeo.runtime.api.Framework;
027
028/**
029 * Unit command to crop and resize an picture.
030 *
031 * @author tiry
032 */
033public class ImageCropperAndResizer extends MagickExecutor {
034
035    public static final String DEFAULT_MAP_COMPONENTS = "rgb";
036
037    public static void cropAndResize(String inputFilePath, String outputFilePath, int tileWidth, int tileHeight,
038            int offsetX, int offsetY, int targetWidth, int targetHeight) throws CommandNotAvailable, CommandException {
039        cropAndResize(inputFilePath, outputFilePath, tileWidth, tileHeight, offsetX, offsetY, targetWidth,
040                targetHeight, null);
041    }
042
043    /** @since 5.9.5. */
044    public static void cropAndResize(String inputFilePath, String outputFilePath, int tileWidth, int tileHeight,
045            int offsetX, int offsetY, int targetWidth, int targetHeight, String mapComponents)
046            throws CommandNotAvailable, CommandException {
047
048        if (mapComponents == null) {
049            mapComponents = DEFAULT_MAP_COMPONENTS;
050        }
051
052        CommandLineExecutorService cles = Framework.getLocalService(CommandLineExecutorService.class);
053        CmdParameters params = cles.getDefaultCmdParameters();
054        params.addNamedParameter("tileWidth", String.valueOf(tileWidth));
055        params.addNamedParameter("tileHeight", String.valueOf(tileHeight));
056        params.addNamedParameter("offsetX", String.valueOf(offsetX));
057        params.addNamedParameter("offsetY", String.valueOf(offsetY));
058        params.addNamedParameter("targetWidth", String.valueOf(targetWidth));
059        params.addNamedParameter("targetHeight", String.valueOf(targetHeight));
060        params.addNamedParameter("inputFilePath", inputFilePath);
061        params.addNamedParameter("outputFilePath", outputFilePath);
062        params.addNamedParameter("mapComponents", mapComponents);
063        ExecResult res = cles.execCommand("cropAndResize", params);
064        if (!res.isSuccessful()) {
065            throw res.getError();
066        }
067    }
068
069}