Guest User

Untitled

a guest
Dec 10th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.util.List;
  3. import java.util.ArrayList;
  4.  
  5. /**
  6. * Ujian Akhir Semester
  7. * Kelas PBO B
  8. * @author Hendra Ramadani (05111740000055)
  9. * 10 December 2018
  10. */
  11. public class SmoothFilter extends Filter
  12. {
  13. private OFImage original;
  14. private int width;
  15. private int height;
  16.  
  17. /**
  18. * Constructor for objects of class SmoothFilter.
  19. * @param name The name of the filter.
  20. */
  21. public SmoothFilter(String name)
  22. {
  23. super(name);
  24. }
  25.  
  26. /**
  27. * Apply this filter to an image.
  28. *
  29. * @param image The image to be changed by this filter.
  30. */
  31. public void apply(OFImage image)
  32. {
  33. original = new OFImage(image);
  34. width = original.getWidth();
  35. height = original.getHeight();
  36.  
  37. for(int y = 0; y < height; y++) {
  38. for(int x = 0; x < width; x++) {
  39. image.setPixel(x, y, smooth(x, y));
  40. }
  41. }
  42. }
  43.  
  44. /**
  45. * Return a new color that is the smoothed color of a given
  46. * position. The "smoothed color" is the color value that is the
  47. * average of this pixel and all the adjacent pixels.
  48. * @param xpos The xposition of the pixel.
  49. * @param ypos The yposition of the pixel.
  50. * @return The smoothed color.
  51. */
  52. private Color smooth(int xpos, int ypos)
  53. {
  54. List<Color> pixels = new ArrayList<Color>(9);
  55.  
  56. for(int y = ypos - 1; y <= ypos + 1; y++) {
  57. for(int x = xpos - 1; x <= xpos + 1; x++) {
  58. if( x >= 0 && x < width && y >= 0 && y < height )
  59. pixels.add(original.getPixel(x, y));
  60. }
  61. }
  62.  
  63. return new Color(avgRed(pixels), avgGreen(pixels), avgBlue(pixels));
  64. }
  65.  
  66. /**
  67. * @param pixels The list of pixels.
  68. * @return The average of all the red values in the given list of pixels.
  69. */
  70. private int avgRed(List<Color> pixels)
  71. {
  72. int total = 0;
  73. for(Color color : pixels) {
  74. total += color.getRed();
  75. }
  76. return total / pixels.size();
  77. }
  78.  
  79. /**
  80. * @param pixels The list of pixels.
  81. * @return The average of all the green values in the given list of pixels.
  82. */
  83. private int avgGreen(List<Color> pixels)
  84. {
  85. int total = 0;
  86. for(Color color : pixels) {
  87. total += color.getGreen();
  88. }
  89. return total / pixels.size();
  90. }
  91.  
  92. /**
  93. * @param pixels The list of pixels.
  94. * @return The average of all the blue values in the given list of pixels.
  95. */
  96. private int avgBlue(List<Color> pixels)
  97. {
  98. int total = 0;
  99. for(Color color : pixels) {
  100. total += color.getBlue();
  101. }
  102. return total / pixels.size();
  103. }
  104. }
Add Comment
Please, Sign In to add comment