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