Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @author Vita Wiebe
- * @version PA1
- */
- public class Gaussian implements Filter
- {
- /**
- * Replace the image with a blurred version.
- *
- * @param theImage The image to modify
- */
- public void filter(PixelImage theImage) {
- // get the data from the image
- Pixel[][] data = theImage.getData();
- // for each row....
- for (int row = 0; row < theImage.getHeight(); row++) {
- // for each column in said row, replace pixels with gray version
- for (int col = 0; col < theImage.getWidth(); col++) {
- // each pixel px in data[row][col] gets an RGB array[3] corresponding to colors
- int[] px = data[row][col].rgb;
- int red = px[Pixel.RED];
- int green = px[Pixel.GREEN];
- int blue = px[Pixel.BLUE];
- // Initialize temp accumulators to calc new RGB values
- int newRed = 0;
- int newGreen = 0;
- int newBlue = 0;
- // A counter to store the current "index" of the 3x3 matrix.
- int index = 1;
- // Initialize temp counters to calculate scaling factor of each color value.
- int redScale = 0;
- int greenScale = 0;
- int blueScale = 0;
- // Iterate thru a 3x3 matrix of pixels
- for (int i = 1; i < 4; i++) {
- for (int j = 1; j < 4; j++) {
- // Scaling factor temp variable for each color value in RGB.
- redScale += data[row][col].rgb[0];
- // new<color> is new color value for new pixel multiplied by weight.
- newRed += data[row][col].rgb[0]*index;
- greenScale += data[row][col].rgb[1];
- newGreen += data[row][col].rgb[1]*index;
- blueScale += data[row][col].rgb[2];
- newBlue += data[row][col].rgb[2]*index;
- // Update index variable with current matrix position(for weighting)
- index++;
- // tempLast holds old RBG values for old pixel
- Pixel tempLast = data[row][col];
- data[row][col] =
- new Pixel(newRed/redScale, newGreen/greenScale, newBlue/blueScale);
- }
- }
- }
- }
- theImage.setData(data);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement