Guest User

Untitled

a guest
Dec 10th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. import java.awt.Color;
  2.  
  3. /**
  4. * Ujian Akhir Semester
  5. * Kelas PBO B
  6. * @author Hendra Ramadani (05111740000055)
  7. * 10 December 2018
  8. */
  9. public class FishEyeFilter extends Filter
  10. {
  11. // constants:
  12. private final static int SCALE = 20; // this defines the strenght of the filter
  13. private final static double TWO_PI = 2 * Math.PI;
  14.  
  15. /**
  16. * Constructor for objects of class LensFilter.
  17. * @param name The name of the filter.
  18. */
  19. public FishEyeFilter(String name)
  20. {
  21. super(name);
  22. }
  23.  
  24. /**
  25. * Apply this filter to an image.
  26. *
  27. * @param image The image to be changed by this filter.
  28. */
  29. public void apply(OFImage image)
  30. {
  31. int height = image.getHeight();
  32. int width = image.getWidth();
  33. OFImage original = new OFImage(image);
  34.  
  35. int[] xa = computeXArray(width);
  36. int[] ya = computeYArray(height);
  37.  
  38. for(int y = 0; y < height; y++) {
  39. for(int x = 0; x < width; x++) {
  40. image.setPixel(x, y, original.getPixel(x + xa[x], y + ya[y]));
  41. }
  42. }
  43. }
  44.  
  45. /**
  46. * Compute and return an array of horizontal offsets for each pixel column.
  47. * These can then be applied as the horizontal offset for each pixel.
  48. */
  49. private int[] computeXArray(int width)
  50. {
  51. int[] xArray = new int[width];
  52.  
  53. for(int i=0; i < width; i++) {
  54. xArray[i] = (int)(Math.sin( ((double)i / width) * TWO_PI) * SCALE);
  55. }
  56. return xArray;
  57. }
  58.  
  59. /**
  60. * Compute and return an array of vertical offsets for each pixel row.
  61. * These can then be applied as the vertical offset for each pixel.
  62. */
  63. private int[] computeYArray(int height)
  64. {
  65. int[] yArray = new int[height];
  66.  
  67. for(int i=0; i < height; i++) {
  68. yArray[i] = (int)(Math.sin( ((double)i / height) * TWO_PI) * SCALE);
  69. }
  70. return yArray;
  71. }
  72. }
Add Comment
Please, Sign In to add comment