igede

Untitled

Nov 26th, 2018
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.05 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.image.*;
  3. import javax.swing.*;
  4.  
  5. /**
  6.  * OFImage is a class that defines an image in OF (Objects First) format.
  7.  *
  8.  * @author  Gede
  9.  * @version (26/11/2018)
  10.  */
  11. public class OFImage extends BufferedImage
  12. {
  13.     /**
  14.      * Create an OFImage copied from a BufferedImage.
  15.      * @param image The image to copy.
  16.      */
  17.     public OFImage(BufferedImage image)
  18.     {
  19.          super(image.getColorModel(), image.copyData(null),
  20.                image.isAlphaPremultiplied(), null);
  21.     }
  22.  
  23.     /**
  24.      * Create an OFImage with specified size and unspecified content.
  25.      * @param width The width of the image.
  26.      * @param height The height of the image.
  27.      */
  28.     public OFImage(int width, int height)
  29.     {
  30.         super(width, height, TYPE_INT_RGB);
  31.     }
  32.  
  33.     /**
  34.      * Set a given pixel of this image to a specified color. The
  35.      * color is represented as an (r,g,b) value.
  36.      * @param x The x position of the pixel.
  37.      * @param y The y position of the pixel.
  38.      * @param col The color of the pixel.
  39.      */
  40.     public void setPixel(int x, int y, Color col)
  41.     {
  42.         int pixel = col.getRGB();
  43.         setRGB(x, y, pixel);
  44.     }
  45.    
  46.     /**
  47.      * Get the color value at a specified pixel position.
  48.      * @param x The x position of the pixel.
  49.      * @param y The y position of the pixel.
  50.      * @return The color of the pixel at the given position.
  51.      */
  52.     public Color getPixel(int x, int y)
  53.     {
  54.         int pixel = getRGB(x, y);
  55.         return new Color(pixel);
  56.     }
  57.  
  58.     /**
  59.      * Make this image a bit darker.
  60.      */
  61.     public void darker()
  62.     {
  63.         int height = getHeight();
  64.         int width = getWidth();
  65.         for(int y = 0; y < height; y++) {
  66.             for(int x = 0; x < width; x++) {
  67.                 setPixel(x, y, getPixel(x, y).darker());
  68.             }
  69.         }
  70.     }
  71.  
  72.     /**
  73.      * Make this image a bit lighter.
  74.      */
  75.     public void lighter()
  76.     {
  77.         int height = getHeight();
  78.         int width = getWidth();
  79.         for(int y = 0; y < height; y++) {
  80.             for(int x = 0; x < width; x++) {
  81.                 setPixel(x, y, getPixel(x, y).brighter());
  82.             }
  83.         }
  84.     }
  85.  
  86.     /**
  87.      * Perform a three level threshold operation.
  88.      * That is: repaint the image with only three color values:
  89.      *          white, gray, and black.
  90.      */
  91.     public void threshold()
  92.     {
  93.         int height = getHeight();
  94.         int width = getWidth();
  95.         for(int y = 0; y < height; y++) {
  96.             for(int x = 0; x < width; x++) {
  97.                 Color pixel = getPixel(x, y);
  98.                 int brightness = (pixel.getRed() + pixel.getBlue() + pixel.getGreen()) / 3;
  99.                 if(brightness <= 85) {
  100.                     setPixel(x, y, Color.BLACK);
  101.                 }
  102.                 else if(brightness <= 170) {
  103.                     setPixel(x, y, Color.GRAY);
  104.                 }
  105.                 else {
  106.                     setPixel(x, y, Color.WHITE);
  107.                 }
  108.             }
  109.         }
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment