Advertisement
Vita_Harvey

PA1 Gaussian

Jan 13th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.74 KB | None | 0 0
  1. /**
  2.  * @author Vita Wiebe
  3.  * @version PA1
  4.  */
  5.  
  6. public class Gaussian implements Filter
  7. {
  8.  
  9. /**
  10.      * Replace the image with a blurred version.
  11.      *
  12.      * @param theImage The image to modify
  13.      */
  14.     public void filter(PixelImage theImage) {
  15.        
  16.         // get the data from the image
  17.         Pixel[][] data = theImage.getData();
  18.        
  19.         // for each row....  
  20.         for (int row = 0; row < theImage.getHeight(); row++) {
  21.        
  22.             // for each column in said row, replace pixels with gray version
  23.             for (int col = 0; col < theImage.getWidth(); col++) {
  24.            
  25.                // each pixel px in data[row][col] gets an RGB array[3] corresponding to colors            
  26.                int[] px = data[row][col].rgb;
  27.                int red = px[Pixel.RED];
  28.                int green = px[Pixel.GREEN];
  29.                int blue = px[Pixel.BLUE];
  30.                
  31.                // Initialize temp accumulators to calc new RGB values
  32.                int newRed = 0;
  33.                int newGreen = 0;
  34.                int newBlue = 0;
  35.                
  36.                // A counter to store the current "index" of the 3x3 matrix.
  37.                int index = 1;
  38.                
  39.                // Initialize temp counters to calculate scaling factor of each color value.
  40.                int redScale = 0;
  41.                int greenScale = 0;
  42.                int blueScale = 0;
  43.                
  44.                // Iterate thru a 3x3 matrix of pixels
  45.                for (int i = 1; i < 4; i++) {
  46.            
  47.                   for (int j = 1; j < 4; j++) {
  48.                                      
  49.                      // Scaling factor temp variable for each color value in RGB.
  50.                      redScale += data[row][col].rgb[0];
  51.                      // new<color> is new color value for new pixel multiplied by weight.    
  52.                      newRed += data[row][col].rgb[0]*index;
  53.                      
  54.                      greenScale += data[row][col].rgb[1];
  55.                      newGreen += data[row][col].rgb[1]*index;
  56.                      
  57.                      blueScale += data[row][col].rgb[2];
  58.                      newBlue += data[row][col].rgb[2]*index;
  59.                  
  60.                      // Update index variable with current matrix position(for weighting)
  61.                      index++;
  62.                      // tempLast holds old RBG values for old pixel
  63.                      Pixel tempLast = data[row][col];          
  64.                      data[row][col] =
  65.                         new Pixel(newRed/redScale, newGreen/greenScale, newBlue/blueScale);
  66.                   }
  67.                }
  68.             }                            
  69.         }
  70.         theImage.setData(data);
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement