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