Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. import java.awt.Color;
  2.  
  3. /**
  4. * An image filter to create an effect similar to a fisheye camera lens. (versi 3.0)
  5. * (Works especially well on portraits.)
  6. *
  7. * @author Yemima Sutanto
  8. * @version 1.0(29 November 2018)
  9. */
  10. public class FishEyeFilter extends Filter
  11. {
  12. // constants:
  13. private final static int SCALE = 20; // this defines the strenght of the filter
  14. private final static double TWO_PI = 2 * Math.PI;
  15.  
  16. /**
  17. * Constructor for objects of class LensFilter.
  18. * @param name The name of the filter.
  19. */
  20. public FishEyeFilter(String name)
  21. {
  22. super(name);
  23. }
  24.  
  25. /**
  26. * Apply this filter to an image.
  27. *
  28. * @param image The image to be changed by this filter.
  29. */
  30. public void apply(OFImage image)
  31. {
  32. int height = image.getHeight();
  33. int width = image.getWidth();
  34. OFImage original = new OFImage(image);
  35.  
  36. int[] xa = computeXArray(width);
  37. int[] ya = computeYArray(height);
  38.  
  39. for(int y = 0; y < height; y++) {
  40. for(int x = 0; x < width; x++) {
  41. image.setPixel(x, y, original.getPixel(x + xa[x], y + ya[y]));
  42. }
  43. }
  44. }
  45.  
  46. /**
  47. * Compute and return an array of horizontal offsets for each pixel column.
  48. * These can then be applied as the horizontal offset for each pixel.
  49. */
  50. private int[] computeXArray(int width)
  51. {
  52. int[] xArray = new int[width];
  53.  
  54. for(int i=0; i < width; i++) {
  55. xArray[i] = (int)(Math.sin( ((double)i / width) * TWO_PI) * SCALE);
  56. }
  57. return xArray;
  58. }
  59.  
  60. /**
  61. * Compute and return an array of vertical offsets for each pixel row.
  62. * These can then be applied as the vertical offset for each pixel.
  63. */
  64. private int[] computeYArray(int height)
  65. {
  66. int[] yArray = new int[height];
  67.  
  68. for(int i=0; i < height; i++) {
  69. yArray[i] = (int)(Math.sin( ((double)i / height) * TWO_PI) * SCALE);
  70. }
  71. return yArray;
  72. }
  73. }