Advertisement
Guest User

Untitled

a guest
Apr 1st, 2015
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.89 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.image.BufferedImage;
  3.  
  4. @SuppressWarnings("unused")
  5. public class Sharpen implements BaseFilter {
  6.    
  7.     float[][] matrix =
  8.         {
  9.             {0f,    -0.67f, 0f},
  10.             {-0.67f, 3.67f, -0.67f},
  11.             {0f,  -0.67f, 0f}
  12.         };
  13.    
  14.     /*
  15.      * (2, 1): (0, 0) = (1, 0)
  16.      * (2, 1): (1, 0) = (2, 0)
  17.      * (2, 1): (2, 0) = (3, 0)
  18.      * (2, 1): (0, 0) = (1, 0)
  19.      * */
  20.    
  21.     @Override
  22.     public BufferedImage process(BufferedImage source) {
  23.         // TODO : implementacija
  24.        
  25.         int width = source.getWidth();
  26.         int height = source.getHeight();
  27.        
  28.         BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  29.        
  30.         for (int i = 0; i < width; i++) {
  31.             result.setRGB(i, 0, source.getRGB(i, 0));
  32.             result.setRGB(i, height - 1, source.getRGB(i, height - 1));
  33.         }
  34.        
  35.         for (int j = 0; j < height; j++) {
  36.             result.setRGB(0, j, source.getRGB(0, j));
  37.             result.setRGB(width-1, j, source.getRGB(width-1, j));
  38.         }
  39.        
  40.         for (int x = 1; x < width - 1; x++)
  41.             for (int y = 1; y < height - 1; y++) {
  42.                 int redValue = 0;
  43.                 int blueValue = 0;
  44.                 int greenValue = 0;
  45.                
  46.                 for (int xm = 0; xm < 3; xm++)
  47.                     for (int ym = 0; ym < 3; ym++) {
  48.                         int newX = x + xm - 1;
  49.                         int newY = y + ym -1;
  50.                        
  51.                         Color color = new Color(source.getRGB(newX, newY));
  52.                         float factor = matrix[xm][ym];
  53.                        
  54.                         int red = (int)(color.getRed() * factor);
  55.                         int green = (int)(color.getGreen() * factor);
  56.                         int blue = (int)(color.getBlue() * factor);
  57.                        
  58.                         redValue +=  red;
  59.                         blueValue += blue;
  60.                         greenValue += green;
  61.                     }
  62.                
  63.                 redValue = Math.min(255, Math.max(0, redValue));
  64.                 blueValue = Math.min(255, Math.max(0, blueValue));
  65.                 greenValue = Math.min(255, Math.max(0, greenValue));
  66.                
  67.                 int newRGB = new Color(redValue, greenValue, blueValue).getRGB();
  68.                 result.setRGB(x, y, newRGB);
  69.             }
  70.        
  71.         return result;
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement