Advertisement
Vita_Harvey

PA1 Gaussian_02

Jan 13th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.12 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.                Pixel tempLast = data[row][col];
  37.                // Iterate thru a 3x3 matrix of pixels
  38.                for (int i = 1; i < 10; i++) {
  39.                      if (i != 5) {      
  40.                      // new<color> is new color value for new pixel multiplied by weight.    
  41.                      newRed += data[row][col].rgb[0] * i;
  42.                      newRed += tempLast.rgb[0];
  43.                      // greenScale += data[row][col].rgb[1];
  44.                      newGreen += data[row][col].rgb[1] * i;
  45.                      newGreen += tempLast.rgb[1];
  46.                      // blueScale += data[row][col].rgb[2];
  47.                      newBlue += data[row][col].rgb[2] * i;
  48.                      newBlue += tempLast.rgb[2];          
  49.                      data[row][col] =
  50.                         new Pixel(newRed/45, newGreen/45, newBlue/45);              
  51.                }
  52.              }                            
  53.          }
  54.         theImage.setData(data);
  55.         }
  56.     }  
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement