001/*
002 * (C) Copyright 2002-2007 Nuxeo SAS (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.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.convert;
019
020import static org.nuxeo.ecm.platform.picture.api.ImagingConvertConstants.CONVERSION_FORMAT;
021import static org.nuxeo.ecm.platform.picture.api.ImagingConvertConstants.OPTION_RESIZE_DEPTH;
022import static org.nuxeo.ecm.platform.picture.api.ImagingConvertConstants.OPTION_RESIZE_HEIGHT;
023import static org.nuxeo.ecm.platform.picture.api.ImagingConvertConstants.OPTION_RESIZE_WIDTH;
024
025import java.io.Serializable;
026import java.util.ArrayList;
027import java.util.List;
028import java.util.Map;
029
030import org.apache.commons.logging.Log;
031import org.apache.commons.logging.LogFactory;
032import org.nuxeo.ecm.core.api.Blob;
033import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
034import org.nuxeo.ecm.core.convert.api.ConversionException;
035import org.nuxeo.ecm.core.convert.cache.SimpleCachableBlobHolder;
036import org.nuxeo.ecm.core.convert.extension.Converter;
037import org.nuxeo.ecm.core.convert.extension.ConverterDescriptor;
038import org.nuxeo.ecm.platform.picture.api.ImagingService;
039import org.nuxeo.runtime.api.Framework;
040
041/**
042 * @author <a href="mailto:ldoguin@nuxeo.com">Laurent Doguin</a>
043 */
044public class ResizePictureConverter implements Converter {
045
046    private static final Log log = LogFactory.getLog(ResizePictureConverter.class);
047
048    @Override
049    public BlobHolder convert(BlobHolder blobHolder, Map<String, Serializable> parameters) throws ConversionException {
050        ImagingService service = Framework.getService(ImagingService.class);
051        List<Blob> sources = blobHolder.getBlobs();
052        List<Blob> results = new ArrayList<Blob>(sources.size());
053        Serializable h = parameters.get(OPTION_RESIZE_HEIGHT);
054        int height = ConverterUtils.getInteger(h);
055        Serializable w = parameters.get(OPTION_RESIZE_WIDTH);
056        int width = ConverterUtils.getInteger(w);
057        Serializable d = parameters.get(OPTION_RESIZE_DEPTH);
058        int depth = ConverterUtils.getInteger(d);
059        // use the registered conversion format
060        String format = (String) parameters.get(CONVERSION_FORMAT);
061        for (Blob source : sources) {
062            if (source != null) {
063                Blob result = service.resize(source, format, width, height, depth);
064                if (result != null) {
065                    results.add(result);
066                }
067            }
068        }
069        return new SimpleCachableBlobHolder(results);
070    }
071
072    @Override
073    public void init(ConverterDescriptor descriptor) {
074    }
075
076}