001/*
002 * (C) Copyright 2006-2008 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 * $Id$
018 *
019 */
020package org.nuxeo.ecm.platform.pictures.tiles.gimp.tiler;
021
022import java.io.IOException;
023import java.util.ArrayList;
024import java.util.List;
025import java.util.Map;
026
027import org.nuxeo.ecm.core.api.NuxeoException;
028import org.nuxeo.ecm.platform.picture.api.ImageInfo;
029import org.nuxeo.ecm.platform.pictures.tiles.api.PictureTiles;
030import org.nuxeo.ecm.platform.pictures.tiles.api.PictureTilesImpl;
031import org.nuxeo.ecm.platform.pictures.tiles.gimp.GimpExecutor;
032import org.nuxeo.ecm.platform.pictures.tiles.tilers.BasePictureTiler;
033
034/**
035 * Gimp based tiler uses a Gimp specific procedure to generate tiles from an picture file Tiles can be generated one by
036 * one or 9 by 9
037 *
038 * @author tiry
039 */
040public class GimpTiler extends BasePictureTiler {
041
042    public String getName() {
043        return "GimpTiler";
044    }
045
046    public boolean needsSync() {
047        return true;
048    }
049
050    public PictureTiles getTilesFromFile(ImageInfo input, String outputDirPath, int tileWidth, int tileHeight,
051            int maxTiles, int xCenter, int yCenter, long lastModificationTime, boolean fullGeneration)
052            {
053
054        String inputFilePath = input.getFilePath();
055
056        if (fullGeneration) {
057            // force full generation
058            xCenter = -1;
059            yCenter = -1;
060        }
061
062        Map<String, String> result = null;
063
064        if (!outputDirPath.endsWith("/"))
065            outputDirPath += "/";
066
067        try {
068            result = exec("python-fu-nx-tiles", inputFilePath, outputDirPath, tileWidth, tileHeight, maxTiles, xCenter,
069                    yCenter);
070        } catch (IOException e) {
071            throw new NuxeoException("Error while calling gimp command line", e);
072        }
073
074        result.put(PictureTilesImpl.TILE_INPUT_FILE_KEY, inputFilePath);
075        result.put(PictureTilesImpl.TILES_WIDTH_KEY, Integer.toString(tileWidth));
076        result.put(PictureTilesImpl.TILES_HEIGHT_KEY, Integer.toString(tileHeight));
077        result.put(PictureTilesImpl.MAX_TILES_KEY, Integer.toString(maxTiles));
078        result.put(PictureTilesImpl.TILE_OUTPUT_DIR_KEY, outputDirPath);
079        result.put(PictureTilesImpl.TILES_PREFIX_KEY, "tile");
080        result.put(PictureTilesImpl.TILES_SUFFIX_KEY, ".jpg");
081        result.put(PictureTilesImpl.PROGRESSIVE_TILING_KEY, Boolean.toString(!fullGeneration));
082
083        PictureTiles tiles = null;
084        tiles = new PictureTilesImpl(result);
085        return tiles;
086
087    }
088
089    protected Map<String, String> exec(String procName, String inputFilePath, String outputPath, int tileX, int tileY,
090            int nbTiles, int centerXTile, int centerYTile) throws IOException {
091
092        List<Object> params = new ArrayList<Object>();
093
094        params.add(inputFilePath);
095        params.add(new Integer(tileX));
096        params.add(new Integer(tileY));
097        params.add(new Integer(nbTiles));
098        params.add(outputPath);
099        params.add(new Integer(centerXTile));
100        params.add(new Integer(centerYTile));
101
102        return GimpExecutor.exec(procName, params);
103    }
104
105}