view gnv-artifacts/src/main/java/de/intevation/gnv/raster/KernelFilter.java @ 1145:dfe1ac687c7f tip

added tags
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:16:15 +0200
parents f953c9a559d8
children
line wrap: on
line source
/*
 * Copyright (c) 2010 by Intevation GmbH
 *
 * This program is free software under the LGPL (>=v2.1)
 * Read the file LGPL.txt coming with the software for details
 * or visit http://www.gnu.org/licenses/ if it does not exist.
 */

package de.intevation.gnv.raster;

import org.apache.log4j.Logger;

import org.w3c.dom.Element;

/**
 * An implemenation of raster filters based on given kernels.
 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a>
 */
public class KernelFilter
implements   Filter
{
    private static Logger log = Logger.getLogger(KernelFilter.class);

    /**
     *  Implemens a factory which produces KernelFilters with Gauss kernels.
     */
    public static class GaussFactory
    implements          Filter.Factory
    {
        /**
         * Default sigma of Gauss kernel: {@value}
         */
        public static final double DEFAULT_SIGMA  = 1.0d;
        /**
         * Default radius of Gauss kernel: {@value}
         */
        public static final int    DEFAULT_RADIUS = 5;

        /**
         * Configured sigma.
         */
        protected double sigma;
        /**
         * Configured radius.
         */
        protected int    radius;

        /**
         * Default constructor.
         */
        public GaussFactory() {
            sigma  = DEFAULT_SIGMA;
            radius = DEFAULT_RADIUS;
        }

        public void init(Element element) {
            String s = element.getAttribute("sigma");
            String r = element.getAttribute("radius");

            if ((s = s.trim()).length() > 0) {
                try {
                    sigma = Math.abs(Double.parseDouble(s));
                }
                catch (NumberFormatException nfe) {
                    log.warn("gauss sigma '" + s + "' not a valid float value.");
                }
            }

            if ((r = r.trim()).length() > 0) {
                try {
                    radius = Math.min(3, Math.abs(Integer.parseInt(r)));
                }
                catch (NumberFormatException nfe) {
                    log.warn("gauss radius '" + r + "' not a valid integer value.");
                }
            }
        }

        public Filter create() {
            return new KernelFilter(Raster.Kernel.createGauss(sigma, radius));
        }
    } // class GaussFactory

    /**
     * The kernel used by this filter.
     */
    protected Raster.Kernel kernel;

    /**
     * Default constructor.
     */
    public KernelFilter() {
    }

    /**
     * Constructor to create a filter with a given kernel.
     * @param kernel The kernel to be used.
     */
    public KernelFilter(Raster.Kernel kernel) {
        this.kernel = kernel;
    }

    public Raster filter(Raster raster) {
        return raster.create(kernel);
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org