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