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 static org.nuxeo.ecm.platform.picture.api.ImagingConvertConstants.JPEG_CONVERSATION_FORMAT;
021
022import java.io.File;
023
024import org.nuxeo.ecm.platform.commandline.executor.api.CmdParameters;
025import org.nuxeo.ecm.platform.commandline.executor.api.CommandException;
026import org.nuxeo.ecm.platform.commandline.executor.api.CommandLineExecutorService;
027import org.nuxeo.ecm.platform.commandline.executor.api.CommandNotAvailable;
028import org.nuxeo.ecm.platform.commandline.executor.api.ExecResult;
029import org.nuxeo.ecm.platform.picture.api.ImageInfo;
030import org.nuxeo.ecm.platform.picture.magick.MagickExecutor;
031import org.nuxeo.runtime.api.Framework;
032
033/**
034 * Unit command to extract a simplified view of a JPEG file using ImageMagick = extract the needed picture information
035 * to reach the target definition level
036 *
037 * @author tiry
038 */
039public class ImageResizer extends MagickExecutor {
040
041    public static ImageInfo resize(String inputFile, String outputFile, int targetWidth, int targetHeight,
042            int targetDepth) throws CommandNotAvailable, CommandException {
043        if (targetDepth == -1) {
044            targetDepth = ImageIdentifier.getInfo(inputFile).getDepth();
045        }
046        CommandLineExecutorService cles = Framework.getLocalService(CommandLineExecutorService.class);
047        CmdParameters params = cles.getDefaultCmdParameters();
048        params.addNamedParameter("targetWidth", String.valueOf(targetWidth));
049        params.addNamedParameter("targetHeight", String.valueOf(targetHeight));
050        params.addNamedParameter("inputFilePath", inputFile);
051        params.addNamedParameter("outputFilePath", outputFile);
052        params.addNamedParameter("targetDepth", String.valueOf(targetDepth));
053        String commandName = "resizer";
054        // hack to manage jpeg default background
055        if (outputFile.endsWith(JPEG_CONVERSATION_FORMAT)) {
056            commandName = "jpegResizer";
057        }
058        ExecResult res = cles.execCommand(commandName, params);
059        if (!res.isSuccessful()) {
060            throw res.getError();
061        }
062        if (new File(outputFile).exists()) {
063            return ImageIdentifier.getInfo(outputFile);
064        } else {
065            return null;
066        }
067    }
068
069}