sitchof

Untitled

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