Advertisement
lamaulfarid

OFImage

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