package ac.ooechs.oil.edgedetection;

import ac.essex.ooechs.imaging.commons.PixelLoader;
import ac.essex.ooechs.imaging.commons.StatisticsSolver;
import ac.essex.ooechs.imaging.commons.fast.FastStatistics;

import java.awt.image.BufferedImage;
import java.awt.*;

/**
 * <p/>
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version,
 * provided that any use properly credits the author.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details at http://www.gnu.org
 * </p>
 *
 * @author Olly Oechsle, University of Essex, Date: 25-Feb-2008
 * @version 1.0
 */
public class PipeEdgeDetector {

    final static int blockSize = 10;
    final static int threshold = 540;

    public static boolean[][] run(PixelLoader image) {

        boolean[][] mask = new boolean[image.getWidth()][image.getHeight()];

        for (int by = 0; by <= image.getHeight() - blockSize; by += blockSize)

            for (int bx = 0; bx <= image.getWidth() - blockSize; bx += blockSize) {

                FastStatistics s = new FastStatistics();

                for (int dy = 0; dy < blockSize; dy++) {

                    int y = by + dy;

                    for (int dx = 0; dx < blockSize; dx++) {

                        int x = bx + dx;

                        s.addData(image.get3x3Variance(x, y));

                    }
                }

                if (s.getMean() < threshold) {
                    for (int dy = 0; dy < blockSize; dy++) {

                        int y = by + dy;

                        for (int dx = 0; dx < blockSize; dx++) {

                            int x = bx + dx;

                            mask[x][y] = true;

                        }
                    }
                }

            }
        
        return mask;
    }

}
