Java BufferedImage Filter verticalFiltering(BufferedImage pbImage, int iOutH)

Here you can find the source of verticalFiltering(BufferedImage pbImage, int iOutH)

Description

vertical Filtering

License

Open Source License

Declaration

private static BufferedImage verticalFiltering(BufferedImage pbImage, int iOutH) 

Method Source Code


//package com.java2s;
import java.awt.image.BufferedImage;

public class Main {
    private static double[] contrib;
    private static double[] normContrib;
    private static double[] tmpContrib;
    private static int nDots;
    private static int nHalfDots;

    private static BufferedImage verticalFiltering(BufferedImage pbImage, int iOutH) {
        int iW = pbImage.getWidth();
        int iH = pbImage.getHeight();
        int value = 0;
        BufferedImage pbOut = new BufferedImage(iW, iOutH, BufferedImage.TYPE_INT_RGB);
        for (int y = 0; y < iOutH; y++) {
            int startY;
            int start;
            int Y = (int) (((double) y) * ((double) iH) / ((double) iOutH) + 0.5);

            startY = Y - nHalfDots;//from w ww  .  ja v  a2 s. c o m
            if (startY < 0) {
                startY = 0;
                start = nHalfDots - Y;
            } else {
                start = 0;
            }

            int stop;
            int stopY = Y + nHalfDots;
            if (stopY > (int) (iH - 1)) {
                stopY = iH - 1;
                stop = nHalfDots + (iH - 1 - Y);
            } else {
                stop = nHalfDots * 2;
            }

            if (start > 0 || stop < nDots - 1) {
                calTempContrib(start, stop);
                for (int x = 0; x < iW; x++) {
                    value = verticalFilter(pbImage, startY, stopY, start, stop, x, tmpContrib);
                    pbOut.setRGB(x, y, value);
                }
            } else {
                for (int x = 0; x < iW; x++) {
                    value = verticalFilter(pbImage, startY, stopY, start, stop, x, normContrib);
                    pbOut.setRGB(x, y, value);
                }
            }
        }

        return pbOut;
    }

    private static void calTempContrib(int start, int stop) {
        double weight = 0;

        int i = 0;
        for (i = start; i <= stop; i++) {
            weight += contrib[i];
        }

        for (i = start; i <= stop; i++) {
            tmpContrib[i] = contrib[i] / weight;
        }
    }

    private static int verticalFilter(BufferedImage pbInImage, int startY, int stopY, int start, int stop, int x,
            double[] pContrib) {
        double valueRed = 0.0;
        double valueGreen = 0.0;
        double valueBlue = 0.0;
        int valueRGB = 0;
        int i, j;

        for (i = startY, j = start; i <= stopY; i++, j++) {
            valueRGB = pbInImage.getRGB(x, i);

            valueRed += getRedValue(valueRGB) * pContrib[j];
            valueGreen += getGreenValue(valueRGB) * pContrib[j];
            valueBlue += getBlueValue(valueRGB) * pContrib[j];
        }

        return comRGB(clip((int) valueRed), clip((int) valueGreen), clip((int) valueBlue));
    }

    private static int getRedValue(int rgbValue) {
        int temp = rgbValue & 0x00ff0000;

        return temp >> 16;
    }

    private static int getGreenValue(int rgbValue) {
        int temp = rgbValue & 0x0000ff00;

        return temp >> 8;
    }

    private static int getBlueValue(int rgbValue) {

        return rgbValue & 0x000000ff;
    }

    private static int comRGB(int redValue, int greenValue, int blueValue) {

        return (redValue << 16) + (greenValue << 8) + blueValue;
    }

    private static int clip(int x) {
        if (x < 0)
            return 0;

        if (x > 255)
            return 255;

        return x;
    }
}

Related

  1. filterScale(BufferedImage img, float ratio)
  2. filterSmooth(BufferedImage img)
  3. filterThreshold(BufferedImage img, int threshold)
  4. horizontalFilter(BufferedImage bufImg, int startX, int stopX, int start, int stop, int y, double[] pContrib)
  5. thresholdFilter(int threshholdLevel, BufferedImage image)