001/*
002 * (C) Copyright 2006-2015 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 */
020package org.nuxeo.ecm.platform.picture.magick.utils;
021
022import static org.nuxeo.ecm.platform.picture.api.ImagingConvertConstants.JPEG_CONVERSATION_FORMAT;
023
024import java.io.File;
025
026import org.nuxeo.ecm.platform.commandline.executor.api.CmdParameters;
027import org.nuxeo.ecm.platform.commandline.executor.api.CommandException;
028import org.nuxeo.ecm.platform.commandline.executor.api.CommandLineExecutorService;
029import org.nuxeo.ecm.platform.commandline.executor.api.CommandNotAvailable;
030import org.nuxeo.ecm.platform.commandline.executor.api.ExecResult;
031import org.nuxeo.ecm.platform.picture.api.ImageInfo;
032import org.nuxeo.ecm.platform.picture.magick.MagickExecutor;
033import org.nuxeo.runtime.api.Framework;
034
035/**
036 * Unit command to extract a simplified view of a JPEG file using ImageMagick = extract the needed picture information
037 * to reach the target definition level
038 *
039 * @author tiry
040 */
041public class ImageResizer extends MagickExecutor {
042
043    public static ImageInfo resize(String inputFile, String outputFile, int targetWidth, int targetHeight,
044            int targetDepth) throws CommandNotAvailable, CommandException {
045        if (targetDepth == -1) {
046            targetDepth = ImageIdentifier.getInfo(inputFile).getDepth();
047        }
048        CommandLineExecutorService cles = Framework.getLocalService(CommandLineExecutorService.class);
049        CmdParameters params = cles.getDefaultCmdParameters();
050        params.addNamedParameter("targetWidth", String.valueOf(targetWidth));
051        params.addNamedParameter("targetHeight", String.valueOf(targetHeight));
052        params.addNamedParameter("inputFilePath", inputFile);
053        params.addNamedParameter("outputFilePath", outputFile);
054        params.addNamedParameter("targetDepth", String.valueOf(targetDepth));
055        String commandName = "resizer";
056        // hack to manage jpeg default background
057        if (outputFile.endsWith(JPEG_CONVERSATION_FORMAT)) {
058            commandName = "jpegResizer";
059        }
060        ExecResult res = cles.execCommand(commandName, params);
061        if (!res.isSuccessful()) {
062            throw res.getError();
063        }
064        if (new File(outputFile).exists()) {
065            return ImageIdentifier.getInfo(outputFile);
066        } else {
067            return null;
068        }
069    }
070
071}